src/Notaris/AdminBundle/Entity/FaqRepository.php line 162

Open in your IDE?
  1. <?php
  2. namespace Notaris\AdminBundle\Entity;
  3. use Doctrine\ORM\EntityRepository;
  4. /**
  5.  * FaqRepository
  6.  *
  7.  * This class was generated by the Doctrine ORM. Add your own custom
  8.  * repository methods below.
  9.  */
  10. class FaqRepository extends EntityRepository
  11. {
  12.     /**
  13.      * @param $locale
  14.      * @param int $offset
  15.      * @param array $sort_arr
  16.      * @param int $limit
  17.      * @return array|bool
  18.      */
  19.     public function getAllFaq($locale, $offset = 0, $sort_arr = array(), $limit = 50)
  20.     {
  21.         //start orders
  22.         $orders_arr = array();
  23.         $order_type = $sort_arr['type'];
  24.         $order_columns = array(
  25.             0 => 'f.pub_date',
  26.             'f.title',
  27.             'f.count_clicked',
  28.             'subsites',
  29.             'is_active',
  30.             'f.news_id',
  31.         );
  32.         if (isset($order_columns[$sort_arr['column']]) && $order_columns[$sort_arr['column']]) {
  33.             $orders_arr[] = $order_columns[$sort_arr['column']] . ' ' . $order_type;
  34.         }
  35.         //end orders
  36.         $em = $this->getEntityManager();
  37.         $con = $em->getConnection();
  38.         $sql = "SELECT f.id, f.pub_date, f.title, f.count_clicked,
  39.                   IF (f.is_active = 1,(IF(f.pub_date <= NOW(), f.is_active, 2)),f.is_active) AS is_active,
  40.                   f.news_id, f.is_general,
  41.                   GROUP_CONCAT(DISTINCT s.name ORDER BY bs.position ASC SEPARATOR '|') as subsites
  42.                   FROM faq f
  43.                   LEFT JOIN faq_subsite bs ON bs.faq_id = f.id
  44.                   LEFT JOIN subsite s ON s.id = bs.subsite_id and s.is_active = 1
  45.                   WHERE f.locale = :locale
  46.                   GROUP BY f.id
  47.                   ORDER BY " . implode(', ', $orders_arr) . '
  48.                   LIMIT ' . $offset . ', ' . $limit;
  49.         $stmt = $con->prepare($sql);
  50.         $stmt->bindValue('locale', $locale);
  51.         $stmt = $stmt->execute();
  52.         $result = $stmt->fetchAll();
  53.         //all count
  54.         $sql = "SELECT count(*) as count FROM faq f WHERE f.locale = :locale";
  55.         $stmt = $con->prepare($sql);
  56.         $stmt->bindValue('locale', $locale);
  57.         $stmt = $stmt->execute();
  58.         $count = $stmt->fetchOne();
  59.         $online = $offline = $scheduled = 0;
  60.         if ($count) {
  61.             $time = new \DateTime('now');
  62.             $now = $time->format('Y-m-d 00:00:00');
  63.             //online
  64.             $sql = "SELECT count(*) as count FROM faq f WHERE f.is_active = 1 AND f.pub_date <= :now AND f.locale = :locale";
  65.             $stmt = $con->prepare($sql);
  66.             $stmt->bindValue('now', $now);
  67.             $stmt->bindValue('locale', $locale);
  68.             $stmt = $stmt->execute();
  69.             $online = $stmt->fetchOne();
  70.             //scheduled
  71.             $sql = "SELECT count(*) as count FROM faq f WHERE f.is_active = 1 AND f.pub_date > :now AND f.locale = :locale";
  72.             $stmt = $con->prepare($sql);
  73.             $stmt->bindValue('now', $now);
  74.             $stmt->bindValue('locale', $locale);
  75.             $stmt = $stmt->execute();
  76.             $scheduled = $stmt->fetchOne();
  77.             //offline
  78.             $sql = "SELECT count(*) as count FROM faq f WHERE f.is_active = 0 AND f.locale = :locale";
  79.             $stmt = $con->prepare($sql);
  80.             $stmt->bindValue('locale', $locale);
  81.             $stmt = $stmt->execute();
  82.             $offline = $stmt->fetchOne();
  83.         }
  84.         if ($result) {
  85.             return array(
  86.                 'result' => $result,
  87.                 'count' => $count,
  88.                 'online' => $online,
  89.                 'offline' => $offline,
  90.                 'scheduled' => $scheduled
  91.             );
  92.         }
  93.         return false;
  94. //        //LEFT JOIN NotarisAdminBundle:FaqSubsite fs WITH fs.faq = f.id
  95. //        //LEFT JOIN NotarisAdminBundle:News n WITH n.id = f.news
  96. //        $query = $this->getEntityManager()
  97. //            ->createQuery('SELECT f FROM
  98. //                    NotarisAdminBundle:Faq f
  99. //                    WHERE f.locale = :locale
  100. //                    ORDER BY f.publicationDate DESC')
  101. //
  102. //            ->setParameter('locale', $locale);
  103. //
  104. //        $tags  = $query->getResult();
  105. //
  106. //        if($tags){
  107. //            return $tags;
  108. //        }
  109. //        return false;
  110.     }
  111.     /**
  112.      * @param $locale
  113.      * @param $subsite_id
  114.      * @return array|bool
  115.      */
  116.     public function getListFaqSubsite($locale, $subsite_id)
  117.     {
  118.         $query = $this->getEntityManager()
  119.             ->createQuery(
  120.                 'SELECT b FROM
  121.                     NotarisAdminBundle:Faq b
  122.                      INNER JOIN NotarisAdminBundle:FaqSubsite fs WITH fs.faq = b.id
  123.                     WHERE b.locale = :locale AND fs.subsite = :subsite_id
  124.                     ORDER BY fs.position ASC, b.publicationDate DESC'
  125.             )
  126.             ->setParameter('locale', $locale)
  127.             ->setParameter('subsite_id', $subsite_id);
  128.         $tags = $query->getResult();
  129.         if ($tags) {
  130.             return $tags;
  131.         }
  132.         return false;
  133.     }
  134.     /**
  135.      * @param $locale
  136.      * @param $subsite_id
  137.      * @return array|bool
  138.      */
  139.     public function getListFaqSubsitePublic($locale, $subsite_id)
  140.     {
  141.         $query = $this->getEntityManager()
  142.             ->createQuery(
  143.                 'SELECT b FROM
  144.                     NotarisAdminBundle:Faq b
  145.                      INNER JOIN NotarisAdminBundle:FaqSubsite fs WITH fs.faq = b.id
  146.                     WHERE b.locale = :locale AND fs.subsite = :subsite_id
  147.                     AND b.isActive = 1 AND b.publicationDate <= :now
  148.                     ORDER BY fs.position ASC, b.publicationDate DESC'
  149.             )
  150.             ->setParameter('locale', $locale)
  151.             ->setParameter('now', new \DateTime('now'))
  152.             ->setParameter('subsite_id', $subsite_id);
  153.         $tags = $query->getResult();
  154.         if ($tags) {
  155.             return $tags;
  156.         }
  157.         return false;
  158.     }
  159.     public function getListFaqFromSubsite($subsite_id)
  160.     {
  161.         $query = $this->getEntityManager()
  162.             ->createQuery(
  163.                 'SELECT b FROM
  164.                     NotarisAdminBundle:Faq b
  165.                      INNER JOIN NotarisAdminBundle:FaqSubsite fs WITH fs.faq = b.id
  166.                     WHERE fs.subsite = :subsite_id'
  167.             )
  168.             ->setParameter('subsite_id', $subsite_id);
  169.         $tags = $query->getResult();
  170.         if ($tags) {
  171.             return $tags;
  172.         }
  173.         return false;
  174.     }
  175.     /**
  176.      * @param $locale
  177.      * @param $subsite_id
  178.      */
  179.     public function getRandomFaqSubsitePublic($locale, $subsite_id, $filterShowSubsite = false)
  180.     {
  181.         $query = $this->getEntityManager()
  182.             ->createQuery(
  183.                 'SELECT b FROM
  184.                     NotarisAdminBundle:Faq b
  185.                      INNER JOIN NotarisAdminBundle:FaqSubsite fs WITH fs.faq = b.id
  186.                     WHERE b.locale = :locale AND fs.subsite = :subsite_id
  187.                     AND b.isActive = 1 AND b.publicationDate <= :now' .
  188.                     ($filterShowSubsite ? ' AND b.showInSubsite = 1' : '') . '
  189.                     ORDER BY fs.position ASC, b.publicationDate DESC'
  190.             )
  191.             ->setParameter('locale', $locale)
  192.             ->setParameter('now', new \DateTime('now'))
  193.             ->setParameter('subsite_id', $subsite_id);
  194.         $faqs = $query->getResult();
  195.         if ($faqs) {
  196.             shuffle($faqs);
  197.             return array_shift($faqs);
  198.         }
  199.         return false;
  200.     }
  201.     /**
  202.      * @param $locale
  203.      * @param $subsite_id
  204.      * @return array|bool
  205.      */
  206.     public function getListFaqGeneralPublic($locale)
  207.     {
  208.         $query = $this->getEntityManager()
  209.             ->createQuery(
  210.                 'SELECT b FROM
  211.                     NotarisAdminBundle:Faq b
  212.                     WHERE b.locale = :locale AND b.isGeneral = 1
  213.                     AND b.isActive = 1 AND b.publicationDate <= :now
  214.                     ORDER BY b.publicationDate DESC'
  215.             )
  216.             ->setParameter('locale', $locale)
  217.             ->setParameter('now', new \DateTime('now'));
  218.         $tags = $query->getResult();
  219.         if ($tags) {
  220.             return $tags;
  221.         }
  222.         return false;
  223.     }
  224.     public function getRandomFaq($locale)
  225.     {
  226.         $em = $this->getEntityManager();
  227.         $con = $em->getConnection();
  228.         $sql = "SELECT f.title, f.slug as faqslug, sub.slug FROM faq as f INNER JOIN faq_subsite as s on f.id = s.faq_id INNER JOIN subsite as sub ON s.subsite_id = sub.id WHERE f.locale = :locale and f.is_active = 1 and f.pub_date <= NOW() ORDER BY rand() limit 1";
  229.         $stmt = $con->prepare($sql);
  230.         $stmt->bindValue('locale', $locale);
  231.         $stmt = $stmt->execute();
  232.         $result = $stmt->fetchAll();
  233.         if ($result) {
  234.             return array_shift($result);
  235.         }
  236.         return false;
  237.     }
  238.     public function search($keyword, $locale='nl') {
  239.         $searchResults = $this->createQueryBuilder('f')
  240.             ->select('f')
  241.             ->addSelect("MATCH_AGAINST (f.title, f.subtitle, f.answer, :address 'IN BOOLEAN MODE') as score")
  242.             ->where('f.locale = :locale AND f.publicationDate <= :now AND f.isActive = 1')
  243.             ->andWhere("MATCH_AGAINST(f.title, f.subtitle, f.answer, :address 'IN BOOLEAN MODE') > 0.2")
  244.             ->setParameter('address', $keyword)
  245.             ->setParameter('locale', $locale)
  246.             ->setParameter('now', new \DateTime('now'))
  247.             ->orderBy('score', 'desc')
  248.             ->getQuery()
  249.             ->getResult();
  250.         if($searchResults){
  251.             return $searchResults;
  252.         }
  253.         return false;
  254.     }
  255. }