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

loadForm() private method

Load the form
private loadForm ( )
    private function loadForm()
    {
        // create form
        $this->frm = new BackendForm('edit');
        // init var
        $defaultId = $this->get('fork.settings')->get('Pages', 'default_template');
        // build available themes
        $themes = array();
        foreach (BackendExtensionsModel::getThemes() as $theme) {
            $themes[$theme['value']] = $theme['label'];
        }
        // create elements
        $this->frm->addDropdown('theme', $themes, $this->get('fork.settings')->get('Core', 'theme', 'core'));
        $this->frm->addText('label', $this->record['label']);
        $this->frm->addText('file', str_replace('Core/Layout/Templates/', '', $this->record['path']));
        $this->frm->addTextarea('format', str_replace('],[', "],\n[", $this->record['data']['format']));
        $this->frm->addCheckbox('active', $this->record['active'] == 'Y');
        $this->frm->addCheckbox('default', $this->record['id'] == $defaultId);
        $this->frm->addCheckbox('overwrite', false);
        $this->frm->addCheckbox('image', $this->record['data']['image']);
        // if this is the default template we can't alter the active/default state
        if ($this->record['id'] == $defaultId) {
            $this->frm->getField('active')->setAttributes(array('disabled' => 'disabled'));
            $this->frm->getField('default')->setAttributes(array('disabled' => 'disabled'));
        }
        // if the template is in use we cant alter the active state
        if (BackendExtensionsModel::isTemplateInUse($this->id)) {
            $this->frm->getField('active')->setAttributes(array('disabled' => 'disabled'));
        }
        // 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 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');
            if (isset($this->extras[$name])) {
                foreach ($this->extras[$name] as $y => $extra) {
                    $position['blocks'][]['formElements']['ddmType'] = $this->frm->addDropdown('type_' . $position['i'] . '_' . $y, $defaultExtras, $extra, false, 'form-control positionBlock', 'form-control positionBlockError');
                }
            }
            $positions[] = $position;
        }
        // assign
        $this->tpl->assign('positions', $positions);
    }