src/Services/OrderFilter.php line 189

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Company;
  4. use App\Entity\Product;
  5. use App\Entity\User;
  6. use App\Exception\FulltextSearchNullResults;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use PhpParser\Builder;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. class OrderFilter
  16. {
  17.     const DATE_LOWER 1;
  18.     const DATE_HIGHER 2;
  19.     /**
  20.      * @var ManagerRegistry
  21.      */
  22.     protected $em;
  23.     public function __construct(
  24.         ManagerRegistry $registry
  25.     )
  26.     {
  27.         $this->em $registry;
  28.     }
  29.     public function storePaginationSettings(Session $sessionRequest $request)
  30.     {
  31.         $getOrder $request->get('ordering'null);
  32.         if ($getOrder != null && ($getOrder == 'asc' || $getOrder == 'desc')) {
  33.             $session->set('orderOrdering'$request->get('ordering'));
  34.         }
  35.         if ($request->get('pageSize'null) != null) {
  36.             $session->set('pageSize', (int)$request->get('pageSize'));
  37.         }
  38.         return $this->loadPaginationSettings($session);
  39.     }
  40.     /**
  41.      * @param Session $session
  42.      * @return array
  43.      */
  44.     public function loadPaginationSettings(Session $session)
  45.     {
  46.         return [
  47.             'ordering' => $session->get('orderOrdering''desc'),
  48.             'pageSize' => $session->get('pageSize'50)
  49.         ];
  50.     }
  51.     public function loadPrefilledData(Session $sessionEntityManagerInterface $entityManager)
  52.     {
  53.         // predvyplneni formulare filtrace
  54.         $prefDataCompany $entityManager->getRepository(Company::class)->findBy([
  55.             'id' => $session->get('filter-order-company'null)
  56.         ]);
  57.         $prefDataMerchant $entityManager->getRepository(User::class)->findBy([
  58.             'id' => $session->get('filter-order-merchant'null)
  59.         ]);
  60.         $prefDataProduct $entityManager->getRepository(Product::class)->findBy([
  61.             'id' => $session->get('filter-order-product'null)
  62.         ]);
  63.         return [
  64.             "company" => (array)$prefDataCompany,
  65.             "merchant" => (array)$prefDataMerchant,
  66.             "product" => (array)$prefDataProduct
  67.         ];
  68.     }
  69.     /**
  70.      * Ulozi formular se do session
  71.      *
  72.      * @param FormInterface $formFilter
  73.      * @param Request $request
  74.      * @param Session $session
  75.      * @return void
  76.      */
  77.     public function storeFilter(FormInterface $formFilterRequest $requestSession $session)
  78.     {
  79.         $formFilter->handleRequest($request);
  80.         if ($formFilter->isSubmitted() && $formFilter->isValid()) {
  81.             $session->set('fulltext'$formFilter->get('fulltext')->getData());
  82.             try {
  83.                 $companyData $formFilter->get('company')->getData();
  84.                 if (isset($companyData)) {
  85.                     $cIds array_map(function ($item) {
  86.                         return $item->getId();
  87.                     }, (array)$companyData);
  88.                     $session->set('filter-order-company'$cIds);
  89.                 }
  90.             }catch (\OutOfBoundsException $exception){
  91.                 // company neexistuje - nejsi admin
  92.             }
  93.             try{
  94.                 $merchantData $formFilter->get('merchant')->getData();
  95.                 if (isset($merchantData)) {
  96.                     $mIds array_map(function ($item) {
  97.                         return $item->getId();
  98.                     }, (array)$merchantData);
  99.                     $session->set('filter-order-merchant'$mIds);
  100.                 }
  101.             }catch (\OutOfBoundsException $e){
  102.                 // merchant neexistuje
  103.             }
  104.             try {
  105.                 $productData $formFilter->get('product')->getData();
  106.                 if (isset($productData)) {
  107.                     $pIds array_map(function ($item) {
  108.                         return $item->getId();
  109.                     }, (array)$productData);
  110.                     $session->set('filter-order-product'$pIds);
  111.                 }
  112.             }catch (\OutOfBoundsException $exception){
  113.                 // kdyby nekdy v budoucnu :D
  114.             }
  115.             $this->storeDateComponent($formFilter$session'inProgress');
  116.             $this->storeDateComponent($formFilter$session'sent');
  117.             $this->storeDateComponent($formFilter$session'paid');
  118.             $this->storeDateComponent($formFilter$session'paid2');
  119.             $this->storeDateComponent($formFilter$session'signed');
  120.             $this->storeDateComponent($formFilter$session'vpDelivery');
  121.             // reinvest filtr
  122.             $session->remove('filter-order-reinvest');
  123.             $dateField $formFilter->get('reinvest')->getData();
  124.             if (isset($dateField)) {
  125.                 $session->set('filter-order-reinvest', (bool)$dateField);
  126.             }
  127.         }
  128.     }
  129.     /**
  130.      * Uklada selectbox i datumove polozky
  131.      *
  132.      * @param FormInterface $formFilter
  133.      * @param Session $session
  134.      * @param $dateFieldName
  135.      * @return void
  136.      */
  137.     protected function storeDateComponent(FormInterface $formFilterSession $session$dateFieldName)
  138.     {
  139.         if($formFilter->offsetExists($dateFieldName) == false){
  140.             return false;
  141.         }
  142.         $session->remove('filter-order-' $dateFieldName);
  143.         $session->remove('filter-order-' $dateFieldName '_from');
  144.         $session->remove('filter-order-' $dateFieldName '_to');
  145.         $dateField $formFilter->get($dateFieldName)->getData();
  146.         if (isset($dateField)) {
  147.             $session->set('filter-order-' $dateFieldName$dateField);
  148.         }
  149.         $dateFieldFrom $formFilter->get($dateFieldName '_from')->getData();
  150.         if (isset($dateFieldFrom)) {
  151.             $session->set('filter-order-' $dateFieldName '_from'$dateFieldFrom);
  152.         }
  153.         $dateFieldTo $formFilter->get($dateFieldName '_to')->getData();
  154.         if (isset($dateFieldTo)) {
  155.             $session->set('filter-order-' $dateFieldName '_to'$dateFieldTo);
  156.         }
  157.     }
  158.     /**
  159.      * Aplikuje filtr na query builder + strankovani a vrati list objednavek
  160.      *
  161.      * @param Session $session
  162.      * @param QueryBuilder $queryBuilder
  163.      */
  164.     public function applyFilter(Session $sessionRequest $requestQueryBuilder $queryBuilder, ?PaginatorInterface $paginator null)
  165.     {
  166.         // fulltext
  167.         $fv $session->get('fulltext''');
  168.         $fvepl explode(' '$fv);
  169.         $fvimpl implode('|'$fvepl);
  170.         if($fv != ''){
  171.             $sql 'SELECT o.id FROM `order` o
  172.                 LEFT JOIN company c ON (o.company_id = c.id)
  173.                 LEFT JOIN product p ON (o.product_id = p.id)
  174.                 LEFT JOIN user m ON (o.merchant_id = m.id)            
  175.             WHERE
  176.                o.id = :id 
  177.                OR REGEXP_LIKE(o.name, :fvimpl)
  178.                OR REGEXP_LIKE(o.surname, :fvimpl)
  179.                OR REGEXP_LIKE(o.merchant_code, :fvimpl)
  180.                OR REGEXP_LIKE(o.street, :fvimpl)
  181.                OR REGEXP_LIKE(o.town, :fvimpl)
  182.                OR REGEXP_LIKE(o.postal, :fvimpl)
  183.                OR REGEXP_LIKE(o.street2, :fvimpl)
  184.                OR REGEXP_LIKE(o.town2, :fvimpl)
  185.                OR REGEXP_LIKE(o.postal2, :fvimpl)
  186.                OR REGEXP_LIKE(o.ic, :fvimpl)
  187.                OR REGEXP_LIKE(o.phone, :fvimpl)
  188.                OR REGEXP_LIKE(o.email, :fvimpl)
  189.                OR REGEXP_LIKE(o.bank_account, :fvimpl)
  190.                OR REGEXP_LIKE(o.swift, :fvimpl)
  191.                OR REGEXP_LIKE(o.iban, :fvimpl)
  192.                OR REGEXP_LIKE(o.message, :fvimpl)
  193.                OR REGEXP_LIKE(p.name, :fvimpl)
  194.                OR REGEXP_LIKE(m.name, :fvimpl)
  195.                OR REGEXP_LIKE(m.surname, :fvimpl)
  196.                OR REGEXP_LIKE(m.code, :fvimpl)
  197.            ';
  198.             $stmt $queryBuilder->getEntityManager()->getConnection()->prepare($sql);
  199.             $result $stmt->executeQuery(['fvimpl' => $fvimpl'id' => (int)$fv]);
  200.             $resultFetched $result->fetchAllAssociative();
  201.             $ids array_map(function($item){
  202.                 return $item['id'];
  203.             }, $resultFetched);
  204.             if(count($ids) == 0) {
  205.                 throw new FulltextSearchNullResults('');
  206.             }
  207.             $queryBuilder->andWhere($queryBuilder->expr()->in('o.id'$ids));
  208.         }
  209.         // endfulltext
  210.         $paginationSettings $this->loadPaginationSettings($session);
  211.         $filterOrderCompany $session->get('filter-order-company'null);
  212.         if (is_array($filterOrderCompany) && count($filterOrderCompany) > 0) {
  213.             $andWhere $queryBuilder->expr()->in('o.company'$filterOrderCompany);
  214.             $queryBuilder->andWhere($andWhere);
  215.         }
  216.         $filterOrderMerchant $session->get('filter-order-merchant'null);
  217.         if (is_array($filterOrderMerchant) && count($filterOrderMerchant) > 0) {
  218.             $andWhere $queryBuilder->expr()->in('o.merchant'$filterOrderMerchant);
  219.             $queryBuilder->andWhere($andWhere);
  220.         }
  221.         $filterOrderProduct $session->get('filter-order-product'null);
  222.         if (is_array($filterOrderProduct) && count($filterOrderProduct) > 0) {
  223.             $andWhere $queryBuilder->expr()->in('o.product'$filterOrderProduct);
  224.             $queryBuilder->andWhere($andWhere);
  225.         }
  226.         $this->applyFilterBool($queryBuilder$session'filter-order-sent''sent');
  227.         $this->applyFilterBool($queryBuilder$session'filter-order-inProgress''inProgress');
  228.         $this->applyFilterBool($queryBuilder$session'filter-order-paid''paid');
  229.         $this->applyFilterBool($queryBuilder$session'filter-order-paid2''paid2');
  230.         $this->applyFilterBool($queryBuilder$session'filter-order-signed''signed');
  231.         $this->applyFilterBool($queryBuilder$session'filter-order-vpDelivery''vpDelivery');
  232.         $this->applyFilterBool($queryBuilder$session'filter-order-reinvest''reinvest');
  233.         $this->applyFilterDate($queryBuilder$session'filter-order-inProgress_from''inProgressDate'self::DATE_HIGHER);
  234.         $this->applyFilterDate($queryBuilder$session'filter-order-inProgress_to''inProgressDate'self::DATE_LOWER);
  235.         $this->applyFilterDate($queryBuilder$session'filter-order-signed_from''signed_date'self::DATE_HIGHER);
  236.         $this->applyFilterDate($queryBuilder$session'filter-order-signed_to''signed_date'self::DATE_LOWER);
  237.         $this->applyFilterDate($queryBuilder$session'filter-order-vpDelivery_from''vpDelivery_date'self::DATE_HIGHER);
  238.         $this->applyFilterDate($queryBuilder$session'filter-order-vpDelivery_to''vpDelivery_date'self::DATE_LOWER);
  239.         $this->applyFilterDate($queryBuilder$session'filter-order-paid_from''paid_date'self::DATE_HIGHER);
  240.         $this->applyFilterDate($queryBuilder$session'filter-order-paid_to''paid_date'self::DATE_LOWER);
  241.         $this->applyFilterDate($queryBuilder$session'filter-order-paid2_from''paidDate2'self::DATE_HIGHER);
  242.         $this->applyFilterDate($queryBuilder$session'filter-order-paid2_to''paidDate2'self::DATE_LOWER);
  243.         $this->applyFilterDate($queryBuilder$session'filter-order-sent_from''sent_date'self::DATE_HIGHER);
  244.         $this->applyFilterDate($queryBuilder$session'filter-order-sent_to''sent_date'self::DATE_LOWER);
  245.         $queryBuilder->orderBy('o.id'$paginationSettings['ordering']);
  246.         if($paginator != false) {
  247.             return $paginator->paginate(
  248.                 $queryBuilder/* query NOT result */
  249.                 (false == is_null($paginator) ? $request->query->getInt('page'1) : null/*page number*/,
  250.                 (false == is_null($paginator) ? $paginationSettings['pageSize'] : null/*limit per page*/
  251.             );
  252.         }
  253.         return $queryBuilder;
  254.     }
  255.     protected function applyFilterDate(QueryBuilder $builderSession $session$fieldName$tableCol$cond)
  256.     {
  257.         /** @var \DateTime $filter */
  258.         $filter $session->get($fieldName"");
  259.         if ($filter != "") {
  260.             $filter $filter->format('Y-m-d');
  261.             $lit $builder->expr()->literal($filter);
  262.             if ($cond == self::DATE_LOWER) {
  263.                 $andWhere $builder->expr()->lte('o.' $tableCol$lit);
  264.                 $builder->andWhere($andWhere);
  265.             } else {
  266.                 $andWhere $builder->expr()->gte('o.' $tableCol$lit);
  267.                 $builder->andWhere($andWhere);
  268.             }
  269.         }
  270.     }
  271.     protected function applyFilterBool(QueryBuilder $builderSession $session$fieldName$tableCol)
  272.     {
  273.         $filter $session->get($fieldName"");
  274.         if ($filter !== "") {
  275.             if((int)$filter == 1) {
  276.                 $andWhere $builder->expr()->eq('o.' $tableCol, (int)$filter);
  277.             }else{
  278.                 $or1 $builder->expr()->eq('o.' $tableCol$builder->expr()->literal('false'));
  279.                 $or2 $builder->expr()->isNull('o.' $tableCol);
  280.                 $andWhere $builder->expr()->orX()->add($or1)->add($or2);
  281.             }
  282.             $builder->andWhere($andWhere);
  283.         }
  284.     }
  285.     public function clearFilter(Session $sessionRequest $request)
  286.     {
  287.         try {
  288.             if ($request->get('clear'null) == "1") {
  289.                 $session->remove('filter-order-company');
  290.                 $session->remove('filter-order-merchant');
  291.                 $session->remove('filter-order-product');
  292.                 $session->remove('filter-order-sent');
  293.                 $session->remove('filter-order-sent_from');
  294.                 $session->remove('filter-order-sent_to');
  295.                 $session->remove('filter-order-inProgress');
  296.                 $session->remove('filter-order-inProgress_from');
  297.                 $session->remove('filter-order-inProgress_to');
  298.                 $session->remove('filter-order-paid');
  299.                 $session->remove('filter-order-paid_from');
  300.                 $session->remove('filter-order-paid_to');
  301.                 $session->remove('filter-order-paid2');
  302.                 $session->remove('filter-order-paid2_from');
  303.                 $session->remove('filter-order-paid2_to');
  304.                 $session->remove('filter-order-signed');
  305.                 $session->remove('filter-order-signed_from');
  306.                 $session->remove('filter-order-signed_to');
  307.                 $session->remove('filter-order-vpDelivery');
  308.                 $session->remove('filter-order-vpDelivery_from');
  309.                 $session->remove('filter-order-vpDelivery_to');
  310.                 $session->remove('filter-order-reinvest');
  311.                 $session->remove('fulltext');
  312.                 return true;
  313.             }
  314.         } catch (\Exception $e) {
  315.             return false;
  316.         }
  317.     }
  318. }