Contao\Backend::getBackendModule PHP Method

getBackendModule() protected method

Open a back end module and return it as HTML
protected getBackendModule ( string $module ) : string
$module string
return string
    protected function getBackendModule($module)
    {
        $arrModule = array();
        foreach ($GLOBALS['BE_MOD'] as &$arrGroup) {
            if (isset($arrGroup[$module])) {
                $arrModule =& $arrGroup[$module];
                break;
            }
        }
        $this->import('BackendUser', 'User');
        // Dynamically add the "personal data" module (see #4193)
        if (\Input::get('do') == 'login') {
            $arrModule = array('tables' => array('tl_user'), 'callback' => 'ModuleUser');
        } elseif ($module != 'undo' && !$this->User->hasAccess($module, 'modules')) {
            throw new AccessDeniedException('Back end module "' . $module . '" is not allowed for user "' . $this->User->username . '".');
        }
        /** @var SessionInterface $objSession */
        $objSession = \System::getContainer()->get('session');
        $arrTables = (array) $arrModule['tables'];
        $strTable = \Input::get('table') ?: $arrTables[0];
        $id = !\Input::get('act') && \Input::get('id') ? \Input::get('id') : $objSession->get('CURRENT_ID');
        // Store the current ID in the current session
        if ($id != $objSession->get('CURRENT_ID')) {
            $objSession->set('CURRENT_ID', $id);
        }
        define('CURRENT_ID', \Input::get('table') ? $id : \Input::get('id'));
        $this->Template->headline = $GLOBALS['TL_LANG']['MOD'][$module][0];
        // Add the module style sheet
        if (isset($arrModule['stylesheet'])) {
            foreach ((array) $arrModule['stylesheet'] as $stylesheet) {
                $GLOBALS['TL_CSS'][] = $stylesheet;
            }
        }
        // Add module javascript
        if (isset($arrModule['javascript'])) {
            foreach ((array) $arrModule['javascript'] as $javascript) {
                $GLOBALS['TL_JAVASCRIPT'][] = $javascript;
            }
        }
        $dc = null;
        // Create the data container object
        if ($strTable != '') {
            if (!in_array($strTable, $arrTables)) {
                throw new AccessDeniedException('Table "' . $strTable . '" is not allowed in module "' . $module . '".');
            }
            // Load the language and DCA file
            \System::loadLanguageFile($strTable);
            $this->loadDataContainer($strTable);
            // Include all excluded fields which are allowed for the current user
            if ($GLOBALS['TL_DCA'][$strTable]['fields']) {
                foreach ($GLOBALS['TL_DCA'][$strTable]['fields'] as $k => $v) {
                    if ($v['exclude']) {
                        if ($this->User->hasAccess($strTable . '::' . $k, 'alexf')) {
                            if ($strTable == 'tl_user_group') {
                                $GLOBALS['TL_DCA'][$strTable]['fields'][$k]['orig_exclude'] = $GLOBALS['TL_DCA'][$strTable]['fields'][$k]['exclude'];
                            }
                            $GLOBALS['TL_DCA'][$strTable]['fields'][$k]['exclude'] = false;
                        }
                    }
                }
            }
            // Fabricate a new data container object
            if ($GLOBALS['TL_DCA'][$strTable]['config']['dataContainer'] == '') {
                $this->log('Missing data container for table "' . $strTable . '"', __METHOD__, TL_ERROR);
                trigger_error('Could not create a data container object', E_USER_ERROR);
            }
            $dataContainer = 'DC_' . $GLOBALS['TL_DCA'][$strTable]['config']['dataContainer'];
            /** @var DataContainer $dc */
            $dc = new $dataContainer($strTable, $arrModule);
        }
        // AJAX request
        if ($_POST && \Environment::get('isAjaxRequest')) {
            $this->objAjax->executePostActions($dc);
        } elseif (class_exists($arrModule['callback'])) {
            /** @var Module $objCallback */
            $objCallback = new $arrModule['callback']($dc);
            $this->Template->main .= $objCallback->generate();
        } elseif (\Input::get('key') && isset($arrModule[\Input::get('key')])) {
            $objCallback = \System::importStatic($arrModule[\Input::get('key')][0]);
            $this->Template->main .= $objCallback->{$arrModule[\Input::get('key')][1]}($dc);
            // Add the name of the parent element
            if (isset($_GET['table']) && in_array(\Input::get('table'), $arrTables) && \Input::get('table') != $arrTables[0]) {
                if ($GLOBALS['TL_DCA'][$strTable]['config']['ptable'] != '') {
                    $objRow = $this->Database->prepare("SELECT * FROM " . $GLOBALS['TL_DCA'][$strTable]['config']['ptable'] . " WHERE id=?")->limit(1)->execute(CURRENT_ID);
                    if ($objRow->title != '') {
                        $this->Template->headline .= ' » ' . $objRow->title;
                    } elseif ($objRow->name != '') {
                        $this->Template->headline .= ' » ' . $objRow->name;
                    }
                }
            }
            // Add the name of the submodule
            $this->Template->headline .= ' » ' . sprintf($GLOBALS['TL_LANG'][$strTable][\Input::get('key')][1], \Input::get('id'));
        } elseif (is_object($dc)) {
            $act = \Input::get('act');
            if ($act == '' || $act == 'paste' || $act == 'select') {
                $act = $dc instanceof \listable ? 'showAll' : 'edit';
            }
            switch ($act) {
                case 'delete':
                case 'show':
                case 'showAll':
                case 'undo':
                    if (!$dc instanceof \listable) {
                        $this->log('Data container ' . $strTable . ' is not listable', __METHOD__, TL_ERROR);
                        trigger_error('The current data container is not listable', E_USER_ERROR);
                    }
                    break;
                case 'create':
                case 'cut':
                case 'cutAll':
                case 'copy':
                case 'copyAll':
                case 'move':
                case 'edit':
                    if (!$dc instanceof \editable) {
                        $this->log('Data container ' . $strTable . ' is not editable', __METHOD__, TL_ERROR);
                        trigger_error('The current data container is not editable', E_USER_ERROR);
                    }
                    break;
            }
            $strFirst = null;
            $strSecond = null;
            // Handle child child tables (e.g. tl_style)
            if (isset($GLOBALS['TL_DCA'][$strTable]['config']['ptable'])) {
                $ptable = $GLOBALS['TL_DCA'][$strTable]['config']['ptable'];
                if (in_array($ptable, $arrTables)) {
                    $this->loadDataContainer($ptable);
                    if (isset($GLOBALS['TL_DCA'][$ptable]['config']['ptable'])) {
                        $ftable = $GLOBALS['TL_DCA'][$ptable]['config']['ptable'];
                        if (in_array($ftable, $arrTables)) {
                            $strFirst = $ftable;
                            $strSecond = $ptable;
                        }
                    }
                }
            }
            // Build the breadcrumb trail
            if ($strFirst !== null && $strSecond !== null) {
                if (!isset($_GET['act']) || \Input::get('act') == 'paste' && \Input::get('mode') == 'create' || \Input::get('act') == 'select' || \Input::get('act') == 'editAll' || \Input::get('act') == 'overrideAll') {
                    if ($strTable == $strSecond) {
                        $strQuery = "SELECT * FROM {$strFirst} WHERE id=?";
                    } else {
                        $strQuery = "SELECT * FROM {$strFirst} WHERE id=(SELECT pid FROM {$strSecond} WHERE id=?)";
                    }
                } else {
                    if ($strTable == $strSecond) {
                        $strQuery = "SELECT * FROM {$strFirst} WHERE id=(SELECT pid FROM {$strSecond} WHERE id=?)";
                    } else {
                        $strQuery = "SELECT * FROM {$strFirst} WHERE id=(SELECT pid FROM {$strSecond} WHERE id=(SELECT pid FROM {$strTable} WHERE id=?))";
                    }
                }
                // Add the first level name
                $objRow = $this->Database->prepare($strQuery)->limit(1)->execute($dc->id);
                if ($objRow->title != '') {
                    $this->Template->headline .= ' » ' . $objRow->title;
                } elseif ($objRow->name != '') {
                    $this->Template->headline .= ' » ' . $objRow->name;
                }
                if (isset($GLOBALS['TL_LANG']['MOD'][$strSecond])) {
                    $this->Template->headline .= ' » ' . $GLOBALS['TL_LANG']['MOD'][$strSecond];
                }
                // Add the second level name
                $objRow = $this->Database->prepare("SELECT * FROM {$strSecond} WHERE id=?")->limit(1)->execute(CURRENT_ID);
                if ($objRow->title != '') {
                    $this->Template->headline .= ' » ' . $objRow->title;
                } elseif ($objRow->name != '') {
                    $this->Template->headline .= ' » ' . $objRow->name;
                }
            } else {
                // Add the name of the parent element
                if ($strTable && in_array($strTable, $arrTables) && $strTable != $arrTables[0]) {
                    if ($GLOBALS['TL_DCA'][$strTable]['config']['ptable'] != '') {
                        $objRow = $this->Database->prepare("SELECT * FROM " . $GLOBALS['TL_DCA'][$strTable]['config']['ptable'] . " WHERE id=?")->limit(1)->execute(CURRENT_ID);
                        if ($objRow->title != '') {
                            $this->Template->headline .= ' » ' . $objRow->title;
                        } elseif ($objRow->name != '') {
                            $this->Template->headline .= ' » ' . $objRow->name;
                        }
                    }
                }
                // Add the name of the submodule
                if ($strTable && isset($GLOBALS['TL_LANG']['MOD'][$strTable])) {
                    $this->Template->headline .= ' » ' . $GLOBALS['TL_LANG']['MOD'][$strTable];
                }
            }
            // Add the current action
            if (\Input::get('act') == 'editAll') {
                $this->Template->headline .= ' » ' . $GLOBALS['TL_LANG']['MSC']['all'][0];
            } elseif (\Input::get('act') == 'overrideAll') {
                $this->Template->headline .= ' » ' . $GLOBALS['TL_LANG']['MSC']['all_override'][0];
            } else {
                if (\Input::get('id')) {
                    if (\Input::get('do') == 'files' || \Input::get('do') == 'tpl_editor') {
                        // Handle new folders (see #7980)
                        if (strpos(\Input::get('id'), '__new__') !== false) {
                            $this->Template->headline .= ' » ' . dirname(\Input::get('id')) . ' » ' . $GLOBALS['TL_LANG'][$strTable]['new'][1];
                        } else {
                            $this->Template->headline .= ' » ' . \Input::get('id');
                        }
                    } elseif (is_array($GLOBALS['TL_LANG'][$strTable][$act])) {
                        $this->Template->headline .= ' » ' . sprintf($GLOBALS['TL_LANG'][$strTable][$act][1], \Input::get('id'));
                    }
                } elseif (\Input::get('pid')) {
                    if (\Input::get('do') == 'files' || \Input::get('do') == 'tpl_editor') {
                        $this->Template->headline .= ' » ' . \Input::get('pid');
                    } elseif (is_array($GLOBALS['TL_LANG'][$strTable][$act])) {
                        $this->Template->headline .= ' » ' . sprintf($GLOBALS['TL_LANG'][$strTable][$act][1], \Input::get('pid'));
                    }
                }
            }
            return $dc->{$act}();
        }
        return null;
    }