Files
pmgr/app_controller.php
abijah cf0da7d3c2 Added query ability to the customer grid. There is a bug with the paging code, but it roughly works.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@109 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-06-13 21:20:11 +00:00

182 lines
6.3 KiB
PHP

<?php
/* SVN FILE: $Id: app_controller.php 7945 2008-12-19 02:16:01Z gwoo $ */
/**
* Short description for file.
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.app
* @since CakePHP(tm) v 0.2.9
* @version $Revision: 7945 $
* @modifiedby $LastChangedBy: gwoo $
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Short description for class.
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package cake
* @subpackage cake.app
*/
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Javascript', 'Format', 'Time');
var $components = array('DebugKit.Toolbar');
function sideMenuLinks() {
return array(
array('name' => 'Common', 'header' => true),
array('name' => 'Site Map', 'url' => array('controller' => 'maps', 'action' => 'view', 1)),
array('name' => 'Units', 'url' => array('controller' => 'units', 'action' => 'index')),
array('name' => 'Leases', 'url' => array('controller' => 'leases', 'action' => 'index')),
array('name' => 'Customers', 'url' => array('controller' => 'customers', 'action' => 'index')),
array('name' => 'Contacts', 'url' => array('controller' => 'contacts', 'action' => 'index')),
array('name' => 'Accounts', 'url' => array('controller' => 'accounts', 'action' => 'index')),
array('name' => 'Ledgers', 'url' => array('controller' => 'ledgers', 'action' => 'index')),
);
}
function beforeRender() {
$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);
if (isset($_search) && $_search === 'true') {
$ops = array('eq' => array('op' => null, 'pre' => '', 'post' => ''),
'ne' => array('op' => '<>', 'pre' => '', 'post' => ''),
'lt' => array('op' => '<', 'pre' => '', 'post' => ''),
'le' => array('op' => '<=', 'pre' => '', 'post' => ''),
'gt' => array('op' => '>', 'pre' => '', 'post' => ''),
'ge' => array('op' => '>=', 'pre' => '', 'post' => ''),
'bw' => array('op' => 'LIKE', 'pre' => '', 'post' => '%'),
'ew' => array('op' => 'LIKE', 'pre' => '%', 'post' => ''),
'cn' => array('op' => 'LIKE', 'pre' => '%', 'post' => '%'),
);
$op = $ops[$searchOper];
$field = $searchField . ($op['op'] ? ' '.$op['op'] : '');
$val = $op['pre'] . $searchString . $op['post'];
$query['conditions'][] = array($field => $val);
}
// 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 " <req><![CDATA[\n" . print_r($this->params['url'], true) . "\n]]></req>\n"; */
/* echo " <query><![CDATA[\n" . print_r($query, true) . "\n]]></query>\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("/&/", "&amp;", $xml);
$xml = preg_replace("/</", "&lt;", $xml);
$xml = preg_replace("/>/", "&gt;", $xml);
echo ("\n<PRE>\n$xml\n</PRE>\n");
}
}
}
?>