Exemplo n.º 1
0
    /**
     * Renders HTML code of form.
     *
     * @return string generated HTML code
     */
    public function getCode()
    {
        $method = $this->usePost ? 'post' : 'get';
        $action = is_null($this->processorUrl) ? application::current()->selfUrl($this->usePost ? array() : false) : $this->processorUrl;
        $mime = $this->maxFileSize > 0 ? ' enctype="multipart/form-data"' : '';
        $size = $this->maxFileSize > 0 ? '<input type="hidden" name="MAX_FILE_SIZE" value="' . $this->maxFileSize . "\"/>\n\t" : '';
        $name = html::idname($this->name);
        $idName = $this->idName();
        $idValue = $this->idValue();
        $labelClass = 'without-labels';
        // compile code of form's rows
        $template = self::getRowTemplate();
        $rows = array_filter($this->rows, function ($row) {
            return !!count($row);
        });
        if ($this->sortingOrder) {
            data::rearrangeArray($rows, $this->sortingOrder, true);
        }
        $rows = array_map(function ($row) use($template, &$labelClass) {
            if (@$row['label']) {
                $labelClass = 'with-labels';
            }
            $label = view::wrapNotEmpty(@$row['label'], '', '');
            $code = view::wrapNotEmpty(@$row['htmlCode'], '', '');
            $hint = view::wrapNotEmpty(@$row['hint'], '<span class="hint">', "</span>\n");
            $error = view::wrapNotEmpty(@$row['error'], '<span class="error">', "</span>\n");
            $mandatory = @$row['mandatory'] ? config::get('html.form.mandatory', '<span class="mandatory">*</span>') : '';
            if (trim($label) !== '') {
                $label = sprintf(config::get('html.form.label', '%s%s:'), $label, $mandatory);
            }
            return sprintf($template, trim('form-row-name-' . @$row['name'] . ' ' . @$row['class']), $label, $code, $error, $hint);
        }, $rows);
        // embed compiled rows in form's custom content
        $code = str_replace('%%%%ROWS_STACK%%%%', implode('', $rows), $this->code);
        $hidden = $this->enableXsrfMode ? "<input type=\"hidden\" name=\"{$idName}\" value=\"{$idValue}\"/>" : '';
        foreach ($this->hidden as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $subName => $subValue) {
                    $hidden .= '<input type="hidden" name="' . html::inAttribute($key) . '[' . html::inAttribute($subName) . ']" value="' . html::inAttribute($subValue) . '"/>';
                }
            } else {
                if ($value !== null) {
                    $hidden .= '<input type="hidden" name="' . html::inAttribute($key) . '" value="' . html::inAttribute($value) . '"/>';
                }
            }
        }
        $class = ' class="' . html::inAttribute(trim($this->class . ' ' . $labelClass)) . '"';
        return <<<EOT
<form action="{$action}" method="{$method}"{$mime} id="{$name}"{$class}>
\t{$size}{$hidden}
{$code}
</form>
EOT;
    }