vendor/alnv/catalog-manager/library/alnv/CatalogManagerInitializer.php line 46

Open in your IDE?
  1. <?php
  2. namespace CatalogManager;
  3. use Contao\CoreBundle\Controller\BackendCsvImportController;
  4. class CatalogManagerInitializer
  5. {
  6.     protected $objIconGetter;
  7.     protected $arrCatalogs = [];
  8.     protected $arrCoreTables = [];
  9.     protected $arrBackendModules = [];
  10.     protected $objI18nCatalogTranslator;
  11.     protected $arrActiveBackendModules = [];
  12.     public function initialize()
  13.     {
  14.         $blnInitializeInBackend TL_MODE == 'BE';
  15.         if (version_compare('4.4'VERSION'<=')) {
  16.             $objRequest \System::getContainer()->get('request_stack')->getCurrentRequest();
  17.             if ($objRequest && $objRequest->get('_route') == 'contao_install') {
  18.                 $blnInitializeInBackend false;
  19.             }
  20.         }
  21.         if ($blnInitializeInBackend) {
  22.             \Database::getInstance();
  23.             $this->objIconGetter = new IconGetter();
  24.             $this->objIconGetter->createCatalogManagerDirectories();
  25.             $this->objI18nCatalogTranslator = new I18nCatalogTranslator();
  26.             $this->objI18nCatalogTranslator->initialize();
  27.             $this->setCatalogs();
  28.             $this->setNavigation();
  29.             $this->modifyCoreModules();
  30.             $this->setBackendModules();
  31.             $this->initializeDataContainerArrays();
  32.             \BackendUser::getInstance();
  33.             if (\Config::get('catalogLicence')) {
  34.                 unset($GLOBALS['BE_MOD']['catalog-manager-extensions']['support']);
  35.             }
  36.         }
  37.         if (TL_MODE == 'FE') {
  38.             \FrontendUser::getInstance();
  39.             \Database::getInstance();
  40.             $this->setCatalogs();
  41.         }
  42.         if (\Config::get('_isBlocked')) {
  43.             $strMessage 'Our system detected an unlicensed catalog manager installation on the domain "' \Environment::get('base') . '". Please enter your valid license or contact your webmaster.';
  44.             \Message::addError($strMessage);
  45.             \System::log($strMessage'CATALOG MANAGER VERIFICATION'TL_ERROR);
  46.         }
  47.     }
  48.     protected function setCatalogs()
  49.     {
  50.         $objDatabase \Database::getInstance();
  51.         if (!$objDatabase->tableExists('tl_catalog')) return null;
  52.         $objCatalog $objDatabase->prepare('SELECT * FROM tl_catalog ORDER BY `pTable` DESC, `tablename` ASC')->execute();
  53.         if ($objCatalog->numRows) {
  54.             while ($objCatalog->next()) {
  55.                 if (!$objCatalog->tablename) {
  56.                     continue;
  57.                 }
  58.                 $arrCatalog Toolkit::parseCatalog($objCatalog->row());
  59.                 $strType Toolkit::isCoreTable($objCatalog->tablename) ? 'arrCoreTables' 'arrCatalogs';
  60.                 $this->{$strType}[$objCatalog->tablename] = $arrCatalog;
  61.                 $GLOBALS['TL_CATALOG_MANAGER']['CATALOG_EXTENSIONS'][$objCatalog->tablename] = $arrCatalog;
  62.             }
  63.         }
  64.         $GLOBALS['TL_CATALOG_MANAGER']['CORE_TABLES'] = array_keys($this->arrCoreTables);
  65.     }
  66.     protected function setNavigation()
  67.     {
  68.         $arrNavigationAreas \Config::get('catalogNavigationAreas');
  69.         $arrNavigationAreas \StringUtil::deserialize($arrNavigationAreastrue);
  70.         if (!empty($arrNavigationAreas) && is_array($arrNavigationAreas)) {
  71.             foreach ($arrNavigationAreas as $intIndex => $arrNavigationArea) {
  72.                 if (!Toolkit::isEmpty($arrNavigationArea['key'])) {
  73.                     $arrNav = [];
  74.                     $arrNav[$arrNavigationArea['key']] = [];
  75.                     array_insert($GLOBALS['BE_MOD'], $intIndex$arrNav);
  76.                     $GLOBALS['TL_LANG']['MOD'][$arrNavigationArea['key']] = $this->objI18nCatalogTranslator->get('nav'$arrNavigationArea['key'], ['title' => $arrNavigationArea['value']]);
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     protected function setBackendModules()
  82.     {
  83.         foreach ($this->arrCatalogs as $strTablename => $arrCatalog) {
  84.             if ($arrCatalog['isBackendModule'] && Toolkit::isEmpty($arrCatalog['pTable'])) {
  85.                 $arrBackendModule = [];
  86.                 $intIndex = (int)$arrCatalog['navPosition'];
  87.                 $strArea $arrCatalog['navArea'] ?: 'system';
  88.                 $strModulename $arrCatalog['modulename'] ?: $strTablename;
  89.                 $arrBackendModule[$strModulename] = $this->createBackendModuleDc($strTablename$arrCatalog);
  90.                 $this->arrActiveBackendModules[] = $strModulename;
  91.                 if (is_array($arrBackendModule[$strModulename]) && isset($arrBackendModule[$strModulename]['tables']) && is_array($arrBackendModule[$strModulename]['tables'])) {
  92.                     if (in_array('tl_content'$arrBackendModule[$strModulename]['tables'])) {
  93.                         $arrBackendModule[$strModulename]['table'] = [BackendCsvImportController::class, 'importTableWizardAction'];
  94.                         $arrBackendModule[$strModulename]['list'] = [BackendCsvImportController::class, 'importListWizardAction'];
  95.                     }
  96.                 }
  97.                 array_insert($GLOBALS['BE_MOD'][$strArea], $intIndex$arrBackendModule);
  98.                 $this->arrBackendModules[$strModulename] = $arrBackendModule[$strModulename];
  99.                 $GLOBALS['TL_LANG']['MOD'][$strModulename] = $this->objI18nCatalogTranslator->get('module'$strTablename);
  100.             }
  101.         }
  102.     }
  103.     protected function modifyCoreModules()
  104.     {
  105.         $strActiveModule \Input::get('do');
  106.         $arrCoreTables array_keys($this->arrCoreTables);
  107.         if (Toolkit::isEmpty($strActiveModule) || empty($arrCoreTables)) return null;
  108.         foreach ($GLOBALS['BE_MOD'] as $strArea => $arrModules) {
  109.             if (isset($arrModules[$strActiveModule]) && is_array($arrModules[$strActiveModule])) {
  110.                 $arrBackendModule = [];
  111.                 $arrModule $arrModules[$strActiveModule];
  112.                 $this->arrActiveBackendModules[] = $strActiveModule;
  113.                 $arrModule['tables'] = isset($arrModule['tables']) && is_array($arrModule['tables']) ? $arrModule['tables'] : [];
  114.                 foreach ($arrCoreTables as $strTablename) {
  115.                     if (in_array($strTablename$arrModule['tables'])) {
  116.                         $arrBackendModule $this->createBackendModuleDc($strTablename$this->arrCoreTables[$strTablename]);
  117.                     }
  118.                 }
  119.                 if (!empty($arrBackendModule) && is_array($arrBackendModule)) {
  120.                     $arrTables $arrBackendModule['tables'];
  121.                     foreach ($arrTables as $strTable) {
  122.                         if (!in_array($strTable$arrModule['tables'])) {
  123.                             $arrModule['tables'][] = $strTable;
  124.                         }
  125.                     }
  126.                     $this->arrBackendModules[$strActiveModule] = $arrModule;
  127.                     $GLOBALS['BE_MOD'][$strArea][$strActiveModule] = $arrModule;
  128.                 }
  129.             }
  130.         }
  131.     }
  132.     protected function initializeDataContainerArrays()
  133.     {
  134.         $strActiveModule $this->getActiveModule();
  135.         if (in_array($strActiveModule$this->arrActiveBackendModules)) {
  136.             $arrModule $this->arrBackendModules[$strActiveModule] ?? [];
  137.             if (isset($arrModule['tables']) && is_array($arrModule['tables'])) {
  138.                 foreach ($arrModule['tables'] as $strTablename) {
  139.                     if (Toolkit::isCoreTable($strTablename)) continue;
  140.                     $this->initializeDcByTablename($strTablename, [], true);
  141.                 }
  142.                 return null;
  143.             }
  144.         }
  145.         foreach ($this->arrCatalogs as $strTablename => $arrCatalog) {
  146.             $this->initializeDcByTablename($strTablename$arrCatalogfalse);
  147.         }
  148.     }
  149.     protected function initializeDcByTablename($strTablename$arrCatalog = [], $blnActive true)
  150.     {
  151.         if (empty($arrCatalog)) $arrCatalog $GLOBALS['TL_CATALOG_MANAGER']['CATALOG_EXTENSIONS'][$strTablename];
  152.         if ($arrCatalog === null) return null;
  153.         $objDcBuilder = new DcBuilder($arrCatalog$blnActive);
  154.         $objDcBuilder->createDataContainerArray();
  155.         if (!Toolkit::isEmpty($arrCatalog['permissionType'])) {
  156.             $GLOBALS['TL_PERMISSIONS'][] = $strTablename 'p';
  157.             if ($arrCatalog['permissionType'] == 'extended') {
  158.                 $GLOBALS['TL_PERMISSIONS'][] = $strTablename;
  159.             }
  160.             $GLOBALS['TL_CATALOG_MANAGER']['PROTECTED_CATALOGS'][] = [
  161.                 'type' => $arrCatalog['permissionType'],
  162.                 'tablename' => $strTablename
  163.             ];
  164.         }
  165.     }
  166.     protected function createBackendModuleDc($strTablename$arrCatalog)
  167.     {
  168.         $arrModule = [];
  169.         $arrTables[] = $strTablename;
  170.         $blnAddContentElements $arrCatalog['addContentElements'] ? true false;
  171.         foreach ($arrCatalog['cTables'] as $strTable) {
  172.             $arrTables[] = $strTable;
  173.         }
  174.         if (!empty($arrCatalog['cTables']) && is_array($arrCatalog['cTables'])) {
  175.             $this->getNestedChildTables($arrTables$arrCatalog['cTables'], '');
  176.         }
  177.         if ($blnAddContentElements || $this->existContentElementInChildrenCatalogs($arrCatalog['cTables'])) {
  178.             $arrTables[] = 'tl_content';
  179.         }
  180.         $arrModule['stylesheet'] = 'system/modules/catalog-manager/assets/catalog.css';
  181.         $arrModule['icon'] = $this->objIconGetter->setCatalogIcon($strTablename);
  182.         $arrModule['name'] = $arrCatalog['name'];
  183.         $arrModule['tables'] = $arrTables;
  184.         if (isset($GLOBALS['TL_HOOKS']['catalogManagerModifyBackendModule']) && is_array($GLOBALS['TL_HOOKS']['catalogManagerModifyBackendModule'])) {
  185.             foreach ($GLOBALS['TL_HOOKS']['catalogManagerModifyBackendModule'] as $arrCallback) {
  186.                 $objHook = new $arrCallback[0]();
  187.                 $objHook->{$arrCallback[1]}($arrModule$arrCatalog);
  188.             }
  189.         }
  190.         return $arrModule;
  191.     }
  192.     protected function getActiveModule()
  193.     {
  194.         $strActiveModule \Input::get('do');
  195.         if (!Toolkit::isEmpty(\Input::get('target')) && $strActiveModule == 'files') {
  196.             $arrTarget explode('.'\Input::get('target'));
  197.             if (!empty($arrTarget) && is_array($arrTarget)) {
  198.                 $strActiveModule = !Toolkit::isEmpty($arrTarget[0]) ? $arrTarget[0] : $strActiveModule;
  199.             }
  200.         }
  201.         return $strActiveModule;
  202.     }
  203.     protected function existContentElementInChildrenCatalogs($arrTables)
  204.     {
  205.         if (!empty($arrTables) && is_array($arrTables)) {
  206.             foreach ($arrTables as $strTable) {
  207.                 $arrChildTables $GLOBALS['TL_CATALOG_MANAGER']['CATALOG_EXTENSIONS'][$strTable]['cTables'] ?? '';
  208.                 if ($GLOBALS['TL_CATALOG_MANAGER']['CATALOG_EXTENSIONS'][$strTable]['addContentElements'] ?? '') {
  209.                     return true;
  210.                 }
  211.                 if (!empty($arrChildTables) && is_array($arrChildTables)) {
  212.                     return $this->existContentElementInChildrenCatalogs($arrChildTables);
  213.                 }
  214.             }
  215.         }
  216.         return false;
  217.     }
  218.     protected function getNestedChildTables(&$arrTables$arrChildTables$strTable '')
  219.     {
  220.         if ($strTable) {
  221.             $arrTables[] = $strTable;
  222.         }
  223.         if (!empty($arrChildTables) && is_array($arrChildTables)) {
  224.             foreach ($arrChildTables as $strChildTable) {
  225.                 $arrNestedChildTables $GLOBALS['TL_CATALOG_MANAGER']['CATALOG_EXTENSIONS'][$strChildTable]['cTables'] ?? '';
  226.                 if (!empty($arrNestedChildTables) && is_array($arrNestedChildTables)) {
  227.                     foreach ($arrNestedChildTables as $strNestedChildTable) {
  228.                         $this->getNestedChildTables($arrTables$arrNestedChildTables$strNestedChildTable);
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.     }
  234. }