vendor/uvdesk/support-center-bundle/Repository/SolutionCategory.php line 55

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 Doctrine\ORM\Query;
  7. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntites;
  8. /**
  9.  * Website
  10.  *
  11.  * This class was generated by the Doctrine ORM. Add your own custom
  12.  * repository methods below.
  13.  */
  14. class SolutionCategory extends EntityRepository
  15. {
  16.     const LIMIT 1000;
  17.     private $defaultSort 'a.id';
  18.     private $direction = ['asc''desc'];
  19.     private $sorting = ['a.name''a.dateAdded''a.sortOrder'];
  20.     private $safeFields = ['page''limit''sort''order''direction'];
  21.     private $allowedFormFields = ['search''name''description''sorting''sortOrder''status'];
  22.     private $defaultImage 'https://s3-ap-southeast-1.amazonaws.com/opencart-hd/website/1/2017/01/02/586a365e5e472.default-icon.png';
  23.     private function validateSorting($sorting)
  24.     {
  25.         return in_array($sorting$this->sorting) ? $sorting $this->defaultSort;
  26.     }
  27.     private function validateDirection($direction)
  28.     {
  29.         return in_array($direction$this->direction) ? $direction Criteria::DESC;
  30.     }
  31.     private function presetting(&$data)
  32.     {
  33.         $data['sort'] = $_GET['sort'] = $this->validateSorting(isset($data['sort']) ? $data['sort'] : false);
  34.         $data['direction'] = $_GET['direction'] = $this->validateDirection(isset($data['direction']) ? $data['direction'] : false);
  35.         $this->cleanAllData($data);
  36.     }
  37.     private function cleanAllData(&$data)
  38.     {
  39.         if(isset($data['isActive'])){
  40.             $data['status'] = $data['isActive'];
  41.             unset($data['isActive']);
  42.             unset($data['solutionId']);
  43.         }
  44.     }
  45.     public function getAllCategories(\Symfony\Component\HttpFoundation\ParameterBag $obj null$container$allResult false)
  46.     {
  47.         $json = array();
  48.         $qb $this->getEntityManager()->createQueryBuilder();
  49.         $qb->select('a')->from($this->getEntityName(), 'a');
  50.         $data $obj $obj->all() : [];
  51.         $data array_reverse($data);
  52.         $categories = [];
  53.         if(isset($data['solutionId'])){
  54.             $qbS $this->getEntityManager()->createQueryBuilder();
  55.             $qbS->select('a.categoryId')->from('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping''a');
  56.             $qbS->where('a.solutionId = :solutionId');
  57.             $qbS->setParameter('solutionId'$data['solutionId']);
  58.             $categories $qbS->getQuery()->getResult();
  59.             $categories $categories $categories : [0];
  60.         }
  61.         $this->presetting($data);
  62.         foreach ($data as $key => $value) {
  63.             if(!in_array($key,$this->safeFields) && in_array($key$this->allowedFormFields)) {
  64.                 if($key!='dateUpdated' AND $key!='dateAdded' AND $key!='search') {
  65.                         $qb->Andwhere('a.'.$key.' = :'.$key);
  66.                         $qb->setParameter($key$value);
  67.                 } else {
  68.                     if($key == 'search') {
  69.                         $qb->orwhere('a.name'.' LIKE :name');
  70.                         $qb->setParameter('name''%'.urldecode(trim($value)).'%');
  71.                         $qb->orwhere('a.description'.' LIKE :description');
  72.                         $qb->setParameter('description''%'.urldecode(trim($value)).'%');
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.         // $qb->Andwhere('a.companyId'.' = :company');
  78.         // $qb->setParameter('company', $container->get('user.service')->getCurrentCompany()->getId());
  79.         if($categories){
  80.             $qb->Andwhere('a.id IN (:categories)');
  81.             $qb->setParameter('categories'$categories);
  82.         }
  83.         if(!$allResult){
  84.             $paginator  $container->get('knp_paginator');
  85.             $results $paginator->paginate(
  86.                 $qb,
  87.                 isset($data['page']) ? $data['page'] : 1,
  88.                 isset($data['limit']) ? $data['limit'] : self::LIMIT,
  89.                 array('distinct' => false)
  90.             );
  91.         }else{
  92.             $qb->select($allResult);
  93.             $results $qb->getQuery()->getResult();
  94.             return $results;
  95.         }
  96.         $newResult = [];
  97.         foreach ($results as $key => $result) {
  98.             $newResult[] = array(
  99.                 'id'                   => $result->getId(),
  100.                 'name'                 => $result->getName(),
  101.                 'description'          => $result->getDescription(),
  102.                 'status'               => $result->getStatus(),
  103.                 'sorting'              => $result->getSorting(),
  104.                 'sortOrder'            => $result->getSortOrder(),
  105.                 'dateAdded'            => date_format($result->getDateAdded(),"d-M h:i A"),
  106.                 'articleCount'         => $this->getArticlesCountByCategory($result->getId()),
  107.                 'solutions'            => ($categories ? [] : $this->getSolutionsByCategory($result->getId())),
  108.             );
  109.         }
  110.         $paginationData $results->getPaginationData();
  111.         $queryParameters $results->getParams();
  112.         unset($queryParameters['solution']);
  113.         $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  114.         $json['results'] = $newResult;
  115.         $json['pagination_data'] = $paginationData;
  116.         return $json;
  117.     }
  118.     public function findCategoryById($filterArray = [])
  119.     {
  120.         $json = array();
  121.         $qb $this->getEntityManager()->createQueryBuilder();
  122.         $qb->select('a')->from($this->getEntityName(), 'a');
  123.         foreach ($filterArray as $key => $value) {
  124.             $qb->Andwhere('a.'.$key.' = :'.$key);
  125.             $qb->setParameter($key$value);
  126.         }
  127.         return $qb->getQuery()->getOneOrNullResult();
  128.        
  129.     }
  130.     public function getArticlesCountByCategory($categoryId$status 1)
  131.     {
  132.         $qbS $this->createQueryBuilder('a');
  133.         $result $qbS->select('COUNT(DISTINCT ac.id)')
  134.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\ArticleCategory','ac','WITH''ac.categoryId = a.id')
  135.             ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Article','aA','WITH''ac.articleId = aA.id')
  136.             ->andwhere('ac.categoryId = :categoryId')
  137.             ->andwhere('aA.status IN (:status)')
  138.             ->setParameters([
  139.                 'categoryId' => $categoryId ,
  140.                 'status' => $status ,
  141.             ])
  142.             ->getQuery()
  143.             ->getSingleScalarResult();
  144.  
  145.         return $result;
  146.     }
  147.     public function getSolutionsByCategory($categoryId)
  148.     {
  149.         $queryBuilder $this->createQueryBuilder('a');
  150.         $results $queryBuilder->select('s.id, s.name')
  151.                  ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','ac','WITH''ac.categoryId = a.id')
  152.                  ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Solutions','s','WITH''ac.solutionId = s.id')
  153.                  ->andwhere('ac.categoryId = :categoryId')
  154.                  ->setParameters([
  155.                      'categoryId' => $categoryId
  156.                  ])
  157.                  ->getQuery()
  158.                  ->getResult()
  159.         ;
  160.         return $results;
  161.     }
  162.     public function getArticlesByCategory($categoryId)
  163.     {
  164.         $queryBuilder $this->createQueryBuilder('sc');
  165.         $results $queryBuilder->select('a.id, a.name, a.slug')
  166.                  ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\ArticleCategery','ac','WITH''ac.categoryId = sc.id')
  167.                  ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Article','a','WITH''ac.id = a.id')
  168.                  ->andwhere('ac.categoryId = :categoryId')
  169.                  ->setParameters([
  170.                      'categoryId' => $categoryId
  171.                  ])
  172.                  ->getQuery()
  173.                  ->getResult()
  174.         ;
  175.         return $results;
  176.     }
  177.     public function removeSolutionsByCategory($categoryId$solutionId)
  178.     {
  179.         $queryBuilder $this->createQueryBuilder('ac');
  180.         $queryBuilder->delete(SupportEntites\SolutionCategoryMapping::class,'ac')
  181.                  ->andwhere('ac.categoryId = :categoryId')
  182.                  ->andwhere('ac.solutionId IN (:solutionId)')
  183.                  ->setParameters([
  184.                      'categoryId' => $categoryId ,
  185.                      'solutionId' => $solutionId ,
  186.                  ])
  187.                  ->getQuery()
  188.                  ->execute()
  189.         ;
  190.     }
  191.     public function removeEntryByCategory($categoryId)
  192.     {
  193.         $where is_array($categoryId) ? 'ac.categoryId IN (:categoryId)' 'ac.categoryId = :categoryId';
  194.         $queryBuilder $this->createQueryBuilder('ac');
  195.         $queryBuilder->delete(SupportEntites\SolutionCategoryMapping::class,'ac')
  196.                  ->andwhere($where)
  197.                  ->setParameters([
  198.                      'categoryId' => $categoryId ,
  199.                  ])
  200.                  ->getQuery()
  201.                  ->execute()
  202.         ;
  203.         $queryBuilder->delete(SupportEntites\ArticleCategory::class,'ac')
  204.                  ->andwhere($where)
  205.                  ->setParameters([
  206.                      'categoryId' => $categoryId ,
  207.                  ])
  208.                  ->getQuery()
  209.                  ->execute()
  210.         ;
  211.     }
  212.     public function bulkCategoryStatusUpdate($categoryIds$status)
  213.     {
  214.         $query 'UPDATE Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory sc SET sc.status = '. (int)$status .' WHERE sc.id IN ('.implode(','$categoryIds).')';
  215.         $this->getEntityManager()->createQuery($query)->execute();
  216.     }
  217.     public function categorySortingUpdate($id$sort)
  218.     {
  219.         $query "UPDATE Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory sc SET sc.sortOrder = '". (int)$sort ."' WHERE sc.id = '". (int)$id ."'";
  220.         $this->getEntityManager()->createQuery($query)->execute();
  221.     }
  222. }