src/Cms/ModuleBundle/Doctrine/ProxyRepository.php line 194

Open in your IDE?
  1. <?php
  2. namespace Cms\ModuleBundle\Doctrine;
  3. use App\Doctrine\Repository\Content\LegacyProxySearch;
  4. use App\Doctrine\Repository\Content\ObjectSearch;
  5. use App\Util\Pagination;
  6. use App\Util\Querying;
  7. use Cms\ContainerBundle\Entity\Container;
  8. use Cms\ContainerBundle\Entity\Containers\GenericContainer;
  9. use Cms\CoreBundle\Model\GlobalSearchInterface;
  10. use Cms\CoreBundle\Model\Interfaces\Lockable\LockableRepositoryInterface;
  11. use Cms\CoreBundle\Model\Interfaces\Lockable\LockableRepositoryTrait;
  12. use Cms\CoreBundle\Model\Search\SearchableInterface;
  13. use Cms\CoreBundle\Model\Search\SearchableTrait;
  14. use Cms\CoreBundle\Model\Search\Search;
  15. use Cms\CoreBundle\Util\Doctrine\EntityRepository;
  16. use Cms\ModuleBundle\Entity\ModuleSettings;
  17. use Cms\ModuleBundle\Entity\Proxy;
  18. use Cms\ModuleBundle\Model\Interfaces\Shareable\ShareableInterface;
  19. use Cms\ModuleBundle\Model\Traits\FrontendActions\CascadingSharesRepositoryTrait;
  20. use Cms\Modules\BlogBundle\Entity\ModuleSettings as BlogModuleSettings;
  21. use Cms\Modules\CalendarBundle\Entity\ModuleSettings as CalendarModuleSettings;
  22. use Cms\Modules\GalleryBundle\Entity\ModuleSettings as GalleryModuleSettings;
  23. use Cms\Modules\NewsBundle\Entity\ModuleSettings as NewsModuleSettings;
  24. use Cms\Modules\PeopleBundle\Entity\ModuleSettings as PeopleModuleSettings;
  25. use Cms\Modules\QuestionBundle\Entity\ModuleSettings as QuestionModuleSettings;
  26. use Doctrine\Common\Util\ClassUtils;
  27. use Doctrine\ORM\Tools\Pagination\Paginator;
  28. use Exception;
  29. use Platform\SecurityBundle\Entity\Identity\Account;
  30. /**
  31.  * Class ProxyRepository
  32.  *
  33.  * @package Cms\ModuleBundle\Doctrine
  34.  *
  35.  * @method Proxy findExact($id)
  36.  * @method array|Proxy[] findAll()
  37.  * @method array|Proxy[] findAllLocked()
  38.  */
  39. class ProxyRepository extends EntityRepository implements SearchableInterfaceGlobalSearchInterfaceLockableRepositoryInterface
  40. {
  41.     use SearchableTrait;
  42.     use LockableRepositoryTrait;
  43.     use CascadingSharesRepositoryTrait;
  44.     /**
  45.      * @param Proxy $proxy
  46.      * @return array|Proxy[]
  47.      */
  48.     public function findAllShares(Proxy $proxy): array
  49.     {
  50.         if ( ! $proxy instanceof ShareableInterface) {
  51.             return [];
  52.         }
  53.         return $this->queryMany(
  54.             $this->createQueryBuilder('shares')
  55.                 ->andWhere('shares.sharedProxy = :proxy')
  56.                 ->andWhere('shares.id <> :proxy')
  57.                 ->setParameter('proxy'$proxy->getId())
  58.         );
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function search(Search $search$space nullModuleSettings $settings null)
  64.     {
  65.         if (empty($space) || ! ($space instanceof Proxy || $space instanceof Container)) {
  66.             throw new Exception('Second parameter must either a Container or Proxy object.');
  67.         }
  68.         // create query builder
  69.         $qb $this->createQueryBuilder('nodes');
  70.         if ($space instanceof Container) {
  71.             if ($settings instanceof BlogModuleSettings || $settings instanceof CalendarModuleSettings || $settings instanceof NewsModuleSettings || $settings instanceOf GalleryModuleSettings || $settings instanceof PeopleModuleSettings || $settings instanceof QuestionModuleSettings) {
  72.                 $space $this->flattenContainers($spaceClassUtils::getClass($settings));
  73.             }
  74.             $qb
  75.                 ->andWhere('nodes.container IN (:container)')
  76.                 ->setParameter('container'$space);
  77.         }
  78.         return $this->query($qb$search);
  79.     }
  80.     /**
  81.      * @param LegacyProxySearch $search
  82.      * @param int|null $limit
  83.      * @param int|null $offset
  84.      * @return Paginator
  85.      */
  86.     public function schoolnow(
  87.         LegacyProxySearch $search,
  88.         ?int $limit null,
  89.         ?int $offset null
  90.     ): Paginator
  91.     {
  92.         $qb $this->createQueryBuilder('proxies');
  93.         if ($search->getVisibility() !== null) {
  94.             $qb
  95.                 ->andWhere('proxies.placeholder = :placeholder')
  96.                 ->setParameter('placeholder', ! $search->getVisibility());
  97.         }
  98.         if ($lookup $search->getLookup()) {
  99.             $qb
  100.                 ->andWhere($qb->expr()->orX(
  101.                     Querying::dqlLike('proxies.data_title LIKE :lookup')
  102.                 ))
  103.                 ->setParameter('lookup'Querying::likeAny($lookup));
  104.         }
  105.         if (
  106.             in_array($search->getFilter(), [ObjectSearch::FILTERS__MY_DRAFTObjectSearch::FILTERS__MY_PUBLISHED], true) &&
  107.             $search->getAccount() instanceof Account
  108.         ) {
  109.             $qb
  110.                 ->andWhere(
  111.                     $qb->expr()->orX(
  112.                         $qb->expr()->eq('proxies.touchedBy'$search->getAccount()->getId()),
  113.                         $qb->expr()->andX(
  114.                             $qb->expr()->isNull('proxies.touchedBy'),
  115.                             $qb->expr()->eq('proxies.updatedBy'$search->getAccount()->getId())
  116.                         ),
  117.                         $qb->expr()->andX(
  118.                             $qb->expr()->isNull('proxies.touchedBy'),
  119.                             $qb->expr()->isNull('proxies.updatedBy'),
  120.                             $qb->expr()->eq('proxies.createdBy'$search->getAccount()->getId())
  121.                         )
  122.                     )
  123.                 )
  124.             ;
  125.         }
  126.         if (in_array($search->getFilter(), [ObjectSearch::FILTERS__MY_DRAFTObjectSearch::FILTERS__DRAFT], true)) {
  127.             $qb
  128.                 ->andWhere('proxies.placeholder = :placeholder')
  129.                 ->setParameter('placeholder'true)
  130.             ;
  131.         }
  132.         if (in_array($search->getFilter(), [ObjectSearch::FILTERS__MY_PUBLISHEDObjectSearch::FILTERS__PUBLISHED], true)) {
  133.             $qb
  134.                 ->andWhere('proxies.placeholder = :placeholder')
  135.                 ->setParameter('placeholder'false)
  136.             ;
  137.         }
  138.         switch ($search->getSort()) {
  139.             case LegacyProxySearch::SORTS__NAME:
  140.                 $qb
  141.                     ->addOrderBy('proxies.data_title'$search->getDirection());
  142.                 break;
  143.             case LegacyProxySearch::SORTS__TIMESTAMP:
  144.                 $qb
  145.                     ->addOrderBy('proxies.touchedAt'$search->getDirection())
  146.                     ->addOrderBy('proxies.data_title''ASC');
  147.                 break;
  148.         }
  149.         return Pagination::paginateThenQuery(
  150.             $qb,
  151.             $limit,
  152.             $offset ?: 0
  153.         );
  154.     }
  155.     /**
  156.      * @return array|Proxy[]
  157.      */
  158.     public function findAllWithContainers()
  159.     {
  160.         $builder $this->createQueryBuilder('proxies');
  161.         $builder
  162.             ->leftJoin('proxies.container''container');
  163.         return $this->queryMany($builder);
  164.     }
  165.     /**
  166.      * @param Container $container
  167.      * @param int $limit
  168.      * @param int $page
  169.      * @return array|Proxy[]
  170.      */
  171.     public function findAllForContainer(Container $container$limit 0$page 0)
  172.     {
  173.         $qb $this->createQueryBuilder('proxies')
  174.             ->andWhere('proxies.container = :container')
  175.             ->setParameter('container'$container);
  176.         return $this->queryMany($qb$limit$page);
  177.     }
  178.     /**
  179.      * {@inheritdoc}
  180.      */
  181.     public function findAllForGlobalSearch($query)
  182.     {
  183.         return $this->queryMany(
  184.             $this->createQueryBuilder('proxy')
  185.                 ->leftJoin('proxy.container''container')
  186.                 ->andWhere('proxy.data_title LIKE :queryLike')
  187.                 ->setParameter('queryLike''%' $query '%')
  188.                 ->andWhere('container INSTANCE OF :class')
  189.                 ->setParameter('class'GenericContainer::DISCR)
  190.                 ->addOrderBy('proxy.data_title''ASC')
  191.         );
  192.     }
  193.     /**
  194.      * @param $regex
  195.      * @param array $fields
  196.      * @return array|Proxy[]
  197.      */
  198.     public function findByRegex($regex, array $fields)
  199.     {
  200.         if ( ! count($fields)) {
  201.             return [];
  202.         }
  203.         $proxyTable $this->getClassMetadata()->getTableName();
  204.         //create native sql
  205.         $sql "SELECT id FROM `$proxyTable`";
  206.         //add condition to native sql
  207.         foreach ($fields as $field) {
  208.             $where[] = "`$field` REGEXP(:regex)";
  209.         }
  210.         if (isset($where)) {
  211.             $sql .= " WHERE " implode(" AND "$where);
  212.         }
  213.         $stmt $this->_em->getConnection()->prepare($sql);
  214.         $stmt->execute(
  215.             array(
  216.                 'regex' => $regex,
  217.             )
  218.         );
  219.         //set parameters for returned only ids array
  220.         $stmt->setFetchMode(\PDO::FETCH_COLUMN0);
  221.         //get only proxies ids
  222.         $ids $stmt->fetchAll();
  223.         if (count($ids) == 0) {
  224.             return [];
  225.         }
  226.         //find proxies by ids
  227.         $qb $this->createQueryBuilder('p');
  228.         $expr $qb->expr();
  229.         $qb->andWhere(
  230.             $expr->in('p.id'$ids)
  231.         );
  232.         return $this->queryMany($qb);
  233.     }
  234. }