reset(); } function reset() { $this->jqGrid_options = array('limit' => 20, 'search_fields' => array(), 'custom_post_data' => array(), ); $this->columns = array(); $this->included = array(); $this->invalid = array(); $this->controller = null; return $this; } // Add to the set of columns for this grid function columns($columns) { if (isset($columns)) { if (is_array($columns)) $this->columns = array_merge($this->columns, $columns); else $this->columns[] = $columns; } return $this; } // 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 $this->included[] = $fields; } return $this; } // 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, $order = null) { $this->jqGrid_options['sort_column'] = $field; if ($order) $this->jqGrid_options['sort_order'] = $order; return $this; } function limit($limit) { $this->jqGrid_options['limit'] = $limit; return $this; } function id_list($items) { $this->jqGrid_options['custom_ids'] = array_map(create_function('$data', 'return $data["id"];'), $items); $this->jqGrid_options['action'] = 'idlist'; return $this; } 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; } /************************************************************************** ************************************************************************** ************************************************************************** * 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($this->columns); elseif (is_bool($included) && !$included) $included = array(); // Convert true/false into an array if (is_bool($excluded) && $excluded) $excluded = array_diff_key(array_keys($this->columns), $included); elseif (is_bool($excluded) && !$excluded) $excluded = array(); // Tack on any config include/exclude requests if (isset($config['include'])) $included = array_merge($included, $config['include']); if (isset($config['exclude'])) $excluded = array_merge($excluded, $config['exclude']); unset($config['include'], $config['exclude']); // Calculate the actual inclusion set $included = array_diff(array_merge($this->included, $included), array_merge($this->invalid, $excluded)); // Defined the columns, based on the inclusion set, // remapping column names as necessary. $this->jqGrid_options['jqGridColumns'] = array(); foreach (array_intersect_key($this->columns, array_flip($included)) AS $name => $col) { if (!empty($config['remap'][$name])) $name = $config['remap'][$name]; $this->jqGrid_options['jqGridColumns'][$name] = $col; } // Make sure search fields are all part of the inclusion set $this->jqGrid_options['search_fields'] = array_intersect($this->jqGrid_options['search_fields'], $included); // 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); unset($config['rows']); } // One more exception, as the search fields get // defined, but not passed to jqGrid unless // specifically requested. if (isset($config['search'])) unset($config['search']); else unset($this->jqGrid_options['search_fields']); // 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); //pr(compact('config') + array('jqGrid_options' => $this->jqGrid_options)); 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 // again later to render an entirely different grid. // Reset the variables to prevent contamination. $this->reset(); return $this; } }