refactored bulk actions & implemented bulk trash for all listings
This commit is contained in:
@ -276,7 +276,11 @@ define(
|
|||||||
this.props.messages['created']();
|
this.props.messages['created']();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.setState({ errors: response });
|
if(response === false) {
|
||||||
|
// unknown error occurred
|
||||||
|
} else {
|
||||||
|
this.setState({ errors: response });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
@ -241,12 +241,12 @@ define(
|
|||||||
|
|
||||||
var data = params || {};
|
var data = params || {};
|
||||||
|
|
||||||
data.selection = selected_ids;
|
|
||||||
data.listing = {
|
data.listing = {
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 0,
|
limit: 0,
|
||||||
group: this.state.group,
|
group: this.state.group,
|
||||||
search: this.state.search
|
search: this.state.search,
|
||||||
|
selection: selected_ids
|
||||||
}
|
}
|
||||||
|
|
||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
|
@ -27,6 +27,13 @@ define(
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
var bulk_actions = [
|
||||||
|
{
|
||||||
|
name: 'trash',
|
||||||
|
label: 'Trash'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
var NewsletterList = React.createClass({
|
var NewsletterList = React.createClass({
|
||||||
renderItem: function(newsletter, actions) {
|
renderItem: function(newsletter, actions) {
|
||||||
var rowClasses = classNames(
|
var rowClasses = classNames(
|
||||||
@ -57,8 +64,8 @@ define(
|
|||||||
<Listing
|
<Listing
|
||||||
endpoint="newsletters"
|
endpoint="newsletters"
|
||||||
onRenderItem={this.renderItem}
|
onRenderItem={this.renderItem}
|
||||||
items={this.getItems}
|
columns={columns}
|
||||||
columns={columns} />
|
bulk_actions={ bulk_actions } />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -27,6 +27,13 @@ define(
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
var bulk_actions = [
|
||||||
|
{
|
||||||
|
name: 'trash',
|
||||||
|
label: 'Trash'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
var SegmentList = React.createClass({
|
var SegmentList = React.createClass({
|
||||||
renderItem: function(segment, actions) {
|
renderItem: function(segment, actions) {
|
||||||
var rowClasses = classNames(
|
var rowClasses = classNames(
|
||||||
@ -57,8 +64,8 @@ define(
|
|||||||
<Listing
|
<Listing
|
||||||
endpoint="segments"
|
endpoint="segments"
|
||||||
onRenderItem={this.renderItem}
|
onRenderItem={this.renderItem}
|
||||||
items={this.getItems}
|
columns={columns}
|
||||||
columns={columns} />
|
bulk_actions={ bulk_actions } />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -67,7 +67,7 @@ define(
|
|||||||
action: 'listing',
|
action: 'listing',
|
||||||
data: {
|
data: {
|
||||||
'offset': 0,
|
'offset': 0,
|
||||||
'limit': 5,
|
'limit': 100,
|
||||||
'search': '',
|
'search': '',
|
||||||
'sort_by': 'name',
|
'sort_by': 'name',
|
||||||
'sort_order': 'asc'
|
'sort_order': 'asc'
|
||||||
@ -145,7 +145,7 @@ define(
|
|||||||
|
|
||||||
var bulk_actions = [
|
var bulk_actions = [
|
||||||
{
|
{
|
||||||
name: 'move',
|
name: 'moveToList',
|
||||||
label: 'Move to list...',
|
label: 'Move to list...',
|
||||||
onSelect: function() {
|
onSelect: function() {
|
||||||
return (
|
return (
|
||||||
@ -161,7 +161,7 @@ define(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'add',
|
name: 'addToList',
|
||||||
label: 'Add to list...',
|
label: 'Add to list...',
|
||||||
onSelect: function() {
|
onSelect: function() {
|
||||||
return (
|
return (
|
||||||
@ -177,7 +177,7 @@ define(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'remove',
|
name: 'removeFromList',
|
||||||
label: 'Remove from list...',
|
label: 'Remove from list...',
|
||||||
onSelect: function() {
|
onSelect: function() {
|
||||||
return (
|
return (
|
||||||
@ -198,7 +198,7 @@ define(
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
var List = React.createClass({
|
var SubscriberList = React.createClass({
|
||||||
renderItem: function(subscriber, actions) {
|
renderItem: function(subscriber, actions) {
|
||||||
var row_classes = classNames(
|
var row_classes = classNames(
|
||||||
'manage-column',
|
'manage-column',
|
||||||
@ -259,6 +259,6 @@ define(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return List;
|
return SubscriberList;
|
||||||
}
|
}
|
||||||
);
|
);
|
@ -29,6 +29,9 @@ class Initializer {
|
|||||||
\ORM::configure('username', Env::$db_username);
|
\ORM::configure('username', Env::$db_username);
|
||||||
\ORM::configure('password', Env::$db_password);
|
\ORM::configure('password', Env::$db_password);
|
||||||
\ORM::configure('logging', WP_DEBUG);
|
\ORM::configure('logging', WP_DEBUG);
|
||||||
|
\ORM::configure('driver_options', array(
|
||||||
|
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
|
||||||
|
));
|
||||||
|
|
||||||
$subscribers = Env::$db_prefix . 'subscribers';
|
$subscribers = Env::$db_prefix . 'subscribers';
|
||||||
$settings = Env::$db_prefix . 'settings';
|
$settings = Env::$db_prefix . 'settings';
|
||||||
|
28
lib/Listing/BulkAction.php
Normal file
28
lib/Listing/BulkAction.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Listing;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class BulkAction {
|
||||||
|
private $listing = null;
|
||||||
|
private $data = null;
|
||||||
|
private $model = null;
|
||||||
|
|
||||||
|
function __construct($model, $data) {
|
||||||
|
$this->model = $model;
|
||||||
|
$this->data = $data;
|
||||||
|
|
||||||
|
$this->listing = new Handler(
|
||||||
|
\Model::factory($this->model),
|
||||||
|
$this->data['listing']
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
function apply() {
|
||||||
|
return call_user_func_array(
|
||||||
|
array($this->model, $this->data['action']),
|
||||||
|
array($this->listing, $this->data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -20,7 +20,9 @@ class Handler {
|
|||||||
'sort_by' => (isset($data['sort_by']) ? $data['sort_by'] : 'id'),
|
'sort_by' => (isset($data['sort_by']) ? $data['sort_by'] : 'id'),
|
||||||
'sort_order' => (isset($data['sort_order']) ? $data['sort_order'] : 'asc'),
|
'sort_order' => (isset($data['sort_order']) ? $data['sort_order'] : 'asc'),
|
||||||
// grouping
|
// grouping
|
||||||
'group' => (isset($data['group']) ? $data['group'] : null)
|
'group' => (isset($data['group']) ? $data['group'] : null),
|
||||||
|
// selection
|
||||||
|
'selection' => (isset($data['selection']) ? $data['selection'] : null)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->setSearch();
|
$this->setSearch();
|
||||||
@ -47,18 +49,18 @@ class Handler {
|
|||||||
return $this->model->filter('group', $this->data['group']);
|
return $this->model->filter('group', $this->data['group']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelection($ids = array()) {
|
function getSelection() {
|
||||||
if(!empty($ids)) {
|
if(!empty($this->data['selection'])) {
|
||||||
$this->model->whereIn('id', $ids);
|
$this->model->whereIn('id', $this->data['selection']);
|
||||||
}
|
}
|
||||||
return $this->model;
|
return $this->model;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelectionIds($ids = array()) {
|
function getSelectionIds() {
|
||||||
$subscribers = $this->getSelection($ids)->select('id')->findMany();
|
$models = $this->getSelection()->select('id')->findMany();
|
||||||
return array_map(function($subscriber) {
|
return array_map(function($model) {
|
||||||
return (int)$subscriber->id;
|
return (int)$model->id;
|
||||||
}, $subscribers);
|
}, $models);
|
||||||
}
|
}
|
||||||
|
|
||||||
function get() {
|
function get() {
|
||||||
|
@ -48,10 +48,18 @@ class Newsletter extends Model {
|
|||||||
|
|
||||||
$saved = $newsletter->save();
|
$saved = $newsletter->save();
|
||||||
|
|
||||||
if($saved === false) {
|
if($saved === true) {
|
||||||
return $newsletter->getValidationErrors();
|
|
||||||
} else {
|
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
$errors = $newsletter->getValidationErrors();
|
||||||
|
if(!empty($errors)) {
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function trash($listing) {
|
||||||
|
return $listing->getSelection()->deleteMany();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,10 +57,18 @@ class Segment extends Model {
|
|||||||
|
|
||||||
$saved = $segment->save();
|
$saved = $segment->save();
|
||||||
|
|
||||||
if($saved === false) {
|
if($saved === true) {
|
||||||
return $segment->getValidationErrors();
|
|
||||||
} else {
|
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
$errors = $segment->getValidationErrors();
|
||||||
|
if(!empty($errors)) {
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function trash($listing) {
|
||||||
|
return $listing->getSelection()->deleteMany();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ class Subscriber extends Model {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
function delete() {
|
function delete() {
|
||||||
// delete all relations to segments
|
// delete all relations to segments
|
||||||
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
|
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
|
||||||
|
|
||||||
@ -95,10 +95,76 @@ class Subscriber extends Model {
|
|||||||
|
|
||||||
$saved = $subscriber->save();
|
$saved = $subscriber->save();
|
||||||
|
|
||||||
if($saved === false) {
|
if($saved === true) {
|
||||||
return $subscriber->getValidationErrors();
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
$errors = $subscriber->getValidationErrors();
|
||||||
|
if(!empty($errors)) {
|
||||||
|
return $errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function moveToList($listing, $data = array()) {
|
||||||
|
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
||||||
|
$segment = Segment::findOne($segment_id);
|
||||||
|
|
||||||
|
if($segment !== false) {
|
||||||
|
$subscribers = $listing->getSelection()->findMany();
|
||||||
|
foreach($subscribers as $subscriber) {
|
||||||
|
// remove subscriber from all segments
|
||||||
|
SubscriberSegment::where('subscriber_id', $subscriber->id)->deleteMany();
|
||||||
|
|
||||||
|
// create relation with segment
|
||||||
|
$association = SubscriberSegment::create();
|
||||||
|
$association->subscriber_id = $subscriber->id;
|
||||||
|
$association->segment_id = $segment->id;
|
||||||
|
$association->save();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function removeFromList($listing, $data = array()) {
|
||||||
|
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
||||||
|
$segment = Segment::findOne($segment_id);
|
||||||
|
|
||||||
|
if($segment !== false) {
|
||||||
|
// delete relations with segment
|
||||||
|
$subscriber_ids = $listing->getSelectionIds();
|
||||||
|
SubscriberSegment::whereIn('subscriber_id', $subscriber_ids)
|
||||||
|
->where('segment_id', $segment->id)
|
||||||
|
->deleteMany();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function addToList($listing, $data = array()) {
|
||||||
|
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
||||||
|
$segment = Segment::findOne($segment_id);
|
||||||
|
|
||||||
|
if($segment !== false) {
|
||||||
|
$subscribers = $listing->getSelection()->findMany();
|
||||||
|
foreach($subscribers as $subscriber) {
|
||||||
|
// create relation with segment
|
||||||
|
$association = \MailPoet\Models\SubscriberSegment::create();
|
||||||
|
$association->subscriber_id = $subscriber->id;
|
||||||
|
$association->segment_id = $segment->id;
|
||||||
|
$association->save();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function trash($listing) {
|
||||||
|
// delete relations with all segments
|
||||||
|
$subscriber_ids = $listing->getSelectionIds();
|
||||||
|
\MailPoet\Models\SubscriberSegment::whereIn('subscriber_id', $subscriber_ids)->deleteMany();
|
||||||
|
|
||||||
|
return $listing->getSelection()->deleteMany();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,4 +62,13 @@ class Newsletters {
|
|||||||
$mailer = new Bridge($newsletter, $subscribers);
|
$mailer = new Bridge($newsletter, $subscribers);
|
||||||
wp_send_json($mailer->send());
|
wp_send_json($mailer->send());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bulk_action($data = array()) {
|
||||||
|
$bulk_action = new Listing\BulkAction(
|
||||||
|
'\MailPoet\Models\Newsletter',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_send_json($bulk_action->apply());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ class Router {
|
|||||||
$class = ucfirst($_POST['endpoint']);
|
$class = ucfirst($_POST['endpoint']);
|
||||||
$endpoint = __NAMESPACE__ . "\\" . $class;
|
$endpoint = __NAMESPACE__ . "\\" . $class;
|
||||||
$method = $_POST['method'];
|
$method = $_POST['method'];
|
||||||
$data = isset($_POST['data']) ? $_POST['data'] : array();
|
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
||||||
$endpoint = new $endpoint();
|
$endpoint = new $endpoint();
|
||||||
$endpoint->$method($data);
|
$endpoint->$method($data);
|
||||||
}
|
}
|
||||||
|
@ -53,4 +53,13 @@ class Segments {
|
|||||||
|
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bulk_action($data = array()) {
|
||||||
|
$bulk_action = new Listing\BulkAction(
|
||||||
|
'\MailPoet\Models\Segment',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_send_json($bulk_action->apply());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,69 +30,6 @@ class Subscribers {
|
|||||||
wp_send_json($listing->get());
|
wp_send_json($listing->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
function bulk_action($data = array()) {
|
|
||||||
$action = $data['action'];
|
|
||||||
$selection = (isset($data['selection']) ? $data['selection'] : null);
|
|
||||||
$listing_data = $data['listing'];
|
|
||||||
|
|
||||||
$listing = new Listing\Handler(
|
|
||||||
\Model::factory('\MailPoet\Models\Subscriber'),
|
|
||||||
$listing_data
|
|
||||||
);
|
|
||||||
|
|
||||||
$selected = $listing->getSelection($selection);
|
|
||||||
$subscribers = $selected->findMany();
|
|
||||||
|
|
||||||
$result = false;
|
|
||||||
switch($action) {
|
|
||||||
case 'move':
|
|
||||||
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
|
||||||
foreach($subscribers as $subscriber) {
|
|
||||||
// remove subscriber from all segments
|
|
||||||
SubscriberSegment::where('subscriber_id', $subscriber->id)->deleteMany();
|
|
||||||
|
|
||||||
// create relation with segment
|
|
||||||
$association = SubscriberSegment::create();
|
|
||||||
$association->subscriber_id = $subscriber->id;
|
|
||||||
$association->segment_id = $segment_id;
|
|
||||||
$association->save();
|
|
||||||
}
|
|
||||||
$result = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'remove':
|
|
||||||
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
|
||||||
// delete relations with segment
|
|
||||||
$subscriber_ids = $listing->getSelectionIds($selection);
|
|
||||||
$result = SubscriberSegment::whereIn('subscriber_id', $subscriber_ids)
|
|
||||||
->where('segment_id', $segment_id)
|
|
||||||
->deleteMany();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'add':
|
|
||||||
$segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0);
|
|
||||||
foreach($subscribers as $subscriber) {
|
|
||||||
// create relation with segment
|
|
||||||
$association = SubscriberSegment::create();
|
|
||||||
$association->subscriber_id = $subscriber->id;
|
|
||||||
$association->segment_id = $segment_id;
|
|
||||||
$association->save();
|
|
||||||
}
|
|
||||||
$result = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'trash':
|
|
||||||
// delete relations with all segments
|
|
||||||
$subscriber_ids = $listing->getSelectionIds($selection);
|
|
||||||
SubscriberSegment::whereIn('subscriber_id', $subscriber_ids)->deleteMany();
|
|
||||||
|
|
||||||
$result = $selected->deleteMany();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
wp_send_json($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAll() {
|
function getAll() {
|
||||||
$collection = Subscriber::findArray();
|
$collection = Subscriber::findArray();
|
||||||
wp_send_json($collection);
|
wp_send_json($collection);
|
||||||
@ -112,4 +49,13 @@ class Subscribers {
|
|||||||
}
|
}
|
||||||
wp_send_json($result);
|
wp_send_json($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bulk_action($data = array()) {
|
||||||
|
$bulk_action = new Listing\BulkAction(
|
||||||
|
'\MailPoet\Models\Subscriber',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_send_json($bulk_action->apply());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,35 +14,35 @@ abstract class ValidModel extends \Model
|
|||||||
// + ON_SET throws immediately when field is set()
|
// + ON_SET throws immediately when field is set()
|
||||||
// + ON_SAVE throws on save()
|
// + ON_SAVE throws on save()
|
||||||
// + NEVER means an exception is never thrown; check for ->getValidaionErrors()
|
// + NEVER means an exception is never thrown; check for ->getValidaionErrors()
|
||||||
);
|
);
|
||||||
|
|
||||||
const ON_SET = 'set';
|
const ON_SET = 'set';
|
||||||
const ON_SAVE = 'save';
|
const ON_SAVE = 'save';
|
||||||
const NEVER = null;
|
const NEVER = null;
|
||||||
|
|
||||||
public function __construct($validatorInstance = null) {
|
public function __construct($validatorInstance = null) {
|
||||||
$this->_validator = $validatorInstance;
|
$this->_validator = $validatorInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setValidationOptions($options)
|
public function setValidationOptions($options)
|
||||||
{
|
{
|
||||||
$this->_validationOptions = array_merge($this->_validationOptions, $options);
|
$this->_validationOptions = array_merge($this->_validationOptions, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addValidation($field, $validation, $message) {
|
public function addValidation($field, $validation, $message) {
|
||||||
if (!isset($this->_validations[$field])) {
|
if (!isset($this->_validations[$field])) {
|
||||||
$this->_validations[$field] = array();
|
$this->_validations[$field] = array();
|
||||||
}
|
}
|
||||||
$this->_validations[$field][] = array(
|
$this->_validations[$field][] = array(
|
||||||
'validation' => $validation,
|
'validation' => $validation,
|
||||||
'message' => $message
|
'message' => $message
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addValidations($field, $validators) {
|
public function addValidations($field, $validators) {
|
||||||
foreach ($validators as $validation => $message) {
|
foreach ($validators as $validation => $message) {
|
||||||
$this->addValidation($field, $validation, $message);
|
$this->addValidation($field, $validation, $message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
// /**
|
||||||
@ -65,39 +65,39 @@ abstract class ValidModel extends \Model
|
|||||||
**/
|
**/
|
||||||
public function validateField($field, $value)
|
public function validateField($field, $value)
|
||||||
{
|
{
|
||||||
$this->setupValidationEngine();
|
$this->setupValidationEngine();
|
||||||
|
|
||||||
if (!isset($this->_validations[$field])) {
|
if (!isset($this->_validations[$field])) {
|
||||||
return true; // No validations, return true by default
|
return true; // No validations, return true by default
|
||||||
|
}
|
||||||
|
|
||||||
|
$success = true;
|
||||||
|
foreach ($this->_validations[$field] as $v) {
|
||||||
|
$checks = explode(' ', $v['validation']);
|
||||||
|
foreach ($checks as $check) {
|
||||||
|
$params = explode('|', $check);
|
||||||
|
$check = array_shift($params);
|
||||||
|
|
||||||
|
if ($this->_validator->executeOne($check, $value, $params)) {
|
||||||
|
$success = $success && true;
|
||||||
|
} else {
|
||||||
|
$this->addValidationError($v['message'], $field);
|
||||||
|
$success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
$success = true;
|
public function getValidationErrors()
|
||||||
foreach ($this->_validations[$field] as $v) {
|
{
|
||||||
$checks = explode(' ', $v['validation']);
|
return $this->_validationErrors;
|
||||||
foreach ($checks as $check) {
|
|
||||||
$params = explode('|', $check);
|
|
||||||
$check = array_shift($params);
|
|
||||||
|
|
||||||
if ($this->_validator->executeOne($check, $value, $params)) {
|
|
||||||
$success = $success && true;
|
|
||||||
} else {
|
|
||||||
$this->addValidationError($v['message'], $field);
|
|
||||||
$success = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $success;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getValidationErrors()
|
public function resetValidationErrors()
|
||||||
{
|
{
|
||||||
return $this->_validationErrors;
|
$this->_validationErrors = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resetValidationErrors()
|
|
||||||
{
|
|
||||||
$this->_validationErrors = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////
|
///////////////////
|
||||||
// Overloaded methods
|
// Overloaded methods
|
||||||
@ -107,7 +107,7 @@ abstract class ValidModel extends \Model
|
|||||||
*/
|
*/
|
||||||
public function __set($name, $value)
|
public function __set($name, $value)
|
||||||
{
|
{
|
||||||
$this->validateAndSet($name, $value);
|
$this->validateAndSet($name, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,16 +116,17 @@ abstract class ValidModel extends \Model
|
|||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
if ($this->isNew()) { //Fields populated by create() or hydrate() don't pass through set()
|
if ($this->isNew()) { //Fields populated by create() or hydrate() don't pass through set()
|
||||||
foreach( array_keys($this->_validations) as $field) {
|
foreach( array_keys($this->_validations) as $field) {
|
||||||
$this->validateField($field, $this->$field);
|
$this->validateField($field, $this->$field);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$errs = $this->getValidationErrors();
|
$errs = $this->getValidationErrors();
|
||||||
if (!empty($errs))
|
if (!empty($errs)) {
|
||||||
$this->doValidationError(self::ON_SAVE);
|
$this->doValidationError(self::ON_SAVE);
|
||||||
|
}
|
||||||
|
|
||||||
parent::save();
|
parent::save();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,14 +134,14 @@ abstract class ValidModel extends \Model
|
|||||||
*/
|
*/
|
||||||
public function set($key, $value = null)
|
public function set($key, $value = null)
|
||||||
{
|
{
|
||||||
if(is_array($key)) {
|
if(is_array($key)) {
|
||||||
// multiple values
|
// multiple values
|
||||||
foreach($key as $field => $value) {
|
foreach($key as $field => $value) {
|
||||||
$this->validateAndSet($field, $value);
|
$this->validateAndSet($field, $value);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->validateAndSet($key, $value);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$this->validateAndSet($key, $value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -148,20 +149,21 @@ abstract class ValidModel extends \Model
|
|||||||
// Protected methods
|
// Protected methods
|
||||||
protected function doValidationError($context)
|
protected function doValidationError($context)
|
||||||
{
|
{
|
||||||
if ($context == $this->_validationOptions['throw'])
|
if ($context == $this->_validationOptions['throw']) {
|
||||||
throw new \Sudzy\ValidationException($this->_validationErrors);
|
throw new \Sudzy\ValidationException($this->_validationErrors);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addValidationError($msg, $field = null)
|
protected function addValidationError($msg, $field = null)
|
||||||
{
|
{
|
||||||
if ($this->_validationOptions['indexedErrors'] && $field !== null) {
|
if ($this->_validationOptions['indexedErrors'] && $field !== null) {
|
||||||
// Only keep the first error found on a field
|
// Only keep the first error found on a field
|
||||||
if (!isset($this->_validationErrors[$field])) {
|
if (!isset($this->_validationErrors[$field])) {
|
||||||
$this->_validationErrors[$field] = $msg;
|
$this->_validationErrors[$field] = $msg;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->_validationErrors[] = $msg;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$this->_validationErrors[] = $msg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -169,12 +171,12 @@ abstract class ValidModel extends \Model
|
|||||||
*/
|
*/
|
||||||
protected function validateAndSet($name, $value)
|
protected function validateAndSet($name, $value)
|
||||||
{
|
{
|
||||||
if (!$this->validateField($name, $value)) $this->doValidationError(self::ON_SET);
|
if (!$this->validateField($name, $value)) $this->doValidationError(self::ON_SET);
|
||||||
parent::set($name, $value);
|
parent::set($name, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setupValidationEngine()
|
protected function setupValidationEngine()
|
||||||
{
|
{
|
||||||
if (null == $this->_validator) $this->_validator = new \Sudzy\Engine(); // Is lazy setup worth it?
|
if (null == $this->_validator) $this->_validator = new \Sudzy\Engine(); // Is lazy setup worth it?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user