Split the jqGrid code out a bit further, giving the application controller full responsibility for handling the jqGrid requests. Virtual functions have been added to allow a derived controller to customize this action as needed.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@111 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-14 00:53:08 +00:00
parent e7b4b21328
commit 90a16c721e
2 changed files with 193 additions and 87 deletions

View File

@@ -46,45 +46,77 @@ class CustomersController extends AppController {
/**************************************************************************
**************************************************************************
**************************************************************************
* action: jqGridData
* - Fetches the actual data requested by jqGrid as XML
* virtuals: jqGridData
* - With the application controller handling the jqGridData action,
* these virutal functions ensure that the correct data is passed
* to jqGrid.
*/
function jqGridData() {
function jqGridDataSetup(&$params) {
parent::jqGridDataSetup($params);
if (!isset($params['action']))
$params['action'] = 'all';
}
$action = 'all';
if (isset($this->params['url']['action']))
$action = $this->params['url']['action'];
// Set up the basic query
$query = array
function jqGridDataTables(&$params) {
return array
('link' =>
array(// Models
'PrimaryContact',
'CurrentLease' => array('fields' => array()),
),
'fields' => 'Customer.*, COUNT(CurrentLease.id) AS lease_count',
);
}
// Calculate the number of rows for the query, and figure out
// any special query conditions that will be necessary
$records = $this->Customer->find('count', array('contain'=>false));
if ($action != 'all') {
$records_past = $this->Customer->find('count',
array('link' => array('CurrentLease' => array('fields' => array())),
'conditions' => 'CurrentLease.id IS NULL'));
function jqGridDataFields(&$params) {
return array('Customer.*',
'COUNT(CurrentLease.id) AS lease_count');
}
if ($action == 'current') {
$records = $records - $records_past;
$query['conditions'][] = 'CurrentLease.id IS NOT NULL';
}
elseif ($action == 'past') {
$records = $records_past;
$query['conditions'][] = 'CurrentLease.id IS NULL';
}
function jqGridDataConditions(&$params) {
$conditions = parent::jqGridDataConditions($params);
if ($params['action'] === 'current') {
$conditions[] = 'CurrentLease.id IS NOT NULL';
}
elseif ($params['action'] === 'past') {
$conditions[] = 'CurrentLease.id IS NULL';
}
parent::jqGridData($this->Customer, $records, $query);
return $conditions;
}
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
// group by for the query, which will destroy the count,
// whether we omit the group by or leave it in.
// So, build a fresh query for counting.
$query['conditions'] = parent::jqGridDataConditions($params);
$count = $model->find('count',
array_merge(array('link' => array_diff_key($query['link'],
array('CurrentLease'=>1))),
array_diff_key($query, array('link'=>1))));
if ($params['action'] === 'all')
return $count;
$query['conditions'][] = 'CurrentLease.id IS NULL';
$count_past = $model->find('count', $query);
// Since we can't easily count 'current' directly, we
// can quickly derive it since 'current' customers
// are mutually exclusive to 'past' customers.
if ($params['action'] == 'current')
$count = $count - $count_past;
elseif ($params['action'] == 'past') {
$count = $count_past;
}
return $count;
}