vendor/contao/core-bundle/src/Resources/contao/library/Contao/DcaLoader.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. /**
  11.  * Loads a set of DCA files
  12.  *
  13.  * The class loads the DCA files of a certain table and stores a combined
  14.  * version in the cache directory.
  15.  *
  16.  * Usage:
  17.  *
  18.  *     $dca = new DcaLoader('tl_user');
  19.  *     $dca->load();
  20.  *
  21.  * @author Leo Feyer <https://github.com/leofeyer>
  22.  */
  23. class DcaLoader extends Controller
  24. {
  25.     /**
  26.      * @var array
  27.      */
  28.     protected static $arrLoaded = array();
  29.     /**
  30.      * Table name
  31.      * @var string
  32.      */
  33.     protected $strTable;
  34.     /**
  35.      * Store the table name
  36.      *
  37.      * @param string $strTable The table name
  38.      *
  39.      * @throws \Exception If $strTable is empty
  40.      */
  41.     public function __construct($strTable)
  42.     {
  43.         if (!$strTable)
  44.         {
  45.             throw new \Exception('The table name must not be empty');
  46.         }
  47.         if (Validator::isInsecurePath($strTable))
  48.         {
  49.             throw new \InvalidArgumentException('The table name contains invalid characters');
  50.         }
  51.         parent::__construct();
  52.         $this->strTable $strTable;
  53.     }
  54.     /**
  55.      * Load a set of DCA files
  56.      *
  57.      * @param boolean $blnNoCache If true, the cache will be bypassed
  58.      */
  59.     public function load($blnNoCache=false)
  60.     {
  61.         $this->loadDcaFiles($blnNoCache);
  62.     }
  63.     /**
  64.      * Load the DCA files
  65.      *
  66.      * @param boolean $blnNoCache
  67.      */
  68.     private function loadDcaFiles($blnNoCache)
  69.     {
  70.         // Return if the data has been loaded already
  71.         if (!$blnNoCache && isset(static::$arrLoaded['dcaFiles'][$this->strTable]))
  72.         {
  73.             return;
  74.         }
  75.         static::$arrLoaded['dcaFiles'][$this->strTable] = true// see #6145
  76.         $strCacheDir System::getContainer()->getParameter('kernel.cache_dir');
  77.         // Try to load from cache
  78.         if (file_exists($strCacheDir '/contao/dca/' $this->strTable '.php'))
  79.         {
  80.             include $strCacheDir '/contao/dca/' $this->strTable '.php';
  81.         }
  82.         else
  83.         {
  84.             try
  85.             {
  86.                 $files System::getContainer()->get('contao.resource_locator')->locate('dca/' $this->strTable '.php'nullfalse);
  87.             }
  88.             catch (\InvalidArgumentException $e)
  89.             {
  90.                 $files = array();
  91.             }
  92.             foreach ($files as $file)
  93.             {
  94.                 include $file;
  95.             }
  96.         }
  97.         // Set the ptable dynamically
  98.         $this->setDynamicPTable();
  99.         // HOOK: allow to load custom settings
  100.         if (isset($GLOBALS['TL_HOOKS']['loadDataContainer']) && \is_array($GLOBALS['TL_HOOKS']['loadDataContainer']))
  101.         {
  102.             foreach ($GLOBALS['TL_HOOKS']['loadDataContainer'] as $callback)
  103.             {
  104.                 $this->import($callback[0]);
  105.                 $this->{$callback[0]}->{$callback[1]}($this->strTable);
  106.             }
  107.         }
  108.         $projectDir System::getContainer()->getParameter('kernel.project_dir');
  109.         // Local configuration file
  110.         if (file_exists($projectDir '/system/config/dcaconfig.php'))
  111.         {
  112.             trigger_deprecation('contao/core-bundle''4.3''Using the "dcaconfig.php" file has been deprecated and will no longer work in Contao 5.0. Create custom DCA files in the "contao/dca" folder instead.');
  113.             include $projectDir '/system/config/dcaconfig.php';
  114.         }
  115.         $this->addDefaultLabels($blnNoCache);
  116.     }
  117.     /**
  118.      * Add the default labels (see #509)
  119.      *
  120.      * @param boolean $blnNoCache
  121.      */
  122.     private function addDefaultLabels($blnNoCache)
  123.     {
  124.         // Operations
  125.         foreach (array('global_operations''operations') as $key)
  126.         {
  127.             if (!isset($GLOBALS['TL_DCA'][$this->strTable]['list'][$key]))
  128.             {
  129.                 continue;
  130.             }
  131.             foreach ($GLOBALS['TL_DCA'][$this->strTable]['list'][$key] as $k=>&$v)
  132.             {
  133.                 if (isset($v['label']))
  134.                 {
  135.                     continue;
  136.                 }
  137.                 if (isset($GLOBALS['TL_LANG'][$this->strTable][$k]) || !isset($GLOBALS['TL_LANG']['DCA'][$k]))
  138.                 {
  139.                     $v['label'] = &$GLOBALS['TL_LANG'][$this->strTable][$k];
  140.                 }
  141.                 elseif (isset($GLOBALS['TL_LANG']['DCA'][$k]))
  142.                 {
  143.                     $v['label'] = &$GLOBALS['TL_LANG']['DCA'][$k];
  144.                 }
  145.             }
  146.             unset($v);
  147.         }
  148.         // Fields
  149.         if (isset($GLOBALS['TL_DCA'][$this->strTable]['fields']))
  150.         {
  151.             foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $k=>&$v)
  152.             {
  153.                 if (isset($v['label']))
  154.                 {
  155.                     continue;
  156.                 }
  157.                 $v['label'] = &$GLOBALS['TL_LANG'][$this->strTable][$k];
  158.             }
  159.             unset($v);
  160.         }
  161.     }
  162.     /**
  163.      * Sets the parent table for the current table, if enabled and not set.
  164.      */
  165.     private function setDynamicPTable(): void
  166.     {
  167.         if (!($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable'] ?? null) || !isset($GLOBALS['BE_MOD']))
  168.         {
  169.             return;
  170.         }
  171.         if (!$do Input::get('do'))
  172.         {
  173.             return;
  174.         }
  175.         foreach (array_merge(...array_values($GLOBALS['BE_MOD'])) as $key => $module)
  176.         {
  177.             if ($do !== $key || !isset($module['tables']) || !\is_array($module['tables']))
  178.             {
  179.                 continue;
  180.             }
  181.             foreach ($module['tables'] as $table)
  182.             {
  183.                 Controller::loadDataContainer($table);
  184.                 $ctable $GLOBALS['TL_DCA'][$table]['config']['ctable'] ?? array();
  185.                 if (\in_array($this->strTable$ctabletrue))
  186.                 {
  187.                     $GLOBALS['TL_DCA'][$this->strTable]['config']['ptable'] = $table;
  188.                     return;
  189.                 }
  190.             }
  191.         }
  192.     }
  193. }
  194. class_alias(DcaLoader::class, 'DcaLoader');