src/Cms/ModuleBundle/Model/AbstractModuleListWidgetProcessor.php line 96

Open in your IDE?
  1. <?php
  2. namespace Cms\ModuleBundle\Model;
  3. use App\Entity\Shared\UlidIdentifiableInterface;
  4. use App\Entity\Shared\UlidTransitionalInterface;
  5. use Cms\ContainerBundle\Entity\Container;
  6. use Cms\CoreBundle\Service\StringUtils\LoremIpsumGenerator;
  7. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  8. use Cms\FrontendBundle\Model\FrontendGlobals;
  9. use Cms\ModuleBundle\Doctrine\ProxyRepository;
  10. use Cms\ModuleBundle\Entity\ModuleSettings;
  11. use Cms\ModuleBundle\Entity\Proxy;
  12. use Cms\TagBundle\Model\Taggable\TaggableModuleRepositoryInterface;
  13. use Cms\WidgetBundle\Model\AbstractWidgetProcessor;
  14. use Cms\WidgetBundle\Model\ModuleLimitedListWidgetProcessorInterface;
  15. use Cms\WidgetBundle\Model\WidgetObject;
  16. use Doctrine\ORM\EntityRepository;
  17. use Doctrine\ORM\Query\Expr;
  18. use Doctrine\ORM\QueryBuilder;
  19. use ReflectionClass;
  20. use Symfony\Component\DependencyInjection\ContainerInterface;
  21. use Ulid\Ulid;
  22. /**
  23.  * Class AbstractModuleListWidgetProcessor
  24.  * @package Cms\ModuleBundle\Model
  25.  */
  26. abstract class AbstractModuleListWidgetProcessor extends AbstractWidgetProcessor
  27. {
  28.     /**
  29.      * @var LoremIpsumGenerator
  30.      */
  31.     protected $lipsum null;
  32.     /**
  33.      * @var EntityManager
  34.      */
  35.     protected $em null;
  36.     /**
  37.      * @return Expr
  38.      */
  39.     protected function expr()
  40.     {
  41.         return $this->em->getExpressionBuilder();
  42.     }
  43.     /**
  44.      * @param string $class
  45.      * @param callable $callback
  46.      * @param int $count
  47.      * @return array|Proxy[]
  48.      */
  49.     protected function samples($class, callable $callback$count null)
  50.     {
  51.         if (empty($count) || intval($count) <= || intval($count) > 24) {
  52.             $count 9;
  53.         }
  54.         $refl = new ReflectionClass($class);
  55.         $samples = [];
  56.         for ($i 0$i $count$i++) {
  57.             $samples[] = $sample $callback(new $class(), $i);
  58.             switch (true) {
  59.                 case $sample instanceof UlidIdentifiableInterface:
  60.                     $sample->setId(Ulid::generate(true));
  61.                     break;
  62.                 case $sample instanceof UlidTransitionalInterface:
  63.                     $sample->setUlid(Ulid::generate(true));
  64.                     break;
  65.                 default:
  66.                     $prop $refl->getProperty('id');
  67.                     $prop->setAccessible(true);
  68.                     $prop->setValue($sample$i);
  69.             }
  70.         }
  71.         return $samples;
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      */
  76.     public function setup(ContainerInterface $container)
  77.     {
  78.         parent::setup($container);
  79.         $this->lipsum $container->get(LoremIpsumGenerator::class);
  80.         $this->em $container->get(EntityManager::class);
  81.     }
  82.     /**
  83.      * @param string $class
  84.      * @param QueryBuilder $qb
  85.      * @param WidgetObject $object
  86.      * @return array|Proxy[]
  87.      */
  88.     protected function query($classQueryBuilder $qbWidgetObject $object)
  89.     {
  90.         if ( ! $class instanceof EntityRepository) {
  91.             $repo $this->repo($class);
  92.         } else {
  93.             $repo $class;
  94.         }
  95.         $limit 9;
  96.         if ($this instanceof ModuleLimitedListWidgetProcessorInterface) {
  97.             $limit $this->getLimit($object);
  98.         }
  99.         return $repo->queryMany($qb$limit);
  100.     }
  101.     /**
  102.      * @param WidgetObject $object
  103.      * @param FrontendGlobals $globals
  104.      * @return array|Container[]
  105.      */
  106.     protected function determineContainers(WidgetObject $objectFrontendGlobals $globals)
  107.     {
  108.         if ($object->getId() === null || ($globals->getContainer() !== null && intval($object->getId()) === $globals->getContainer()->getId())) {
  109.             $container $globals->getContainer();
  110.         } else {
  111.             switch ($object->getId()) {
  112.                 case 'dynamic':
  113.                     $container $globals->getContainer();
  114.                     break;
  115.                 default:
  116.                     $container $this->em->getRepository(Container::class)->findExact(
  117.                         intval($object->getId())
  118.                     );
  119.             }
  120.         }
  121.         if ($container === $globals->getContainer()) {
  122.             return $globals->getContainers();
  123.         } else {
  124.             $ancestors $this->em->getRepository(Container::class)->findAncestors($container);
  125.             $ancestors[] = $container;
  126.             return array_reverse($ancestors);
  127.         }
  128.     }
  129.     /**
  130.      * @param string $class
  131.      * @return ProxyRepository|TaggableModuleRepositoryInterface
  132.      */
  133.     protected function repo($class)
  134.     {
  135.         return $this->em->getRepository($class);
  136.     }
  137.     /**
  138.      * @param string $class
  139.      * @param int|array|Container|int[]|Container[] $container
  140.      * @param array $tags
  141.      * @param bool|null $deep
  142.      * @return QueryBuilder
  143.      */
  144.     protected function qb(
  145.         string $class,
  146.         $container,
  147.         array $tags = [],
  148.         ?bool $deep null
  149.     ): QueryBuilder
  150.     {
  151.         /** @var ProxyRepository $repo */
  152.         $repo $this->repo($class);
  153.         $qb $repo->createQueryBuilder('proxy');
  154.         if ($deep === true) {
  155.             // TODO: need to fix this to be compatible with cross dept sharing...
  156.             $qb
  157.                 ->andWhere($qb->expr()->in(
  158.                     'proxy.id',
  159.                     $this->em->createQueryBuilder()
  160.                         // TODO: take into account sharing
  161.                         ->select('DISTINCT IDENTITY(shares.sharedProxy)')
  162.                         ->from($class'shares')
  163.                         ->leftJoin('shares.container''containerr')
  164.                         // TODO: To get children we need root to be equal, not GREATER or equal, isn't it?
  165.                         ->andWhere('containerr.rt >= :rt')
  166.                         ->andWhere('containerr.lft >= :lft')
  167.                         ->andWhere('containerr.rgt <= :rgt')
  168.                         ->getDQL()
  169.                 ))
  170.                 ->setParameter('rt'$container->getRoot())
  171.                 ->setParameter('lft'$container->getLeft())
  172.                 ->setParameter('rgt'$container->getRight())
  173.                 ->leftJoin('proxy.container''container');
  174.         } else {
  175.             $qb
  176.                 ->andWhere('proxy.container IN (:container)')
  177.                 ->setParameter(
  178.                     'container',
  179.                     ($container instanceof Container) ? array($container->getId()) : $container
  180.                 );
  181.         }
  182.         if ($this->repo($class) instanceof TaggableModuleRepositoryInterface && ! empty($tags)) {
  183.             $qb $this->repo($class)->filterByTags($qb$tags);
  184.         }
  185.         return $qb;
  186.     }
  187. }