diff --git a/site/app_controller.php b/site/app_controller.php index 6ed48b0..2c7784e 100644 --- a/site/app_controller.php +++ b/site/app_controller.php @@ -91,9 +91,9 @@ class AppController extends Controller { $model = $this->jqGridDataModel($params); // Establish the basic query and conditions - $query = array_intersect_key($this->jqGridDataTables($params), + $query = array_intersect_key($this->jqGridDataTables($params, $model), array('link'=>1, 'contain'=>1)); - $query['conditions'] = $this->jqGridDataConditions($params); + $query['conditions'] = $this->jqGridDataConditions($params, $model); // Get the number of records prior to pagination $count = $this->jqGridDataRecordCount($params, $model, $query); @@ -105,22 +105,22 @@ class AppController extends Controller { $start = $limit*$page - $limit; // Grab the actual records taking pagination into account - $query['group'] = $model->alias . '.id'; - $query['order'] = $this->jqGridDataOrder($params, + $query['group'] = $model->alias.'.'.$model->primaryKey; + $query['order'] = $this->jqGridDataOrder($params, $model, isset($params['sidx']) ? $params['sidx'] : null, isset($params['sord']) ? $params['sord'] : null); - $query['limit'] = $this->jqGridDataLimit($params, $start, $limit); - $query['fields'] = $this->jqGridDataFields($params); + $query['limit'] = $this->jqGridDataLimit($params, $model, $start, $limit); + $query['fields'] = $this->jqGridDataFields($params, $model); $results = $this->jqGridDataRecords($params, $model, $query); // DEBUG PURPOSES ONLY $params['query'] = $query; // Finally, dump out the data - $this->jqGridDataOutputHeader($params); + $this->jqGridDataOutputHeader($params, $model); echo "\n"; echo "\n"; - $this->jqGridDataOutputSummary($params, $page, $total, $count); + $this->jqGridDataOutputSummary($params, $model, $page, $total, $count); $this->jqGridDataOutputRecords($params, $model, $results); echo "\n"; @@ -174,11 +174,11 @@ class AppController extends Controller { return $this->{$this->modelClass}; } - function jqGridDataTables(&$params) { + function jqGridDataTables(&$params, &$model) { return array('contain' => false); } - function jqGridDataConditions(&$params) { + function jqGridDataConditions(&$params, &$model) { $searches = array(); if (isset($params['_search']) && $params['_search'] === 'true') { @@ -199,9 +199,6 @@ class AppController extends Controller { 'field' => $params['filtField'], 'value' => $params['filtValue']); } - else { - return array(); - } $ops = array('eq' => array('op' => null, 'pre' => '', 'post' => ''), 'ne' => array('op' => '<>', 'pre' => '', 'post' => ''), @@ -222,30 +219,34 @@ class AppController extends Controller { $conditions[] = array($field => $value); } + if (isset($params['action']) && $params['action'] === 'idlist') { + $conditions[] = array($model->alias.'.'.$model->primaryKey => unserialize($params['idlist'])); + } + return $conditions; } - function jqGridDataFields(&$params) { + function jqGridDataFields(&$params, &$model) { return null; } - function jqGridDataOrder(&$params, $index, $direction) { + function jqGridDataOrder(&$params, &$model, $index, $direction) { return $index ? array($index .' '. $direction) : null; } - function jqGridDataLimit(&$params, $start, $limit) { + function jqGridDataLimit(&$params, &$model, $start, $limit) { return $start . ', ' . $limit; } - function jqGridDataRecordCount(&$params, $model, $query) { + function jqGridDataRecordCount(&$params, &$model, $query) { return $model->find('count', $query); } - function jqGridDataRecords(&$params, $model, $query) { + function jqGridDataRecords(&$params, &$model, $query) { return $model->find('all', $query); } - function jqGridDataOutputHeader(&$params) { + function jqGridDataOutputHeader(&$params, &$model) { if ($params['debug']) { ob_start(); } @@ -254,14 +255,14 @@ class AppController extends Controller { } } - function jqGridDataOutputSummary(&$params, $page, $total, $records) { + function jqGridDataOutputSummary(&$params, &$model, $page, $total, $records) { echo " \n"; echo " $page\n"; echo " $total\n"; echo " $records\n"; } - function jqGridDataOutputRecords(&$params, $model, $records) { + function jqGridDataOutputRecords(&$params, &$model, $records) { $model_alias = $model->alias; foreach ($records AS $record) { diff --git a/site/controllers/contacts_controller.php b/site/controllers/contacts_controller.php index 33a62ab..d61d64d 100644 --- a/site/controllers/contacts_controller.php +++ b/site/controllers/contacts_controller.php @@ -35,8 +35,8 @@ class ContactsController extends AppController { * to jqGrid. */ - function jqGridDataOrder(&$params, $index, $direction) { - $order = parent::jqGridDataOrder($params, $index, $direction); + function jqGridDataOrder(&$params, &$model, $index, $direction) { + $order = parent::jqGridDataOrder($params, $model, $index, $direction); if ($index === 'Contact.last_name') { $order[] = 'Contact.first_name ' . $direction; } diff --git a/site/controllers/customers_controller.php b/site/controllers/customers_controller.php index 3734519..cffca1b 100644 --- a/site/controllers/customers_controller.php +++ b/site/controllers/customers_controller.php @@ -50,7 +50,7 @@ class CustomersController extends AppController { $params['action'] = 'all'; } - function jqGridDataTables(&$params) { + function jqGridDataTables(&$params, &$model) { return array ('link' => array(// Models @@ -60,13 +60,13 @@ class CustomersController extends AppController { ); } - function jqGridDataFields(&$params) { + function jqGridDataFields(&$params, &$model) { return array('Customer.*', 'COUNT(CurrentLease.id) AS lease_count'); } - function jqGridDataConditions(&$params) { - $conditions = parent::jqGridDataConditions($params); + function jqGridDataConditions(&$params, &$model) { + $conditions = parent::jqGridDataConditions($params, $model); if ($params['action'] === 'current') { $conditions[] = 'CurrentLease.id IS NOT NULL'; @@ -78,8 +78,8 @@ class CustomersController extends AppController { return $conditions; } - function jqGridDataOrder(&$params, $index, $direction) { - $order = parent::jqGridDataOrder($params, $index, $direction); + function jqGridDataOrder(&$params, &$model, $index, $direction) { + $order = parent::jqGridDataOrder($params, $model, $index, $direction); if ($index === 'PrimaryContact.last_name') { $order[] = 'PrimaryContact.first_name ' . $direction; } @@ -89,7 +89,7 @@ class CustomersController extends AppController { return $order; } - function jqGridDataRecordCount(&$params, $model, $query) { + function jqGridDataRecordCount(&$params, &$model, $query) { // We don't have a good way to use the query to obtain // our count. The problem is that we're relying on the @@ -97,7 +97,7 @@ class CustomersController extends AppController { // whether we omit the group by or leave it in. // So, build a fresh query for counting. - $query['conditions'] = parent::jqGridDataConditions($params); + $query['conditions'] = parent::jqGridDataConditions($params, $model); $count = $model->find('count', array_merge(array('link' => array_diff_key($query['link'], diff --git a/site/controllers/leases_controller.php b/site/controllers/leases_controller.php index 00420fb..1d4dac1 100644 --- a/site/controllers/leases_controller.php +++ b/site/controllers/leases_controller.php @@ -49,14 +49,14 @@ class LeasesController extends AppController { $params['action'] = 'all'; } - function jqGridDataTables(&$params) { + function jqGridDataTables(&$params, &$model) { return array ('link' => array('Unit' => array('fields' => array('Unit.name')), 'Customer' => array('fields' => array('Customer.name')))); } - function jqGridDataConditions(&$params) { - $conditions = parent::jqGridDataConditions($params); + function jqGridDataConditions(&$params, &$model) { + $conditions = parent::jqGridDataConditions($params, $model); if ($params['action'] === 'active') { $conditions[] = 'Lease.close_date IS NULL'; @@ -68,7 +68,7 @@ class LeasesController extends AppController { return $conditions; } - function jqGridDataRecords(&$params, $model, $query) { + function jqGridDataRecords(&$params, &$model, $query) { $leases = parent::jqGridDataRecords($params, $model, $query); // Get the balance on each lease. diff --git a/site/controllers/units_controller.php b/site/controllers/units_controller.php index 04c2975..5163f19 100644 --- a/site/controllers/units_controller.php +++ b/site/controllers/units_controller.php @@ -51,7 +51,7 @@ class UnitsController extends AppController { $params['action'] = 'all'; } - function jqGridDataTables(&$params) { + function jqGridDataTables(&$params, &$model) { $link = array ('link' => array(// Models @@ -70,8 +70,8 @@ class UnitsController extends AppController { return $link; } - function jqGridDataConditions(&$params) { - $conditions = parent::jqGridDataConditions($params); + function jqGridDataConditions(&$params, &$model) { + $conditions = parent::jqGridDataConditions($params, $model); if ($params['action'] === 'unavailable') { $conditions[] = $this->Unit->conditionUnavailable(); @@ -86,13 +86,14 @@ class UnitsController extends AppController { return $conditions; } - function jqGridDataOrder(&$params, $index, $direction) { + function jqGridDataOrder(&$params, &$model, $index, $direction) { if ($index === 'Unit.name') { $index = 'Unit.sort_order'; } - return parent::jqGridDataOrder($params, $index, $direction); + return parent::jqGridDataOrder($params, $model, $index, $direction); } + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/site/views/elements/contacts.ctp b/site/views/elements/contacts.ctp index ab2ec23..3bf6dfe 100644 --- a/site/views/elements/contacts.ctp +++ b/site/views/elements/contacts.ctp @@ -12,7 +12,18 @@ if (0) { // REVISIT: Need to figure out how to put this in play } $cols['Comment'] = array('index' => 'Contact.comment', 'formatter' => 'comment'); +//pr($contacts); +$jqGrid_options = array('jqGridColumns' => $cols, + 'controller' => 'contacts', + 'caption' => isset($caption) ? $caption : null); -echo $this->element('jqGrid', - array('jqGridColumns' => $cols)); +if (isset($contacts)) { + $jqGrid_options += array('custom_ids' => + array_map(create_function('$data', + 'return $data["id"];'), + $contacts), + 'limit' => 5); +} +//pr($jqGrid_options); +echo $this->element('jqGrid', $jqGrid_options); diff --git a/site/views/elements/jqGrid.ctp b/site/views/elements/jqGrid.ctp index 1c8fe2c..95af93f 100644 --- a/site/views/elements/jqGrid.ctp +++ b/site/views/elements/jqGrid.ctp @@ -6,14 +6,26 @@ if (!isset($caption)) if (!isset($limit)) $limit = 20; -if (!isset($limitOptions)) - $limitOptions = array(10, 20, 50, 200); +if (!isset($limitOptions)) { + $limitOptions = array(); + if ($limit < 10) + $limitOptions += array(2, 5); + if ($limit < 30) + $limitOptions += array(10, 25); + if ($limit > 10) + $limitOptions += array(50, 200); + if ($limit > 20) + $limitOptions += array(500); +} if (!isset($height)) $height = 'auto'; +if (!isset($controller)) + $controller = $this->params['controller']; + if (!isset($gridId)) - $gridId = $this->params['controller'] . '-jqGrid'; + $gridId = $controller . '-jqGrid'; // Do some prework to bring in the appropriate libraries $imgpath = '/pmgr/site/css/jqGrid/basic/images'; @@ -30,22 +42,35 @@ $javascript->link('jqGrid/js/jqDnR', false); // controller in order to be accessible to the view, // we'll just pass the desired fields to the controller // as part of the data fetch. - -$url = $html->url(array('action' => 'jqGridData', +$url = $html->url(array('controller' => $controller, + 'action' => 'jqGridData', 'debug' => 0, )); // Create extra parameters that jqGrid will pass to our -// controller whenever data is requested. 'action' will -// ensure that the controller provides the correct subset -// of data, and 'fields' will allow it to return only the +// controller whenever data is requested. +// 'fields' will allow the controller to return only the // requested fields, and in the right order. Since fields // is a complex structure (an array), we'll need to // serialize it first for transport over HTTP. -$postData = array('action' => $action, - 'fields' => serialize(array_map(create_function('$col', - 'return $col["index"];'), - $jqGridColumns))); +$postData = array(); +$postData['fields'] = serialize(array_map(create_function('$col', + 'return $col["index"];'), + array_values($jqGridColumns))); + +// Determine if we're to be using a custom list, or if +// the data will simply be action based. +if (isset($custom_ids)) { + $action = 'idlist'; + $postData['idlist'] = serialize($custom_ids); +} +elseif (!isset($action)) { + $action = null; +} + +// 'action' will ensure that the controller provides the +// correct subset of data +$postData['action'] = $action; // Perform column customizations.