From 4e2b073110838f36848efa15a65f3c2e561d007f Mon Sep 17 00:00:00 2001 From: abijah Date: Wed, 8 Jul 2009 17:33:43 +0000 Subject: [PATCH] Added a helper to jqGrid, since duplicated code is getting spread through each element. The implementation is nowhere near complete, but the basic idea is there. git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@257 97e9348a-65ac-dc4b-aefc-98561f571b83 --- app_controller.php | 2 +- views/helpers/jq_grid.php | 97 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 views/helpers/jq_grid.php diff --git a/app_controller.php b/app_controller.php index 86da138..4405659 100644 --- a/app_controller.php +++ b/app_controller.php @@ -35,7 +35,7 @@ * @subpackage cake.app */ class AppController extends Controller { - var $helpers = array('Html', 'Form', 'Javascript', 'Format', 'Time'); + var $helpers = array('Html', 'Form', 'Javascript', 'Format', 'Time', 'JqGrid'); var $components = array('DebugKit.Toolbar'); function sideMenuLinks() { diff --git a/views/helpers/jq_grid.php b/views/helpers/jq_grid.php new file mode 100644 index 0000000..9b05dcc --- /dev/null +++ b/views/helpers/jq_grid.php @@ -0,0 +1,97 @@ +reset(); + } + + function reset() { + $this->jqGrid_options + = array('limit' => 20); + + $this->columns = array(); + $this->included = array(); + return $this; + } + + // Add to the set of columns for this grid + function columns($columns) { + if (isset($columns)) { + if (is_array($columns)) + $this->columns += $columns; + else + array_push($this->columns, $columns); + } + return $this; + } + + // Included fields will be included unless excluded. + function included($fields) { + if (isset($fields)) { + if (is_array($fields)) + $this->included = array_merge($this->included, $fields); + else + array_push($this->included, $fields); + } + return $this; + } + + function limit() { + $this->jqGrid_options['limit'] = 20; + return $this; + } + + function id_list($items) { + $this->jqGrid_options + += array('custom_ids' => + array_map(create_function('$data', + 'return $data["id"];'), + $items)); + return $this; + } + + function render($included = array(), $excluded = array()) { + pr("

jqGrid

"); + + $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; + + if (is_bool($included) && $included) + $included = array_keys($columns); + elseif (is_bool($included) && !$included) + $included = array(); + + if (is_bool($excluded) && $excluded) + $excluded = array_keys($columns); + elseif (is_bool($excluded) && !$excluded) + $excluded = array(); + + $hopefully_wanted = array_merge($this->included, $included); + $hopefully_get = array_diff(array_merge($this->included, $included), $excluded); + + $actually_get = array_intersect(array_keys($this->columns), $hopefully_get); + $actually_dont_get = array_diff(array_keys($this->columns), $hopefully_get); + + pr(compact('we_want', 'they_want', 'we_dont_want', 'they_dont_want', + 'hopefully_wanted', 'hopefully_get', + 'actually_get', 'actually_dont_get')); + + // Since we only have one instance of this class + // as a helper, we must assume it could be used + // again later to render an entirely different grid. + // Reset the variables to prevent contamination. + $this->reset(); + return $this; + } + +} + +