vendor/uvdesk/support-center-bundle/Repository/Solutions.php line 58

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Repository;
  3. use Doctrine\Common\Collections\Criteria;
  4. use Doctrine\ORM\EntityRepository;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntites;
  7. class Solutions extends \Doctrine\ORM\EntityRepository
  8. {
  9.     const LIMIT 10;
  10.     private $defaultImage '';
  11.     private $defaultSort 'a.id';
  12.     private $direction = ['asc''desc'];
  13.     private $sorting = ['a.name''a.dateAdded'];
  14.     private $safeFields = ['page''limit''sort''order''direction'];
  15.     private $allowedFormFields = ['search''name''description''visibility'];
  16.     private function validateSorting($sorting)
  17.     {
  18.         return in_array($sorting$this->sorting) ? $sorting $this->defaultSort;
  19.     }
  20.     private function validateDirection($direction)
  21.     {
  22.         return in_array($direction$this->direction) ? $direction Criteria::DESC;
  23.     }
  24.     private function presetting(&$data)
  25.     {
  26.         $data['sort'] = $_GET['sort'] = $this->validateSorting(isset($data['sort']) ? $data['sort'] : false);
  27.         $data['direction'] = $_GET['direction'] = $this->validateDirection(isset($data['direction']) ? $data['direction'] : false);
  28.         $this->cleanAllData($data);
  29.     }
  30.     private function cleanAllData(&$data)
  31.     {
  32.         if(isset($data['isActive'])){
  33.             $data['visibility'] = ($data['isActive'] ? 'public' 'private');
  34.             unset($data['isActive']);
  35.         }
  36.     }
  37.     public function getAllCategories($categoryLimit null$articleLimit null)
  38.     {
  39.         $categoryResponse = [];
  40.         $categoryQB $this->getEntityManager()->createQueryBuilder()->select('sc.id, sc.name, sc.description')
  41.             ->from(SupportEntites\SolutionCategory::class, 'sc')
  42.             ->andWhere('sc.status = :status')->setParameter('status'true)
  43.             ->orderBy('sc.dateAdded''DESC');            
  44.         
  45.         return $categoryQB->getQuery()->getResult();
  46.     }
  47.     public function getAllSolutions(\Symfony\Component\HttpFoundation\ParameterBag $obj null$container$allResult false$status = [01])
  48.     {
  49.         $json = array();
  50.         $qb $this->getEntityManager()->createQueryBuilder();
  51.         $qb->select('a')->from($this->getEntityName(), 'a');
  52.         $data $obj $obj->all() : [];
  53.         $data array_reverse($data);
  54.         $this->presetting($data);
  55.         foreach ($data as $key => $value) {
  56.             if(!in_array($key,$this->safeFields) && in_array($key$this->allowedFormFields)) {
  57.                 if($key!='dateUpdated' AND $key!='dateAdded' AND $key!='search') {
  58.                     $qb->Andwhere('a.'.$key.' = :'.$key);
  59.                     $qb->setParameter($key$value);
  60.                 } else {
  61.                     if($key == 'search') {
  62.                         $qb->orwhere('a.name'.' LIKE :name');
  63.                         $qb->setParameter('name''%'.urldecode(trim($value)).'%');
  64.                         $qb->orwhere('a.description'.' LIKE :description');
  65.                         $qb->setParameter('description''%'.urldecode(trim($value)).'%');
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         if(!$allResult){
  71.             $paginator  $container->get('knp_paginator');
  72.             $results $paginator->paginate(
  73.                 $qb,
  74.                 isset($data['page']) ? $data['page'] : 1,
  75.                 self::LIMIT,
  76.                 array('distinct' => false)
  77.             );
  78.         }else{
  79.             $qb->select($allResult);
  80.             $results $qb->getQuery()->getResult();
  81.             return $results;
  82.         }
  83.       
  84.         $newResult = [];
  85.         foreach ($results as $key => $result) {
  86.            
  87.             $newResult[] = array(
  88.                 'id'                   => $result->getId(),
  89.                 'name'                 => $result->getName(),
  90.                 'description'          => $result->getDescription(),
  91.                 'visibility'           => $result->getVisibility(),
  92.                 'solutionImage'        => ($result->getSolutionImage() == null) ? $this->defaultImage $result->getSolutionImage(),
  93.                 'categoriesCount'      => $this->getCategoriesCountBySolution($result->getId(), $status),
  94.                 'categories'           => $this->getCategoriesWithCountBySolution($result->getId(), $status),
  95.                 'articleCount'         => $this->getArticlesCountBySolution($result->getId(), $status)
  96.             );
  97.         }
  98.         $paginationData $results->getPaginationData();
  99.         $queryParameters $results->getParams();
  100.         $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  101.         $json['results'] = $newResult;
  102.         $json['pagination_data'] = $paginationData;
  103.        
  104.         return $json;
  105.     }
  106.     public function findSolutionById($filterArray = [])
  107.     {
  108.         $json = array();
  109.         $qb $this->getEntityManager()->createQueryBuilder();
  110.         $qb->select('a')->from($this->getEntityName(), 'a');
  111.         foreach ($filterArray as $key => $value) {
  112.             $qb->Andwhere('a.'.$key.' = :'.$key);
  113.             $qb->setParameter($key$value);
  114.         }
  115.         $result $qb->getQuery()->getOneOrNullResult();
  116.         
  117.         return($result);
  118.     }
  119.     public function getCategoriesWithCountBySolution($id$status = [1])
  120.     {
  121.         $queryBuilder $this->createQueryBuilder('a');
  122.         $categories $queryBuilder
  123.             ->select('sc.id, sc.name')
  124.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','ac','WITH''ac.solutionId = a.id')
  125.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory','sc','WITH''ac.categoryId = sc.id')
  126.             ->andwhere('ac.solutionId = :solutionId')
  127.             ->andwhere('sc.status IN (:status)')
  128.             ->setParameters([
  129.                 'solutionId' => $id,
  130.                 'status' => $status,
  131.             ])
  132.             ->orderBy('sc.sortOrder'Criteria::ASC)
  133.             ->getQuery()
  134.             ->getResult()
  135.         ;
  136.         if ($categories) {
  137.             $solutionCategoryRepository $this->getEntityManager()->getRepository(SupportEntites\SolutionCategory::class);
  138.             
  139.             foreach ($categories as $key => $category) {
  140.                 $categories[$key]['articleCount'] = $solutionCategoryRepository->getArticlesCountByCategory($category['id']);
  141.             }
  142.         }
  143.         
  144.         return $categories;
  145.     }
  146.     public function getCategoriesCountBySolution($id$status = [1])
  147.     {
  148.         $queryBuilder $this->createQueryBuilder('a');
  149.         $result $queryBuilder
  150.             ->select('COUNT(a.id)')
  151.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','ac','WITH''ac.solutionId = a.id')
  152.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory','sc','WITH''ac.categoryId = sc.id')
  153.             ->andwhere('ac.solutionId = :solutionId')
  154.             ->andwhere('sc.status IN (:status)')
  155.             ->setParameters([
  156.                 'solutionId' => $id ,
  157.                 'status' => $status,
  158.             ])
  159.             ->getQuery()
  160.             ->getSingleScalarResult()
  161.         ;
  162.         return $result;
  163.     }
  164.     public function getArticlesCountBySolution($id$status = [1])
  165.     {
  166.         $queryBuilder $this->createQueryBuilder('a');
  167.         $result $queryBuilder
  168.             ->select('COUNT(DISTINCT aa.id)')
  169.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','sac','WITH''sac.solutionId = a.id')
  170.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\ArticleCategory','ac','WITH''sac.categoryId = ac.categoryId')
  171.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Article','aa','WITH''ac.articleId = aa.id')
  172.             ->where('sac.solutionId = :solutionId')
  173.             ->andwhere('ac.id IS NOT NULL')
  174.             ->andwhere('aa.status != :status')
  175.             ->setParameters([
  176.                 'solutionId' => $id,
  177.                 'status' => 0
  178.             ])
  179.             ->getQuery()
  180.             ->getSingleScalarResult()
  181.         ;
  182.         return $result;
  183.     }
  184.     
  185.     public function removeEntryBySolution($id)
  186.     {
  187.         $where is_array($id) ? 'ac.solutionId IN (:id)' 'ac.solutionId = :id';
  188.         $queryBuilder $this->createQueryBuilder('ac');
  189.         $queryBuilder
  190.             ->delete(SupportEntites\SolutionCategoryMapping::class,'ac')
  191.             ->andwhere($where)
  192.             ->setParameters([
  193.                 'id' => $id ,
  194.             ])
  195.             ->getQuery()
  196.             ->execute()
  197.         ;
  198.     }
  199. }