/** * Add extra formatter to existing field. * * @param string $field * @param mixed $formatter * @param array $options * * @return $this || Controller_Grid_Format */ public function addFormatter($field, $formatter, $options = null) { if (!isset($this->columns[$field])) { throw new BaseException('Cannot format nonexistant field ' . $field); } if ($this->columns[$field]['type']) { $this->columns[$field]['type'] .= ',' . $formatter; } else { $this->columns[$field]['type'] = $formatter; } if ($options) { $this->columns[$field] = array_merge($this->columns[$field], $options); } $descr = $this->columns[$field]; if (strpos($formatter, '\\') || strpos($formatter, '/')) { // add-on functionality: // http://agiletoolkit.org/codepad/gui/grid#codepad_gui_grid_view_example_7_ex if (!$this->elements[$formatter . '_' . $field]) { $addon = $this->app->normalizeClassName($formatter, 'Controller_Grid_Format'); $this->elements[$formatter . '_' . $field] = $this->add($addon, $formatter); } $addon = $this->getElement($formatter . '_' . $field); if (!$addon instanceof Controller_Grid_Format) { throw $this->exception('Grid formatter class should extend Controller_Grid_Format class')->addMoreInfo('formater', $formatter); } $addon->initField($field, $descr); return $addon; } elseif ($this->hasMethod($m = 'init_' . $formatter)) { // execute formatter $this->{$m}($field, $descr); } return $this; }
/** * Adds field in form. * * @param AbstractView|array|string $type * @param AbstractView|array|string $options * @param string $caption * @param string $attr Deprecated argument * * @return Form_Field */ public function addField($type, $options = null, $caption = null, $attr = null) { $insert_into = $this->layout ?: $this; if (is_object($type) && $type instanceof AbstractView && !$type instanceof Form_Field) { // using callback on a sub-view $insert_into = $type; list(, $type, $options, $caption, $attr) = func_get_args(); } if ($options === null) { $options = $type; $type = 'Line'; } if (is_array($options)) { $name = isset($options['name']) ? $options['name'] : null; } else { $name = $options; // backward compatibility } $name = preg_replace('|[^a-z0-9-_]|i', '_', $name); if ($caption === null) { $caption = ucwords(str_replace('_', ' ', $name)); } /* normalzie name and put name back in options array */ $name = $this->app->normalizeName($name); if (is_array($options)) { $options['name'] = $name; } else { $options = array('name' => $name); } $map = array('dropdown' => 'DropDown', 'checkboxlist' => 'CheckboxList', 'hidden' => 'Hidden', 'text' => 'Text', 'line' => 'Line', 'upload' => 'Upload', 'radio' => 'Radio', 'checkbox' => 'Checkbox', 'password' => 'Password', 'timepicker' => 'TimePicker'); $key = strtolower($type); $class = array_key_exists($key, $map) ? $map[$key] : $type; $class = $this->app->normalizeClassName($class, 'Form_Field'); if ($insert_into === $this) { $template = $this->template->cloneRegion('form_line'); $field = $this->add($class, $options, null, $template); } else { if ($insert_into->template->hasTag($name)) { $this->template->cloneRegion('field_input'); $options['show_input_only'] = true; $field = $insert_into->add($class, $options, $name); } else { $template = $this->template->cloneRegion('form_line'); $field = $insert_into->add($class, $options, null, $template); } // Keep Reference, for $form->getElement(). $this->elements[$options['name']] = $field; } /** @type Form_Field $field */ $field->setCaption($caption); $field->setForm($this); $field->template->trySet('field_type', strtolower($type)); if ($attr) { if ($this->app->compat_42) { $field->setAttr($attr); } else { throw $this->exception('4th argument to addField is obsolete'); } } return $field; }