src/Controller/API/SpecializedController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Pimcore\Translation\Translator;
  9. use App\Validator\Validator;
  10. use Pimcore\Model\DataObject\Solution;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  14. use App\Services\SolutionServices;
  15. use Pimcore\Model\DataObject\Category;
  16. use Pimcore\Model\DataObject\FilterDefinition;
  17. use App\Helper\ObjectJson;
  18. use Pimcore\Model\DataObject\Request as ObjectRequest;
  19. use Pimcore\Model\DataObject\Review;
  20. use Pimcore\Model\Asset\Service as AssetService;
  21. use Pimcore\Model\DataObject\Service as DataObjectService;
  22. use Pimcore\Tool;
  23. use App\Services\VoucherService;
  24. /**
  25.  * @Route("/api")
  26.  */
  27. class SpecializedController extends BaseController
  28. {
  29.     const SOLUTION_PAGE_DEFAULT 1;
  30.     const SOLUTION_PERPAGE_DEFAULT 10;
  31.     const SOLUTION_CLASS_NAME 'Solution';
  32.     private $request;
  33.     private $validator;
  34.     private $translator;
  35.     public function __construct(RequestStack $requestStackValidator $validatorTranslator $translator)
  36.     {
  37.         $this->request $requestStack->getCurrentRequest();
  38.         $this->validator $validator;
  39.         $this->translator $translator;
  40.     }
  41.     /**
  42.      * @Route("/specialized", name="api-specialized", methods={"GET"})
  43.      */
  44.     public function listing(Request $request,ListHelper $listHelper,PaginatorInterface $paginator
  45.     {   
  46.        
  47.         if($this->request->get('star')){
  48.             $star $this->request->get('star')*2;
  49.         }
  50.         $messageError $this->validator->validate([
  51.             'categories'=>'required',
  52.             'page' => $this->request->get('page')?'numeric|positive':'',
  53.             'limit' => $this->request->get('limit')?'numeric|positive':''
  54.         ], $request);
  55.         if($messageError) return $this->sendError($this->translator->trans($messageError));
  56.         $params array_merge($request->query->all(), $request->attributes->all());
  57.         $solutions Factory::getInstance();
  58.         $indexService $solutions->getIndexService();
  59.         $solutionsListing $indexService->getProductListForCurrentTenant();
  60.         $solutionsListing->addCondition("o_classId ='solution'");
  61.         if(!empty($this->request->get('search'))) $solutionsListing->addCondition("text LIKE '%".$this->request->get('search')."%'");
  62.         if(!empty($this->request->get('email'))) $solutionsListing->addCondition("email LIKE '%".$this->request->get('email')."%'");
  63.         if(!empty($this->request->get('star'))) $solutionsListing->addCondition("totalPoint =".$star." OR totalPoint =".$star-1);
  64.         
  65.         $defaultOrderBy $this->request->get('order_key') ?? 'text';
  66.         $defaultOrder $this->request->get('order') ?? 'desc';
  67.         $njOrderKey $defaultOrderBy .' '$defaultOrder;
  68.         $solutionsListing->setOrderKey($njOrderKeyfalse);
  69.       
  70.         $parentCategoryId = (int) $request->get('parentCategoryIds');
  71.         $parentCategory Category::getById($parentCategoryId);
  72.         $rootCategory Category::getByRoot(true1);
  73.         $category $parentCategory ?? $rootCategory;
  74.       
  75.         //get filter definition from document, category or global settings
  76.         $filterDefinition $category->getFilter() ? $category->getFilter() : ($category->getParent() instanceof Category $category->getParent()->getFilter() : Category::getById($parentCategoryId)->getFilter()); //TODO ...get from somewhere;
  77.         $filters = [];
  78.         if($filterDefinition instanceof FilterDefinition){
  79.           
  80.             $filterList $filterDefinition->getFilters();
  81.             $messageError $this->validator->validate(SolutionServices::validatorFilterDefinition($filterList$request), $request);
  82.             if($messageError) return $this->sendError($this->translator->trans($messageError));
  83.             // create and init filter service
  84.             $filterService $solutions->getFilterService();
  85.             $listHelper->setupProductList($filterDefinition$solutionsListing$params$filterServicetrue);
  86.             $filters SolutionServices::getFilterDefinition($filterList$filterService$solutionsListing$params$this->translator);
  87.         }
  88.         // fix cứng star đổ ra filter
  89.         $starFilter = [
  90.             "label" => $this->translator->trans('filter.star-rating'),
  91.             "type" => "FilterMultiSelectStar",
  92.             "fieldname" => "star",
  93.             "currentValue" => [
  94.                 $request->get('star')
  95.             ], 
  96.             // check từ request để lấy giá trị hiện tại
  97.             "data" => [
  98.                 [
  99.                     "name" => "5",
  100.                     "value" => "5",
  101.                 ],
  102.                 [
  103.                     "name" => "4",
  104.                     "value" => "4",
  105.                 ],
  106.                 [
  107.                     "name" => "3",
  108.                     "value" => "3",
  109.                 ],
  110.                 [
  111.                     "name" => "2",
  112.                     "value" => "2",
  113.                 ],
  114.                 [
  115.                     "name" => "1",
  116.                     "value" => "1",
  117.                 ],
  118.             ]
  119.                   
  120.         ];
  121.         $filters[] = $starFilter;
  122.         $paginator $paginator->paginate(
  123.             $solutionsListing,
  124.             $request->get('page'self::SOLUTION_PAGE_DEFAULT),
  125.             $request->get('limit'self::SOLUTION_PERPAGE_DEFAULT),
  126.         );
  127.         $data = [];
  128.         $hiddenFields = ['faq','features','pricing','documents','video','community'];
  129.         $solutionIdArr = [];
  130.         foreach ($paginator as $solution) {
  131.             $dataJson ObjectJson::getJson($solution$hiddenFields);
  132.             $solutionIdArr[] = $dataJson['id'];
  133.             array_push($data$dataJson);
  134.         }
  135.         $data VoucherService::getVoucherSolution($solutionIdArr$data);
  136.         
  137.         return $this->sendResponse([
  138.             'data' => $data,
  139.             'paginator' => $paginator->getPaginationData(),
  140.             'filter' => $filters
  141.         ]);
  142.     }
  143. }