<?php
namespace App\Controller\API;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Pimcore\Translation\Translator;
use Pimcore\Model\DataObject\NewsCategory;
use Pimcore\Model\DataObject\News;
use App\Validator\Validator;
use App\Helper\ObjectJson;
use App\Services\AuthServices;
use App\Services\RequestFormServices;
use Pimcore\Model\DataObject\Resources;
/**
* @Route("/api/resources")
*/
class ResourcesController extends BaseController
{
const CLASS_NAME = 'resources';
protected $translator;
protected $validator;
private $request;
public function __construct(
RequestStack $requestStack,
Translator $translator,
Validator $validator
)
{
$this->request = $requestStack->getCurrentRequest();
$this->translator = $translator;
$this->validator = $validator;
}
/**
* @Route("/listing", methods={"GET"})
*/
public function listing(Request $request, PaginatorInterface $paginator)
{
$data = [];
$response= [];
try {
$page = $request->get('page');
$limit = $request->get('limit');
$order_by = $request->get('order_by');
$order = $request->get('order');
$messageError = $this->validator->validate([
'page' => $page ? 'numeric|positive' : '',
'limit' => $limit ? 'numeric|positive' : '',
'order_by' => $order_by ? 'choice:title,totalView,totalDownload' : '',
'order' => $order ? 'choice:desc,asc' : '',
], $request);
if ($messageError) return $this->sendError($this->translator->trans($messageError));
if(empty($page)) $page = 1;
if(empty($limit)) $limit = 10;
if(empty($order_by)) $order_by = 'o_creationDate';
if(empty($order)) $order = 'desc';
$listing = new Resources\Listing();
if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
if(!empty($request->get('category'))) $listing->addConditionParam("categories LIKE '%".$request->get('category')."%'");
$listing->setOrderKey($order_by);
$listing->setOrder($order);
if ($page && $limit) {
$pagination = $paginator->paginate(
$listing,
$page,
$limit,
);
}
foreach ($listing as $key=>$resource) {
$data[] =ObjectJson::getJson($resource);
}
$response['data'] = $data;
$response['paginator'] = $pagination->getPaginationData();
} catch (\Throwable $e) {
return $this->sendError($e->getMessage(), 500);
}
return $this->sendResponse($response);
}
}