Ejemplo n.º 1
0
    /**
     * @param string $type
     * @param array  $attributes
     * @param string $contents
     *
     * @return string
     */
    public function field($type, array $attributes = array(), $contents = null)
    {
        $_html = null;
        $_type = strtolower(trim($type));
        $_wrapInput = Scalar::in($_type, 'checkbox', 'radio');
        $_labelEnd = null;
        $_labelAttributes = array();
        $_id = Option::get($attributes, 'id', Option::get($attributes, 'name'));
        $_inputAppend = Option::get($attributes, 'append', null, true);
        $_inputPrepend = Option::get($attributes, 'prepend', null, true);
        $this->cleanNames($attributes);
        $_label = Option::get($attributes, 'label', null, true);
        if (is_array($_label)) {
            $_labelAttributes = Option::get($_label, 'attributes', array());
            $_labelText = Option::get($_label, 'value', array());
        } else {
            $_labelText = $_label;
        }
        //	Add .control-label for the class to labels
        if (static::Horizontal == $this->_formType) {
            if (!isset($_labelAttributes['class'])) {
                $_labelAttributes['class'] = null;
            }
            $_labelAttributes['class'] = static::addValue($_labelAttributes['class'], $_wrapInput ? $_type : 'control-label');
        }
        $_label = static::tag('label', $_labelAttributes, $_wrapInput ? null : $_labelText, !$_wrapInput);
        $_labelEnd = $_wrapInput ? $_label . '</label>' : null;
        if (null !== ($_hint = Option::get($attributes, 'hint', null, true))) {
            $_hint = static::tag('span', array('class' => 'help-block'), $_hint);
        }
        $_blockStart = $_blockEnd = null;
        $_inputStart = $_inputEnd = null;
        $_inputWrap = trim(($_inputAppend ? 'input-append' : null) . ' ' . ($_inputPrepend ? 'input-prepend' : null));
        switch ($this->_formType) {
            case static::Horizontal:
                $_blockStart = static::tag('div', array('class' => 'control-group'), null, false);
                $_inputStart = static::tag('div', array('class' => 'controls'), null, false);
                $_inputEnd = $_blockEnd = '</div>';
                if (!$_wrapInput) {
                    $_blockStart .= $_label . $_labelEnd;
                    $_label = $_labelEnd = null;
                }
                break;
            case static::Search:
                $_class = Option::get($attributes, 'class');
                $attributes['class'] = static::addValue($_class, 'search-query');
                break;
        }
        if (null === $contents && !empty($this->_formData)) {
            $contents = Option::get($this->_formData, $_id);
        }
        //	X-editable?
        if (false !== stripos(Option::get($attributes, 'class'), 'x-editable')) {
            //	Replace contents with the x-editable link...
            $contents = static::tag('a', array('class' => 'x-editable', 'href' => '#', 'id' => $_id, 'data-type' => $_type, 'data-pk' => Option::get($this->_attributes, 'x-editable-pk', Option::get($this->_formData, 'id')), 'data-url' => Option::get($this->_attributes, 'x-editable-url', Option::get($attributes, 'x-editable-url', null, true)), 'data-original-title' => $_labelText, 'data-inputclass' => Option::get($attributes, 'class', 'input-xlarge')), $contents);
            //	New type of just HTML...
            $_type = 'html';
        }
        switch ($_type) {
            case 'html':
                $_input = $contents;
                break;
            case 'textarea':
                $attributes['class'] = static::addValue(Option::get($attributes, 'class'), 'input-xxlarge');
                $_input = static::wrap($_type, $contents, $attributes);
                break;
            case 'select':
                if (!isset($attributes['value'])) {
                    $attributes['value'] = $contents;
                }
                $_input = static::select(Option::get($attributes, 'data', array(), true), $attributes);
                break;
            case 'button':
                $_input = static::button($attributes, $contents);
                break;
            default:
                $_input = $this->_handleUnknownField($_type, $attributes, $contents);
                break;
        }
        if (!empty($_inputWrap)) {
            $_input = '<div class="' . $_inputWrap . '">' . $_inputPrepend . $_input . $_inputAppend . '</div>';
        }
        $_html = <<<HTML
{$_blockStart}{$_inputStart}{$_label}{$_input}{$_labelEnd}{$_hint}{$_inputEnd}{$_blockEnd}
HTML;
        if (false !== $this->_builderMode) {
            $this->_contents .= $_html;
        }
        return $_html;
    }