Backend\Modules\Extensions\Actions\AddThemeTemplate::loadForm PHP Method

loadForm() private method

Load the form
private loadForm ( )
    private function loadForm()
    {
        // create form
        $this->frm = new BackendForm('add');
        // create elements
        $this->frm->addDropdown('theme', $this->availableThemes, $this->selectedTheme);
        $this->frm->addText('label');
        $this->frm->addText('file');
        $this->frm->addTextarea('format');
        $this->frm->addCheckbox('active', true);
        $this->frm->addCheckbox('default');
        $this->frm->addCheckbox('image');
        // init vars
        $positions = array();
        $blocks = array();
        $widgets = array();
        $extras = BackendExtensionsModel::getExtras();
        // loop extras to populate the default extras
        foreach ($extras as $item) {
            if ($item['type'] == 'block') {
                $blocks[$item['id']] = \SpoonFilter::ucfirst(BL::lbl($item['label']));
                if (isset($item['data']['extra_label'])) {
                    $blocks[$item['id']] = \SpoonFilter::ucfirst($item['data']['extra_label']);
                }
            } elseif ($item['type'] == 'widget') {
                $widgets[$item['id']] = \SpoonFilter::ucfirst(BL::lbl(\SpoonFilter::toCamelCase($item['module']))) . ': ' . \SpoonFilter::ucfirst(BL::lbl($item['label']));
                if (isset($item['data']['extra_label'])) {
                    $widgets[$item['id']] = \SpoonFilter::ucfirst(BL::lbl(\SpoonFilter::toCamelCase($item['module']))) . ': ' . $item['data']['extra_label'];
                }
            }
        }
        // sort
        asort($blocks, SORT_STRING);
        asort($widgets, SORT_STRING);
        // create array
        $defaultExtras = array('' => array(0 => \SpoonFilter::ucfirst(BL::lbl('Editor'))), \SpoonFilter::ucfirst(BL::lbl('Widgets')) => $widgets);
        // create default position field
        $position = array();
        $position['i'] = 0;
        $position['formElements']['txtPosition'] = $this->frm->addText('position_' . $position['i'], null, 255, 'form-control positionName', 'form-control danger positionName');
        $position['blocks'][]['formElements']['ddmType'] = $this->frm->addDropdown('type_' . $position['i'] . '_' . 0, $defaultExtras, null, false, 'form-control positionBlock', 'form-control danger positionBlockError');
        $positions[] = $position;
        // content has been submitted: re-create submitted content rather than the db-fetched content
        if (isset($_POST['position_0'])) {
            // init vars
            $this->names = array();
            $this->extras = array();
            $i = 1;
            $errors = array();
            // loop submitted positions
            while (isset($_POST['position_' . $i])) {
                // init vars
                $j = 0;
                $extras = array();
                // gather position names
                $name = $_POST['position_' . $i];
                // loop submitted blocks
                while (isset($_POST['type_' . $i . '_' . $j])) {
                    // gather blocks id
                    $extras[] = (int) $_POST['type_' . $i . '_' . $j];
                    // increment counter; go fetch next block
                    ++$j;
                }
                // increment counter; go fetch next position
                ++$i;
                // position already exists -> error
                if (in_array($name, $this->names)) {
                    $errors[] = sprintf(BL::getError('DuplicatePositionName'), $name);
                }
                // position name == fallback -> error
                if ($name == 'fallback') {
                    $errors[] = sprintf(BL::getError('ReservedPositionName'), $name);
                }
                // not alphanumeric -> error
                if (!\SpoonFilter::isValidAgainstRegexp('/^[a-z0-9]+$/i', $name)) {
                    $errors[] = sprintf(BL::getError('NoAlphaNumPositionName'), $name);
                }
                // save positions
                $this->names[] = $name;
                $this->extras[$name] = $extras;
            }
            // add errors
            if ($errors) {
                $this->frm->addError(implode('<br />', array_unique($errors)));
            }
        }
        // build blocks array
        foreach ($this->names as $i => $name) {
            // create default position field
            $position = array();
            $position['i'] = $i + 1;
            $position['formElements']['txtPosition'] = $this->frm->addText('position_' . $position['i'], $name, 255, 'form-control positionName', 'form-control danger positionName');
            foreach ($this->extras[$name] as $extra) {
                $position['blocks'][]['formElements']['ddmType'] = $this->frm->addDropdown('type_' . $position['i'] . '_' . 0, $defaultExtras, $extra, false, 'form-control positionBlock', 'form-control danger positionBlockError');
            }
            $positions[] = $position;
        }
        // assign
        $this->tpl->assign('positions', $positions);
    }