Another attempt to get the customer table working. Cake's query mechanism is really torquing me, it never seems to be able to do what I need (perhaps because they've generalized it to work with so many SQL engines. I'm just going to pull unit information out all together, and I can add it back in another day if needed.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@106 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-13 15:52:09 +00:00
parent 94c78ccb2a
commit 54291b4467
2 changed files with 82 additions and 162 deletions

View File

@@ -25,122 +25,39 @@ class CustomersController extends AppController {
/**************************************************************************
**************************************************************************
**************************************************************************
* action: index
* - Lists all current tenants
* action: index / current / past / all
* - Creates a list of tenants
*/
function index() {
$this->current();
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: current
* - Lists all current tenants
*/
function current() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('Lease.close_date IS NULL')
));
$title = 'Current Tenants';
function jqGridSetup($action, $title) {
$this->set('title', $title); $this->set('heading', $title);
$this->set('customers', $this->paginate());
// 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');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: past
* - Lists all past tenants
*/
function past() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'Lease' =>
array('fields' => array(),
'type' => 'INNER',
),
),
'conditions' => array('Lease.close_date IS NOT NULL')
));
$title = 'Past Tenants';
$this->set('title', $title); $this->set('heading', $title);
$this->set('customers', $this->paginate());
$this->render('index');
}
function index() { $this->current(); }
function current() { $this->jqGridSetup('current', 'Current Tenants'); }
function past() { $this->jqGridSetup('past', 'Past Tenants'); }
function all() { $this->jqGridSetup('all', 'All Tenants'); }
/**************************************************************************
**************************************************************************
**************************************************************************
* action: all
* - Lists all customers, including non-tenants
* action: jqGridData
* - Fetches the actual data requested by jqGrid as XML
*/
function all() {
$title = 'All Customers';
$this->set('title', $title); $this->set('heading', $title);
$this->set('params', $this->params);
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: data
* -
*/
function jqGridData($debug = true) {
/* pr(array('fields' => $fields, */
/* 'explode' => explode(";", $fields_str))); */
/* $debug=true; */
/* $tf = unserialize($fields); */
/* $tp = unserialize($params); */
/* $fields = array('Customer.id'); */
//$action_params = unserialize($action_params);
//$fields = explode(";", $fields_str);
//$fields=array();
/* foreach (explode(";", $fields_str) AS $i => $field) { */
/* pr(array('field' => $field, */
/* 'explode' => explode(".", $field))); */
/* list($tbl, $col) = explode(".", $field); */
/* unset($fields[$i]); */
/* $fields[$tbl][] = $field; */
/* } */
//pr(array('fields' => $fields));
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'];
$this->autoRender = false;
if (!$debug) {
$this->layout = null;
$this->autoLayout = false;
@@ -148,88 +65,91 @@ class CustomersController extends AppController {
Configure::write('debug', '0');
}
$page = 1; // page number
$rows = 20; // rows in the grid - rowNum parameter
$sidx = 'Customer.id'; // sort column - index from colModel
$sord = 'ASC'; // sort order
// 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
$fields = array('Customer.id');
// 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');
$fields = array('Customer.id', 'Customer.name');
if (isset($params))
$params = unserialize($params);
else
$params = array('action' => 'all');
// calculate the number of rows for the query. We need this for paging the result
$row = $this->Customer->findCount();
$count = $row;
// calculate the total pages for the query
if( $count > 0 ) {
$total_pages = ceil($count/$rows);
} else {
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages) $page=$total_pages;
// calculate the starting position of the rows
$start = $rows*$page - $rows;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if($start <0) $start = 0;
$cond = array();
if ($params['action'] == 'current') {
} elseif ($params['action'] == 'past') {
} else {
}
// the actual query for the grid data
$customers = $this->Customer->find
('all',
array('contain' => array
(// Models
'PrimaryContact',
// Set up the basic 'contain' field to query the
// pertinent information.
$contain =
array(// Models
'PrimaryContact',
/* array(// Models */
/* 'ContactPhone', */
/* 'ContactEmail', */
/* 'ContactAddress', */
/* ), */
'Account' =>
array('fields' => array('Account.id')),
/* 'Account' => */
/* array('fields' => array('Account.id')), */
'CurrentLease' =>
array('fields' => array(),
'Unit' =>
array('fields' => array('name')),
),
),
//'link' => array('fields' => $fields),
'order' => "$sidx $sord",
'limit' => "$start, $rows",
'CurrentLease' =>
array('fields' => array(),
'Unit' =>
array('fields' => array('name')),
),
);
// Calculate the number of rows for the query, and figure out
// any special query conditions that will be necessary
$count = $this->Customer->find('count');
if ($action != 'all') {
$count_past = $this->Customer->find('count',
array('link' => array('CurrentLease'),
'conditions' => 'CurrentLease.id IS NULL'));
if ($action == 'current') {
$count = $count - $count_past;
$contain['CurrentLease']['conditions'] = 'CurrentLease.id IS NOT NULL';
}
elseif ($action == 'past') {
$count = $count_past;
$contain['CurrentLease']['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",
));
//pr($customers);
/* $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") ) {

View File

@@ -74,7 +74,7 @@ jQuery(document).ready(function(){
url: '<?php echo $url; ?>',
datatype: 'xml',
mtype: 'GET',
postData: { params: '<?php echo serialize($params); ?>',
postData: { action: '<?php echo $action; ?>',
fields: '<?php echo serialize($colFields); ?>' },
colNames: [ '<?php echo implode("', '", array_keys($cols)); ?>' ],
colModel: [ <?php echo "\n " . implode(",\n ", $colModels); ?> ],