src/Notaris/AdminBundle/Entity/BrochureRepository.php line 167

Open in your IDE?
  1. <?php
  2. namespace Notaris\AdminBundle\Entity;
  3. use Doctrine\ORM\EntityRepository;
  4. use Notaris\AdminBundle\ValueObject\BrochureType;
  5. /**
  6.  * BrochureRepository
  7.  *
  8.  * This class was generated by the Doctrine ORM. Add your own custom
  9.  * repository methods below.
  10.  */
  11. class BrochureRepository extends EntityRepository
  12. {
  13.     /**
  14.      * @param $locale
  15.      * @param int $offset
  16.      * @param array $sort_arr
  17.      * @param int $limit
  18.      * @return array|bool
  19.      */
  20.     public function getAllBrochures($locale, $offset = 0, $sort_arr = array(), $limit = 50)
  21.     {
  22.         //start orders
  23.         $orders_arr = array();
  24.         $order_type = $sort_arr['type'];
  25.         $order_columns = array(
  26.             0 => 'b.pub_date',
  27.             'b.title',
  28.             'b.is_izimi',
  29.             'b.count_clicked',
  30.             'primary_subsite_name',
  31.             'is_active',
  32.             'b.news_id',
  33.         );
  34.         if (isset($order_columns[$sort_arr['column']]) && $order_columns[$sort_arr['column']]) {
  35.             $orders_arr[] = $order_columns[$sort_arr['column']] . ' ' . $order_type;
  36.         }
  37.         //end orders
  38.         $em = $this->getEntityManager();
  39.         $con = $em->getConnection();
  40.         $sql = "SELECT b.id, b.pub_date, b.title, b.count_clicked, b.is_izimi,
  41.                   IF (b.is_active = 1,(IF(b.pub_date <= NOW(), b.is_active, 2)),b.is_active) AS is_active,
  42.                   b.is_general, b.is_general_primary,
  43.                   IF(b.is_general = 1 and is_general_primary = 1, 'General', sp.name) as primary_subsite_name, b.news_id, bs.brochure,
  44.                   GROUP_CONCAT(DISTINCT s.name ORDER BY bs.position ASC SEPARATOR '|') as subsites
  45.                   FROM brochure b
  46.                   LEFT JOIN brochure_subsite bs ON bs.brochure = b.id
  47.                   LEFT JOIN subsite s ON s.id = bs.subsite_id and s.is_active = 1
  48.                   LEFT JOIN subsite sp ON sp.id = b.primary_subsite and sp.is_active = 1
  49.                   WHERE b.locale = :locale
  50.                   GROUP BY b.id
  51.                   ORDER BY " . implode(', ', $orders_arr) . '
  52.                   LIMIT ' . $offset . ', ' . $limit;
  53.         $stmt = $con->prepare($sql);
  54.         $stmt->bindValue('locale', $locale);
  55.         $stmt = $stmt->execute();
  56.         $result = $stmt->fetchAll();
  57.         //all count
  58.         $sql = "SELECT count(*) as count FROM brochure b WHERE b.locale = :locale";
  59.         $stmt = $con->prepare($sql);
  60.         $stmt->bindValue('locale', $locale);
  61.         $stmt = $stmt->execute();
  62.         $count = $stmt->fetchOne();
  63.         $online = $offline = $scheduled = 0;
  64.         if ($count) {
  65.             $time = new \DateTime('now');
  66.             $now = $time->format('Y-m-d 00:00:00');
  67.             //online
  68.             $sql = "SELECT count(*) as count FROM brochure b WHERE b.is_active = 1 AND b.pub_date <= :now AND b.locale = :locale";
  69.             $stmt = $con->prepare($sql);
  70.             $stmt->bindValue('now', $now);
  71.             $stmt->bindValue('locale', $locale);
  72.             $stmt = $stmt->execute();
  73.             $online = $stmt->fetchOne();
  74.             //scheduled
  75.             $sql = "SELECT count(*) as count FROM brochure b WHERE b.is_active = 1 AND b.pub_date > :now AND b.locale = :locale";
  76.             $stmt = $con->prepare($sql);
  77.             $stmt->bindValue('now', $now);
  78.             $stmt->bindValue('locale', $locale);
  79.             $stmt = $stmt->execute();
  80.             $scheduled = $stmt->fetchOne();
  81.             //offline
  82.             $sql = "SELECT count(*) as count FROM brochure b WHERE b.is_active = 0 AND b.locale = :locale";
  83.             $stmt = $con->prepare($sql);
  84.             $stmt->bindValue('locale', $locale);
  85.             $stmt = $stmt->execute();
  86.             $offline = $stmt->fetchOne();
  87.         }
  88.         if ($result) {
  89.             return array(
  90.                 'result' => $result,
  91.                 'count' => $count,
  92.                 'online' => $online,
  93.                 'offline' => $offline,
  94.                 'scheduled' => $scheduled
  95.             );
  96.         }
  97.         return false;
  98.     }
  99.     /**
  100.      * @param $locale
  101.      * @param $subsite_id
  102.      * @return array|bool
  103.      */
  104.     public function getListBrochuresSubsite($locale, $subsite_id)
  105.     {
  106.         $query = $this->getEntityManager()
  107.             ->createQuery(
  108.                 'SELECT b FROM
  109.                     NotarisAdminBundle:Brochure b
  110.                      INNER JOIN NotarisAdminBundle:BrochureSubsite bs WITH bs.brochure = b.id
  111.                     WHERE b.locale = :locale AND bs.subsite = :subsite_id
  112.                     ORDER BY bs.position ASC, b.publicationDate DESC'
  113.             )
  114.             ->setParameter('locale', $locale)
  115.             ->setParameter('subsite_id', $subsite_id);
  116.         $tags = $query->getResult();
  117.         if ($tags) {
  118.             return $tags;
  119.         }
  120.         return false;
  121.     }
  122.     public function getAllBrochuresSubsite($subsite_id)
  123.     {
  124.         $query = $this->getEntityManager()
  125.             ->createQuery(
  126.                 'SELECT b FROM
  127.                     NotarisAdminBundle:Brochure b
  128.                      INNER JOIN NotarisAdminBundle:BrochureSubsite bs WITH bs.brochure = b.id
  129.                     WHERE bs.subsite = :subsite_id'
  130.             )
  131.             ->setParameter('subsite_id', $subsite_id);
  132.         $tags = $query->getResult();
  133.         if ($tags) {
  134.             return $tags;
  135.         }
  136.         return false;
  137.     }
  138.     /**
  139.      * @param $locale
  140.      * @param $subsite_id
  141.      * @return array|bool
  142.      */
  143.     public function getListBrochuresSubsitePublic($locale, $subsite_id, BrochureType $brochureType = null)
  144.     {
  145.         if ($brochureType instanceof BrochureType) {
  146.             $query = $this->getEntityManager()
  147.                 ->createQuery(
  148.                     'SELECT b FROM
  149.                     NotarisAdminBundle:Brochure b
  150.                      INNER JOIN NotarisAdminBundle:BrochureSubsite bs WITH bs.brochure = b.id
  151.                     WHERE b.locale = :locale AND bs.subsite = :subsite_id  AND b.isActive = 1  AND b.publicationDate <= :now AND b.brochureType = :brochureType
  152.                     ORDER BY bs.position ASC, b.publicationDate DESC'
  153.                 )
  154.                 ->setParameter('locale', $locale)
  155.                 ->setParameter('now', new \DateTime('now'))
  156.                 ->setParameter('subsite_id', $subsite_id)
  157.                 ->setParameter('brochureType', (string) $brochureType);
  158.         } else {
  159.             $query = $this->getEntityManager()
  160.                 ->createQuery(
  161.                     'SELECT b FROM
  162.                     NotarisAdminBundle:Brochure b
  163.                      INNER JOIN NotarisAdminBundle:BrochureSubsite bs WITH bs.brochure = b.id
  164.                     WHERE b.locale = :locale AND bs.subsite = :subsite_id  AND b.isActive = 1  AND b.publicationDate <= :now
  165.                     ORDER BY bs.position ASC, b.publicationDate DESC'
  166.                 )
  167.                 ->setParameter('locale', $locale)
  168.                 ->setParameter('now', new \DateTime('now'))
  169.                 ->setParameter('subsite_id', $subsite_id);
  170.         }
  171.         $tags = $query->getResult();
  172.         if ($tags) {
  173.             return $tags;
  174.         }
  175.         return false;
  176.     }
  177.     /**
  178.      * @param $locale
  179.      * @return array|bool
  180.      */
  181.     public function getListBrochuresIsGeneral($locale)
  182.     {
  183.         $query = $this->getEntityManager()
  184.             ->createQuery(
  185.                 'SELECT b FROM
  186.                     NotarisAdminBundle:Brochure b
  187.                     WHERE b.locale = :locale AND b.isGeneral = :is_general
  188.                     ORDER BY b.generalPosition ASC, b.publicationDate DESC'
  189.             )
  190.             ->setParameter('locale', $locale)
  191.             ->setParameter('is_general', 1);
  192.         $tags = $query->getResult();
  193.         if ($tags) {
  194.             return $tags;
  195.         }
  196.         return false;
  197.     }
  198.     /**
  199.      * @param $locale
  200.      * @return array|bool
  201.      */
  202.     public function getListBrochuresIsGeneralPublic($locale, BrochureType $brochureType = null){
  203.         if ($brochureType instanceof BrochureType) {
  204.             $query = $this->getEntityManager()
  205.                 ->createQuery('SELECT b FROM
  206.                     NotarisAdminBundle:Brochure b
  207.                     WHERE b.locale = :locale AND b.isGeneral = :is_general AND b.isActive = 1  AND b.publicationDate <= :now AND b.brochureType = :brochureType
  208.                     ORDER BY b.generalPosition ASC, b.publicationDate DESC')
  209.                 ->setParameter('locale', $locale)
  210.                 ->setParameter('now', new \DateTime('now'))
  211.                 ->setParameter('is_general', 1)
  212.                 ->setParameter('brochureType', $brochureType->getType());
  213.         } else {
  214.             $query = $this->getEntityManager()
  215.                 ->createQuery('SELECT b FROM
  216.                     NotarisAdminBundle:Brochure b
  217.                     WHERE b.locale = :locale AND b.isGeneral = :is_general AND b.isActive = 1  AND b.publicationDate <= :now
  218.                     ORDER BY b.generalPosition ASC, b.publicationDate DESC')
  219.                 ->setParameter('locale', $locale)
  220.                 ->setParameter('now', new \DateTime('now'))
  221.                 ->setParameter('is_general', 1);
  222.         }
  223.         $tags  = $query->getResult();
  224.         if($tags){
  225.             return $tags;
  226.         }
  227.         return false;
  228.     }
  229.     public function getAllIzimiBrochures($page, $perPage, $locale = null, $minUpdateDate = null, $maxUpdateDate = null, $sortBy = null, $sortDirection = null, $category = null)
  230.     {
  231.         $brochures = $this->createQueryBuilder('n');
  232.         if (!empty($category)) {
  233.             $brochures = $brochures->leftJoin('n.categories', 'c');
  234.         }
  235.         $brochures = $brochures->where('n.isIzimi = 1 AND n.isActive = 1');
  236.         if (!empty($locale)) {
  237.             $brochures = $brochures->andWhere('n.locale = :locale');
  238.             $brochures = $brochures->setParameter('locale', $locale);
  239.         }
  240.         if (!empty($minUpdateDate)) {
  241.             $brochures = $brochures->andWhere('n.updated >= :minUpdateDate');
  242.             $brochures = $brochures->setParameter('minUpdateDate', $minUpdateDate);
  243.         }
  244.         if (!empty($maxUpdateDate)) {
  245.             $brochures = $brochures->andWhere('n.updated <= :maxUpdateDate');
  246.             $brochures = $brochures->setParameter('maxUpdateDate', $maxUpdateDate);
  247.         }
  248.         if (!empty($category)) {
  249.             $brochures = $brochures->andWhere('c.id = :category');
  250.             $brochures = $brochures->setParameter('category', $category);
  251.         }
  252.         if (!empty($sortBy)) {
  253.             $order = $sortDirection === 'desc' ? 'desc' : 'asc';
  254.             if ($sortBy === 'created_datetime') {
  255.                 $brochures = $brochures->orderBy('n.created', $order);
  256.             }
  257.             if ($sortBy === 'updated_datetime') {
  258.                 $brochures = $brochures->orderBy('n.updated', $order);
  259.             }
  260.             if ($sortBy === 'publication_date') {
  261.                 $brochures = $brochures->orderBy('n.publicationDate', $order);
  262.             }
  263.         }
  264.         $brochuresTotalResults = $brochures->getQuery()
  265.             ->getResult();
  266.         $brochuresTotalCount = empty($brochuresTotalResults) ? 0 : count($brochuresTotalResults);
  267.         if (empty($perPage)) {
  268.             $brochures = $brochuresTotalResults;
  269.         } else {
  270.             $brochures = $brochures->setFirstResult($perPage * ($page - 1))
  271.                 ->setMaxResults($perPage)
  272.                 ->getQuery()
  273.                 ->getResult();
  274.         }
  275.         if(!empty($brochures)){
  276.             return [
  277.                 'items' => $brochures,
  278.                 'totalCount' => $brochuresTotalCount,
  279.             ];
  280.         }
  281.         return [
  282.             'items' => [],
  283.             'totalCount' => $brochuresTotalCount,
  284.         ];
  285.     }
  286. }