src/Controller/API/CompareController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
  8. use Pimcore\Translation\Translator;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Pimcore\Model\DataObject\Solution;
  11. use Pimcore\Model\DataObject\Comment;
  12. use App\Validator\Validator;
  13. use App\Helper\ObjectJson;
  14. use Pimcore\Model\DataObject;
  15. use Pimcore\Model\DataObject\Feature;
  16. use Pimcore\Model\DataObject\Review;
  17. use Pimcore\Model\DataObject\FeaturePoints;
  18. use App\Services\SolutionServices;
  19. /**
  20.  * @Route("/api")
  21.  */
  22. class CompareController extends BaseController
  23. {
  24.     const COMMENT_PAGE_DEFAULT 1;
  25.     const COMMENT_PERPAGE_DEFAULT 10;
  26.     const CLASS_NAME 'solution';
  27.     private $validator;
  28.     private $translator;
  29.     private $request;
  30.     public function __construct(
  31.         Translator $translator,
  32.         Validator $validator,
  33.         RequestStack $requestStack 
  34.     )
  35.     {
  36.         $this->request $requestStack->getCurrentRequest();
  37.         $this->translator $translator;
  38.         $this->validator $validator;
  39.     }
  40.     
  41.     /**
  42.      * @Route("/compare", name="api-compare", methods={"GET"})
  43.      */
  44.     public function compare(Request $request,PaginatorInterface $paginator)
  45.     {   
  46.         $messageError $this->validator->validate([
  47.             'solution' => 'required',
  48.         ], $request);
  49.         if ($messageError) return $this->sendError($this->translator->trans($messageError));
  50.         // ẩn tất cả các thông tin không cần thiết của solution
  51.         $hidenSolution=['description','categories','website','community','action','segment','video',
  52.         'banner','documents','features','faq','interactive','training','support','freeVersion','freeTrialDays',
  53.         'freeTrial','isPriority','deployment'];
  54.         $data=[];
  55.         // Lấy Solution trang mảng truyển vào
  56.         foreach($this->request->get('solution') as $key => $value){
  57.             // get solution
  58.             $solution DataObject::getById($value);
  59.             // kiểm tra object xem có phải Class Solution
  60.             if($solution instanceof Solution){
  61.                 // tạo mảng lấy dữ liệu solution
  62.                 $data[$key] = ObjectJson::getJson($solution,$hidenSolution);
  63.                 $data[$key]['featuresPoint']=[];
  64.                 
  65.                 // Lấy điểm của Features
  66.                 foreach ($solution->getFeatures() as $k=> $item) {
  67.                    
  68.                         $featurePoints = new FeaturePoints\Listing();
  69.                         $featurePoints->addConditionParam('feature__id = ?',$item->getId());
  70.                         $featurePoints->addConditionParam('solution__id = ?',$solution->getId());
  71.     
  72.                         //Tính tổng điểm
  73.                         $point=0;
  74.                         foreach($featurePoints as $feature){
  75.                             $point+=$feature->getPoint();
  76.                         }
  77.                         $total $featurePoints->count() ? $point/$featurePoints->count() : 0;
  78.     
  79.                         $hidenCat = ['totalPoint','category','parent_id','code'];
  80.                       
  81.                         // gán điểm vào mảng và tạo nhóm Features
  82.                         if (array_key_exists($item->getParentId(), $data[$key]['featuresPoint'])) {
  83.                             // nếu tồn tại key nhóm Features thì thêm vào mảng đã tồn tại
  84.                             $dataItem ObjectJson::getJson($item,$hidenCat);
  85.                             
  86.                             $dataItem['point'] =$total;
  87.                             $dataItem['totalReview']=$featurePoints->count();
  88.                             $data[$key]['featuresPoint'][$item->getParentId()]['featureGroup'][] = $dataItem;
  89.                         } else {
  90.                             // chưa tồn tại key nhóm Features thì tạo mới
  91.                             $data[$key]['featuresPoint'][$item->getParentId()]=ObjectJson::getJson($item->getParent(),$hidenCat);
  92.     
  93.                             $dataItem ObjectJson::getJson($item,$hidenCat);
  94.                             $dataItem['point'] =$total;
  95.                             $dataItem['totalReview']=$featurePoints->count();
  96.                             $data[$key]['featuresPoint'][$item->getParentId()]['featureGroup'][] = $dataItem;
  97.                         }
  98.                 }
  99.              
  100.                 // ẩn tất cả thông tin không cần thiết của review
  101.                 $hidden = ['title','role','solution','categories','images','companyName',
  102.                 'companyWebsite','statusUsing','checkImage','date','jobTitle','problemsEncountered','recommendation',
  103.                 'answer','displayAccepted','pricing','policyAccepted'];
  104.                 
  105.                 // Lấy dữ liệu phần Định vị sản phẩm
  106.                 $review SolutionServices::listingReview($solution->getId(),$hidden);
  107.         
  108.                 // tạo mảng companySize
  109.                 $companySizes DataObject\Service::getOptionsForSelectField(new Review"companySize");
  110.                 $companySizes array_fill_keys(array_keys($companySizes), 0);
  111.                 
  112.                 // tạo mảng industry
  113.                 $industry = [];
  114.                 //
  115.                 $recommend 0;
  116.                 $companySizes['personal']=0;
  117.                 // dd($review);
  118.                 foreach($review as $value) {
  119.                     if($value['purpose'] == 'business'){
  120.                         $companySizes[$value['companySize']]++;
  121.                     }else{
  122.                         $companySizes['personal']++;
  123.                     }
  124.                     if (isset($value['industry']['name'])) {
  125.                         if(array_key_exists($value['industry']['name'],$industry)){
  126.                             $industry[$value['industry']['name']]++;
  127.                         }else{
  128.     
  129.                             $industry[$value['industry']['name']]=1;
  130.                         }
  131.                         if($value['recommend']){
  132.                             $recommend++;
  133.                         }
  134.                     }
  135.                 }
  136.                
  137.                 // tính phần trăm companySizes
  138.                 foreach($companySizes as $keyCom=>$companySize){
  139.                     $companySizes[$keyCom]=count($review) ? round(($companySize/count($review))*100) : 0;
  140.                 }
  141.                 // gán vào mảng
  142.                 $data[$key]['companySize']=$companySizes;
  143.                 // tính phần trăm industry
  144.                 foreach($industry as $keyInd=>$item){
  145.                     $industry[$keyInd]=count($review) ? round(($item/count($review))*100) : 0;
  146.                 }
  147.                 // gán vào mảng
  148.                 $data[$key]['industry']= (object) $industry;
  149.                 // Lấy điểm của các mục 
  150.                 $pointReview SolutionServices::pointReview($solution->getId(),$hidden);
  151.                 $data[$key]['scoringItem']=$pointReview;
  152.                 // Lấy review hài lòng
  153.                 $outstandingReview SolutionServices::outstandingReview($solution->getId(),$hidden,5);
  154.                 $data[$key]['outstandingReview']=$outstandingReview;
  155.                 // Lấy phần trăm đề xuất
  156.                 $data[$key]['recommend']=count($review) ? round(($recommend/count($review))*100) : 0;
  157.             }
  158.             
  159.         }
  160.         // dd($data,array_column($data,'id'));
  161.         return $this->sendResponse([
  162.             'data'=>$data,
  163.             'featuresList'=>$this->extractData($data,array_column($data,'id'))
  164.         ]);
  165.     }
  166.     
  167.     private function extractData($solutions$solution_ids){
  168.         // dd($solutions);
  169.         $data = [];
  170.         foreach($solutions as $solution){
  171.             foreach($solution['featuresPoint'] as $groupFeatures){
  172.                 if(array_key_exists('id',$groupFeatures)){
  173.                     if(!isset($data[$groupFeatures['id']])){
  174.                         $data[$groupFeatures['id']] = [
  175.                             'id_group'   => $groupFeatures['id'],
  176.                             'name_group' => $groupFeatures['name'],
  177.                             'data'       => []
  178.                         ];
  179.                     }
  180.                     
  181.                     foreach($groupFeatures['featureGroup'] as $function){
  182.                         $data[$groupFeatures['id']]['data'][$function['id']]['info'] = [
  183.                             'id' => $function['id'],
  184.                             'name' => $function['name'],
  185.                             'description' => $function['description']
  186.                         ]; 
  187.                         foreach($solution_ids as $solution_id){
  188.                             if($solution_id == $solution['id']){
  189.                                 $data[$groupFeatures['id']]['data'][$function['id']]['solutions'][$solution_id]['point'] = $function['point'];
  190.                                 $data[$groupFeatures['id']]['data'][$function['id']]['solutions'][$solution_id]['totalReview'] = $function['totalReview'];
  191.                             } else {
  192.                                 if(!isset($data[$groupFeatures['id']]['data'][$function['id']]['solutions'][$solution_id]) || $data[$groupFeatures['id']]['data'][$function['id']]['solutions'][$solution_id] == 0){
  193.                                     $data[$groupFeatures['id']]['data'][$function['id']]['solutions'][$solution_id]['point'] = 0;
  194.                                     $data[$groupFeatures['id']]['data'][$function['id']]['solutions'][$solution_id]['totalReview'] = 0;
  195.                                 }
  196.                             }
  197.                         }
  198.                     }
  199.                 }
  200.                
  201.             }
  202.         }
  203.         return $data;
  204.     }
  205. }