Backend\Modules\Extensions\Engine\Model::getExtras PHP Method

getExtras() public static method

Get extras
public static getExtras ( ) : array
return array
    public static function getExtras()
    {
        $extras = (array) BackendModel::getContainer()->get('database')->getRecords('SELECT i.id, i.module, i.type, i.label, i.data
             FROM modules_extras AS i
             INNER JOIN modules AS m ON i.module = m.name
             WHERE i.hidden = ?
             ORDER BY i.module, i.sequence', array('N'), 'id');
        $itemsToRemove = array();
        foreach ($extras as $id => &$row) {
            $row['data'] = @unserialize($row['data']);
            if (isset($row['data']['language']) && $row['data']['language'] != BL::getWorkingLanguage()) {
                $itemsToRemove[] = $id;
            }
            // set URL if needed, we use '' instead of null, because otherwise the module of the current action (modules) is used.
            if (!isset($row['data']['url'])) {
                $row['data']['url'] = BackendModel::createURLForAction('', $row['module']);
            }
            $name = \SpoonFilter::ucfirst(BL::lbl($row['label']));
            if (isset($row['data']['extra_label'])) {
                $name = $row['data']['extra_label'];
            }
            if (isset($row['data']['label_variables'])) {
                $name = vsprintf($name, $row['data']['label_variables']);
            }
            // add human readable name
            $module = \SpoonFilter::ucfirst(BL::lbl(\SpoonFilter::toCamelCase($row['module'])));
            $row['human_name'] = \SpoonFilter::ucfirst(BL::lbl(\SpoonFilter::toCamelCase('ExtraType_' . $row['type']))) . ': ' . $name;
            $row['path'] = \SpoonFilter::ucfirst(BL::lbl(\SpoonFilter::toCamelCase('ExtraType_' . $row['type']))) . ' › ' . $module . ($module != $name ? ' › ' . $name : '');
        }
        // any items to remove?
        if (!empty($itemsToRemove)) {
            foreach ($itemsToRemove as $id) {
                unset($extras[$id]);
            }
        }
        return $extras;
    }

Usage Example

Example #1
0
 /**
  * Load the form
  */
 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);
 }
All Usage Examples Of Backend\Modules\Extensions\Engine\Model::getExtras