RESTController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array();
  38. if (isset($filters['tenancy_id'])) {
  39. // tengo que buscar por tenencia.
  40. $tenancyService = $this->container->get('base_tenancy.tenancy_service');
  41. $tenancyService->setTenancy($filters['tenancy_id']);
  42. unset($filters['tenancy_id']);
  43. }
  44. if (isset($filters['qb-criteria'])) {
  45. try {
  46. unset($filters['qb-criteria']);
  47. $criteria = new \Doctrine\Common\Collections\Criteria();
  48. foreach ($filters as $field => $value) {
  49. $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
  50. }
  51. if (!is_null($paramFetcher->get('offset'))) {
  52. $criteria->setFirstResult($paramFetcher->get('offset'));
  53. }
  54. if (!is_null($paramFetcher->get('limit'))) {
  55. $criteria->setMaxResults($paramFetcher->get('limit'));
  56. }
  57. if ($paramFetcher->get('order_by')) {
  58. $order_by = $paramFetcher->get('order_by');
  59. $orderBy = array();
  60. foreach ($order_by as $field => $order) {
  61. $orderBy[$field] = $order;
  62. }
  63. $criteria->orderBy($orderBy);
  64. }
  65. $repo = $em->getRepository($this->repository);
  66. $entities = $repo->matching($criteria)->toArray();
  67. if ($entities) {
  68. return $entities;
  69. }
  70. } catch (\Exception $e) {
  71. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  72. }
  73. } else {
  74. try {
  75. $offset = $paramFetcher->get('offset');
  76. $limit = $paramFetcher->get('limit');
  77. $order_by = $paramFetcher->get('order_by');
  78. $entities = $em->getRepository($this->repository)->findBy($filters, $order_by, $limit, $offset);
  79. if ($entities) {
  80. return $entities;
  81. }
  82. } catch (\Exception $e) {
  83. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  84. }
  85. }
  86. return array();
  87. }
  88. }