Newsletters multi-listing

This commit is contained in:
Jonathan Labreuille
2016-06-01 12:36:28 +02:00
parent dc6c973574
commit 3b97a26a8a
5 changed files with 120 additions and 28 deletions

View File

@ -427,9 +427,13 @@ define(
this.clearSelection();
const action = (this.props.action !== undefined)
? this.props.action
: 'listing';
MailPoet.Ajax.post({
endpoint: this.props.endpoint,
action: 'listing',
action: action,
data: {
offset: (this.state.page - 1) * this.state.limit,
limit: this.state.limit,

View File

@ -295,6 +295,31 @@ define(
limit={ mailpoet_listing_per_page }
params={ this.props.params }
endpoint="newsletters"
action="listingStandard"
onRenderItem={this.renderItem}
columns={columns}
bulk_actions={ bulk_actions }
item_actions={ item_actions }
messages={ messages }
auto_refresh={ true } />
<Listing
limit={ mailpoet_listing_per_page }
params={ this.props.params }
endpoint="newsletters"
action="listingWelcome"
onRenderItem={this.renderItem}
columns={columns}
bulk_actions={ bulk_actions }
item_actions={ item_actions }
messages={ messages }
auto_refresh={ true } />
<Listing
limit={ mailpoet_listing_per_page }
params={ this.props.params }
endpoint="newsletters"
action="listingNotification"
onRenderItem={this.renderItem}
columns={columns}
bulk_actions={ bulk_actions }

View File

@ -8,11 +8,22 @@ class Handler {
private $data = array();
private $model = null;
private $model_class = null;
private $query_function = false;
function __construct($model_class, $data = array(), $query_function = false) {
$this->table_name = $model_class::$_table;
$this->model_class = $model_class;
$this->model = \Model::factory($this->model_class);
if($query_function !== false) {
// execute query function to filter results
$query_function($this->model);
// store query function for later use with groups/filters
$this->query_function = $query_function;
}
function __construct($model_class, $data = array()) {
$class = new \ReflectionClass($model_class);
$this->table_name = $class->getStaticPropertyValue('_table');
$this->model = $model_class::select('*');
$this->data = array(
// pagination
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
@ -32,11 +43,6 @@ class Handler {
// selection
'selection' => (isset($data['selection']) ? $data['selection'] : null)
);
$this->setFilter();
$this->setSearch();
$this->setGroup();
$this->setOrder();
}
private function setSearch() {
@ -83,7 +89,24 @@ class Handler {
}, $models);
}
function get() {
function getData() {
// get groups
$groups = call_user_func_array(
array($this->model_class, 'groups'),
array($this->query_function)
);
// get filters
$filters = call_user_func_array(
array($this->model_class, 'filters'),
array($this->query_function, $this->data['group'])
);
$this->setGroup();
$this->setFilter();
$this->setSearch();
$this->setOrder();
$count = $this->model->count();
$items = $this->model
@ -91,10 +114,11 @@ class Handler {
->limit($this->data['limit'])
->findMany();
return array(
'count' => $count,
'filters' => $this->model->filter('filters', $this->data['group']),
'groups' => $this->model->filter('groups'),
'filters' => $filters,
'groups' => $groups,
'items' => $items
);
}

View File

@ -5,7 +5,9 @@ if(!defined('ABSPATH')) exit;
class Newsletter extends Model {
public static $_table = MP_NEWSLETTERS_TABLE;
const TYPE_WELCOME = 'wecome';
const TYPE_STANDARD = 'standard';
const TYPE_WELCOME = 'welcome';
const TYPE_NOTIFICATION = 'notification';
function __construct() {
@ -127,7 +129,7 @@ class Newsletter extends Model {
return $orm->where_like('subject', '%' . $search . '%');
}
static function filters($orm, $group = 'all') {
static function filters($query_function = false, $group = 'all') {
$segments = Segment::orderByAsc('name')->findMany();
$segment_list = array();
$segment_list[] = array(
@ -136,13 +138,16 @@ class Newsletter extends Model {
);
foreach($segments as $segment) {
$newsletters_count = $segment->newsletters()
->filter('groupBy', $group)
->count();
$newsletters = $segment->newsletters()->filter('groupBy', $group);
if($query_function !== false) {
$newsletters = $query_function($newsletters);
}
$newsletters_count = $newsletters->count();
if($newsletters_count > 0) {
$segment_list[] = array(
'label' => sprintf('%s (%d)', $segment->name, $newsletters_count),
'value' => $segment->id()
'value' => $segment->id
);
}
}
@ -190,26 +195,27 @@ class Newsletter extends Model {
return $orm;
}
static function groups() {
static function groups($query_function = false) {
return array(
array(
'name' => 'all',
'label' => __('All'),
'count' => Newsletter::getPublished()->count()
'count' => $query_function(Newsletter::getPublished())->count()
),
array(
'name' => 'trash',
'label' => __('Trash'),
'count' => Newsletter::getTrashed()->count()
'count' => $query_function(Newsletter::getTrashed())->count()
)
);
}
static function groupBy($orm, $group = null) {
if($group === 'trash') {
return $orm->whereNotNull('deleted_at');
$orm->whereNotNull('deleted_at');
}
return $orm->whereNull('deleted_at');
$orm->whereNull('deleted_at');
return $orm;
}
static function createOrUpdate($data = array()) {

View File

@ -216,19 +216,52 @@ class Newsletters {
}
}
function listing($data = array()) {
function listingStandard($data = array()) {
$listing = new Listing\Handler(
'\MailPoet\Models\Newsletter',
$data
$data,
function($orm) {
return $orm ->where('type', Newsletter::TYPE_STANDARD);
}
);
$listing_data = $listing->get();
return $this->getListingData($listing, $data);
}
function listingWelcome($data = array()) {
$listing = new Listing\Handler(
'\MailPoet\Models\Newsletter',
$data,
function($orm) {
return $orm->where('type', Newsletter::TYPE_WELCOME);
}
);
return $this->getListingData($listing, $data);
}
function listingNotification($data = array()) {
$listing = new Listing\Handler(
'\MailPoet\Models\Newsletter',
$data,
function($orm) {
return $orm->where('type', Newsletter::TYPE_NOTIFICATION);
}
);
return $this->getListingData($listing, $data);
}
function getListingData($listing, $data = array()) {
$listing_data = $listing->getData();
foreach($listing_data['items'] as $key => $newsletter) {
$newsletter = $newsletter
->withSegments()
->withSendingQueue();
if((boolean) Setting::getValue('tracking.enabled')) {
if((bool) Setting::getValue('tracking.enabled')) {
$newsletter = $newsletter->withStatistics();
}
$listing_data['items'][$key] = $newsletter->asArray();