diff --git a/site/views/helpers/grid.php b/site/views/helpers/grid.php
index 2649913..dd3b98c 100644
--- a/site/views/helpers/grid.php
+++ b/site/views/helpers/grid.php
@@ -3,8 +3,9 @@
class GridHelper extends AppHelper {
var $jqGrid_options;
- var $included;
+ var $included, $invalid;
var $columns;
+ var $controller;
function __construct() {
$this->reset();
@@ -16,6 +17,9 @@ class GridHelper extends AppHelper {
$this->columns = array();
$this->included = array();
+ $this->invalid = array();
+ $this->controller = null;
+
return $this;
}
@@ -23,66 +27,167 @@ class GridHelper extends AppHelper {
function columns($columns) {
if (isset($columns)) {
if (is_array($columns))
- $this->columns += $columns;
+ $this->columns = array_merge($this->columns, $columns);
else
- array_push($this->columns, $columns);
+ $this->columns[] = $columns;
}
return $this;
}
- // Included fields will be included unless excluded.
- function included($fields) {
+ // Default fields will be included unless excluded.
+ // This should be used for fields that ALWAYS need
+ // to be included unless specifically overridden.
+ function defaultFields($fields) {
if (isset($fields)) {
if (is_array($fields))
$this->included = array_merge($this->included, $fields);
else
- array_push($this->included, $fields);
+ $this->included[] = $fields;
}
return $this;
}
- function limit() {
- $this->jqGrid_options['limit'] = 20;
+ // Invalid fields will simply not be accepted as
+ // part of the inclusion set, even if specified as
+ // a column AND explicit requested for inclusion.
+ function invalidFields($fields) {
+ if (isset($fields)) {
+ if (is_array($fields))
+ $this->invalid = array_merge($this->invalid, $fields);
+ else
+ $this->invalid[] = $fields;
+ }
+ return $this;
+ }
+
+ function searchFields($fields) {
+ if (isset($fields)) {
+ if (is_array($fields))
+ $this->jqGrid_options['search_fields']
+ = array_merge($this->jqGrid_options['search_fields'], $fields);
+ else
+ $this->jqGrid_options['search_fields'][] = $fields;
+ }
+ return $this;
+ }
+
+ function sortField($field) {
+ $this->jqGrid_options['sort_column'] = $field;
+ return $this;
+ }
+
+ function limit($limit) {
+ $this->jqGrid_options['limit'] = $limit;
return $this;
}
function id_list($items) {
- $this->jqGrid_options
- += array('custom_ids' =>
- array_map(create_function('$data',
- 'return $data["id"];'),
- $items));
+ $this->jqGrid_options['custom_ids']
+ = array_map(create_function('$data',
+ 'return $data["id"];'),
+ $items);
return $this;
}
- function render($included = array(), $excluded = array()) {
- pr("
jqGrid
");
+ function customData($data) {
+ if (isset($data)) {
+ if (is_array($data))
+ $this->jqGrid_options['custom_post_data']
+ = array_merge($this->jqGrid_options['custom_post_data'], $data);
+ else
+ $this->jqGrid_options['custom_post_data'][] = $data;
+ }
+ return $this;
+ }
- $columns = $this->columns;
- $we_want = $this->included;
- $they_want = $included;
- $we_dont_want = array_diff(array_keys($this->columns), $this->included);
- $they_dont_want = $excluded;
+ /**************************************************************************
+ **************************************************************************
+ **************************************************************************
+ * function: render
+ * - Renders the grid.
+ * Unless manipulated with the included / excluded parameters,
+ * the grid will contain the columns defined as part of the
+ * standard set, as set through use of the the included() function.
+ *
+ * args
+ * included:
+ * Array of column names to include in the grid.
+ * - OR -
+ * true: includes all columns.
+ * - OR -
+ * false: includes no additional columns beyond
+ * the standard defined set.
+
+ * excluded:
+ * Array of column names to exclude from the grid.
+ * These will be excluded even if explicitly included
+ * through the included parameter.
+ * - OR -
+ * true: excludes all columns other than those
+ * explicitly included through the included parameter.
+ * - OR -
+ * false: does not exclude any included columns.
+ */
+
+ function render($view, $config = null, $included = true, $excluded = false) {
+ // Handle null values for included/excluded
+ if (!isset($included))
+ $included = false;
+ if (!isset($excluded))
+ $included = false;
+
+ // Convert true/false into an array
if (is_bool($included) && $included)
- $included = array_keys($columns);
+ $included = array_keys($this->columns);
elseif (is_bool($included) && !$included)
$included = array();
+ // Convert true/false into an array
if (is_bool($excluded) && $excluded)
- $excluded = array_keys($columns);
+ $excluded = array_diff_key(array_keys($this->columns), $included);
elseif (is_bool($excluded) && !$excluded)
$excluded = array();
- $hopefully_wanted = array_merge($this->included, $included);
- $hopefully_get = array_diff(array_merge($this->included, $included), $excluded);
+ // Calculate the actual inclusion set
+ $included = array_diff(array_merge($this->included, $included),
+ array_merge($this->invalid, $excluded));
- $actually_get = array_intersect(array_keys($this->columns), $hopefully_get);
- $actually_dont_get = array_diff(array_keys($this->columns), $hopefully_get);
+ // Extract the columns that correspond to the inclusion set
+ $this->jqGrid_options['jqGridColumns']
+ = array_intersect_key($this->columns, array_flip($included));
- pr(compact('we_want', 'they_want', 'we_dont_want', 'they_dont_want',
- 'hopefully_wanted', 'hopefully_get',
- 'actually_get', 'actually_dont_get'));
+ // As an exception to the normal config variables,
+ // handle 'rows' here. The reason is so that we
+ // ease the burden on views which have a list of
+ // items in a CakePHP style array, but jqGrid is
+ // expecting just an array if IDs. So, we'll run
+ // as middle man (that is our purpose, after all)
+ if (isset($config['rows'])) {
+ // Shrink the limit... user can always override
+ $this->id_list($config['rows'])->limit(10);
+ }
+
+ // Figure out what controller we're using to
+ // populate the grid via ajax, and set it.
+ $controller = $this->controller;
+ if (!isset($controller)) {
+ // Try to get the controller based on the convention
+ // that our grid elements are named after the controller.
+ // If this doesn't work, user will have to manually set
+ // $this->controller to whatever they need.
+ $trace = debug_backtrace(false);
+ if (preg_match('%elements[/\\\\]([^.]+)\.ctp$%',
+ $trace[0]['file'], $matches))
+ $controller = $matches[1];
+ }
+ $this->jqGrid_options['controller'] = $controller;
+
+ // Incorporate all other user options
+ if (isset($config))
+ $this->jqGrid_options = array_merge($this->jqGrid_options, $config);
+
+ echo $view->element('jqGrid', $this->jqGrid_options);
// Since we only have one instance of this class
// as a helper, we must assume it could be used
@@ -93,5 +198,3 @@ class GridHelper extends AppHelper {
}
}
-
-