Things are finally working fairly well for the customers grid (although I've not yet taken any speed issues into account). I removed the index.ctp file, since it was largely useless and I figured out how to render directly to the element. I also moved most of the jqGrid XML code into the application controller, since it will be reused by most of our controllers.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@108 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -55,5 +55,108 @@ class AppController extends Controller {
|
||||
$this->set('sidemenu', $this->sideMenuLinks());
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* action: jqGridData
|
||||
* - Fetches the actual data requested by jqGrid as XML
|
||||
*/
|
||||
|
||||
function jqGridData($model, $records, $query) {
|
||||
// Assume we're debugging.
|
||||
// The actual jqGrid request will set this to false
|
||||
$debug = true;
|
||||
|
||||
$model_alias = $model->alias;
|
||||
$model_id = $model_alias . '.id';
|
||||
|
||||
if (isset($this->passedArgs['debug']))
|
||||
$debug = $this->passedArgs['debug'];
|
||||
|
||||
if (!$debug) {
|
||||
$this->layout = null;
|
||||
$this->autoLayout = false;
|
||||
$this->autoRender = false;
|
||||
Configure::write('debug', '0');
|
||||
}
|
||||
|
||||
// Set up some defaults
|
||||
$page = 1; // page number
|
||||
$rows = 20; // rows in the grid - rowNum parameter
|
||||
$sidx = $model_id; // sort column - index from colModel
|
||||
$sord = 'ASC'; // sort order
|
||||
|
||||
// Extract the actual settings from the jqGrid request
|
||||
if (isset($this->params['url']) && is_array($this->params['url']))
|
||||
extract($this->params['url']);
|
||||
|
||||
// Unserialize our complex structure of fields
|
||||
// This SHOULD be always set, except when debugging
|
||||
if (isset($fields))
|
||||
$fields = unserialize($fields);
|
||||
else
|
||||
$fields = array($model_id);
|
||||
|
||||
// Verify a few parameters and determine our starting row
|
||||
$total = ($records < 0) ? 0 : ceil($records/$rows);
|
||||
$page = ($page < 1) ? 1 : (($page > $total) ? $total : $page);
|
||||
$start = $rows*$page - $rows;
|
||||
|
||||
// the actual query for the grid data
|
||||
$query['group'] = $model_id;
|
||||
$query['order'] = "$sidx $sord";
|
||||
$query['limit'] = "$start, $rows";
|
||||
$results = $model->find('all', $query);
|
||||
|
||||
if ($debug) {
|
||||
ob_start();
|
||||
print_r(compact('records', 'rows', 'total', 'page', 'start'));
|
||||
}
|
||||
else {
|
||||
header("Content-type: text/xml;charset=utf-8");
|
||||
}
|
||||
|
||||
echo "<?xml version='1.0' encoding='utf-8'?>\n";
|
||||
echo "<rows>\n";
|
||||
echo " <page>$page</page>\n";
|
||||
echo " <total>$total</total>\n";
|
||||
echo " <records>$records</records>\n";
|
||||
|
||||
foreach ($results AS $result) {
|
||||
// Add the calculated fields (if any), to the model fields
|
||||
if (isset($result[0]))
|
||||
$result[$model_alias] += $result[0];
|
||||
|
||||
echo " <row id='{$result[$model_alias]['id']}'>\n";
|
||||
foreach ($fields AS $field) {
|
||||
list($tbl, $col) = explode(".", $field);
|
||||
if (isset($col))
|
||||
$data = $result[$tbl][$col];
|
||||
else
|
||||
$data = $result[$model_alias][$tbl];
|
||||
|
||||
// be sure to put text data in CDATA
|
||||
if (preg_match("/^\d*$/", $data))
|
||||
echo " <cell>$data</cell>\n";
|
||||
else
|
||||
echo " <cell><![CDATA[$data]]></cell>\n";
|
||||
}
|
||||
echo " </row>\n";
|
||||
}
|
||||
echo "</rows>\n";
|
||||
|
||||
if ($debug) {
|
||||
$xml = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
$xml = preg_replace("/&/", "&", $xml);
|
||||
$xml = preg_replace("/</", "<", $xml);
|
||||
$xml = preg_replace("/>/", ">", $xml);
|
||||
|
||||
echo ("\n<PRE>\n$xml\n</PRE>\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
@@ -34,7 +34,7 @@ class CustomersController extends AppController {
|
||||
// The resulting page will contain a jqGrid, which will
|
||||
// use ajax to obtain the actual data for this action
|
||||
$this->set('action', $action);
|
||||
$this->render('index');
|
||||
$this->render('/elements/customers');
|
||||
}
|
||||
|
||||
function index() { $this->current(); }
|
||||
@@ -51,159 +51,40 @@ class CustomersController extends AppController {
|
||||
*/
|
||||
|
||||
function jqGridData() {
|
||||
// Assume we're debugging.
|
||||
// The actual jqGrid request will set this to false
|
||||
$debug = true;
|
||||
|
||||
if (isset($this->passedArgs['debug']))
|
||||
$debug = $this->passedArgs['debug'];
|
||||
$action = 'all';
|
||||
if (isset($this->params['url']['action']))
|
||||
$action = $this->params['url']['action'];
|
||||
|
||||
if (!$debug) {
|
||||
$this->layout = null;
|
||||
$this->autoLayout = false;
|
||||
$this->autoRender = false;
|
||||
Configure::write('debug', '0');
|
||||
}
|
||||
|
||||
// Set up some defaults
|
||||
$page = 1; // page number
|
||||
$rows = 20; // rows in the grid - rowNum parameter
|
||||
$sidx = 'Customer.id'; // sort column - index from colModel
|
||||
$sord = 'ASC'; // sort order
|
||||
$action = 'current'; // specific customer list
|
||||
|
||||
// Extract the actual settings from the jqGrid request
|
||||
if (isset($this->params['url']) && is_array($this->params['url']))
|
||||
extract($this->params['url']);
|
||||
|
||||
// Unserialize our complex structure
|
||||
if (isset($fields))
|
||||
$fields = unserialize($fields);
|
||||
else
|
||||
$fields = array('Customer.id', 'Customer.name');
|
||||
|
||||
// Set up the basic 'contain' field to query the
|
||||
// pertinent information.
|
||||
$contain =
|
||||
array(// Models
|
||||
// Set up the basic query
|
||||
$query = array
|
||||
('link' =>
|
||||
array(// Models
|
||||
'PrimaryContact',
|
||||
/* array(// Models */
|
||||
/* 'ContactPhone', */
|
||||
/* 'ContactEmail', */
|
||||
/* 'ContactAddress', */
|
||||
/* ), */
|
||||
|
||||
/* 'Account' => */
|
||||
/* array('fields' => array('Account.id')), */
|
||||
|
||||
'CurrentLease' =>
|
||||
array('fields' => array(),
|
||||
'Unit' =>
|
||||
array('fields' => array('name')),
|
||||
),
|
||||
);
|
||||
'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
|
||||
$count = $this->Customer->find('count');
|
||||
$records = $this->Customer->find('count', array('contain'=>false));
|
||||
if ($action != 'all') {
|
||||
$count_past = $this->Customer->find('count',
|
||||
array('link' => array('CurrentLease'),
|
||||
'conditions' => 'CurrentLease.id IS NULL'));
|
||||
$records_past = $this->Customer->find('count',
|
||||
array('link' => array('CurrentLease' => array('fields' => array())),
|
||||
'conditions' => 'CurrentLease.id IS NULL'));
|
||||
|
||||
if ($action == 'current') {
|
||||
$count = $count - $count_past;
|
||||
$contain['CurrentLease']['conditions'] = 'CurrentLease.id IS NOT NULL';
|
||||
$records = $records - $records_past;
|
||||
$query['conditions'][] = 'CurrentLease.id IS NOT NULL';
|
||||
}
|
||||
elseif ($action == 'past') {
|
||||
$count = $count_past;
|
||||
$contain['CurrentLease']['conditions'] = 'CurrentLease.id IS NULL';
|
||||
$records = $records_past;
|
||||
$query['conditions'][] = 'CurrentLease.id IS NULL';
|
||||
}
|
||||
}
|
||||
|
||||
// Verify a few parameters and determine our starting row
|
||||
$total_pages = ($count < 0) ? 0 : ceil($count/$rows);
|
||||
$page = ($page < 1) ? 1 : (($page > $total_pages) ? $total_pages : $page);
|
||||
$start = $rows*$page - $rows;
|
||||
|
||||
// the actual query for the grid data
|
||||
$customers = $this->Customer->find
|
||||
('all',
|
||||
array('contain' => $contain,
|
||||
'order' => "$sidx $sord",
|
||||
'limit' => "$start, $rows",
|
||||
));
|
||||
|
||||
/* $query['order'] = "$sidx $sord"; */
|
||||
/* $query['limit'] = "$start, $rows"; */
|
||||
/* $query['group'] = 'Customer.id', */
|
||||
/* $query = array('link' => */
|
||||
/* array(// Models */
|
||||
/* 'PrimaryContact', */
|
||||
/* 'CurrentLease' => */
|
||||
/* array('fields' => array(), */
|
||||
/* 'Unit' => */
|
||||
/* array('fields' => array('name')), */
|
||||
/* ), */
|
||||
/* ), */
|
||||
/* ); */
|
||||
|
||||
if ($debug) {
|
||||
ob_start();
|
||||
print_r(compact('count', 'rows', 'total_pages', 'page', 'start', 'customers'));
|
||||
}
|
||||
else {
|
||||
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
|
||||
header("Content-type: application/xhtml+xml;charset=utf-8");
|
||||
} else {
|
||||
header("Content-type: text/xml;charset=utf-8");
|
||||
}
|
||||
|
||||
echo "<?xml version='1.0' encoding='utf-8'?>\n";
|
||||
}
|
||||
|
||||
echo "<rows>\n";
|
||||
/* echo " <parms>\n"; */
|
||||
/* echo " <a><![CDATA[{$a}]]></a>\n"; */
|
||||
/* echo " <b><![CDATA[{$b}]]></b>\n"; */
|
||||
/* echo " <c><![CDATA[{$c}]]></c>\n"; */
|
||||
/* echo " <d><![CDATA[{$d}]]></d>\n"; */
|
||||
/* echo " <named><![CDATA[" . print_r($this->params, true) . "]]></named>\n"; */
|
||||
/* echo " <passed><![CDATA[" . print_r($this->passedArgs, true) . "]]></passed>\n"; */
|
||||
/* echo " <params><![CDATA[" . print_r($params, true) . "]]></params>\n"; */
|
||||
/* echo " <tp><![CDATA[" . print_r($tp, true) . "]]></tp>\n"; */
|
||||
/* echo " </parms>\n"; */
|
||||
echo " <page>".$page."</page>\n";
|
||||
echo " <total>".$total_pages."</total>\n";
|
||||
echo " <records>".$count."</records>\n";
|
||||
|
||||
// be sure to put text data in CDATA
|
||||
foreach ($customers AS $customer) {
|
||||
$customer['Customer']['units'] =
|
||||
implode("; ",
|
||||
array_map(create_function
|
||||
('$lease', 'return $lease["Unit"]["name"];'),
|
||||
$customer['CurrentLease']));
|
||||
|
||||
|
||||
echo " <row id='{$customer['Customer']['id']}'>\n";
|
||||
foreach ($fields AS $field) {
|
||||
list($tbl, $col) = explode(".", $field);
|
||||
echo " <cell><![CDATA[{$customer[$tbl][$col]}]]></cell>\n";
|
||||
}
|
||||
echo " </row>\n";
|
||||
}
|
||||
echo "</rows>\n";
|
||||
|
||||
if ($debug) {
|
||||
$xml = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
$xml = preg_replace("/&/", "&", $xml);
|
||||
$xml = preg_replace("/</", "<", $xml);
|
||||
$xml = preg_replace("/>/", ">", $xml);
|
||||
|
||||
echo ("\n<PRE>\n$xml\n</PRE>\n");
|
||||
}
|
||||
parent::jqGridData($this->Customer, $records, $query);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
<div class="customers index">
|
||||
<?php echo $this->element('customers', array('heading' => '<h2>'.$heading.'</h2>')) ?>
|
||||
</div>
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php /* -*- mode:PHP -*- */
|
||||
|
||||
//unset($caption);
|
||||
if (!isset($caption))
|
||||
$caption = '<H2>'.$this->pageTitle.'</H2>';
|
||||
|
||||
if (!isset($limit))
|
||||
$limit = 20;
|
||||
|
||||
@@ -8,7 +10,7 @@ if (!isset($limitOptions))
|
||||
$limitOptions = array(10, 20, 50, 200);
|
||||
|
||||
if (!isset($height))
|
||||
$height = '80%';
|
||||
$height = 'auto';
|
||||
|
||||
// Do some prework to bring in the appropriate libraries
|
||||
$imgpath = '/pmgr/site/css/jqGrid/basic/images';
|
||||
@@ -26,12 +28,12 @@ if (isset($customers[0]['ContactsCustomer']))
|
||||
$cols['Name'] = array('index' => 'Customer.name', 'width' => '150', 'align' => 'left');
|
||||
$cols['Last Name'] = array('index' => 'PrimaryContact.last_name', 'width' => '100', 'align' => 'left');
|
||||
$cols['First Name'] = array('index' => 'PrimaryContact.first_name', 'width' => '100', 'align' => 'left');
|
||||
$cols['Unit(s)'] = array('index' => 'Customer.units', 'width' => '80', 'align' => 'center');
|
||||
$cols['Comment'] = array('index' => 'Customer.comment', 'width' => '300', 'align' => 'center');
|
||||
$cols['Leases'] = array('index' => 'lease_count', 'width' => '60', 'align' => 'center');
|
||||
$cols['Comment'] = array('index' => 'Customer.comment', 'width' => '300', 'align' => 'left');
|
||||
|
||||
// Some of the columns should not be sortable
|
||||
foreach (array('Unit(s)') AS $field)
|
||||
$cols[$field]['sortable'] = false;
|
||||
foreach (array_intersect_key($cols, array('Comment'=>1)) AS $k => $v)
|
||||
$cols[$k]['sortable'] = false;
|
||||
|
||||
// Create the javascript code for jqGrid to create each table column
|
||||
$colModels = array();
|
||||
@@ -79,20 +81,31 @@ jQuery(document).ready(function(){
|
||||
colNames: [ '<?php echo implode("', '", array_keys($cols)); ?>' ],
|
||||
colModel: [ <?php echo "\n " . implode(",\n ", $colModels); ?> ],
|
||||
pager: jQuery('#customer-pager'),
|
||||
height: '100%',
|
||||
//height: 'auto',
|
||||
height: '<?php echo $height; ?>',
|
||||
rowNum: <?php echo $limit; ?>,
|
||||
rowList: [<?php echo implode(",",$limitOptions); ?>],
|
||||
sortname: 'Customer.id',
|
||||
sortorder: "ASC",
|
||||
viewrecords: true,
|
||||
imgpath: '<?php echo $imgpath; ?>',
|
||||
caption: <?php echo isset($caption) ? "'$caption'" : "null"; ?>,
|
||||
caption: <?php echo $caption ? "'$caption'" : "null"; ?>,
|
||||
});
|
||||
});
|
||||
|
||||
--></script>
|
||||
|
||||
<div id="everything">
|
||||
<table id="customer-list" class="scroll"></table>
|
||||
<div id="customer-pager" class="scroll" style="text-align:center;"></div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
/* <div><a href="#" onClick="$('#debug').html(htmlEncode($('#everything').html())); return false;">Get Table Code</a></div> */
|
||||
/* <div id="debug"></div> */
|
||||
|
||||
/* <P> */
|
||||
/* <input type="text" id="debval" value="1.0em"><BR> */
|
||||
/* <a href="#" onClick="$('#load_customer-list').css('display', 'inline'); return false;">Show Loading</a><BR> */
|
||||
/* <a href="#" onClick="$('#load_customer-list').css('margin-top', $('#debval').val()); return false;">Set Top Margin</a><BR> */
|
||||
/* <a href="#" onClick="$('#load_customer-list').css('margin-left', $('#debval').val()); return false;">Set Left Margin</a><BR> */
|
||||
?>
|
||||
Reference in New Issue
Block a user