Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.2 KiB

5 years ago
5 years ago
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Admin;
  4. use Sonata\AdminBundle\Controller\CRUDController;
  5. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use HttpInvalidParamException;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Intl\Countries;
  11. use Symfony\Component\Routing\Exception\InvalidParameterException;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. final class ServicioAdminController extends IsOwnerCRUDController
  14. {
  15. /* public function deleteAction($id) {
  16. if(!$this->isOwner()){
  17. throw new AccessDeniedHttpException();
  18. }
  19. return parent::deleteAction($id);
  20. }*/
  21. public function editAction($deprecatedId = null)
  22. {
  23. if(!$this->isOwner()){
  24. throw new AccessDeniedHttpException();
  25. }
  26. return parent::editAction($deprecatedId);
  27. }
  28. public function showAction($deprecatedId = null)
  29. {
  30. if(!$this->isOwner()){
  31. throw new AccessDeniedHttpException();
  32. }
  33. return parent::showAction($deprecatedId); // TODO: Change the autogenerated stub
  34. }
  35. /**
  36. * Export data to specified format.
  37. *
  38. * @param Request $request
  39. *
  40. * @return Response
  41. *
  42. * @throws AccessDeniedException If access is not granted
  43. * @throws \RuntimeException If the export format is invalid
  44. */
  45. public function exportAction(Request $request = null)
  46. {
  47. //$request = $this->resolveRequest($request);
  48. $request = $this->getRequest();
  49. $this->admin->checkAccess('export');
  50. $format = $request->get('format');
  51. $allowedExportFormats = (array) $this->admin->getExportFormats();
  52. if (!in_array($format, $allowedExportFormats)) {
  53. throw new \RuntimeException(
  54. sprintf(
  55. 'Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`',
  56. $format,
  57. $this->admin->getClass(),
  58. implode(', ', $allowedExportFormats)
  59. )
  60. );
  61. }
  62. $filename = sprintf(
  63. 'export_%s_%s.%s',
  64. strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
  65. date('Y_m_d_H_i_s', strtotime('now')),
  66. $format
  67. );
  68. return $this->get('sonata.admin.exporter')->getResponse(
  69. $format,
  70. $filename,
  71. $this->admin->getDataSourceIterator()
  72. );
  73. }
  74. /**
  75. * Delete action.
  76. *
  77. * @param int|string|null $id
  78. *
  79. * @throws NotFoundHttpException If the object does not exist
  80. * @throws AccessDeniedException If access is not granted
  81. *
  82. * @return Response|RedirectResponse
  83. */
  84. public function deleteAction($id) // NEXT_MAJOR: Remove the unused $id parameter
  85. {
  86. $request = $this->getRequest();
  87. $id = $request->get($this->admin->getIdParameter());
  88. $object = $this->admin->getObject($id);
  89. if (!$object) {
  90. throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
  91. }
  92. $this->checkParentChildAssociation($request, $object);
  93. $this->admin->checkAccess('delete', $object);
  94. $preResponse = $this->preDelete($request, $object);
  95. if (null !== $preResponse) {
  96. return $preResponse;
  97. }
  98. if (Request::METHOD_DELETE === $this->getRestMethod()) {
  99. // check the csrf token
  100. $this->validateCsrfToken('sonata.delete');
  101. $objectName = $this->admin->toString($object);
  102. try {
  103. $this->admin->delete($object);
  104. if ($this->isXmlHttpRequest()) {
  105. return $this->renderJson(['result' => 'ok'], Response::HTTP_OK, []);
  106. }
  107. $this->addFlash(
  108. 'sonata_flash_success',
  109. $this->trans(
  110. 'flash_delete_success',
  111. ['%name%' => $this->escapeHtml($objectName)],
  112. 'SonataAdminBundle'
  113. )
  114. );
  115. } catch (ModelManagerException $e) { } catch (ModelManagerException $e) {
  116. $this->handleModelManagerException($e);
  117. if ($this->isXmlHttpRequest()) {
  118. return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []);
  119. }
  120. $this->addFlash(
  121. 'sonata_flash_error',
  122. $this->trans(
  123. 'flash_delete_error',
  124. ['%name%' => $this->escapeHtml($objectName)],
  125. 'SonataAdminBundle'
  126. )
  127. );
  128. }
  129. return $this->redirectTo($object);
  130. }
  131. // NEXT_MAJOR: Remove this line and use commented line below it instead
  132. $template = $this->admin->getTemplate('delete');
  133. // $template = $this->templateRegistry->getTemplate('delete');
  134. return $this->renderWithExtraParams($template, [
  135. 'object' => $object,
  136. 'action' => 'delete',
  137. 'csrf_token' => $this->getCsrfToken('sonata.delete'),
  138. ], null);
  139. }
  140. private function checkParentChildAssociation(\Symfony\Component\HttpFoundation\Request $request, $object): void
  141. {
  142. if (!($parentAdmin = $this->admin->getParent())) {
  143. return;
  144. }
  145. // NEXT_MAJOR: remove this check
  146. if (!$this->admin->getParentAssociationMapping()) {
  147. return;
  148. }
  149. $parentId = $request->get($parentAdmin->getIdParameter());
  150. $propertyAccessor = PropertyAccess::createPropertyAccessor();
  151. $propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());
  152. if ($parentAdmin->getObject($parentId) !== $propertyAccessor->getValue($object, $propertyPath)) {
  153. // NEXT_MAJOR: make this exception
  154. @trigger_error(
  155. "Accessing a child that isn't connected to a given parent is"
  156. ." deprecated since sonata-project/admin-bundle 3.34 and won't be allowed in 4.0.",
  157. E_USER_DEPRECATED
  158. );
  159. }
  160. }
  161. }