src/Controller/API/ResourcesController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Pimcore\Translation\Translator;
  8. use Pimcore\Model\DataObject\NewsCategory;
  9. use Pimcore\Model\DataObject\News;
  10. use App\Validator\Validator;
  11. use App\Helper\ObjectJson;
  12. use App\Services\AuthServices;
  13. use App\Services\RequestFormServices;
  14. use Pimcore\Model\DataObject\Resources;
  15. /**
  16.  * @Route("/api/resources")
  17.  */
  18. class ResourcesController extends BaseController
  19. {
  20.     const CLASS_NAME 'resources';
  21.     protected $translator;
  22.     protected $validator;
  23.     private $request;
  24.     public function __construct(
  25.         RequestStack $requestStack,
  26.         Translator $translator,
  27.         Validator $validator
  28.     )
  29.     {
  30.         $this->request $requestStack->getCurrentRequest();
  31.         $this->translator $translator;
  32.         $this->validator $validator;
  33.     }
  34.     /**
  35.      * @Route("/listing", methods={"GET"})
  36.      */
  37.     public function listing(Request $requestPaginatorInterface $paginator)
  38.     {
  39.         $data = [];
  40.         $response= [];
  41.         try {
  42.             $page $request->get('page');
  43.             $limit $request->get('limit');
  44.             
  45.             $order_by $request->get('order_by');
  46.             $order $request->get('order');
  47.             $messageError $this->validator->validate([
  48.                 'page' => $page 'numeric|positive' '',
  49.                 'limit' => $limit 'numeric|positive' '',
  50.                 'order_by' => $order_by 'choice:title,totalView,totalDownload' '',
  51.                 'order' => $order 'choice:desc,asc' '',
  52.             ], $request);
  53.             if ($messageError) return $this->sendError($this->translator->trans($messageError));
  54.             if(empty($page)) $page 1;
  55.             if(empty($limit)) $limit 10;
  56.             if(empty($order_by)) $order_by 'o_creationDate';
  57.             if(empty($order)) $order 'desc';
  58.             $listing = new Resources\Listing();
  59.            
  60.             if(!empty($request->get('search'))) $listing->addConditionParam("title LIKE '%".$request->get('search')."%'");
  61.             if(!empty($request->get('category'))) $listing->addConditionParam("categories LIKE '%".$request->get('category')."%'");
  62.             $listing->setOrderKey($order_by);
  63.             $listing->setOrder($order);
  64.           
  65.             if ($page && $limit) {
  66.                 $pagination $paginator->paginate(
  67.                     $listing,
  68.                     $page,
  69.                     $limit,
  70.                 );
  71.             } 
  72.             foreach ($listing as $key=>$resource) {
  73.                 $data[] =ObjectJson::getJson($resource);
  74.             }
  75.             $response['data'] = $data;
  76.             $response['paginator'] = $pagination->getPaginationData();
  77.         } catch (\Throwable $e) {
  78.             
  79.             return $this->sendError($e->getMessage(), 500);
  80.         }
  81.         return $this->sendResponse($response);
  82.     }
  83.     
  84. }