RESTController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. abstract class RESTController extends VoryxController
  16. {
  17. /**
  18. * @return string Retorna el nombre de la Entity de trabajo.
  19. */
  20. abstract public function getRepository();
  21. /**
  22. * Get all entities.
  23. *
  24. * @View(serializerEnableMaxDepthChecks=true)
  25. *
  26. * @param ParamFetcherInterface $paramFetcher
  27. * @param string $repository
  28. *
  29. * @return Response
  30. *
  31. * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.")
  32. * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.")
  33. * @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")
  34. * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3")
  35. * &filters[qb-criteria] => Utilizará el matching con criteria donde los parámetros filters realizarán "field like %value%".
  36. */
  37. public function cgetAction(ParamFetcherInterface $paramFetcher)
  38. {
  39. $em = $this->getDoctrine()->getManager();
  40. $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array();
  41. if (isset($filters['tenancy_id'])) {
  42. // tengo que buscar por tenencia.
  43. $tenancyService = $this->container->get('base_tenancy.tenancy_service');
  44. $tenancyService->setTenancy($filters['tenancy_id']);
  45. unset($filters['tenancy_id']);
  46. }
  47. if (isset($filters['qb-criteria'])) {
  48. try {
  49. unset($filters['qb-criteria']);
  50. $criteria = new \Doctrine\Common\Collections\Criteria();
  51. foreach ($filters as $field => $value) {
  52. $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
  53. }
  54. if (!is_null($paramFetcher->get('offset'))) {
  55. $criteria->setFirstResult($paramFetcher->get('offset'));
  56. }
  57. if (!is_null($paramFetcher->get('limit'))) {
  58. $criteria->setMaxResults($paramFetcher->get('limit'));
  59. }
  60. if ($paramFetcher->get('order_by')) {
  61. $order_by = $paramFetcher->get('order_by');
  62. $orderBy = array();
  63. foreach ($order_by as $field => $order) {
  64. $orderBy[$field] = $order;
  65. }
  66. $criteria->orderBy($orderBy);
  67. }
  68. $repo = $em->getRepository($this->getRepository());
  69. $entities = $repo->matching($criteria)->toArray();
  70. if ($entities) {
  71. return $entities;
  72. }
  73. } catch (\Exception $e) {
  74. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  75. }
  76. } else {
  77. try {
  78. $offset = $paramFetcher->get('offset');
  79. $limit = $paramFetcher->get('limit');
  80. $order_by = $paramFetcher->get('order_by');
  81. $entities = $em->getRepository($this->getRepository())->findBy($filters, $order_by, $limit, $offset);
  82. if ($entities) {
  83. return $entities;
  84. }
  85. } catch (\Exception $e) {
  86. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  87. }
  88. }
  89. return array();
  90. }
  91. }