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

<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpFoundation\Request;
use HttpInvalidParamException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\HttpFoundation\RedirectResponse;
final class ServicioAdminController extends IsOwnerCRUDController
{
/* public function deleteAction($id) {
if(!$this->isOwner()){
throw new AccessDeniedHttpException();
}
return parent::deleteAction($id);
}*/
public function editAction($deprecatedId = null)
{
if(!$this->isOwner()){
throw new AccessDeniedHttpException();
}
return parent::editAction($deprecatedId);
}
public function showAction($deprecatedId = null)
{
if(!$this->isOwner()){
throw new AccessDeniedHttpException();
}
return parent::showAction($deprecatedId); // TODO: Change the autogenerated stub
}
/**
* Export data to specified format.
*
* @param Request $request
*
* @return Response
*
* @throws AccessDeniedException If access is not granted
* @throws \RuntimeException If the export format is invalid
*/
public function exportAction(Request $request = null)
{
//$request = $this->resolveRequest($request);
$request = $this->getRequest();
$this->admin->checkAccess('export');
$format = $request->get('format');
$allowedExportFormats = (array) $this->admin->getExportFormats();
if (!in_array($format, $allowedExportFormats)) {
throw new \RuntimeException(
sprintf(
'Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`',
$format,
$this->admin->getClass(),
implode(', ', $allowedExportFormats)
)
);
}
$filename = sprintf(
'export_%s_%s.%s',
strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
date('Y_m_d_H_i_s', strtotime('now')),
$format
);
return $this->get('sonata.admin.exporter')->getResponse(
$format,
$filename,
$this->admin->getDataSourceIterator()
);
}
/**
* Delete action.
*
* @param int|string|null $id
*
* @throws NotFoundHttpException If the object does not exist
* @throws AccessDeniedException If access is not granted
*
* @return Response|RedirectResponse
*/
public function deleteAction($id) // NEXT_MAJOR: Remove the unused $id parameter
{
$request = $this->getRequest();
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);
if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->checkParentChildAssociation($request, $object);
$this->admin->checkAccess('delete', $object);
$preResponse = $this->preDelete($request, $object);
if (null !== $preResponse) {
return $preResponse;
}
if (Request::METHOD_DELETE === $this->getRestMethod()) {
// check the csrf token
$this->validateCsrfToken('sonata.delete');
$objectName = $this->admin->toString($object);
try {
$this->admin->delete($object);
if ($this->isXmlHttpRequest()) {
return $this->renderJson(['result' => 'ok'], Response::HTTP_OK, []);
}
$this->addFlash(
'sonata_flash_success',
$this->trans(
'flash_delete_success',
['%name%' => $this->escapeHtml($objectName)],
'SonataAdminBundle'
)
);
} catch (ModelManagerException $e) { } catch (ModelManagerException $e) {
$this->handleModelManagerException($e);
if ($this->isXmlHttpRequest()) {
return $this->renderJson(['result' => 'error'], Response::HTTP_OK, []);
}
$this->addFlash(
'sonata_flash_error',
$this->trans(
'flash_delete_error',
['%name%' => $this->escapeHtml($objectName)],
'SonataAdminBundle'
)
);
}
return $this->redirectTo($object);
}
// NEXT_MAJOR: Remove this line and use commented line below it instead
$template = $this->admin->getTemplate('delete');
// $template = $this->templateRegistry->getTemplate('delete');
return $this->renderWithExtraParams($template, [
'object' => $object,
'action' => 'delete',
'csrf_token' => $this->getCsrfToken('sonata.delete'),
], null);
}
private function checkParentChildAssociation(\Symfony\Component\HttpFoundation\Request $request, $object): void
{
if (!($parentAdmin = $this->admin->getParent())) {
return;
}
// NEXT_MAJOR: remove this check
if (!$this->admin->getParentAssociationMapping()) {
return;
}
$parentId = $request->get($parentAdmin->getIdParameter());
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());
if ($parentAdmin->getObject($parentId) !== $propertyAccessor->getValue($object, $propertyPath)) {
// NEXT_MAJOR: make this exception
@trigger_error(
"Accessing a child that isn't connected to a given parent is"
." deprecated since sonata-project/admin-bundle 3.34 and won't be allowed in 4.0.",
E_USER_DEPRECATED
);
}
}
}