<?php
namespace App\Controller\API;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestStack;
use Pimcore\Translation\Translator;
use App\Validator\Validator;
use Pimcore\Model\DataObject\Provider;
use Pimcore\Model\DataObject\Reviewer;
use Pimcore\Model\DataObject\Industry;
use Pimcore\Model\DataObject\Features;
use Pimcore\Model\DataObject\Solution;
use App\Services\ReviewerServices;
use App\Helper\ObjectJson;
use App\Services\AuthServices;
use Pimcore\Model\DataObject\Review;
use Pimcore\Model\DataObject\Request as ObjectRequest;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\Asset\Service as AssetService;
use Pimcore\Model\DataObject\Service as DataObjectService;
use Pimcore\Tool\Admin;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
/**
* @Route("/api/review")
*/
class ReviewController extends BaseController
{
const REVIEW_PAGE_DEFAULT = 1;
const REVIEW_PERPAGE_DEFAULT = 10;
private $request;
private $validator;
private $translator;
public function __construct(RequestStack $requestStack, Validator $validator, Translator $translator)
{
$this->request = $requestStack->getCurrentRequest();
$this->validator = $validator;
$this->translator = $translator;
}
/**
* @Route("/listing", name="api-review-listing", methods={"GET"})
*/
public function listing(Request $request, PaginatorInterface $paginator){
$data = [];
$response= [];
try {
$user = '';
if ($request->headers->has('Authorization')) {
$token = $request->headers->get('Authorization');
$result = AuthServices::getUserByToken($token);
if ($result && $result instanceof Reviewer) {
$user = $result;
}
}
$order_by = $request->get('order_by');
$order = $request->get('order');
$star = $request->get('star')*2;
$messageError = $this->validator->validate([
'page' => $this->request->get('page') ? 'numeric|positive' : '',
'limit' => $this->request->get('limit') ? 'numeric|positive' : '',
'order_by' => $order_by ? 'choice:title' : '',
'order' => $order ? 'choice:desc,asc' : ''
], $request);
if ($messageError) return $this->sendError($this->translator->trans($messageError));
if(empty($order_by)) $order_by = 'o_creationDate';
if(empty($order)) $order = 'desc';
$listing = new Review\Listing();
if ($user && $request->get('myReview') == 1) {
$listing->addConditionParam("reviewer__id = ?", $user->getId());
}
if(!empty($request->get('solution'))) $listing->addConditionParam("solution__id = ?",$request->get('solution'));
if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
if(!empty($request->get('role'))&& is_array($request->get('role'))){
$roles = $request->get('role');
$roleConditions = implode(" OR ", array_fill(0, count($roles), "role LIKE ?"));
$roleParams = array_map(function($role) {
return '%,' . $role . ',%';
}, $roles);
$listing->addConditionParam($roleConditions,$roleParams);
}
if(!empty($request->get('star'))) $listing->addConditionParam("generalScore =".$star." OR generalScore =".$star-1);
if(!empty($request->get('checkImage'))) $listing->addConditionParam("checkImage =".$request->get('checkImage'));
if(!empty($request->get('user'))&& is_array($request->get('user'))){
foreach($request->get('user') as $value){
if(!empty($value)) $listing->addConditionParam("purpose LIKE '%".$value."%'");
}
}
if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
// Lọc theo điều kiện Review xác thực
$ratingType = $request->get('ratingType');
if(isset($ratingType)){
if($request->get('ratingType') == 'all'){
$listing->addConditionParam("accuracy = 1 OR accuracy = 0 OR accuracy is NULL");
}else{
$listing->addConditionParam("accuracy = ?",$request->get('ratingType'));
}
}
// Lọc theo nội dung
$ratingContent = $request->get('ratingContent');
if(isset($ratingContent)){
if($request->get('ratingContent') == 'all'){
$listing->addConditionParam("checkImage = 1 OR checkImage = 0 OR checkImage is NULL");
}else{
$listing->addConditionParam("checkImage = ?",$request->get('ratingContent'));
}
}
$listing->setOrderKey($order_by);
$listing->setOrder($order);
$pagination = $paginator->paginate(
$listing,
$request->get('page', self::REVIEW_PAGE_DEFAULT),
$request->get('limit', self::REVIEW_PERPAGE_DEFAULT),
);
$likedReview = [];
if ($user instanceof Reviewer) {
$likedReview = $user->getLikeReview();
}
foreach ($listing as $review) {
$dataJson = ObjectJson::getJson($review);
// Genarate new creationDate
if (isset($dataJson['creationDateRenew']) && $dataJson['creationDateRenew']) {
$dataJson['creationDate'] = date('Y-m-d H:i:s', strtotime($dataJson['creationDateRenew']));
}
unset($dataJson['creationDateRenew']);
$dataSolution=[
'id'=>$dataJson['solution']['id'],
'name'=>$dataJson['solution']['name'],
'categories'=>$dataJson['solution']['categories'],
'logo'=>$dataJson['solution']['logo']
];
$dataJson['solution']=$dataSolution;
$dataJson['liked'] = in_array($review, $likedReview);
array_push($data, $dataJson);
}
$response['data'] = $data;
$response['paginator'] = $pagination->getPaginationData();
} catch (\Throwable $e) {
return $this->sendError($e->getMessage(), 500);
}
return $this->sendResponse($response);
}
/**
* @Route("/option-data", name="api-review-option-data", methods={"GET"})
*/
public function reviewOption(Request $request){
$messageError = $this->validator->validate([
'solution' => 'required'
], $request);
if ($messageError) return $this->sendError($this->translator->trans($messageError));
$solution = Solution::getById($this->request->get('solution'));
$data=[];
$review = new Review;
$purpose = DataObject\Service::getOptionsForSelectField($review, "purpose");
$companySize = DataObject\Service::getOptionsForSelectField($review, "companySize");
$statusUsing = DataObject\Service::getOptionsForSelectField($review, "statusUsing");
$usedTime = DataObject\Service::getOptionsForSelectField($review, "usedTime");
$role = DataObject\Service::getOptionsForSelectField($review, "role");
$accuracy = [
"all" => "Tất cả đánh giá",
"1" => "Đánh giá tin cậy",
"0" => "Chưa xác thực"
];
$ratingContent = [
"all" => "Tất cả",
"0" => "Chỉ có chữ",
"1" => "Có ảnh/video"
];
$data = [
'purpose'=> $purpose,
'companySize'=> $companySize,
'statusUsing'=> $statusUsing,
'usedTime'=> $usedTime,
'role'=> $role,
'ratingType'=>$accuracy,
'ratingContent' => $ratingContent,
'features'=>[]
];
// Lấy industry
$industry = new Industry\Listing();
foreach($industry as $value){
$data['industry'][] = ObjectJson::getJson($value);
}
if($solution){
// Lấy features
$hidenCat = ['totalPoint','category','parent_id','code'];
foreach($solution->getFeatures() as $key => $value){
// dd($data['features']);
if (array_key_exists($value->getParentId(), $data['features'])) {
$data['features'][$value->getParentId()]['featureGroup'][] = ObjectJson::getJson($value,$hidenCat);
} else {
$data['features'][$value->getParentId()]=ObjectJson::getJson($value->getParent(),$hidenCat);
$data['features'][$value->getParentId()]['featureGroup'][] = ObjectJson::getJson($value,$hidenCat);
}
}
// Lấy pricing
foreach($solution->getPricing() as $key => $value){
$data['pricing'][] = ObjectJson::getJson($value);
}
}else{
return $this->sendResponse([
'message' => $this->translator->trans('solution.not.found'),
'status'=> 0
]);
}
return $this->sendResponse($data);
}
}