src/Controller/API/SolutionController.php line 93

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 Pimcore\Model\DataObject\News;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  14. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  15. use App\Services\SolutionServices;
  16. use Pimcore\Model\DataObject\Category;
  17. use Pimcore\Model\DataObject\FilterDefinition;
  18. use App\Helper\ObjectJson;
  19. use App\Services\AuthServices;
  20. use Pimcore\Model\DataObject\Request as ObjectRequest;
  21. use Pimcore\Model\DataObject\Review;
  22. use Pimcore\Model\Asset\Service as AssetService;
  23. use Pimcore\Model\DataObject\Service as DataObjectService;
  24. use Pimcore\Tool;
  25. use App\Services\ReviewServices;
  26. use Pimcore\Model\DataObject\FAQ;
  27. use App\Services\VoucherService;
  28. /**
  29.  * @Route("/api")
  30.  */
  31. class SolutionController extends BaseController
  32. {
  33.     const SOLUTION_PAGE_DEFAULT 1;
  34.     const SOLUTION_PERPAGE_DEFAULT 10;
  35.     const SOLUTION_CLASS_NAME 'Solution';
  36.     private $request;
  37.     private $validator;
  38.     private $translator;
  39.     public function __construct(RequestStack $requestStackValidator $validatorTranslator $translator)
  40.     {
  41.         $this->request $requestStack->getCurrentRequest();
  42.         $this->validator $validator;
  43.         $this->translator $translator;
  44.     }
  45.     /**
  46.      * @Route("/solutions", name="api-solutions", methods={"GET"})
  47.      */
  48.     public function listing(Request $request,ListHelper $listHelper,PaginatorInterface $paginator)
  49.     {
  50.         $messageError $this->validator->validate([
  51.             'page' => $this->request->get('page') ? 'numeric|positive' '',
  52.             'limit' => $this->request->get('limit') ? 'numeric|positive' ''
  53.         ], $request);
  54.         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  55.         $params array_merge($request->query->all(), $request->attributes->all());
  56.         $solutions Factory::getInstance();
  57.         $indexService $solutions->getIndexService();
  58.         $solutionsListing $indexService->getProductListForCurrentTenant();
  59.         $solutionsListing->addCondition("o_classId ='solution'");
  60.         if (!empty($this->request->get('search'))) 
  61.             $solutionsListing->addCondition("text LIKE '%" $this->request->get('search') . "%'");
  62.         if (!empty($this->request->get('email'))) 
  63.             $solutionsListing->addCondition("email LIKE '%" $this->request->get('email') . "%'");
  64.         if (!empty($this->request->get('provider')) && $this->request->get('type') == "hiddenFilter"
  65.             $solutionsListing->addRelationCondition('provider'"dest IN (" $this->request->get('provider') . ")");
  66.         $defaultOrderBy $request->get('order_by') ?? 'text';
  67.         $defaultOrder $request->get('order') ?? 'desc';
  68.         $njOrderKey $defaultOrderBy ' ' $defaultOrder;
  69.         $solutionsListing->setOrderKey($njOrderKeyfalse);
  70.         $parentCategoryId = (int) $request->get('parentCategoryIds');
  71.         $parentCategory Category::getById($parentCategoryId);
  72.         $rootCategory Category::getByRoot(true1);
  73.         $category $parentCategory ?? $rootCategory;
  74.         //get filter definition from document, category or global settings
  75.         $filterDefinition $category->getFilter() ?? ($category->getParent() instanceof Category $category->getParent()->getFilter() : Category::getByRoot(true1)->getFilter());
  76.         $filters = [];
  77.         if ($this->request->get('type') != "hiddenFilter") {
  78.             if ($filterDefinition instanceof FilterDefinition) {
  79.                 $filterList $filterDefinition->getFilters();
  80.                 $messageError $this->validator->validate(SolutionServices::validatorFilterDefinition($filterList$request), $request);
  81.                 if ($messageError) return $this->sendError($this->translator->trans($messageError));
  82.                 // create and init filter service
  83.                 $filterService $solutions->getFilterService();
  84.                 $listHelper->setupProductList($filterDefinition$solutionsListing$params$filterServicetrue);
  85.                 $filters SolutionServices::getFilterDefinition($filterList$filterService$solutionsListing$params$this->translator);
  86.             }
  87.         }
  88.         $paginator $paginator->paginate(
  89.             $solutionsListing,
  90.             $request->get('page'self::SOLUTION_PAGE_DEFAULT),
  91.             $request->get('limit'self::SOLUTION_PERPAGE_DEFAULT),
  92.         );
  93.         $data = [];
  94.         $hiddenFields = [
  95.             'code''community''isPriority''segment''photos''video''documents''pricing',
  96.             'features''deployment''interactive''support''training''freeTrial''freeTrialDays''freeVersion''action''banner'
  97.         ];
  98.         $solutionIdArr = [];
  99.         foreach ($paginator as $solution) {
  100.             $dataJson ObjectJson::getJson($solution$hiddenFields);
  101.             $solutionIdArr[] = $dataJson['id'];
  102.             $dataJson['totalScore'] = ReviewServices::totalStar($dataJson['id']);
  103.             array_push($data$dataJson);
  104.         }
  105.         $data VoucherService::getVoucherSolution($solutionIdArr$data);
  106.         return $this->sendResponse([
  107.             'data' => $data,
  108.             'paginator' => $paginator->getPaginationData(),
  109.             'filter' => $filters
  110.         ]);
  111.     }
  112.     /**
  113.      * @Route("/solution/{id}", requirements={"id"="\d+"}, methods={"GET"})
  114.      */
  115.     public function detailAction($idRequest $request)
  116.     {
  117.         $messageError $this->validator->validate([
  118.             'id' => 'required',
  119.            
  120.         ], $request);
  121.         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  122.         
  123.         $solution Solution::getById($id);
  124.         if($solution){
  125.             $liked false;
  126.             $followed false;
  127.             if($request->headers->has('Authorization')){
  128.                 $token  $request->headers->get('Authorization');
  129.                 $user   AuthServices::getUserByToken($token);
  130.                 if ($user) {
  131.                     foreach ($user->getLikeSolution() as $solutions) {
  132.                         if ($solutions->getId() == $id) {
  133.                             $liked true;
  134.                             break;
  135.                         }
  136.                     }
  137.                     foreach ($user->getFollowSolution() as $solutions) {
  138.                         if ($solutions->getId() == $id) {
  139.                             $followed true;
  140.                             break;
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.             
  146.             $solution->setTotalView($solution->getTotalView()+1);
  147.             if($solution->save()){
  148.                 $detail $solution ObjectJson::getJson($solution,['features']) : [];
  149.                 
  150.                 // get features solution
  151.                 $data=[];
  152.                 $i=0;
  153.                 $hiden=['category','parent_id','code'];
  154.                 foreach($solution->getFeatures() as $key=>$value){
  155.                     if($value->getCategory()){
  156.                         $data[$value->getId()]=ObjectJson::getJson($value,$hiden);
  157.                         $i++;
  158.                     }
  159.                 }
  160.               
  161.                 foreach($solution->getFeatures() as $value){
  162.                     if(!$value->getCategory()){
  163.                         if (array_key_exists($value->getParent()->getId(), $data)) {
  164.                             $data[$value->getParent()->getId()]['children'][] =ObjectJson::getJson($value,$hiden);
  165.                         }
  166.                     }
  167.                 }
  168.                 $datafeatures=[];
  169.                 $i=0;
  170.                 foreach($data as $key=>$value){
  171.                     $datafeatures[]=$value;
  172.                     $i++;
  173.                 }
  174.                 $faq FAQ::getBySolution($solution);
  175.                 $detail['totalFAQ'] = $faq->count();
  176.                 
  177.                 $detail['features']= $datafeatures;
  178.                 $detail['totalScore']=ReviewServices::totalStar($detail['id']);
  179.                 $detail['liked'] = $liked;
  180.                 $detail['followed'] = $followed;
  181.                 $detail VoucherService::getVoucherSolution([$detail['id']], [$detail]);
  182.                 return $this->sendResponse(['data' => $detail[0]]);
  183.             }
  184.         }
  185.         
  186.         return $this->sendError($this->translator->trans('solution.not.exist'));
  187.     }
  188.     // /**
  189.     //  * @Route("/solution/review", name="api-solution-review-listing", methods={"GET"})
  190.     //  */
  191.     // public function review(Request $request, PaginatorInterface $paginator){
  192.     //     $data = [];
  193.     //     $response= [];
  194.     //     try {
  195.     //         $page = $request->get('page');
  196.     //         $limit = $request->get('limit');
  197.             
  198.     //         $order_by = $request->get('order_by');
  199.     //         $order = $request->get('order');
  200.     //         $star = $request->get('star')*2;
  201.     //         $messageError = $this->validator->validate([
  202.     //             'solution' => 'required',
  203.             
  204.     //             // 'role'=> 'choiceArray:technical_support,data_engineer,consultant,ceo,admin,normal_user',
  205.     //             // 'user'=> 'choiceArray:personal,business',
  206.     //             // 'star'=> 'choice:1,2,3,4,5',
  207.     //             'page' => $page ? 'numeric|positive' : '',
  208.     //             'limit' => $limit ? 'numeric|positive' : '',
  209.     //             'order_by' => $order_by ? 'choice:title' : '',
  210.     //             'order' => $order ? 'choice:desc,asc' : '',
  211.     //         ], $request);
  212.     //         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  213.     //         if(empty($page)) $page = 1;
  214.     //         if(empty($limit)) $limit = 10;
  215.     //         if(empty($order_by)) $order_by = 'title';
  216.     //         if(empty($order)) $order = 'desc';
  217.     //         $listing = new Review\Listing();
  218.     //         $listing->addConditionParam("solution__id =".$request->get('solution'));
  219.             
  220.     //         if(!empty($request->get('role'))&& is_array($request->get('role'))){
  221.     //             foreach ($request->get('role') as $key => $value) {
  222.     //                 if(!empty($value)) $listing->addConditionParam("role LIKE '%".$value."%'");
  223.     //             }
  224.     //         }
  225.            
  226.     //         if(!empty($request->get('star'))) $listing->addConditionParam("generalScore =".$star." OR generalScore =".$star-1);
  227.     //         if(!empty($request->get('checkImage'))) $listing->addConditionParam("checkImage =".$request->get('checkImage'));
  228.     //         if(!empty($request->get('user'))&& is_array($request->get('user'))){
  229.     //             foreach($request->get('user') as $value){
  230.     //                 if(!empty($value)) $listing->addConditionParam("purpose LIKE '%".$value."%'");
  231.     //             }
  232.     //         }
  233.            
  234.     //         if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
  235.     //         $listing->setOrderKey($order_by);
  236.     //         $listing->setOrder($order);
  237.           
  238.     //         if ($page && $limit) {
  239.     //             $pagination = $paginator->paginate(
  240.     //                 $listing,
  241.     //                 $page,
  242.     //                 $limit,
  243.     //             );
  244.     //         } 
  245.     //         foreach ($listing as $review) {
  246.     //             // $data[] = $news->getJson();
  247.     //             $dataJson = ObjectJson::getJson($review,['solution']);
  248.     //             array_push($data, $dataJson);
  249.     //         }
  250.             
  251.     //         $response['data'] = $data;
  252.     //         $response['paginator'] = $pagination->getPaginationData();
  253.     //     } catch (\Throwable $e) {
  254.             
  255.     //         return $this->sendError($e->getMessage(), 500);
  256.     //     }
  257.     //     return $this->sendResponse($response);
  258.     // }
  259.    
  260.     /**
  261.      * @Route("/solution/share", methods={"POST"})
  262.      */
  263.     public function share()
  264.     {
  265.         $messageError $this->validator->validate([
  266.             'solution' => 'required'
  267.         ], $this->request);
  268.         
  269.         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  270.         $solution Solution::getById($this->request->get("solution"));
  271.         if ($solution) {
  272.             $solution->setTotalShare($solution->getTotalShare() + 1);
  273.             $solution->save();
  274.             return $this->sendResponse([
  275.                 'message' => $this->translator->trans('solution.share.success'),
  276.                 'status'  => 1
  277.             ]);
  278.         }
  279.         return $this->sendResponse([
  280.             'message' => $this->translator->trans('solution.not.found'),
  281.             'status'  => 0
  282.         ]);
  283.     }
  284.     
  285.     /**
  286.      * @Route("/solution/relate-blog/{id}", methods={"GET"})
  287.      */
  288.     public function relateBlog(Request $req)
  289.     {
  290.         $messageError $this->validator->validate([
  291.             'id' => 'required',
  292.         ], $req);
  293.         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  294.         
  295.         $list = new News\Listing();
  296.         $list->addConditionParam("solution__id = ?"$req->get('id'));
  297.         $list->setLimit(3);
  298.         $list->setOrder('desc');
  299.         $list->setOrderKey('o_creationDate');
  300.         
  301.         $data = [];
  302.         foreach ($list as $news) {
  303.             $data[] = ObjectJson::getJson($news);
  304.         }
  305.         return $this->sendResponse($data);
  306.     }
  307. }