'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' => 'Accounts', 'url' => array('controller' => 'accounts', 'action' => 'index')), array('name' => 'Debug', 'header' => true), array('name' => 'Contacts', 'url' => array('controller' => 'contacts', 'action' => 'index')), array('name' => 'Ledgers', 'url' => array('controller' => 'ledgers', 'action' => 'index')), array('name' => 'New Ledgers', 'url' => array('controller' => 'accounts', 'action' => 'newledger')), array('name' => 'RESET DATA', 'url' => array('controller' => 'accounts', 'action' => 'reset_data')), ); } function beforeRender() { $this->set('sidemenu', $this->sideMenuLinks()); } function reset_data() { $this->layout = null; $this->autoLayout = false; $this->autoRender = false; Configure::write('debug', '0'); $script = $_SERVER['DOCUMENT_ROOT'] . '/pmgr/build.cmd'; echo "

" . date('r') . "\n"; //echo "

Script: $script" . "\n"; $db = & $this->Account->getDataSource(); $script .= ' "' . $db->config['database'] . '"'; $script .= ' "' . $db->config['login'] . '"'; $script .= ' "' . $db->config['password'] . '"'; $handle = popen($script . ' 2>&1', 'r'); //echo "

Handle: $handle; " . gettype($handle) . "\n"; echo "

\n";
    while (($read = fread($handle, 2096))) {
      echo $read;
    }
    echo "
\n"; pclose($handle); } /************************************************************************** ************************************************************************** ************************************************************************** * helper: gridView * - called by derived controllers to create an index listing */ function gridView($title, $action = null, $element = null) { $this->set('title', $title); // The resulting page will contain a grid, which will // use ajax to obtain the actual data for this action $this->set('action', $action ? $action : $this->params['action']); $this->render('/elements/' . ($element ? $element : $this->params['controller'])); } /************************************************************************** ************************************************************************** ************************************************************************** ************************************************************************** ************************************************************************** * action: gridData * - Fetches the actual data requested by grid as XML */ function gridData() { // Grab a copy of the parameters that control this request $params = array(); if (isset($this->params['url']) && is_array($this->params['url'])) $params = $this->params['url']; // Do any preliminary setup necessary $this->gridDataSetup($params); // Get the top level model for this grid $model = $this->gridDataModel($params); // Get the number of records prior to pagination $count = $this->gridDataCount($params, $model); // Determine pagination configuration (and save to $params) $pagination = $this->gridDataPagination($params, $model, $count); // Retreive the appropriate subset of data $records = $this->gridDataRecords($params, $model, $pagination); // Post process the records $this->gridDataPostProcess($params, $model, $records); // Output the resulting record set $this->gridDataOutput($params, $model, $records, $pagination); // Call out to finalize everything $this->gridDataFinalize($params); } /************************************************************************** ************************************************************************** ************************************************************************** ************************************************************************** ************************************************************************** * virtual: gridData* functions * - These set up the context for the grid data, and will * need to be overridden in the derived class for anything * other than the most basic of grids. */ /************************************************************************** ************************************************************************** ************************************************************************** * gridData SETUP / CLEANUP */ function gridDataModel(&$params) { return $this->{$this->modelClass}; } function gridDataSetup(&$params) { // Assume we're debugging. // The actual grid request will set this to false $debug = true; if (isset($this->passedArgs['debug'])) $debug = $this->passedArgs['debug']; $params['debug'] = $debug; if ($debug) { ob_start(); } else { $this->layout = null; $this->autoLayout = false; $this->autoRender = false; Configure::write('debug', '0'); } // Establish some defaults (except for serialized items) $params = array_merge(array('page' => 1, 'rows' => 20), $params); // Unserialize our complex structure of fields // This SHOULD always be set, except when debugging if (isset($params['post'])) $params['post'] = unserialize($params['post']); // This SHOULD always be set, except when debugging if (!isset($params['post']['fields'])) $params['post']['fields'] = array($this->{$this->modelClass}->alias .'.'. $this->{$this->modelClass}->primaryKey); // Make sure the action parameter at least exists, and // promote it to the top level (since it drives the operation). if (isset($params['post']['action'])) $params['action'] = $params['post']['action']; else $params['action'] = null; } function gridDataFinalize(&$params) { if ($params['debug']) { $xml = ob_get_contents(); ob_end_clean(); $xml = preg_replace("/&/", "&", $xml); $xml = preg_replace("//", ">", $xml); echo ("\n
\n$xml\n
\n"); } } /************************************************************************** ************************************************************************** ************************************************************************** * gridData COUNTING */ function gridDataCount(&$params, &$model) { // Establish the tables and conditions for counting $query = array_intersect_key($this->gridDataCountTableSet($params, $model), array('link'=>1, 'contain'=>1)); // Conditions for the count $query['conditions'] = $this->gridDataCountConditionSet($params, $model); // Grouping (which would not be typical) $query['group'] = $this->gridDataCountGroup($params, $model); // DEBUG PURPOSES ONLY! $params['count_query'] = $query; // Get the number of records prior to pagination return $this->gridDataCountExecute($params, $model, $query); } function gridDataCountExecute(&$params, &$model, $query) { return $model->find('count', $query); } function gridDataCountTables(&$params, &$model) { // Same tables for counting as for retreiving return $this->gridDataTables($params, $model); } function gridDataCountTableSet(&$params, &$model) { // Preliminary set of tables $query = array_intersect_key($this->gridDataCountTables($params, $model), array('link'=>1, 'contain'=>1)); // Perform filtering based on user request: $params['post']['filter'] return array_intersect_key($this->gridDataFilterTables($params, $model, $query), array('link'=>1, 'contain'=>1)); } function gridDataCountConditions(&$params, &$model) { // Same conditions for counting as for retreiving return $this->gridDataConditions($params, $model); } function gridDataCountConditionSet(&$params, &$model) { // Conditions for the count $conditions = $this->gridDataCountConditions($params, $model); // Perform filtering based on user request: $params['post']['filter'] return $this->gridDataFilterConditions($params, $model, $conditions); } function gridDataCountGroup(&$params, &$model) { // Grouping will screw up the count, since it // causes the results to be split into chunks // based on the GROUP BY clause. We can't have // more than one row of data in the count query, // just a _single_ row with the actual count. return null; } /************************************************************************** ************************************************************************** ************************************************************************** * gridData FILTERING */ function gridDataFilterTables(&$params, &$model, $query) { if (isset($query['contain'])) $link = 'contain'; else $link = 'link'; if (empty($params['post']['filter'])) return $query; foreach ($params['post']['filter'] AS $filter => $value) { if (preg_match("/\./", $filter)) { list($tbl, $id) = explode(".", $filter); } elseif (preg_match('/^(.*)_(id)$/', $filter, $matches)) { list($tbl, $id) = array_slice($matches, 1); } else { $tbl = $filter; $id = null; } $table = $this->gridDataFilterTablesTable($params, $model, $tbl); if (!$table) continue; $config = $this->gridDataFilterTablesConfig($params, $model, $tbl); /* pr(array('pre-filter-table-config' => */ /* array('query[link]' => $query[$link], */ /* 'config' => $config))); */ // If the table is already part of the query, append to it if ($table == $model->alias) { $query[$link] = array_merge_recursive($query[$link], $config); } elseif (isset($query[$link][$table])) { $query[$link][$table] = array_merge_recursive($query[$link][$table], $config); } elseif (in_array($table, $query[$link])) { // REVISIT : 20090727 // This presents a dilema. $config may specify fields // as array(), whereas the user has already indicated // they desire ALL fields (by the fact that table is // a member of the query['link'] array without anything // specified). However, $config _could_ specify a // non-standard field in the array, such as using SUM() // or other equation. In that case, we'd have to add // in all the fields of table to the array. That // wouldn't be very hard, but I'll ignore it at the // moment and wait until it's needed. $key = array_search($table, $query[$link]); $query[$link][$table] = array('fields' => null) + $config; unset($query[$link][$key]); } else { // Table is NOT already part of the query... set it now $query[$link][$table] = $config; } /* pr(array('post-filter-table-config' => */ /* array('query[link]' => $query[$link]))); */ } return $query; } function gridDataFilterTablesTable(&$params, &$model, $table) { return $this->gridDataFilterTableName($params, $model, $table); } function gridDataFilterTablesConfig(&$params, &$model, $table) { return array('fields' => array()); } function gridDataFilterConditions(&$params, &$model, $conditions) { if (empty($params['post']['filter'])) return $conditions; foreach ($params['post']['filter'] AS $filter => $value) { if (preg_match("/\./", $filter)) { list($tbl, $id) = explode(".", $filter); } elseif (preg_match('/^(.*)_(id)$/', $filter, $matches)) { list($tbl, $id) = array_slice($matches, 1); } else { $tbl = $filter; $id = null; } $table = $this->gridDataFilterConditionsTable($params, $model, $tbl); if (!$table) continue; $key = $this->gridDataFilterConditionsKey($params, $model, $tbl, $id); if (!$key) continue; $conditions[] = $this->gridDataFilterConditionsStatement($params, $model, $tbl, $key, $value); } return $conditions; } function gridDataFilterConditionsTable(&$params, &$model, $table) { return $this->gridDataFilterTableName($params, $model, $table); } function gridDataFilterConditionsKey(&$params, &$model, $table, $id) { // REVISIT : 20090722 // When $id is null, we could instantiate the table, // and use the _actual_ primary key. However, I don't // expect that functionality to be used, and will just // stick with 'id' for now. return $id ? $id : 'id'; } function gridDataFilterConditionsStatement(&$params, &$model, $table, $key, $value) { return array("{$table}.{$key}" => $value); } function gridDataFilterTableName(&$params, &$model, $table) { return Inflector::camelize($table); } /************************************************************************** ************************************************************************** * gridData PAGINATION */ function gridDataPagination(&$params, &$model, $record_count) { // Retrieve, or calculate, all parameters necesssary for // pagination. Verify the passed in parameters are valid. $limit = $params['rows'] > 0 ? $params['rows'] : 10; $total = ($record_count < 0) ? 0 : ceil($record_count/$limit); $page = ($params['page'] <= 1) ? 1 : (($params['page'] > $total) ? $total : $params['page']); $start = $limit * ($page - 1); return compact('record_count', 'limit', 'page', 'start', 'total'); } /************************************************************************** ************************************************************************** * gridData RETREIVAL */ function gridDataRecords(&$params, &$model, $pagination) { // Establish the tables for this query $query = array_intersect_key($this->gridDataTableSet($params, $model), array('link'=>1, 'contain'=>1)); // Specify the fields for the query $query['fields'] = $this->gridDataFields($params, $model); // Conditions of the query $query['conditions'] = $this->gridDataConditionSet($params, $model); // Data record grouping $query['group'] = $this->gridDataGroup($params, $model); // The subset of data based on pagination $query['limit'] = $this->gridDataLimit($params, $model, $pagination['start'], $pagination['limit']); // Ordering based on user request $query['order'] = $this->gridDataOrder($params, $model, isset($params['sidx']) ? $params['sidx'] : null, isset($params['sord']) ? $params['sord'] : null); // DEBUG PURPOSES ONLY! $params['query'] = $query; return $this->gridDataRecordsExecute($params, $model, $query); } function gridDataRecordsExecute(&$params, &$model, $query) { return $model->find('all', $query); } function gridDataTables(&$params, &$model) { return array('link' => array()); } function gridDataTableSet(&$params, &$model) { // Preliminary set of tables $query = array_intersect_key($this->gridDataTables($params, $model), array('link'=>1, 'contain'=>1)); // Perform filtering based on user request: $params['post']['filter'] $query = array_intersect_key($this->gridDataFilterTables($params, $model, $query), array('link'=>1, 'contain'=>1)); return $query; } function gridDataConditions(&$params, &$model) { $searches = array(); if (isset($params['_search']) && $params['_search'] === 'true') { if (isset($params['searchOper'])) { $searches[] = array('op' => $params['searchOper'], 'field' => $params['searchField'], 'value' => $params['searchString']); } else { // DOH! Crappy mechanism puts toolbar search terms // directly into params as name/value pairs. No // way to know which elements of params are search // terms, so skipping this at the moment. } } elseif (isset($params['filt']) && $params['filt']) { $searches[] = array('op' => 'bw', 'field' => $params['filtField'], 'value' => $params['filtValue']); } $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' => '%'), ); $conditions = array(); foreach ($searches AS $search) { $op = $ops[$search['op']]; $field = $search['field'] . ($op['op'] ? ' '.$op['op'] : ''); $value = $op['pre'] . $search['value']. $op['post']; $conditions[] = array($field => $value); } if (isset($params['action']) && $params['action'] === 'idlist') { if (count($params['post']['idlist'])) $conditions[] = array($model->alias.'.'.$model->primaryKey => $params['post']['idlist']); else $conditions[] = '0=1'; } return $conditions; } function gridDataConditionSet(&$params, &$model) { // Conditions for record retrieval $conditions = $this->gridDataConditions($params, $model); // Perform filtering based on user request: $params['post']['filter'] return $this->gridDataFilterConditions($params, $model, $conditions); } function gridDataFields(&$params, &$model) { $db = &$model->getDataSource(); $fields = $db->fields($model, $model->alias); return $fields; } function gridDataGroup(&$params, &$model) { return $model->alias.'.'.$model->primaryKey; } function gridDataOrder(&$params, &$model, $index, $direction) { return $index ? array($index .' '. $direction) : null; } function gridDataLimit(&$params, &$model, $start, $limit) { return $start . ', ' . $limit; } /************************************************************************** ************************************************************************** * gridData POST PROCESSING */ function gridDataPostProcess(&$params, &$model, &$records) { // Add grid IDs to each record $this->gridDataPostProcessGridIDs($params, $model, $records); // Add the calculated fields (if any), to the model fields $this->gridDataPostProcessCalculatedFields($params, $model, $records); // Perform any requested subtotaling of fields $this->gridDataPostProcessSubtotal($params, $model, $records); // Add in any needed hyperlinks $this->gridDataPostProcessLinks($params, $model, $records, array()); // DEBUG PURPOSES ONLY! //$params['records'] = $records; } function gridDataPostProcessGridIDs(&$params, &$model, &$records) { $model_alias = $model->alias; $id = $model->primaryKey; foreach ($records AS &$record) { $record['grid_id'] = $record[$model_alias][$id]; } } function gridDataPostProcessCalculatedFields(&$params, &$model, &$records) { $model_alias = $model->alias; foreach ($records AS &$record) { // Add the calculated fields (if any), to the model fields if (isset($record[0])) { $record[$model_alias] += $record[0]; unset($record[0]); } } } function gridDataPostProcessSubtotal(&$params, &$model, &$records) { $model_alias = $model->alias; // REVISIT : 20090722 // Horrible solution to something that should be done // in SQL. But, it works for now, so what the heck... $subtotals = array(); foreach ($params['post']['fields'] AS $field) { if (preg_match('/subtotal-(.*)$/', $field, $matches)) $subtotals[] = array('field' => $matches[1], 'name' => $field, 'amount' => 0); } foreach ($records AS &$record) { foreach ($subtotals AS &$subtotal) { $field = $subtotal['field']; if (preg_match("/\./", $field)) { list($tbl, $col) = explode(".", $field); $record['subtotal-'.$tbl][$col] = ($subtotal['amount'] += $record[$tbl][$col]); } else { $record[$model->alias]['subtotal-'.$field] = ($subtotal['amount'] += $record[$model->alias][$field]); } } } } function gridDataPostProcessLinks(&$params, &$model, &$records, $links) { // Don't create any links if ordered not to. if (isset($params['nolinks'])) return; foreach ($links AS $table => $fields) { $special = array('controller', 'id'); $controller = Inflector::pluralize(Inflector::underscore($table)); $id = 'id'; extract(array_intersect_key($fields, array_flip($special))); foreach ($records AS &$record) { if (!isset($record[$table])) continue; foreach (array_diff_key($fields, array_flip($special)) AS $field) { if (!isset($record[$table][$id]) || !isset($record[$table][$field])) continue; // DEBUG PURPOSES ONLY! //$params['linkrecord'][] = compact('table', 'field', 'id', 'controller', 'record'); $record[$table][$field] = '' . $record[$table][$field] . ''; } } } } /************************************************************************** ************************************************************************** * gridData OUTPUT */ function gridDataOutput(&$params, &$model, &$records, $pagination) { $this->gridDataOutputHeader($params, $model); $this->gridDataOutputXMLHeader($params, $model); $this->gridDataOutputRootNodeBegin($params, $model); $this->gridDataOutputSummary($params, $model, $pagination); $this->gridDataOutputRecords($params, $model, $records); $this->gridDataOutputRootNodeEnd($params, $model); } function gridDataOutputHeader(&$params, &$model) { if (!$params['debug']) { header("Content-type: text/xml;charset=utf-8"); } } function gridDataOutputXMLHeader(&$params, &$model) { echo "\n"; } function gridDataOutputRootNodeBegin(&$params, &$model) { echo "\n"; } function gridDataOutputRootNodeEnd(&$params, &$model) { echo "\n"; } function gridDataOutputSummary(&$params, &$model, $pagination) { echo " \n"; echo " {$pagination['page']}\n"; echo " {$pagination['total']}\n"; echo " {$pagination['record_count']}\n"; if (isset($params['userdata'])) { foreach ($params['userdata'] AS $field => $value) echo ' ' . "{$value}\n"; } } function gridDataOutputRecords(&$params, &$model, &$records) { $id_field = 'grid_id'; foreach ($records AS $record) { $this->gridDataOutputRecord($params, $model, $record, $record[$id_field], $params['post']['fields']); } } function gridDataOutputRecord(&$params, &$model, &$record, $id, $fields) { echo " \n"; foreach ($fields AS $field) { $this->gridDataOutputRecordField($params, $model, $record, $field); } echo " \n"; } function gridDataOutputRecordField(&$params, &$model, &$record, $field) { if (preg_match("/\./", $field)) { list($tbl, $col) = explode(".", $field); //pr(compact('record', 'field', 'tbl', 'col')); $data = $record[$tbl][$col]; } else { $data = $record[$model->alias][$field]; } $this->gridDataOutputRecordCell($params, $model, $record, $field, $data); } function gridDataOutputRecordCell(&$params, &$model, &$record, $field, $data) { // be sure to put text data in CDATA if (preg_match("/^\d*$/", $data)) echo " $data\n"; else echo " \n"; } } ?>