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(); this.clearSelection();
const action = (this.props.action !== undefined)
? this.props.action
: 'listing';
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: this.props.endpoint, endpoint: this.props.endpoint,
action: 'listing', action: action,
data: { data: {
offset: (this.state.page - 1) * this.state.limit, offset: (this.state.page - 1) * this.state.limit,
limit: this.state.limit, limit: this.state.limit,

View File

@@ -295,6 +295,31 @@ define(
limit={ mailpoet_listing_per_page } limit={ mailpoet_listing_per_page }
params={ this.props.params } params={ this.props.params }
endpoint="newsletters" 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} onRenderItem={this.renderItem}
columns={columns} columns={columns}
bulk_actions={ bulk_actions } bulk_actions={ bulk_actions }

View File

@@ -8,11 +8,22 @@ class Handler {
private $data = array(); private $data = array();
private $model = null; 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( $this->data = array(
// pagination // pagination
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0), 'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
@@ -32,11 +43,6 @@ class Handler {
// selection // selection
'selection' => (isset($data['selection']) ? $data['selection'] : null) 'selection' => (isset($data['selection']) ? $data['selection'] : null)
); );
$this->setFilter();
$this->setSearch();
$this->setGroup();
$this->setOrder();
} }
private function setSearch() { private function setSearch() {
@@ -83,7 +89,24 @@ class Handler {
}, $models); }, $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(); $count = $this->model->count();
$items = $this->model $items = $this->model
@@ -91,10 +114,11 @@ class Handler {
->limit($this->data['limit']) ->limit($this->data['limit'])
->findMany(); ->findMany();
return array( return array(
'count' => $count, 'count' => $count,
'filters' => $this->model->filter('filters', $this->data['group']), 'filters' => $filters,
'groups' => $this->model->filter('groups'), 'groups' => $groups,
'items' => $items 'items' => $items
); );
} }

View File

@@ -5,7 +5,9 @@ if(!defined('ABSPATH')) exit;
class Newsletter extends Model { class Newsletter extends Model {
public static $_table = MP_NEWSLETTERS_TABLE; public static $_table = MP_NEWSLETTERS_TABLE;
const TYPE_WELCOME = 'wecome';
const TYPE_STANDARD = 'standard';
const TYPE_WELCOME = 'welcome';
const TYPE_NOTIFICATION = 'notification'; const TYPE_NOTIFICATION = 'notification';
function __construct() { function __construct() {
@@ -127,7 +129,7 @@ class Newsletter extends Model {
return $orm->where_like('subject', '%' . $search . '%'); 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(); $segments = Segment::orderByAsc('name')->findMany();
$segment_list = array(); $segment_list = array();
$segment_list[] = array( $segment_list[] = array(
@@ -136,13 +138,16 @@ class Newsletter extends Model {
); );
foreach($segments as $segment) { foreach($segments as $segment) {
$newsletters_count = $segment->newsletters() $newsletters = $segment->newsletters()->filter('groupBy', $group);
->filter('groupBy', $group) if($query_function !== false) {
->count(); $newsletters = $query_function($newsletters);
}
$newsletters_count = $newsletters->count();
if($newsletters_count > 0) { if($newsletters_count > 0) {
$segment_list[] = array( $segment_list[] = array(
'label' => sprintf('%s (%d)', $segment->name, $newsletters_count), 'label' => sprintf('%s (%d)', $segment->name, $newsletters_count),
'value' => $segment->id() 'value' => $segment->id
); );
} }
} }
@@ -190,26 +195,27 @@ class Newsletter extends Model {
return $orm; return $orm;
} }
static function groups() { static function groups($query_function = false) {
return array( return array(
array( array(
'name' => 'all', 'name' => 'all',
'label' => __('All'), 'label' => __('All'),
'count' => Newsletter::getPublished()->count() 'count' => $query_function(Newsletter::getPublished())->count()
), ),
array( array(
'name' => 'trash', 'name' => 'trash',
'label' => __('Trash'), 'label' => __('Trash'),
'count' => Newsletter::getTrashed()->count() 'count' => $query_function(Newsletter::getTrashed())->count()
) )
); );
} }
static function groupBy($orm, $group = null) { static function groupBy($orm, $group = null) {
if($group === 'trash') { 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()) { 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( $listing = new Listing\Handler(
'\MailPoet\Models\Newsletter', '\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) { foreach($listing_data['items'] as $key => $newsletter) {
$newsletter = $newsletter $newsletter = $newsletter
->withSegments() ->withSegments()
->withSendingQueue(); ->withSendingQueue();
if((boolean) Setting::getValue('tracking.enabled')) {
if((bool) Setting::getValue('tracking.enabled')) {
$newsletter = $newsletter->withStatistics(); $newsletter = $newsletter->withStatistics();
} }
$listing_data['items'][$key] = $newsletter->asArray(); $listing_data['items'][$key] = $newsletter->asArray();