Grid_Basic::addColumn PHP 메소드

addColumn() 공개 메소드

Add column to grid.
public addColumn ( mixed $formatters, string $name = null, string | array $descr = null )
$formatters mixed
$name string
$descr string | array
    public function addColumn($formatters, $name = null, $descr = null)
    {
        if ($name === null) {
            $name = $formatters;
            $formatters = 'text';
        }
        if ($descr === null) {
            $descr = ucwords(str_replace('_', ' ', $name));
        }
        if (is_array($descr)) {
            $descr['descr'] = $this->app->_($descr['descr']);
        } else {
            $descr = $this->app->_($descr);
        }
        $this->columns[$name] = array('type' => $formatters);
        if (is_array($descr)) {
            $this->columns[$name] = array_merge($this->columns[$name], $descr);
        } else {
            $this->columns[$name]['descr'] = $descr;
        }
        if ($this->columns[$name]['icon']) {
            if ($this->columns[$name]['icon'][0] != '<') {
                $this->columns[$name]['icon'] = '<i class="icon-' . $this->columns[$name]['icon'] . '"></i>&nbsp;';
            } else {
                throw $this->exception('obsolete way of using icon. Do not specify HTML code, but juts the icon');
            }
        }
        $this->last_column = $name;
        if (!is_string($formatters) && is_callable($formatters)) {
            $this->columns[$name]['fx'] = $formatters;
            return $this;
        }
        // TODO call addFormatter instead!
        $subtypes = explode(',', $formatters);
        foreach ($subtypes as $subtype) {
            if (strpos($subtype, '\\') || strpos($subtype, '/')) {
                // add-on functionality:
                // http://agiletoolkit.org/codepad/gui/grid#codepad_gui_grid_view_example_7_ex
                if (!$this->elements[$subtype . '_' . $name]) {
                    $addon = $this->app->normalizeClassName($subtype, 'Controller_Grid_Format');
                    $this->elements[$subtype . '_' . $name] = $this->add($addon);
                }
                $addon = $this->getElement($subtype . '_' . $name);
                if (!$addon instanceof Controller_Grid_Format) {
                    throw $this->exception('Grid formatter class should extend Controller_Grid_Format class')->addMoreInfo('formater', $subtype);
                }
                $addon->initField($name, $descr);
                return $addon;
            } elseif (!$this->hasMethod($m = 'init_' . $subtype)) {
                if (!$this->hasMethod($m = 'format_' . $subtype)) {
                    // exception if formatter doesn't exist
                    throw $this->exception('No such formatter')->addMoreInfo('formater', $subtype);
                }
            } else {
                // execute formatter init_*
                $this->{$m}($name, $descr);
            }
        }
        return $this;
    }