Listing Handler
- added a Listing Handler class to take care of listing params - added status column to Subscriber - added specific methods to both Newsletter & Subscriber models for listing
This commit is contained in:
@ -119,7 +119,7 @@ define(
|
|||||||
'has-row-actions'
|
'has-row-actions'
|
||||||
);
|
);
|
||||||
|
|
||||||
var status;
|
var status = '';
|
||||||
|
|
||||||
switch(parseInt(subscriber.status, 10)) {
|
switch(parseInt(subscriber.status, 10)) {
|
||||||
case 1:
|
case 1:
|
||||||
|
@ -44,6 +44,7 @@ class Migrator {
|
|||||||
'first_name tinytext NOT NULL,',
|
'first_name tinytext NOT NULL,',
|
||||||
'last_name tinytext NOT NULL,',
|
'last_name tinytext NOT NULL,',
|
||||||
'email varchar(150) NOT NULL,',
|
'email varchar(150) NOT NULL,',
|
||||||
|
'status tinyint(1) NOT NULL DEFAULT 0,',
|
||||||
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
||||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||||
'PRIMARY KEY (id),',
|
'PRIMARY KEY (id),',
|
||||||
|
65
lib/Listing/Handler.php
Normal file
65
lib/Listing/Handler.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Listing;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class Handler {
|
||||||
|
|
||||||
|
private $data = array();
|
||||||
|
private $model = null;
|
||||||
|
|
||||||
|
function __construct($model, $data = array()) {
|
||||||
|
$this->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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -18,4 +18,21 @@ class Newsletter extends Model {
|
|||||||
'isString' => 'body_is_not_string'
|
'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) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ if (!defined('ABSPATH')) exit;
|
|||||||
|
|
||||||
class Subscriber extends Model {
|
class Subscriber extends Model {
|
||||||
public static $_table = MP_SUBSCRIBERS_TABLE;
|
public static $_table = MP_SUBSCRIBERS_TABLE;
|
||||||
|
|
||||||
const STATE_SUBSCRIBED = 1;
|
const STATE_SUBSCRIBED = 1;
|
||||||
const STATE_UNCONFIRMED = 0;
|
const STATE_UNCONFIRMED = 0;
|
||||||
const STATE_UNSUBSCRIBED = -1;
|
const STATE_UNSUBSCRIBED = -1;
|
||||||
@ -17,4 +18,61 @@ class Subscriber extends Model {
|
|||||||
'isEmail' => __('Your email address is invalid.')
|
'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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Router;
|
namespace MailPoet\Router;
|
||||||
use \MailPoet\Models\Newsletter;
|
use \MailPoet\Models\Newsletter;
|
||||||
|
use \MailPoet\Models\Subscriber;
|
||||||
use \MailPoet\Mailer\Bridge;
|
use \MailPoet\Mailer\Bridge;
|
||||||
|
use \MailPoet\Listing;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -10,47 +12,8 @@ class Newsletters {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get($data = array()) {
|
function get($data = array()) {
|
||||||
// pagination
|
$listing = new Listing\Handler('Newsletter', $data);
|
||||||
$offset = (isset($data['offset']) ? (int)$data['offset'] : 0);
|
wp_send_json($listing->get());
|
||||||
$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);
|
|
||||||
$groups = array(
|
|
||||||
array(
|
|
||||||
'name' => '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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAll() {
|
function getAll() {
|
||||||
@ -80,7 +43,7 @@ class Newsletters {
|
|||||||
|
|
||||||
function send($id) {
|
function send($id) {
|
||||||
$newsletter = Newsletter::find_one($id)->as_array();
|
$newsletter = Newsletter::find_one($id)->as_array();
|
||||||
$subscribers = Newsletter::find_array();
|
$subscribers = Subscriber::find_array();
|
||||||
$mailer = new Bridge($newsletter, $subscribers);
|
$mailer = new Bridge($newsletter, $subscribers);
|
||||||
wp_send_json($mailer->send());
|
wp_send_json($mailer->send());
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Router;
|
namespace MailPoet\Router;
|
||||||
use \MailPoet\Models\Subscriber;
|
use \MailPoet\Models\Subscriber;
|
||||||
|
use \MailPoet\Listing;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -9,80 +10,8 @@ class Subscribers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get($data = array()) {
|
function get($data = array()) {
|
||||||
// pagination
|
$listing = new Listing\Handler('Subscriber', $data);
|
||||||
$offset = (isset($data['offset']) ? (int)$data['offset'] : 0);
|
wp_send_json($listing->get());
|
||||||
$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);
|
|
||||||
$groups = 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()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAll() {
|
function getAll() {
|
||||||
|
Reference in New Issue
Block a user