diff --git a/lib/API/JSON/v1/Forms.php b/lib/API/JSON/v1/Forms.php index 5eaec5777d..64be673ae0 100644 --- a/lib/API/JSON/v1/Forms.php +++ b/lib/API/JSON/v1/Forms.php @@ -39,12 +39,8 @@ class Forms extends APIEndpoint { } function listing($data = array()) { - $listing = new Listing\Handler( - '\MailPoet\Models\Form', - $data - ); - - $listing_data = $listing->get(); + $listing = new Listing\Handler(); + $listing_data = $listing->get('\MailPoet\Models\Form', $data); $data = array(); foreach($listing_data['items'] as $form) { diff --git a/lib/API/JSON/v1/Newsletters.php b/lib/API/JSON/v1/Newsletters.php index 7379ae0e45..81c97ef5d8 100644 --- a/lib/API/JSON/v1/Newsletters.php +++ b/lib/API/JSON/v1/Newsletters.php @@ -383,11 +383,8 @@ class Newsletters extends APIEndpoint { } function listing($data = array()) { - $listing = new Listing\Handler( - '\MailPoet\Models\Newsletter', - $data - ); - $listing_data = $listing->get(); + $listing = new Listing\Handler(); + $listing_data = $listing->get('\MailPoet\Models\Newsletter', $data); $data = array(); foreach($listing_data['items'] as $newsletter) { diff --git a/lib/API/JSON/v1/Segments.php b/lib/API/JSON/v1/Segments.php index d6410ef6ac..ae43129cb7 100644 --- a/lib/API/JSON/v1/Segments.php +++ b/lib/API/JSON/v1/Segments.php @@ -36,12 +36,8 @@ class Segments extends APIEndpoint { } function listing($data = array()) { - $listing = new Listing\Handler( - '\MailPoet\Models\Segment', - $data - ); - - $listing_data = $listing->get(); + $listing = new Listing\Handler(); + $listing_data = $listing->get('\MailPoet\Models\Segment', $data); $data = array(); foreach($listing_data['items'] as $segment) { diff --git a/lib/API/JSON/v1/Subscribers.php b/lib/API/JSON/v1/Subscribers.php index 9733c97b33..49c2df580a 100644 --- a/lib/API/JSON/v1/Subscribers.php +++ b/lib/API/JSON/v1/Subscribers.php @@ -56,9 +56,9 @@ class Subscribers extends APIEndpoint { function listing($data = array()) { if(!isset($data['filter']['segment'])) { - $listing = new Listing\Handler('\MailPoet\Models\Subscriber', $data); + $listing = new Listing\Handler(); - $listing_data = $listing->get(); + $listing_data = $listing->get('\MailPoet\Models\Subscriber', $data); } else { $listings = new SubscribersListings(); $listing_data = $listings->getListingsInSegment($data); diff --git a/lib/Listing/BulkActionController.php b/lib/Listing/BulkActionController.php index b35717569b..b1241230ed 100644 --- a/lib/Listing/BulkActionController.php +++ b/lib/Listing/BulkActionController.php @@ -14,14 +14,11 @@ class BulkActionController { ); } - $listing_handler = new Handler( - $model_class, - $data['listing'] - ); + $listing_handler = new Handler(); return call_user_func_array( array($model_class, $bulk_action_method), - array($listing_handler->getSelection(), $data) + array($listing_handler->getSelection($model_class, $data['listing']), $data) ); } } diff --git a/lib/Listing/Handler.php b/lib/Listing/Handler.php index 501675190b..989926aaec 100644 --- a/lib/Listing/Handler.php +++ b/lib/Listing/Handler.php @@ -6,15 +6,120 @@ if(!defined('ABSPATH')) exit; class Handler { const DEFAULT_LIMIT_PER_PAGE = 20; - private $data = array(); - private $model = null; - private $model_class = null; + function getSelection($model_class, array $data) { + $data = $this->processData($data); + $table_name = $model_class::$_table; + $model = \Model::factory($model_class); - function __construct($model_class, $data = array()) { - $this->table_name = $model_class::$_table; - $this->model_class = $model_class; - $this->model = \Model::factory($this->model_class); + if(method_exists($model_class, 'listingQuery')) { + $custom_query = call_user_func_array( + [$model_class, 'listingQuery'], + [$data] + ); + if(!empty($data['selection'])) { + $custom_query->whereIn($table_name.'.id', $data['selection']); + } + return $custom_query; + } else { + $model = $this->setFilter($model, $data); + $this->setGroup($model, $data); + $this->setSearch($model, $data); + if(!empty($data['selection'])) { + $model->whereIn($table_name.'.id', $data['selection']); + } + return $model; + } + } + + function get($model_class, array $data) { + $data = $this->processData($data); + $table_name = $model_class::$_table; + $model = \Model::factory($model_class); + // get groups + $groups = array(); + if(method_exists($model_class, 'groups')) { + $groups = call_user_func_array( + array($model_class, 'groups'), + array($data) + ); + } + + // get filters + $filters = array(); + if(method_exists($model_class, 'filters')) { + $filters = call_user_func_array( + array($model_class, 'filters'), + array($data) + ); + } + + // get items and total count + if(method_exists($model_class, 'listingQuery')) { + $custom_query = call_user_func_array( + array($model_class, 'listingQuery'), + array($data) + ); + + $count = $custom_query->count(); + + $items = $custom_query + ->offset($data['offset']) + ->limit($data['limit']) + ->{'order_by_'.$data['sort_order']}( + $table_name.'.'.$data['sort_by'] + ) + ->findMany(); + } else { + $model = $this->setFilter($model, $data); + $this->setGroup($model, $data); + $this->setSearch($model, $data); + $this->setOrder($model, $data, $table_name); + + $count = $model->count(); + + $items = $model + ->offset($data['offset']) + ->limit($data['limit']) + ->findMany(); + } + + return array( + 'count' => $count, + 'filters' => $filters, + 'groups' => $groups, + 'items' => $items + ); + } + + private function setSearch(\ORMWrapper $model, array $data) { + if(empty($data['search'])) { + return; + } + return $model->filter('search', $data['search']); + } + + private function setOrder(\ORMWrapper $model, array $data, $table_name) { + return $model + ->{'order_by_'.$data['sort_order']}( + $table_name.'.'.$data['sort_by']); + } + + private function setGroup(\ORMWrapper $model, array $data) { + if($data['group'] === null) { + return; + } + $model->filter('groupBy', $data['group']); + } + + private function setFilter(\ORMWrapper $model, array $data) { + if($data['filter'] === null) { + return $model; + } + return $model->filter('filterBy', $data['filter']); + } + + private function processData(array $data) { // check if sort order was specified or default to "asc" $sort_order = (!empty($data['sort_order'])) ? $data['sort_order'] : 'asc'; // constrain sort order value to either be "asc" or "desc" @@ -29,9 +134,9 @@ class Handler { $sort_by = 'id'; } - $this->data = array( + $data = [ // extra parameters - 'params' => (isset($data['params']) ? $data['params'] : array()), + 'params' => (isset($data['params']) ? $data['params'] : []), // pagination 'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0), 'limit' => (isset($data['limit']) @@ -49,112 +154,8 @@ class Handler { 'filter' => (isset($data['filter']) ? $data['filter'] : null), // selection 'selection' => (isset($data['selection']) ? $data['selection'] : null) - ); + ]; + + return $data; } - - private function setSearch() { - if(empty($this->data['search'])) { - return; - } - return $this->model->filter('search', $this->data['search']); - } - - private function setOrder() { - return $this->model - ->{'order_by_'.$this->data['sort_order']}( - $this->table_name.'.'.$this->data['sort_by']); - } - - private function setGroup() { - if($this->data['group'] === null) { - return; - } - return $this->model->filter('groupBy', $this->data['group']); - } - - private function setFilter() { - if($this->data['filter'] === null) { - return; - } - $this->model = $this->model->filter('filterBy', $this->data['filter']); - } - - function getSelection() { - if(method_exists($this->model_class, 'listingQuery')) { - $custom_query = call_user_func_array( - array($this->model_class, 'listingQuery'), - array($this->data) - ); - if(!empty($this->data['selection'])) { - $custom_query->whereIn($this->table_name.'.id', $this->data['selection']); - } - return $custom_query; - } else { - $this->setFilter(); - $this->setGroup(); - $this->setSearch(); - - if(!empty($this->data['selection'])) { - $this->model->whereIn($this->table_name.'.id', $this->data['selection']); - } - return $this->model; - } - } - - function get() { - // get groups - $groups = array(); - if(method_exists($this->model_class, 'groups')) { - $groups = call_user_func_array( - array($this->model_class, 'groups'), - array($this->data) - ); - } - - // get filters - $filters = array(); - if(method_exists($this->model_class, 'filters')) { - $filters = call_user_func_array( - array($this->model_class, 'filters'), - array($this->data) - ); - } - - // get items and total count - if(method_exists($this->model_class, 'listingQuery')) { - $custom_query = call_user_func_array( - array($this->model_class, 'listingQuery'), - array($this->data) - ); - - $count = $custom_query->count(); - - $items = $custom_query - ->offset($this->data['offset']) - ->limit($this->data['limit']) - ->{'order_by_'.$this->data['sort_order']}( - $this->table_name.'.'.$this->data['sort_by'] - ) - ->findMany(); - } else { - $this->setFilter(); - $this->setGroup(); - $this->setSearch(); - $this->setOrder(); - - $count = $this->model->count(); - - $items = $this->model - ->offset($this->data['offset']) - ->limit($this->data['limit']) - ->findMany(); - } - - return array( - 'count' => $count, - 'filters' => $filters, - 'groups' => $groups, - 'items' => $items - ); - } -} \ No newline at end of file +} diff --git a/lib/Segments/BulkAction.php b/lib/Segments/BulkAction.php index 5ff021fde0..d45dacface 100644 --- a/lib/Segments/BulkAction.php +++ b/lib/Segments/BulkAction.php @@ -2,6 +2,7 @@ namespace MailPoet\Segments; +use MailPoet\Listing\Handler; use MailPoet\Models\Segment; use MailPoet\WP\Hooks; @@ -36,7 +37,7 @@ class BulkAction { */ private function applySegment($segment) { if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) { - $bulk_action = new \MailPoet\Listing\BulkActionController(); + $bulk_action = new \MailPoet\Listing\BulkActionController(new Handler()); return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data); } else { $handlers = Hooks::applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array()); diff --git a/lib/Segments/SubscribersListings.php b/lib/Segments/SubscribersListings.php index 5d011e2c32..c1f592d8cf 100644 --- a/lib/Segments/SubscribersListings.php +++ b/lib/Segments/SubscribersListings.php @@ -19,9 +19,9 @@ class SubscribersListings { private function getListings($data, Segment $segment = null) { if(!$segment || $segment->type === Segment::TYPE_DEFAULT || $segment->type === Segment::TYPE_WP_USERS) { - $listing = new Handler('\MailPoet\Models\Subscriber', $data); + $listing = new Handler(); - return $listing_data = $listing->get(); + return $listing_data = $listing->get('\MailPoet\Models\Subscriber', $data); } $handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array()); foreach($handlers as $handler) {