<?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\Solution;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use App\Services\SolutionServices;
use Pimcore\Model\DataObject\Category;
use Pimcore\Model\DataObject\FilterDefinition;
use App\Helper\ObjectJson;
use Pimcore\Model\DataObject\Request as ObjectRequest;
use Pimcore\Model\DataObject\Review;
use Pimcore\Model\Asset\Service as AssetService;
use Pimcore\Model\DataObject\Service as DataObjectService;
use Pimcore\Tool;
use App\Services\VoucherService;
/**
* @Route("/api")
*/
class SpecializedController extends BaseController
{
const SOLUTION_PAGE_DEFAULT = 1;
const SOLUTION_PERPAGE_DEFAULT = 10;
const SOLUTION_CLASS_NAME = 'Solution';
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("/specialized", name="api-specialized", methods={"GET"})
*/
public function listing(Request $request,ListHelper $listHelper,PaginatorInterface $paginator)
{
if($this->request->get('star')){
$star = $this->request->get('star')*2;
}
$messageError = $this->validator->validate([
'categories'=>'required',
'page' => $this->request->get('page')?'numeric|positive':'',
'limit' => $this->request->get('limit')?'numeric|positive':''
], $request);
if($messageError) return $this->sendError($this->translator->trans($messageError));
$params = array_merge($request->query->all(), $request->attributes->all());
$solutions = Factory::getInstance();
$indexService = $solutions->getIndexService();
$solutionsListing = $indexService->getProductListForCurrentTenant();
$solutionsListing->addCondition("o_classId ='solution'");
if(!empty($this->request->get('search'))) $solutionsListing->addCondition("text LIKE '%".$this->request->get('search')."%'");
if(!empty($this->request->get('email'))) $solutionsListing->addCondition("email LIKE '%".$this->request->get('email')."%'");
if(!empty($this->request->get('star'))) $solutionsListing->addCondition("totalPoint =".$star." OR totalPoint =".$star-1);
$defaultOrderBy = $this->request->get('order_key') ?? 'text';
$defaultOrder = $this->request->get('order') ?? 'desc';
$njOrderKey = $defaultOrderBy .' '. $defaultOrder;
$solutionsListing->setOrderKey($njOrderKey, false);
$parentCategoryId = (int) $request->get('parentCategoryIds');
$parentCategory = Category::getById($parentCategoryId);
$rootCategory = Category::getByRoot(true, 1);
$category = $parentCategory ?? $rootCategory;
//get filter definition from document, category or global settings
$filterDefinition = $category->getFilter() ? $category->getFilter() : ($category->getParent() instanceof Category ? $category->getParent()->getFilter() : Category::getById($parentCategoryId)->getFilter()); //TODO ...get from somewhere;
$filters = [];
if($filterDefinition instanceof FilterDefinition){
$filterList = $filterDefinition->getFilters();
$messageError = $this->validator->validate(SolutionServices::validatorFilterDefinition($filterList, $request), $request);
if($messageError) return $this->sendError($this->translator->trans($messageError));
// create and init filter service
$filterService = $solutions->getFilterService();
$listHelper->setupProductList($filterDefinition, $solutionsListing, $params, $filterService, true);
$filters = SolutionServices::getFilterDefinition($filterList, $filterService, $solutionsListing, $params, $this->translator);
}
// fix cứng star đổ ra filter
$starFilter = [
"label" => $this->translator->trans('filter.star-rating'),
"type" => "FilterMultiSelectStar",
"fieldname" => "star",
"currentValue" => [
$request->get('star')
],
// check từ request để lấy giá trị hiện tại
"data" => [
[
"name" => "5",
"value" => "5",
],
[
"name" => "4",
"value" => "4",
],
[
"name" => "3",
"value" => "3",
],
[
"name" => "2",
"value" => "2",
],
[
"name" => "1",
"value" => "1",
],
]
];
$filters[] = $starFilter;
$paginator = $paginator->paginate(
$solutionsListing,
$request->get('page', self::SOLUTION_PAGE_DEFAULT),
$request->get('limit', self::SOLUTION_PERPAGE_DEFAULT),
);
$data = [];
$hiddenFields = ['faq','features','pricing','documents','video','community'];
$solutionIdArr = [];
foreach ($paginator as $solution) {
$dataJson = ObjectJson::getJson($solution, $hiddenFields);
$solutionIdArr[] = $dataJson['id'];
array_push($data, $dataJson);
}
$data = VoucherService::getVoucherSolution($solutionIdArr, $data);
return $this->sendResponse([
'data' => $data,
'paginator' => $paginator->getPaginationData(),
'filter' => $filters
]);
}
}