RESTController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace WebserviceBundle\Controller;
  3. use FOS\RestBundle\Controller\Annotations\QueryParam;
  4. use FOS\RestBundle\Controller\Annotations\RouteResource;
  5. use FOS\RestBundle\Controller\Annotations\View;
  6. use FOS\RestBundle\Request\ParamFetcherInterface;
  7. use FOS\RestBundle\Util\Codes;
  8. use FOS\RestBundle\View\View as FOSView;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  11. use Symfony\Component\Form\Form;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Voryx\RESTGeneratorBundle\Controller\VoryxController;
  15. class RESTController extends VoryxController
  16. {
  17. var $repository = 'ClientBundle:Client';
  18. /**
  19. * Get all entities.
  20. *
  21. * @View(serializerEnableMaxDepthChecks=true)
  22. *
  23. * @param ParamFetcherInterface $paramFetcher
  24. * @param string $repository
  25. *
  26. * @return Response
  27. *
  28. * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.")
  29. * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.")
  30. * @QueryParam(name="order_by", nullable=true, array=true, description="Order by fields. Must be an array ie. &order_by[name]=ASC&order_by[description]=DESC")
  31. * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3")
  32. * &filters[qb-criteria] => Utilizará el matching con criteria donde los parámetros filters realizarán "field like %value%".
  33. */
  34. public function cgetAction(ParamFetcherInterface $paramFetcher)
  35. {
  36. $em = $this->getDoctrine()->getManager();
  37. $disableTenancy = false;
  38. $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array();
  39. if (isset($filters['tenancyId'])) {
  40. // tengo que buscar por tenencia.
  41. $tenancyService = $this->container->get('base_tenancy.tenancy_service');
  42. $tenancyService->setTenancy($filters['tenancyId']);
  43. unset($filters['tenancyId']);
  44. }
  45. if (isset($filters['disableTenancy'])) {
  46. $disableTenancy = true;
  47. // tengo que buscar por tenencia.
  48. $tenancyService = $this->container->get('base_tenancy.tenancy_service');
  49. $tenancyService->disableFilter();
  50. unset($filters['disableTenancy']);
  51. }
  52. if (isset($filters['qb-criteria'])) {
  53. try {
  54. unset($filters['qb-criteria']);
  55. $criteria = new \Doctrine\Common\Collections\Criteria();
  56. foreach ($filters as $field => $value) {
  57. $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
  58. }
  59. if (!is_null($paramFetcher->get('offset'))) {
  60. $criteria->setFirstResult($paramFetcher->get('offset'));
  61. }
  62. if (!is_null($paramFetcher->get('limit'))) {
  63. $criteria->setMaxResults($paramFetcher->get('limit'));
  64. }
  65. if ($paramFetcher->get('order_by')) {
  66. $order_by = $paramFetcher->get('order_by');
  67. $orderBy = array();
  68. foreach ($order_by as $field => $order) {
  69. $orderBy[$field] = $order;
  70. }
  71. $criteria->orderBy($orderBy);
  72. }
  73. $repo = $em->getRepository($this->repository);
  74. $entities = $repo->matching($criteria)->toArray();
  75. if($disableTenancy) $tenancyService->enableFilter();
  76. if ($entities) {
  77. return $entities;
  78. }
  79. } catch (\Exception $e) {
  80. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  81. }
  82. } else {
  83. try {
  84. $offset = $paramFetcher->get('offset');
  85. $limit = $paramFetcher->get('limit');
  86. $order_by = $paramFetcher->get('order_by');
  87. $entities = $em->getRepository($this->repository)->findBy($filters, $order_by, $limit, $offset);
  88. if($disableTenancy) $tenancyService->enableFilter();
  89. if ($entities) {
  90. return $entities;
  91. }
  92. } catch (\Exception $e) {
  93. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  94. }
  95. }
  96. return array();
  97. }
  98. }