From a2cd6e1434352736a80dc29c86d36d8ce4ab8955 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Mon, 29 Dec 2014 12:10:05 -0500 Subject: [PATCH] Add pagination support to Events in the API /api/events.json now accepts the ?page querystring, e.g. /api/events.json?page=2 or /api/events.json --- web/api/app/Controller/EventsController.php | 22 +++++++++++++++------ web/api/app/View/Events/json/index.ctp | 6 +++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/web/api/app/Controller/EventsController.php b/web/api/app/Controller/EventsController.php index df954ba67..9b92b0b7c 100644 --- a/web/api/app/Controller/EventsController.php +++ b/web/api/app/Controller/EventsController.php @@ -12,7 +12,7 @@ class EventsController extends AppController { * * @var array */ - public $components = array('RequestHandler', 'Scaler', 'Image'); + public $components = array('RequestHandler', 'Scaler', 'Image', 'Paginator'); /** * index method @@ -22,18 +22,28 @@ class EventsController extends AppController { */ public function index() { $this->Event->recursive = -1; - $events = $this->Event->find('all'); + // How many events to return + $this->loadModel('Config'); + $limit = $this->Config->find('list', array( + 'conditions' => array('Name' => 'ZM_WEB_EVENTS_PER_PAGE'), + 'fields' => array('Name', 'Value') + )); + $this->Paginator->settings = array( + 'limit' => $limit['ZM_WEB_EVENTS_PER_PAGE'], + 'order' => array('StartTime', 'MaxScore'), + 'paramType' => 'querystring' + ); + $events = $this->Paginator->paginate('Event'); + + // For each event, get its thumbnail data (path, width, height) foreach ($events as $key => $value) { $thumbData = $this->createThumbnail($value['Event']['Id']); $events[$key]['thumbData'] = $thumbData; } - $this->set(array( - 'events' => $events, - '_serialize' => array('events') - )); + $this->set(compact('events')); } /** diff --git a/web/api/app/View/Events/json/index.ctp b/web/api/app/View/Events/json/index.ctp index 13cb1244b..d54386749 100644 --- a/web/api/app/View/Events/json/index.ctp +++ b/web/api/app/View/Events/json/index.ctp @@ -1 +1,5 @@ -echo json_encode($events); +Paginator->params(); + echo json_encode($array); +?>