git-svn-id: file:///svn-source/pmgr/branches/v0.3_work@1031 97e9348a-65ac-dc4b-aefc-98561f571b83
342 lines
11 KiB
PHP
342 lines
11 KiB
PHP
<?php /* -*- mode:PHP -*- */
|
|
|
|
if (!isset($caption))
|
|
$caption = '<H2>'.$this->pageTitle.'</H2>';
|
|
|
|
if (!isset($limit))
|
|
$limit = 20;
|
|
|
|
if (!isset($limitOptions)) {
|
|
$limitOptions = array($limit);
|
|
if ($limit <= 10)
|
|
array_push($limitOptions, 2, 5);
|
|
if ($limit <= 30)
|
|
array_push($limitOptions, 10, 25);
|
|
if ($limit > 10)
|
|
array_push($limitOptions, 25, 50, 200);
|
|
if ($limit > 20)
|
|
array_push($limitOptions, 500);
|
|
}
|
|
sort($limitOptions, SORT_NUMERIC);
|
|
$limitOptions = array_unique($limitOptions, SORT_NUMERIC);
|
|
//$limitOptions[] = 'ALL'; // Would be nice... jqGrid shows 'NaN of NaN'
|
|
|
|
if (!isset($height))
|
|
$height = 'auto';
|
|
|
|
if (!isset($controller))
|
|
$controller = $this->params['controller'];
|
|
|
|
if (!isset($grid_div_class))
|
|
$grid_div_class = '';
|
|
$grid_div_class = 'item grid list' . ($grid_div_class ? ' '.$grid_div_class : '');
|
|
|
|
if (!isset($grid_div_id))
|
|
$grid_div_id = $controller . '-list';
|
|
|
|
if (!isset($grid_id))
|
|
$grid_id = $grid_div_id . '-jqGrid';
|
|
|
|
if (!isset($search_fields))
|
|
$search_fields = array();
|
|
|
|
if (!isset($grid_events))
|
|
$grid_events = array();
|
|
|
|
if (!isset($grid_setup))
|
|
$grid_setup = array();
|
|
|
|
// Do some prework to bring in the appropriate libraries
|
|
$html->css('ui.jqgrid', null, null, false);
|
|
$javascript->link('jqGrid/grid.locale-en', false);
|
|
$javascript->link('jqGrid/jquery.jqGrid.min', false);
|
|
$javascript->link('jqGrid/grid.postext', false);
|
|
$javascript->link('pmgr_jqGrid', false);
|
|
|
|
|
|
// Define the URL to fetch data from.
|
|
// To prevent having to keep the controller and the view
|
|
// in sync on which fields need to be queried by the
|
|
// controller in order to be accessible to the view,
|
|
// we'll just pass the desired fields to the controller
|
|
// as part of the data fetch.
|
|
$url = $html->url(array('controller' => $controller,
|
|
'action' => 'gridData',
|
|
));
|
|
|
|
// Create extra parameters that jqGrid will pass to our
|
|
// controller whenever data is requested.
|
|
// 'fields' will allow the controller to return only the
|
|
// requested fields, and in the right order.
|
|
$postData = array();
|
|
$postData['fields'] = array_map(create_function('$col',
|
|
'return $col["index"];'),
|
|
array_values($jqGridColumns));
|
|
|
|
// Determine if we're to be using a custom list, or if
|
|
// the data will simply be action based.
|
|
if (isset($custom_ids)) {
|
|
if (!isset($action))
|
|
$action = 'idlist';
|
|
$postData['idlist'] = $custom_ids;
|
|
}
|
|
|
|
if (isset($nolinks))
|
|
$postData['nolinks'] = true;
|
|
|
|
// 'action' will ensure that the controller does the right thing
|
|
// 'filter' allows the app controller to automagic filter
|
|
// 'custom' is for use solely by derived controllers
|
|
$postData['action'] = isset($action) ? $action : null;
|
|
$postData['filter'] = isset($filter) ? $filter : null;
|
|
$postData['custom'] = isset($custom_post_data) ? $custom_post_data : null;
|
|
|
|
|
|
// Perform column customizations.
|
|
// This will largely be based off of the 'formatter' parameter,
|
|
// but could be on any pertinent condition.
|
|
foreach ($jqGridColumns AS $header => &$col) {
|
|
$default = array();
|
|
|
|
// Make sure every column has a name
|
|
$default['name'] = preg_replace("/\./", '-', $col['index']);
|
|
$default['force'] = isset($col['forcewidth']) ? $col['forcewidth'] : null;
|
|
|
|
// Perform customization based on formatter
|
|
if (isset($col['formatter'])) {
|
|
if ($col['formatter'] === 'id') {
|
|
// Use our custom formatting for ids
|
|
$col['formatter'] = array('--special' => 'idFormatter');
|
|
$default['width'] = 50;
|
|
$default['align'] = 'center';
|
|
|
|
// For IDs, force the width by default,
|
|
// unless otherwise instructed NOT to.
|
|
if (!isset($default['force']))
|
|
$default['force'] = true;
|
|
}
|
|
elseif ($col['formatter'] === 'number') {
|
|
$default['width'] = 60;
|
|
$default['align'] = 'right';
|
|
|
|
// No special formatting for number
|
|
unset($col['formatter']);
|
|
}
|
|
elseif ($col['formatter'] === 'percentage') {
|
|
$col['formatter'] = array('--special' => 'percentageFormatter');
|
|
$default['width'] = 60;
|
|
$default['align'] = 'right';
|
|
}
|
|
elseif ($col['formatter'] === 'currency') {
|
|
// Use our custom formatting for currency
|
|
$col['formatter'] = array('--special' => 'currencyFormatter');
|
|
$default['width'] = 65;
|
|
$default['align'] = 'right';
|
|
}
|
|
elseif ($col['formatter'] === 'date') {
|
|
$default['formatoptions'] = array('newformat' => 'm/d/Y');
|
|
$default['width'] = 90;
|
|
$default['align'] = 'center';
|
|
}
|
|
elseif (preg_match("/^(long|short)?name$/",
|
|
$col['formatter'], $matches)) {
|
|
$default['width'] = 100;
|
|
if (!empty($matches[1]) && $matches[1] === 'long')
|
|
$default['width'] *= 1.5;
|
|
if (!empty($matches[1]) && $matches[1] === 'short')
|
|
$default['width'] *= 0.7;
|
|
|
|
// No special formatting for name
|
|
unset($col['formatter']);
|
|
}
|
|
elseif (preg_match("/^(long|short)?enum$/",
|
|
$col['formatter'], $matches)) {
|
|
$default['width'] = 60;
|
|
if (!empty($matches[1]) && $matches[1] === 'long')
|
|
$default['width'] *= 1.5;
|
|
if (!empty($matches[1]) && $matches[1] === 'short')
|
|
$default['width'] *= 0.7;
|
|
|
|
//$default['align'] = 'right';
|
|
|
|
// No special formatting for enum
|
|
unset($col['formatter']);
|
|
}
|
|
elseif ($col['formatter'] === 'comment') {
|
|
$default['width'] = 150;
|
|
$default['sortable'] = false;
|
|
|
|
// No special formatting for comment
|
|
unset($col['formatter']);
|
|
}
|
|
// else just let the formatter pass through untouched
|
|
|
|
// Just a rough approximation to ensure columns
|
|
// are wide enough to fully display their header.
|
|
$min_width = strlen($header) * 7;
|
|
$min_width = 0; // REVISIT <AP>: 20090829; if/while jqGrid is fixed width
|
|
if ((!isset($default['width']) || $default['width'] < $min_width) && !$default['force'])
|
|
$default['width'] = $min_width;
|
|
}
|
|
|
|
$col = array_merge($default, $col);
|
|
}
|
|
|
|
// Set the default sort column
|
|
if (isset($sort_column)) {
|
|
$sortname = $jqGridColumns[$sort_column];
|
|
} else {
|
|
reset($jqGridColumns);
|
|
$sortname = current($jqGridColumns);
|
|
}
|
|
$sortname = $sortname['index'];
|
|
|
|
// Set the default sort order
|
|
if (isset($sort_order)) {
|
|
$sortorder = $sort_order;
|
|
} else {
|
|
$sortorder = 'ASC';
|
|
}
|
|
|
|
$debug = !empty($this->params['dev']);
|
|
if ($debug)
|
|
$caption .= '<span class="debug grid-query"> :: <span id="'.$grid_id.'-query"></span></span>';
|
|
|
|
$caption .= ('<span class="grid-error" id="'.$grid_id.'-error"' .
|
|
' style="display:none"> :: Error (Please Reload)</span>');
|
|
|
|
foreach (array_merge(array('loadComplete' => '', 'loadError' => ''),
|
|
$grid_events) AS $event => $statement) {
|
|
$params = '';
|
|
if (is_array($statement)) {
|
|
$params = key($statement);
|
|
$statement = current($statement);
|
|
}
|
|
|
|
if ($event == 'loadComplete' && $debug) {
|
|
$grid_events[$event] =
|
|
array('--special' => "function($params) {url=jQuery('#{$grid_id}').getGridParam('url');url=url+'/debug:1?'; pd=jQuery('#{$grid_id}').getPostData();$.each(pd,function(i){ url+=i+'='+escape(pd[i])+'&'; }); jQuery('#{$grid_id}-query').html('<A HREF=\"'+url+'\">Grid Query</A><BR>'); $statement;}");
|
|
}
|
|
elseif ($event == 'loadError' && $debug) {
|
|
$grid_events[$event] =
|
|
array('--special' => "function($params) {url=jQuery('#{$grid_id}').getGridParam('url');url=url+'/debug:1?'; pd=jQuery('#{$grid_id}').getPostData();$.each(pd,function(i){ url+=i+'='+escape(pd[i])+'&'; }); jQuery('#{$grid_id}-query').html('<A HREF=\"'+url+'\">Grid Error Query</A><BR>'); $statement;}");
|
|
}
|
|
elseif ($event == 'loadComplete' && !$debug) {
|
|
$grid_events[$event] =
|
|
array('--special' => "function($params) {jQuery('#{$grid_id}-error').hide(); $statement;}");
|
|
}
|
|
elseif ($event == 'loadError' && !$debug) {
|
|
$grid_events[$event] =
|
|
array('--special' => "function($params) {jQuery('#{$grid_id}-error').show(); $statement;}");
|
|
}
|
|
else {
|
|
$grid_events[$event] =
|
|
array('--special' => "function($params) { $statement; }");
|
|
}
|
|
}
|
|
|
|
|
|
// Configure the grid setup, giving priority to user defined parameters
|
|
$jqGrid_setup = array_merge
|
|
(array('mtype' => 'GET',
|
|
'datatype' => 'xml',
|
|
'url' => $url,
|
|
// Since postData is a complex structure (an array), we'll
|
|
// need to serialize it first for transport over HTTP.
|
|
'postData' => array('post' => serialize($postData)),
|
|
'colNames' => array_keys($jqGridColumns),
|
|
'colModel' => array('--special' => $jqGridColumns),
|
|
'height' => $height,
|
|
'width' => 700,
|
|
'rowNum' => $limit,
|
|
'rowList' => $limitOptions,
|
|
'sortname' => $sortname,
|
|
'sortorder' => $sortorder,
|
|
'caption' => $caption,
|
|
'viewrecords' => true,
|
|
'gridview' => true,
|
|
'pager' => $grid_id.'-pager',
|
|
),
|
|
$grid_events,
|
|
$grid_setup
|
|
);
|
|
//pr(compact('grid_setup', 'jqGrid_setup'));
|
|
|
|
// OK, now that everything is in place, get out of PHP mode,
|
|
// and add the javascript code (along with a touch of HTML)
|
|
// to kick this thing off.
|
|
?>
|
|
|
|
<?php if ($first_grid): ?>
|
|
<script type="text/javascript"><!--
|
|
var currencyFormatter = function(cellval, opts, rowObject) {
|
|
if (!cellval)
|
|
return "";
|
|
return fmtCurrency(cellval);
|
|
}
|
|
|
|
var percentageFormatter = function(cellval, opts, rowObject) {
|
|
var precision;
|
|
if (typeof(opts.colModel) != 'undefined' &&
|
|
typeof(opts.colModel.formatoptions) != 'undefined' &&
|
|
typeof(opts.colModel.formatoptions.precision) != 'undefined')
|
|
precision = opts.colModel.formatoptions.precision;
|
|
else
|
|
precision = 0;
|
|
amount = cellval.toString().replace(/\%/g,'');
|
|
amount = (amount*100).toFixed(precision);
|
|
return amount+'%';
|
|
}
|
|
|
|
var idFormatter = function(cellval, opts, rowObject) {
|
|
if (!cellval)
|
|
return cellval;
|
|
return '#'+cellval;
|
|
}
|
|
--></script>
|
|
<?php endif; ?>
|
|
|
|
|
|
<DIV ID="<?php echo $grid_div_id; ?>" CLASS="<?php echo $grid_div_class; ?>">
|
|
<table id="<?php echo $grid_id; ?>" class="scroll"></table>
|
|
<div id="<?php echo $grid_id; ?>-pager" class="scroll" style="text-align:right"></div>
|
|
<script type="text/javascript"><!--
|
|
jQuery(document).ready(function(){
|
|
jQuery('#<?php echo $grid_id; ?>').jqGrid(
|
|
<?php echo FormatHelper::phpVarToJavascript($jqGrid_setup) . "\n"; ?>
|
|
).navGrid('#<?php echo $grid_id; ?>-pager', { view:false,edit:false,add:false,del:false,search:true,refresh:true});
|
|
});
|
|
--></script>
|
|
<?php
|
|
if (count($search_fields) > 0) {
|
|
echo('<div>Search By:<BR>' . "\n");
|
|
echo('<input type="checkbox" CLASS="filt-autosearch"'.
|
|
' onclick="enableAutosubmit(this.checked)" CHECKED>'.
|
|
'Enable Autosearch' .
|
|
'<BR>' . "\n");
|
|
|
|
foreach ($search_fields AS $field) {
|
|
echo($field . "<BR>\n");
|
|
echo('<input type="text" CLASS="filt-text filt-text-'.$grid_id.'"' .
|
|
' id="filt_'.$jqGridColumns[$field]['name'].'"' .
|
|
' filt-grid="'.$grid_id.'"' .
|
|
' filt-index="'.$jqGridColumns[$field]['index'].'"' .
|
|
'/>' . "\n");
|
|
echo('<button CLASS="filt-submit filt-submit-'.$grid_id.'"' .
|
|
' id="submit_'.$jqGridColumns[$field]['name'].'"' .
|
|
' style="margin-left:30px;">' .
|
|
'Search' .
|
|
'</button><BR>' . "\n");
|
|
}
|
|
echo('<div>' . "\n");
|
|
echo('<button CLASS="filt-clear" ONCLICK="'."gridRestore('$grid_id')".'">' .
|
|
'Clear' .
|
|
'</button>' . "\n");
|
|
echo('</div>' . "\n");
|
|
echo('</div>' . "\n");
|
|
}
|
|
|
|
// Finally, back to HTML mode, and end the grid DIV we created
|
|
?>
|
|
</DIV>
|