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:
Jonathan Labreuille
2015-09-02 14:47:18 +02:00
parent 12a664f44e
commit 3fe895a7fe
7 changed files with 153 additions and 120 deletions

View File

@ -1,6 +1,7 @@
<?php
namespace MailPoet\Router;
use \MailPoet\Models\Subscriber;
use \MailPoet\Listing;
if(!defined('ABSPATH')) exit;
@ -9,80 +10,8 @@ class Subscribers {
}
function get($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);
$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);
$listing = new Listing\Handler('Subscriber', $data);
wp_send_json($listing->get());
}
function getAll() {