diff --git a/assets/js/src/subscribers/list.jsx b/assets/js/src/subscribers/list.jsx index dc6bbf4859..fe19873766 100644 --- a/assets/js/src/subscribers/list.jsx +++ b/assets/js/src/subscribers/list.jsx @@ -119,7 +119,7 @@ define( 'has-row-actions' ); - var status; + var status = ''; switch(parseInt(subscriber.status, 10)) { case 1: diff --git a/lib/Config/Migrator.php b/lib/Config/Migrator.php index cdf8196791..0eecc72319 100644 --- a/lib/Config/Migrator.php +++ b/lib/Config/Migrator.php @@ -44,9 +44,10 @@ class Migrator { 'first_name tinytext NOT NULL,', 'last_name tinytext NOT NULL,', 'email varchar(150) NOT NULL,', + 'status tinyint(1) NOT NULL DEFAULT 0,', 'created_at TIMESTAMP NOT NULL DEFAULT 0,', 'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', - 'PRIMARY KEY (id),', + 'PRIMARY KEY (id),', 'UNIQUE KEY email (email)' ); return $this->sqlify(__FUNCTION__, $attributes); @@ -59,7 +60,7 @@ class Migrator { 'value varchar(255) NOT NULL,', 'created_at TIMESTAMP NOT NULL DEFAULT 0,', 'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', - 'PRIMARY KEY (id),', + 'PRIMARY KEY (id),', 'UNIQUE KEY name (name)' ); return $this->sqlify(__FUNCTION__, $attributes); @@ -72,7 +73,7 @@ class Migrator { 'body longtext,', 'created_at TIMESTAMP NOT NULL DEFAULT 0,', 'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', - 'PRIMARY KEY (id)' + 'PRIMARY KEY (id)' ); return $this->sqlify(__FUNCTION__, $attributes); } diff --git a/lib/Listing/Handler.php b/lib/Listing/Handler.php new file mode 100644 index 0000000000..d14b3b7299 --- /dev/null +++ b/lib/Listing/Handler.php @@ -0,0 +1,65 @@ +model = $this->getModel($model); + $this->data = array( + // pagination + 'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0), + 'limit' => (isset($data['limit']) ? (int)$data['limit'] : 50), + // searching + 'search' => (isset($data['search']) ? $data['search'] : null), + // sorting + 'sort_by' => (isset($data['sort_by']) ? $data['sort_by'] : 'id'), + 'sort_order' => (isset($data['sort_order']) ? $data['sort_order'] : 'asc'), + // grouping + 'group' => (isset($data['group']) ? $data['group'] : null) + ); + + $this->setSearch(); + $this->setOrder(); + $this->setGroup(); + } + + private function getModel($model) { + return \Model::factory('\MailPoet\Models\\'.$model); + } + + private function setSearch() { + if($this->data['search'] === null) { + return; + } + return $this->model->filter('search', $this->data['search']); + } + + private function setOrder() { + return $this->model + ->{'order_by_'.$this->data['sort_order']}($this->data['sort_by']); + } + + private function setGroup() { + if($this->data['group'] === null) { + return; + } + return $this->model->filter('group', $this->data['group']); + } + + function get() { + return array( + 'count' => $this->model->count(), + 'filters' => [], + 'groups' => $this->model->filter('groups'), + 'items' => $this->model + ->offset($this->data['offset']) + ->limit($this->data['limit']) + ->find_array() + ); + } +} \ No newline at end of file diff --git a/lib/Models/Newsletter.php b/lib/Models/Newsletter.php index 687d217db1..79ce366a78 100644 --- a/lib/Models/Newsletter.php +++ b/lib/Models/Newsletter.php @@ -18,4 +18,21 @@ class Newsletter extends Model { 'isString' => 'body_is_not_string' )); } + + static function search($orm, $search = '') { + return $orm->where_like('subject', '%'.$search.'%'); + } + + static function groups() { + return array( + array( + 'name' => 'all', + 'label' => __('All'), + 'count' => Newsletter::count() + ) + ); + } + + static function group($orm, $group = null) { + } } diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index 77c1eb2df8..6e7c8cf168 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -5,6 +5,7 @@ if (!defined('ABSPATH')) exit; class Subscriber extends Model { public static $_table = MP_SUBSCRIBERS_TABLE; + const STATE_SUBSCRIBED = 1; const STATE_UNCONFIRMED = 0; const STATE_UNSUBSCRIBED = -1; @@ -17,4 +18,61 @@ class Subscriber extends Model { 'isEmail' => __('Your email address is invalid.') )); } + + static function search($orm, $search = '') { + return $orm->where_raw( + '(`email` LIKE ? OR `first_name` LIKE ? OR `last_name` LIKE ?)', + array('%'.$search.'%', '%'.$search.'%', '%'.$search.'%') + ); + } + + static function groups() { + return array( + array( + 'name' => 'all', + 'label' => __('All'), + 'count' => Subscriber::count() + ), + array( + 'name' => 'subscribed', + 'label' => __('Subscribed'), + 'count' => Subscriber::where( + 'status', + Subscriber::STATE_SUBSCRIBED + )->count() + ), + array( + 'name' => 'unconfirmed', + 'label' => __('Unconfirmed'), + 'count' => Subscriber::where( + 'status', + Subscriber::STATE_UNCONFIRMED + )->count() + ), + array( + 'name' => 'unsubscribed', + 'label' => __('Unsubscribed'), + 'count' => Subscriber::where( + 'status', + Subscriber::STATE_UNSUBSCRIBED + )->count() + ) + ); + } + + static function group($orm, $group = null) { + switch($group) { + case 'subscribed': + return $orm->where('status', Subscriber::STATE_SUBSCRIBED); + break; + + case 'unconfirmed': + return $orm->where('status', Subscriber::STATE_UNCONFIRMED); + break; + + case 'unsubscribed': + return $orm->where('status', Subscriber::STATE_UNSUBSCRIBED); + break; + } + } } diff --git a/lib/Router/Newsletters.php b/lib/Router/Newsletters.php index c172d266e8..d6e299a3c6 100644 --- a/lib/Router/Newsletters.php +++ b/lib/Router/Newsletters.php @@ -1,7 +1,9 @@ 'all', - 'label' => __('All'), - 'count' => Newsletter::count() - ) - ); - - // instantiate subscriber collection - $collection = Newsletter::{'order_by_'.$sort_order}($sort_by); - - // handle search - if($search !== null) { - $collection->where_like('subject', '%'.$search.'%'); - } - - // handle filters - $filters = array(); - - // return result - $collection = array( - 'count' => $collection->count(), - 'filters' => $filters, - 'groups' => $groups, - 'items' => $collection - ->offset($offset) - ->limit($limit) - ->find_array() - ); - - wp_send_json($collection); + $listing = new Listing\Handler('Newsletter', $data); + wp_send_json($listing->get()); } function getAll() { @@ -80,7 +43,7 @@ class Newsletters { function send($id) { $newsletter = Newsletter::find_one($id)->as_array(); - $subscribers = Newsletter::find_array(); + $subscribers = Subscriber::find_array(); $mailer = new Bridge($newsletter, $subscribers); wp_send_json($mailer->send()); } diff --git a/lib/Router/Subscribers.php b/lib/Router/Subscribers.php index c331368be6..edb76c76d6 100644 --- a/lib/Router/Subscribers.php +++ b/lib/Router/Subscribers.php @@ -1,6 +1,7 @@ 'all', - 'label' => __('All'), - 'count' => Subscriber::count() - ), - array( - 'name' => 'subscribed', - 'label' => __('Subscribed'), - 'count' => Subscriber::where('status', Subscriber::STATE_SUBSCRIBED)->count() - ), - array( - 'name' => 'unconfirmed', - 'label' => __('Unconfirmed'), - 'count' => Subscriber::where('status', Subscriber::STATE_UNCONFIRMED)->count() - ), - array( - 'name' => 'unsubscribed', - 'label' => __('Unsubscribed'), - 'count' => Subscriber::where('status', Subscriber::STATE_UNSUBSCRIBED)->count() - ) - ); - - // instantiate subscriber collection - $collection = Subscriber::{'order_by_'.$sort_order}($sort_by); - - // handle group - switch($group) { - case 'subscribed': - $collection = $collection->where('status', Subscriber::STATE_SUBSCRIBED); - break; - - case 'unconfirmed': - $collection = $collection->where('status', Subscriber::STATE_UNCONFIRMED); - break; - - case 'unsubscribed': - $collection = $collection->where('status', Subscriber::STATE_UNSUBSCRIBED); - break; - } - - // handle search - if($search !== null) { - $collection->where_raw( - '(`email` LIKE ? OR `first_name` LIKE ? OR `last_name` LIKE ?)', - array('%'.$search.'%', '%'.$search.'%', '%'.$search.'%') - ); - } - - // handle filters - $filters = array(); - - // return result - $collection = array( - 'count' => $collection->count(), - 'filters' => $filters, - 'groups' => $groups, - 'items' => $collection - ->offset($offset) - ->limit($limit) - ->find_array() - ); - - wp_send_json($collection); + $listing = new Listing\Handler('Subscriber', $data); + wp_send_json($listing->get()); } function getAll() {