vendor/metamodels/core/src/ViewCombination/ViewCombinationBuilder.php line 74

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.  * @copyright  2012-2019 The MetaModels team.
  16.  * @license    https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
  17.  * @filesource
  18.  */
  19. namespace MetaModels\ViewCombination;
  20. use Doctrine\DBAL\Connection;
  21. use MetaModels\IFactory;
  22. /**
  23.  * This builds the view combinations for an user.
  24.  */
  25. class ViewCombinationBuilder
  26. {
  27.     /**
  28.      * The database connection.
  29.      *
  30.      * @var Connection
  31.      */
  32.     private $connection;
  33.     /**
  34.      * The MetaModels factory.
  35.      *
  36.      * @var IFactory
  37.      */
  38.     private $factory;
  39.     /**
  40.      * Create a new instance.
  41.      *
  42.      * @param Connection $connection The database connection.
  43.      * @param IFactory   $factory    The MetaModels factory.
  44.      */
  45.     public function __construct(Connection $connectionIFactory $factory)
  46.     {
  47.         $this->connection $connection;
  48.         $this->factory    $factory;
  49.     }
  50.     /**
  51.      * Retrieve the combinations for the passed user.
  52.      *
  53.      * @param string[] $userGroups The user groups.
  54.      * @param string   $userType   The user type ('fe' or 'be').
  55.      *
  56.      * @return array|null
  57.      *
  58.      * @throws \InvalidArgumentException When the user type is unknown.
  59.      */
  60.     public function getCombinationsForUser($userGroups$userType)
  61.     {
  62.         $userType strtolower($userType);
  63.         if ('fe' !== $userType && 'be' !== $userType) {
  64.             throw new \InvalidArgumentException('Unknown user type: ' $userType);
  65.         }
  66.         return $this->getCombinationsFromDatabase($userGroups$userType);
  67.     }
  68.     /**
  69.      * Retrieve the palette combinations from the database.
  70.      *
  71.      * @param string $userGroups The user groups of the user to fetch information for.
  72.      * @param string $userType   The user type.
  73.      *
  74.      * @return null|array
  75.      */
  76.     private function getCombinationsFromDatabase($userGroups$userType)
  77.     {
  78.         if (empty($userGroups)) {
  79.             return null;
  80.         }
  81.         $builder $this
  82.             ->connection
  83.             ->createQueryBuilder();
  84.         $combinations $builder
  85.             ->select('*')
  86.             ->from('tl_metamodel_dca_combine')
  87.             ->where($builder->expr()->in($userType '_group'':groupList'))
  88.             ->setParameter('groupList'$userGroupsConnection::PARAM_STR_ARRAY)
  89.             ->orWhere($userType '_group=0')
  90.             ->orderBy('pid')
  91.             ->addOrderBy('sorting')
  92.             ->execute()
  93.             ->fetchAll(\PDO::FETCH_ASSOC);
  94.         $result = [
  95.             'byName' => [],
  96.             'byId' => []
  97.         ];
  98.         foreach ($combinations as $combination) {
  99.             $metaModelId $combination['pid'];
  100.             if (isset($result['byId'][$metaModelId])) {
  101.                 continue;
  102.             }
  103.             $name $this->factory->translateIdToMetaModelName($metaModelId);
  104.             $result['byId'][$metaModelId] = $result['byName'][$name] = [
  105.                 'dca_id'   => $combination['dca_id'],
  106.                 'view_id' => $combination['view_id']
  107.             ];
  108.         }
  109.         return $result;
  110.     }
  111. }