src/Controller/API/ReviewController.php line 225

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\Provider;
  11. use Pimcore\Model\DataObject\Reviewer;
  12. use Pimcore\Model\DataObject\Industry;
  13. use Pimcore\Model\DataObject\Features;
  14. use Pimcore\Model\DataObject\Solution;
  15. use App\Services\ReviewerServices;
  16. use App\Helper\ObjectJson;
  17. use App\Services\AuthServices;
  18. use Pimcore\Model\DataObject\Review;
  19. use Pimcore\Model\DataObject\Request as ObjectRequest;
  20. use Knp\Component\Pager\PaginatorInterface;
  21. use Pimcore\Model\Asset\Service as AssetService;
  22. use Pimcore\Model\DataObject\Service as DataObjectService;
  23. use Pimcore\Tool\Admin;
  24. use Pimcore\Model\Asset;
  25. use Pimcore\Model\DataObject;
  26. /**
  27.  * @Route("/api/review")
  28.  */
  29. class ReviewController extends BaseController
  30. {
  31.     const REVIEW_PAGE_DEFAULT 1;
  32.     const REVIEW_PERPAGE_DEFAULT 10;
  33.     private $request;
  34.     private $validator;
  35.     private $translator;
  36.     public function __construct(RequestStack $requestStackValidator $validatorTranslator $translator)
  37.     {
  38.         $this->request $requestStack->getCurrentRequest();
  39.         $this->validator $validator;
  40.         $this->translator $translator;
  41.     }
  42.     
  43.     /**
  44.      * @Route("/listing", name="api-review-listing", methods={"GET"})
  45.      */
  46.     public function listing(Request $requestPaginatorInterface $paginator){ 
  47.         $data = [];
  48.         $response= [];
  49.         try {
  50.             $user '';
  51.             if ($request->headers->has('Authorization')) {
  52.                 $token  $request->headers->get('Authorization');
  53.                 $result AuthServices::getUserByToken($token);
  54.                 if ($result && $result instanceof Reviewer) {
  55.                     $user $result;
  56.                 }
  57.             }
  58.             $order_by $request->get('order_by');
  59.             $order $request->get('order');
  60.             $star $request->get('star')*2;
  61.             
  62.             $messageError $this->validator->validate([
  63.                 'page' => $this->request->get('page') ? 'numeric|positive' '',
  64.                 'limit' => $this->request->get('limit') ? 'numeric|positive' '',
  65.                 'order_by' => $order_by 'choice:title' '',
  66.                 'order' => $order 'choice:desc,asc' ''
  67.             ], $request);
  68.             if ($messageError) return $this->sendError($this->translator->trans($messageError));
  69.             
  70.             if(empty($order_by)) $order_by 'o_creationDate';
  71.             if(empty($order)) $order 'desc';
  72.             $listing = new Review\Listing();
  73.             if ($user && $request->get('myReview') == 1) {
  74.                 $listing->addConditionParam("reviewer__id = ?"$user->getId());
  75.             }
  76.             if(!empty($request->get('solution'))) $listing->addConditionParam("solution__id = ?",$request->get('solution'));
  77.             if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
  78.           
  79.             if(!empty($request->get('role'))&& is_array($request->get('role'))){
  80.                 $roles $request->get('role');
  81.                 $roleConditions implode(" OR "array_fill(0count($roles), "role LIKE ?"));
  82.                 $roleParams array_map(function($role) {
  83.                     return '%,' $role ',%';
  84.                 }, $roles);
  85.                 
  86.                 $listing->addConditionParam($roleConditions,$roleParams);
  87.             }
  88.             if(!empty($request->get('star'))) $listing->addConditionParam("generalScore =".$star." OR generalScore =".$star-1);
  89.             if(!empty($request->get('checkImage'))) $listing->addConditionParam("checkImage =".$request->get('checkImage'));
  90.             if(!empty($request->get('user'))&& is_array($request->get('user'))){
  91.                 foreach($request->get('user') as $value){
  92.                     if(!empty($value)) $listing->addConditionParam("purpose LIKE '%".$value."%'");
  93.                 }
  94.             }
  95.            
  96.             if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
  97.             
  98.             // Lọc theo điều kiện Review xác thực
  99.             $ratingType $request->get('ratingType');
  100.             if(isset($ratingType)){
  101.                 if($request->get('ratingType') == 'all'){
  102.                     $listing->addConditionParam("accuracy = 1 OR accuracy = 0 OR accuracy is NULL");
  103.                 }else{
  104.                     $listing->addConditionParam("accuracy = ?",$request->get('ratingType'));
  105.                 }
  106.             }
  107.             // Lọc theo nội dung
  108.             $ratingContent $request->get('ratingContent');
  109.             if(isset($ratingContent)){
  110.                 if($request->get('ratingContent') == 'all'){
  111.                     $listing->addConditionParam("checkImage = 1 OR checkImage = 0 OR checkImage is NULL");
  112.                 }else{
  113.                     $listing->addConditionParam("checkImage = ?",$request->get('ratingContent'));
  114.                 }
  115.             }
  116.             
  117.             $listing->setOrderKey($order_by);
  118.             $listing->setOrder($order);
  119.           
  120.             $pagination $paginator->paginate(
  121.                 $listing,
  122.                 $request->get('page'self::REVIEW_PAGE_DEFAULT),
  123.                 $request->get('limit'self::REVIEW_PERPAGE_DEFAULT),
  124.             );
  125.             $likedReview = [];
  126.             if ($user instanceof Reviewer) {
  127.                 $likedReview $user->getLikeReview();
  128.             }
  129.             foreach ($listing as $review) {
  130.                 $dataJson ObjectJson::getJson($review);
  131.                 
  132.                 // Genarate new creationDate 
  133.                 if (isset($dataJson['creationDateRenew']) && $dataJson['creationDateRenew']) {
  134.                     $dataJson['creationDate'] = date('Y-m-d H:i:s'strtotime($dataJson['creationDateRenew']));
  135.                 }
  136.                 unset($dataJson['creationDateRenew']);
  137.                 $dataSolution=[
  138.                     'id'=>$dataJson['solution']['id'],
  139.                     'name'=>$dataJson['solution']['name'],
  140.                     'categories'=>$dataJson['solution']['categories'],
  141.                     'logo'=>$dataJson['solution']['logo']
  142.                 ];
  143.                 $dataJson['solution']=$dataSolution;
  144.                 $dataJson['liked'] = in_array($review$likedReview);
  145.                 array_push($data$dataJson);
  146.             }
  147.             
  148.             $response['data'] = $data;
  149.             $response['paginator'] = $pagination->getPaginationData();
  150.         } catch (\Throwable $e) {
  151.             
  152.             return $this->sendError($e->getMessage(), 500);
  153.         }
  154.         return $this->sendResponse($response);
  155.     }
  156.     /**
  157.      * @Route("/option-data", name="api-review-option-data", methods={"GET"})
  158.      */
  159.     public function reviewOption(Request $request){
  160.        
  161.         $messageError $this->validator->validate([
  162.             'solution' => 'required'
  163.          
  164.         ], $request);
  165.         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  166.         $solution Solution::getById($this->request->get('solution'));
  167.         
  168.         $data=[];
  169.         $review = new Review;
  170.         
  171.         $purpose DataObject\Service::getOptionsForSelectField($review"purpose");
  172.         $companySize DataObject\Service::getOptionsForSelectField($review"companySize");
  173.         $statusUsing DataObject\Service::getOptionsForSelectField($review"statusUsing");
  174.         $usedTime DataObject\Service::getOptionsForSelectField($review"usedTime");
  175.         $role DataObject\Service::getOptionsForSelectField($review"role");
  176.         $accuracy = [
  177.             "all" => "Tất cả đánh giá",
  178.             "1" => "Đánh giá tin cậy",
  179.             "0" => "Chưa xác thực"
  180.         ];
  181.         $ratingContent = [
  182.             "all" => "Tất cả",
  183.             "0" => "Chỉ có chữ",
  184.             "1" => "Có ảnh/video"
  185.         ];
  186.         $data = [
  187.             'purpose'=> $purpose,
  188.             'companySize'=> $companySize,
  189.             'statusUsing'=> $statusUsing,
  190.             'usedTime'=> $usedTime,
  191.             'role'=> $role,
  192.             'ratingType'=>$accuracy,
  193.             'ratingContent' => $ratingContent,
  194.             'features'=>[]
  195.         ];
  196.         // Lấy industry
  197.         $industry = new Industry\Listing();
  198.         foreach($industry as $value){
  199.             $data['industry'][] = ObjectJson::getJson($value);
  200.         }
  201.         if($solution){
  202.             // Lấy features
  203.             $hidenCat = ['totalPoint','category','parent_id','code'];
  204.             foreach($solution->getFeatures() as $key => $value){
  205.                 // dd($data['features']);
  206.                 if (array_key_exists($value->getParentId(), $data['features'])) {
  207.                     
  208.                     $data['features'][$value->getParentId()]['featureGroup'][] = ObjectJson::getJson($value,$hidenCat);
  209.                 } else {
  210.                     $data['features'][$value->getParentId()]=ObjectJson::getJson($value->getParent(),$hidenCat);
  211.                     $data['features'][$value->getParentId()]['featureGroup'][] = ObjectJson::getJson($value,$hidenCat);
  212.                 }
  213.             }
  214.             // Lấy pricing
  215.             foreach($solution->getPricing() as $key => $value){
  216.                 $data['pricing'][] = ObjectJson::getJson($value);
  217.             }
  218.         }else{
  219.             return $this->sendResponse([
  220.                 'message' => $this->translator->trans('solution.not.found'),
  221.                 'status'=> 0
  222.             ]);
  223.         }
  224.         return $this->sendResponse($data);
  225.     }
  226. }