Fixed a bug in which jqGrid would fetch data from the current controller, not the controller responsible for populating the data. Added the ability to fetch data based strictly off of an ID list, which is useful for lists that are embedded in other pages, where the list conditions are entirely foreign to the controller responsible for providing the data. To assist with this one jqGrid virtual function needed the model added as a parameter, so I added it to them all and modified the jqGrid functions in the existing derived controllers. Right now the elements (such as contacts.ctp, the only working one) receive a full list of data to populate themselves. I could have simply taken this and passed it directly to jqGrid, with no need for further server interaction. However, this could result in some very large lists, and although pagination will ensure the user is not overwhelmed, the connection or the browser could be. Unless we have a strong performance need, I'll keep it this way. In the meantime, I'll leave the underlying data available to each element, in case I have to switch over. When all is proven out, I can simplify all the queries such that the extra data is never requested in the first place.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@122 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-14 22:05:44 +00:00
parent 2cbd5da8ee
commit b6317d3ee9
7 changed files with 92 additions and 54 deletions

View File

@@ -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 "<?xml version='1.0' encoding='utf-8'?>\n";
echo "<rows>\n";
$this->jqGridDataOutputSummary($params, $page, $total, $count);
$this->jqGridDataOutputSummary($params, $model, $page, $total, $count);
$this->jqGridDataOutputRecords($params, $model, $results);
echo "</rows>\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 " <params><![CDATA[\n" . print_r($params, true) . "\n]]></params>\n";
echo " <page>$page</page>\n";
echo " <total>$total</total>\n";
echo " <records>$records</records>\n";
}
function jqGridDataOutputRecords(&$params, $model, $records) {
function jqGridDataOutputRecords(&$params, &$model, $records) {
$model_alias = $model->alias;
foreach ($records AS $record) {

View File

@@ -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;
}

View File

@@ -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'],

View File

@@ -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.

View File

@@ -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);
}
/**************************************************************************
**************************************************************************
**************************************************************************

View File

@@ -12,7 +12,18 @@ if (0) { // REVISIT<AP>: 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);

View File

@@ -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.