vendor/metamodels/core/src/ViewCombination/ViewCombination.php line 87

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of MetaModels/core.
  4.  *
  5.  * (c) 2012-2019 The MetaModels team.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  *
  10.  * This project is provided in good faith and hope to be usable by anyone.
  11.  *
  12.  * @package    MetaModels/core
  13.  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  14.  * @author     Sven Baumann <baumann.sv@gmail.com>
  15.  * @author     Richard Henkenjohann <richardhenkenjohann@googlemail.com>
  16.  * @copyright  2012-2019 The MetaModels team.
  17.  * @license    https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
  18.  * @filesource
  19.  */
  20. namespace MetaModels\ViewCombination;
  21. use Contao\BackendUser;
  22. use Contao\FrontendUser;
  23. use Doctrine\Common\Cache\Cache;
  24. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  25. /**
  26.  * This class serves as central information endpoint for view combinations.
  27.  */
  28. class ViewCombination
  29. {
  30.     /**
  31.      * The cache.
  32.      *
  33.      * @var Cache
  34.      */
  35.     private $cache;
  36.     /**
  37.      * The token storage.
  38.      *
  39.      * @var TokenStorageInterface
  40.      */
  41.     private $tokenStorage;
  42.     /**
  43.      * The combination builder.
  44.      *
  45.      * @var ViewCombinationBuilder
  46.      */
  47.     private $builder;
  48.     /**
  49.      * The input screen information builder.
  50.      *
  51.      * @var InputScreenInformationBuilder
  52.      */
  53.     private $inputScreens;
  54.     /**
  55.      * Create a new instance.
  56.      *
  57.      * @param Cache                         $cache        The cache.
  58.      * @param TokenStorageInterface         $tokenStorage The token storage.
  59.      * @param ViewCombinationBuilder        $builder      The combination builder.
  60.      * @param InputScreenInformationBuilder $inputScreens The input screen information builder.
  61.      */
  62.     public function __construct(
  63.         Cache $cache,
  64.         TokenStorageInterface $tokenStorage,
  65.         ViewCombinationBuilder $builder,
  66.         InputScreenInformationBuilder $inputScreens
  67.     ) {
  68.         $this->cache        $cache;
  69.         $this->tokenStorage $tokenStorage;
  70.         $this->builder      $builder;
  71.         $this->inputScreens $inputScreens;
  72.     }
  73.     /**
  74.      * Obtain the combinations for the current user.
  75.      *
  76.      * @return array|null
  77.      */
  78.     public function getCombinations()
  79.     {
  80.         $user $this->getUser();
  81.         switch (true) {
  82.             case ($user instanceof BackendUser):
  83.                 $mode 'be';
  84.                 // Try to get the group(s)
  85.                 // there might be a NULL in there as BE admins have no groups and
  86.                 // user might have one but it is not mandatory.
  87.                 // I would prefer a default group for both, fe and be groups.
  88.                 $groups $user->groups;
  89.                 // Special case in combinations, admins have the implicit group id -1.
  90.                 if ($user->admin) {
  91.                     $groups[] = -1;
  92.                 }
  93.                 break;
  94.             case ($user instanceof FrontendUser):
  95.                 $mode   'fe';
  96.                 $groups $user->groups;
  97.                 // Special case in combinations, anonymous frontend users have the implicit group id -1.
  98.                 if (!$this->getUser()->id) {
  99.                     $groups = [-1];
  100.                 }
  101.                 break;
  102.             default:
  103.                 // Default handled as frontend anonymous.
  104.                 $mode   'fe';
  105.                 $groups = [-1];
  106.         }
  107.         $groups array_filter($groups);
  108.         if ($this->cache->contains($cacheKey 'combinations_' $mode '_' implode(','$groups))) {
  109.             return $this->cache->fetch($cacheKey);
  110.         }
  111.         $combinations $this->builder->getCombinationsForUser($groups$mode);
  112.         $this->cache->save($cacheKey$combinations);
  113.         return $combinations;
  114.     }
  115.     /**
  116.      * Retrieve a combination for a table.
  117.      *
  118.      * @param string $tableName The table name.
  119.      *
  120.      * @return array|null
  121.      */
  122.     public function getCombination($tableName)
  123.     {
  124.         $combinations $this->getCombinations();
  125.         if (isset($combinations['byName'][$tableName])) {
  126.             return $combinations['byName'][$tableName];
  127.         }
  128.         return null;
  129.     }
  130.     /**
  131.      * Obtain stand alone input screens.
  132.      *
  133.      * @return array
  134.      */
  135.     public function getStandalone()
  136.     {
  137.         $inputScreens array_filter($this->getInputScreens(), function ($inputScreen) {
  138.             return $inputScreen['meta']['rendertype'] === 'standalone';
  139.         });
  140.         return $inputScreens;
  141.     }
  142.     /**
  143.      * Obtain parented input screens.
  144.      *
  145.      * @return array
  146.      */
  147.     public function getParented()
  148.     {
  149.         $inputScreens array_filter($this->getInputScreens(), function ($inputScreen) {
  150.             return $inputScreen['meta']['rendertype'] === 'ctable';
  151.         });
  152.         return $inputScreens;
  153.     }
  154.     /**
  155.      * Obtain child input screens of the passed parent.
  156.      *
  157.      * @param string $parentTable The parent table to assemble the children of.
  158.      *
  159.      * @return array
  160.      */
  161.     public function getChildrenOf($parentTable)
  162.     {
  163.         $inputScreens array_filter($this->getInputScreens(), function ($inputScreen) use ($parentTable) {
  164.             return ($inputScreen['meta']['rendertype'] === 'ctable')
  165.                    && ($inputScreen['meta']['ptable'] === $parentTable);
  166.         });
  167.         return $inputScreens;
  168.     }
  169.     /**
  170.      * Obtain parented input screens.
  171.      *
  172.      * @param string $tableName The table name.
  173.      *
  174.      * @return array|null
  175.      */
  176.     public function getScreen($tableName)
  177.     {
  178.         $inputScreens $this->getInputScreens();
  179.         if (isset($inputScreens[$tableName])) {
  180.             return $inputScreens[$tableName];
  181.         }
  182.         return null;
  183.     }
  184.     /**
  185.      * Retrieve the input screens.
  186.      *
  187.      * @return array
  188.      */
  189.     private function getInputScreens()
  190.     {
  191.         $combinations $this->getCombinations();
  192.         if (null === $combinations) {
  193.             return [];
  194.         }
  195.         $screenIds array_map(function ($combination) {
  196.             return $combination['dca_id'];
  197.         }, $combinations['byName']);
  198.         if ($this->cache->contains($cacheKey 'screens_' implode(','$screenIds))) {
  199.             return $this->cache->fetch($cacheKey);
  200.         }
  201.         $screens $this->inputScreens->fetchInputScreens($screenIds);
  202.         $this->cache->save($cacheKey$screens);
  203.         return $screens;
  204.     }
  205.     /**
  206.      * The user.
  207.      *
  208.      * @return mixed
  209.      */
  210.     private function getUser()
  211.     {
  212.         if (null === $token $this->tokenStorage->getToken()) {
  213.             return null;
  214.         }
  215.         return $token->getUser();
  216.     }
  217. }