1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Controller;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Sonata\AdminBundle\Exception\ModelManagerException;
- use Symfony\Component\HttpFoundation\Request;
- use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
- use Sonata\AdminBundle\Admin\BaseFieldDescription;
- use Sonata\AdminBundle\Util\AdminObjectAclData;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Psr\Log\NullLogger;
- class CRUDController extends Controller
- {
- /**
- * The related Admin class
- *
- * @var AdminInterface
- */
- protected $admin;
- /**
- * Render JSON
- *
- * @param mixed $data
- * @param integer $status
- * @param array $headers
- *
- * @return Response with json encoded data
- */
- protected function renderJson($data, $status = 200, $headers = array())
- {
- // fake content-type so browser does not show the download popup when this
- // response is rendered through an iframe (used by the jquery.form.js plugin)
- // => don't know yet if it is the best solution
- if ($this->get('request')->get('_xml_http_request')
- && strpos($this->get('request')->headers->get('Content-Type'), 'multipart/form-data') === 0) {
- $headers['Content-Type'] = 'text/plain';
- } else {
- $headers['Content-Type'] = 'application/json';
- }
- return new Response(json_encode($data), $status, $headers);
- }
- /**
- * Returns true if the request is a XMLHttpRequest.
- *
- * @return bool True if the request is an XMLHttpRequest, false otherwise
- */
- protected function isXmlHttpRequest()
- {
- return $this->get('request')->isXmlHttpRequest() || $this->get('request')->get('_xml_http_request');
- }
- /**
- * Returns the correct RESTful verb, given either by the request itself or
- * via the "_method" parameter.
- *
- * @return string HTTP method, either
- */
- protected function getRestMethod()
- {
- $request = $this->getRequest();
- if (Request::getHttpMethodParameterOverride() || !$request->request->has('_method')) {
- return $request->getMethod();
- }
- return $request->request->get('_method');
- }
- /**
- * Sets the Container associated with this Controller.
- *
- * @param ContainerInterface $container A ContainerInterface instance
- */
- public function setContainer(ContainerInterface $container = null)
- {
- $this->container = $container;
- $this->configure();
- }
- /**
- * Contextualize the admin class depends on the current request
- *
- * @throws \RuntimeException
- */
- protected function configure()
- {
- $adminCode = $this->container->get('request')->get('_sonata_admin');
- if (!$adminCode) {
- throw new \RuntimeException(sprintf(
- 'There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`',
- get_class($this),
- $this->container->get('request')->get('_route')
- ));
- }
- $this->admin = $this->container->get('sonata.admin.pool')->getAdminByAdminCode($adminCode);
- if (!$this->admin) {
- throw new \RuntimeException(sprintf(
- 'Unable to find the admin class related to the current controller (%s)',
- get_class($this)
- ));
- }
- $rootAdmin = $this->admin;
- if ($this->admin->isChild()) {
- $this->admin->setCurrentChild(true);
- $rootAdmin = $rootAdmin->getParent();
- }
- $request = $this->container->get('request');
- $rootAdmin->setRequest($request);
- if ($request->get('uniqid')) {
- $this->admin->setUniqid($request->get('uniqid'));
- }
- }
- /**
- * Proxy for the logger service of the container.
- * If no such service is found, a NullLogger is returned.
- *
- * @return Psr\Log\LoggerInterface
- */
- protected function getLogger()
- {
- if ($this->container->has('logger')) {
- return $this->container->get('logger');
- } else {
- return new NullLogger();
- }
- }
- /**
- * Returns the base template name
- *
- * @return string The template name
- */
- protected function getBaseTemplate()
- {
- if ($this->isXmlHttpRequest()) {
- return $this->admin->getTemplate('ajax');
- }
- return $this->admin->getTemplate('layout');
- }
- /**
- * {@inheritdoc}
- */
- public function render($view, array $parameters = array(), Response $response = null)
- {
- $parameters['admin'] = isset($parameters['admin']) ?
- $parameters['admin'] :
- $this->admin;
- $parameters['base_template'] = isset($parameters['base_template']) ?
- $parameters['base_template'] :
- $this->getBaseTemplate();
- $parameters['admin_pool'] = $this->get('sonata.admin.pool');
- return parent::render($view, $parameters, $response);
- }
- private function logModelManagerException($e)
- {
- $context = array('exception' => $e);
- if ($e->getPrevious()) {
- $context['previous_exception_message'] = $e->getPrevious()->getMessage();
- }
- $this->getLogger()->error($e->getMessage(), $context);
- }
- /**
- * List action
- *
- * @return Response
- *
- * @throws AccessDeniedException If access is not granted
- */
- public function listAction()
- {
- if (false === $this->admin->isGranted('LIST')) {
- throw new AccessDeniedException();
- }
- $datagrid = $this->admin->getDatagrid();
- $formView = $datagrid->getForm()->createView();
- // set the theme for the current Admin Form
- $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
- return $this->render($this->admin->getTemplate('list'), array(
- 'action' => 'list',
- 'form' => $formView,
- 'datagrid' => $datagrid,
- 'csrf_token' => $this->getCsrfToken('sonata.batch'),
- ));
- }
- /**
- * Execute a batch delete
- *
- * @param ProxyQueryInterface $query
- *
- * @return RedirectResponse
- *
- * @throws AccessDeniedException If access is not granted
- */
- public function batchActionDelete(ProxyQueryInterface $query)
- {
- if (false === $this->admin->isGranted('DELETE')) {
- throw new AccessDeniedException();
- }
- $modelManager = $this->admin->getModelManager();
- try {
- $modelManager->batchDelete($this->admin->getClass(), $query);
- $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
- } catch (ModelManagerException $e) {
- $this->logModelManagerException($e);
- $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
- }
- return new RedirectResponse($this->admin->generateUrl(
- 'list',
- array('filter' => $this->admin->getFilterParameters())
- ));
- }
- /**
- * Delete action
- *
- * @param int|string|null $id
- *
- * @return Response|RedirectResponse
- *
- * @throws NotFoundHttpException If the object does not exist
- * @throws AccessDeniedException If access is not granted
- */
- public function deleteAction($id)
- {
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('DELETE', $object)) {
- throw new AccessDeniedException();
- }
- if ($this->getRestMethod() == 'DELETE') {
- // check the csrf token
- $this->validateCsrfToken('sonata.delete');
- try {
- $this->admin->delete($object);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array('result' => 'ok'));
- }
- $this->addFlash(
- 'sonata_flash_success',
- $this->admin->trans(
- 'flash_delete_success',
- array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle'
- )
- );
- } catch (ModelManagerException $e) {
- $this->logModelManagerException($e);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array('result' => 'error'));
- }
- $this->addFlash(
- 'sonata_flash_error',
- $this->admin->trans(
- 'flash_delete_error',
- array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle'
- )
- );
- }
- return $this->redirectTo($object);
- }
- return $this->render($this->admin->getTemplate('delete'), array(
- 'object' => $object,
- 'action' => 'delete',
- 'csrf_token' => $this->getCsrfToken('sonata.delete')
- ));
- }
- /**
- * Edit action
- *
- * @param int|string|null $id
- *
- * @return Response|RedirectResponse
- *
- * @throws NotFoundHttpException If the object does not exist
- * @throws AccessDeniedException If access is not granted
- */
- public function editAction($id = null)
- {
- // the key used to lookup the template
- $templateKey = 'edit';
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('EDIT', $object)) {
- throw new AccessDeniedException();
- }
- $this->admin->setSubject($object);
- /** @var $form \Symfony\Component\Form\Form */
- $form = $this->admin->getForm();
- $form->setData($object);
- if ($this->getRestMethod() == 'POST') {
- $form->submit($this->get('request'));
- $isFormValid = $form->isValid();
- // persist if the form was valid and if in preview mode the preview was approved
- if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
- try {
- $object = $this->admin->update($object);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array(
- 'result' => 'ok',
- 'objectId' => $this->admin->getNormalizedIdentifier($object)
- ));
- }
- $this->addFlash(
- 'sonata_flash_success',
- $this->admin->trans(
- 'flash_edit_success',
- array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle'
- )
- );
- // redirect to edit mode
- return $this->redirectTo($object);
- } catch (ModelManagerException $e) {
- $this->logModelManagerException($e);
- $isFormValid = false;
- }
- }
- // show an error message if the form failed validation
- if (!$isFormValid) {
- if (!$this->isXmlHttpRequest()) {
- $this->addFlash(
- 'sonata_flash_error',
- $this->admin->trans(
- 'flash_edit_error',
- array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle'
- )
- );
- }
- } elseif ($this->isPreviewRequested()) {
- // enable the preview template if the form was valid and preview was requested
- $templateKey = 'preview';
- $this->admin->getShow();
- }
- }
- $view = $form->createView();
- // set the theme for the current Admin Form
- $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
- return $this->render($this->admin->getTemplate($templateKey), array(
- 'action' => 'edit',
- 'form' => $view,
- 'object' => $object,
- ));
- }
- /**
- * Redirect the user depend on this choice
- *
- * @param object $object
- *
- * @return RedirectResponse
- */
- protected function redirectTo($object)
- {
- $url = false;
- if (null !== $this->get('request')->get('btn_update_and_list')) {
- $url = $this->admin->generateUrl('list');
- }
- if (null !== $this->get('request')->get('btn_create_and_list')) {
- $url = $this->admin->generateUrl('list');
- }
- if (null !== $this->get('request')->get('btn_create_and_create')) {
- $params = array();
- if ($this->admin->hasActiveSubClass()) {
- $params['subclass'] = $this->get('request')->get('subclass');
- }
- $url = $this->admin->generateUrl('create', $params);
- }
- if ($this->getRestMethod() == 'DELETE') {
- $url = $this->admin->generateUrl('list');
- }
- if (!$url) {
- $url = $this->admin->generateObjectUrl('edit', $object);
- }
- return new RedirectResponse($url);
- }
- /**
- * Batch action
- *
- * @return Response|RedirectResponse
- *
- * @throws NotFoundHttpException If the HTTP method is not POST
- * @throws \RuntimeException If the batch action is not defined
- */
- public function batchAction()
- {
- $restMethod = $this->getRestMethod();
- if ('POST' !== $restMethod) {
- throw $this->createNotFoundException(sprintf('Invalid request type "%s", POST expected', $restMethod));
- }
- // check the csrf token
- $this->validateCsrfToken('sonata.batch');
- $confirmation = $this->get('request')->get('confirmation', false);
- if ($data = json_decode($this->get('request')->get('data'), true)) {
- $action = $data['action'];
- $idx = $data['idx'];
- $allElements = $data['all_elements'];
- $this->get('request')->request->replace($data);
- } else {
- $this->get('request')->request->set('idx', $this->get('request')->get('idx', array()));
- $this->get('request')->request->set('all_elements', $this->get('request')->get('all_elements', false));
- $action = $this->get('request')->get('action');
- $idx = $this->get('request')->get('idx');
- $allElements = $this->get('request')->get('all_elements');
- $data = $this->get('request')->request->all();
- unset($data['_sonata_csrf_token']);
- }
- $batchActions = $this->admin->getBatchActions();
- if (!array_key_exists($action, $batchActions)) {
- throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
- }
- $camelizedAction = BaseFieldDescription::camelize($action);
- $isRelevantAction = sprintf('batchAction%sIsRelevant', ucfirst($camelizedAction));
- if (method_exists($this, $isRelevantAction)) {
- $nonRelevantMessage = call_user_func(array($this, $isRelevantAction), $idx, $allElements);
- } else {
- $nonRelevantMessage = count($idx) != 0 || $allElements; // at least one item is selected
- }
- if (!$nonRelevantMessage) { // default non relevant message (if false of null)
- $nonRelevantMessage = 'flash_batch_empty';
- }
- $datagrid = $this->admin->getDatagrid();
- $datagrid->buildPager();
- if (true !== $nonRelevantMessage) {
- $this->addFlash('sonata_flash_info', $nonRelevantMessage);
- return new RedirectResponse(
- $this->admin->generateUrl(
- 'list',
- array('filter' => $this->admin->getFilterParameters())
- )
- );
- }
- $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ?
- $batchActions[$action]['ask_confirmation'] :
- true;
- if ($askConfirmation && $confirmation != 'ok') {
- $actionLabel = $batchActions[$action]['label'];
- $formView = $datagrid->getForm()->createView();
- return $this->render($this->admin->getTemplate('batch_confirmation'), array(
- 'action' => 'list',
- 'action_label' => $actionLabel,
- 'datagrid' => $datagrid,
- 'form' => $formView,
- 'data' => $data,
- 'csrf_token' => $this->getCsrfToken('sonata.batch'),
- ));
- }
- // execute the action, batchActionXxxxx
- $finalAction = sprintf('batchAction%s', ucfirst($camelizedAction));
- if (!method_exists($this, $finalAction)) {
- throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $finalAction));
- }
- $query = $datagrid->getQuery();
- $query->setFirstResult(null);
- $query->setMaxResults(null);
- $this->admin->preBatchAction($action, $query, $idx, $allElements);
- if (count($idx) > 0) {
- $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
- } elseif (!$allElements) {
- $query = null;
- }
- return call_user_func(array($this, $finalAction), $query);
- }
- /**
- * Create action
- *
- * @return Response
- *
- * @throws AccessDeniedException If access is not granted
- */
- public function createAction()
- {
- // the key used to lookup the template
- $templateKey = 'edit';
- if (false === $this->admin->isGranted('CREATE')) {
- throw new AccessDeniedException();
- }
- $object = $this->admin->getNewInstance();
- $this->admin->setSubject($object);
- /** @var $form \Symfony\Component\Form\Form */
- $form = $this->admin->getForm();
- $form->setData($object);
- if ($this->getRestMethod()== 'POST') {
- $form->submit($this->get('request'));
- $isFormValid = $form->isValid();
- // persist if the form was valid and if in preview mode the preview was approved
- if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
- if (false === $this->admin->isGranted('CREATE', $object)) {
- throw new AccessDeniedException();
- }
- try {
- $object = $this->admin->create($object);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array(
- 'result' => 'ok',
- 'objectId' => $this->admin->getNormalizedIdentifier($object)
- ));
- }
- $this->addFlash(
- 'sonata_flash_success',
- $this->admin->trans(
- 'flash_create_success',
- array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle'
- )
- );
- // redirect to edit mode
- return $this->redirectTo($object);
- } catch (ModelManagerException $e) {
- $this->logModelManagerException($e);
- $isFormValid = false;
- }
- }
- // show an error message if the form failed validation
- if (!$isFormValid) {
- if (!$this->isXmlHttpRequest()) {
- $this->addFlash(
- 'sonata_flash_error',
- $this->admin->trans(
- 'flash_create_error',
- array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle'
- )
- );
- }
- } elseif ($this->isPreviewRequested()) {
- // pick the preview template if the form was valid and preview was requested
- $templateKey = 'preview';
- $this->admin->getShow();
- }
- }
- $view = $form->createView();
- // set the theme for the current Admin Form
- $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
- return $this->render($this->admin->getTemplate($templateKey), array(
- 'action' => 'create',
- 'form' => $view,
- 'object' => $object,
- ));
- }
- /**
- * Returns true if the preview is requested to be shown
- *
- * @return bool
- */
- protected function isPreviewRequested()
- {
- return ($this->get('request')->get('btn_preview') !== null);
- }
- /**
- * Returns true if the preview has been approved
- *
- * @return bool
- */
- protected function isPreviewApproved()
- {
- return ($this->get('request')->get('btn_preview_approve') !== null);
- }
- /**
- * Returns true if the request is in the preview workflow
- *
- * That means either a preview is requested or the preview has already been shown
- * and it got approved/declined.
- *
- * @return bool
- */
- protected function isInPreviewMode()
- {
- return $this->admin->supportsPreviewMode()
- && ($this->isPreviewRequested()
- || $this->isPreviewApproved()
- || $this->isPreviewDeclined());
- }
- /**
- * Returns true if the preview has been declined
- *
- * @return bool
- */
- protected function isPreviewDeclined()
- {
- return ($this->get('request')->get('btn_preview_decline') !== null);
- }
- /**
- * Show action
- *
- * @param int|string|null $id
- *
- * @return Response
- *
- * @throws NotFoundHttpException If the object does not exist
- * @throws AccessDeniedException If access is not granted
- */
- public function showAction($id = null)
- {
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('VIEW', $object)) {
- throw new AccessDeniedException();
- }
- $this->admin->setSubject($object);
- return $this->render($this->admin->getTemplate('show'), array(
- 'action' => 'show',
- 'object' => $object,
- 'elements' => $this->admin->getShow(),
- ));
- }
- /**
- * Show history revisions for object
- *
- * @param int|string|null $id
- *
- * @return Response
- *
- * @throws AccessDeniedException If access is not granted
- * @throws NotFoundHttpException If the object does not exist or the audit reader is not available
- */
- public function historyAction($id = null)
- {
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('EDIT', $object)) {
- throw new AccessDeniedException();
- }
- $manager = $this->get('sonata.admin.audit.manager');
- if (!$manager->hasReader($this->admin->getClass())) {
- throw new NotFoundHttpException(
- sprintf(
- 'unable to find the audit reader for class : %s',
- $this->admin->getClass()
- )
- );
- }
- $reader = $manager->getReader($this->admin->getClass());
- $revisions = $reader->findRevisions($this->admin->getClass(), $id);
- return $this->render($this->admin->getTemplate('history'), array(
- 'action' => 'history',
- 'object' => $object,
- 'revisions' => $revisions,
- 'currentRevision' => $revisions ? current($revisions) : false,
- ));
- }
- /**
- * View history revision of object
- *
- * @param int|string|null $id
- * @param string|null $revision
- *
- * @return Response
- *
- * @throws AccessDeniedException If access is not granted
- * @throws NotFoundHttpException If the object or revision does not exist or the audit reader is not available
- */
- public function historyViewRevisionAction($id = null, $revision = null)
- {
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('EDIT', $object)) {
- throw new AccessDeniedException();
- }
- $manager = $this->get('sonata.admin.audit.manager');
- if (!$manager->hasReader($this->admin->getClass())) {
- throw new NotFoundHttpException(
- sprintf(
- 'unable to find the audit reader for class : %s',
- $this->admin->getClass()
- )
- );
- }
- $reader = $manager->getReader($this->admin->getClass());
- // retrieve the revisioned object
- $object = $reader->find($this->admin->getClass(), $id, $revision);
- if (!$object) {
- throw new NotFoundHttpException(
- sprintf(
- 'unable to find the targeted object `%s` from the revision `%s` with classname : `%s`',
- $id,
- $revision,
- $this->admin->getClass()
- )
- );
- }
- $this->admin->setSubject($object);
- return $this->render($this->admin->getTemplate('show'), array(
- 'action' => 'show',
- 'object' => $object,
- 'elements' => $this->admin->getShow(),
- ));
- }
- /**
- * Compare history revisions of object
- *
- * @param int|string|null $id
- * @param int|string|null $base_revision
- * @param int|string|null $compare_revision
- *
- * @return Response
- *
- * @throws AccessDeniedException If access is not granted
- * @throws NotFoundHttpException If the object or revision does not exist or the audit reader is not available
- */
- public function historyCompareRevisionsAction($id = null, $base_revision = null, $compare_revision = null)
- {
- if (false === $this->admin->isGranted('EDIT')) {
- throw new AccessDeniedException();
- }
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- $manager = $this->get('sonata.admin.audit.manager');
- if (!$manager->hasReader($this->admin->getClass())) {
- throw new NotFoundHttpException(
- sprintf(
- 'unable to find the audit reader for class : %s',
- $this->admin->getClass()
- )
- );
- }
- $reader = $manager->getReader($this->admin->getClass());
- // retrieve the base revision
- $base_object = $reader->find($this->admin->getClass(), $id, $base_revision);
- if (!$base_object) {
- throw new NotFoundHttpException(
- sprintf(
- 'unable to find the targeted object `%s` from the revision `%s` with classname : `%s`',
- $id,
- $base_revision,
- $this->admin->getClass()
- )
- );
- }
- // retrieve the compare revision
- $compare_object = $reader->find($this->admin->getClass(), $id, $compare_revision);
- if (!$compare_object) {
- throw new NotFoundHttpException(
- sprintf(
- 'unable to find the targeted object `%s` from the revision `%s` with classname : `%s`',
- $id,
- $compare_revision,
- $this->admin->getClass()
- )
- );
- }
- $this->admin->setSubject($base_object);
- return $this->render($this->admin->getTemplate('show_compare'), array(
- 'action' => 'show',
- 'object' => $base_object,
- 'object_compare' => $compare_object,
- 'elements' => $this->admin->getShow()
- ));
- }
- /**
- * 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)
- {
- if (false === $this->admin->isGranted('EXPORT')) {
- throw new AccessDeniedException();
- }
- $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()
- );
- }
- /**
- * Gets ACL users
- *
- * @return \Traversable
- */
- protected function getAclUsers()
- {
- $aclUsers = array();
- $userManagerServiceName = $this->container->getParameter('sonata.admin.security.acl_user_manager');
- if ($userManagerServiceName !== null && $this->has($userManagerServiceName)) {
- $userManager = $this->get($userManagerServiceName);
- if (method_exists($userManager, 'findUsers')) {
- $aclUsers = $userManager->findUsers();
- }
- }
- return is_array($aclUsers) ? new \ArrayIterator($aclUsers) : $aclUsers;
- }
- /**
- * Returns the Response object associated to the acl action
- *
- * @param int|string|null $id
- *
- * @return Response|RedirectResponse
- *
- * @throws AccessDeniedException If access is not granted.
- * @throws NotFoundHttpException If the object does not exist or the ACL is not enabled
- */
- public function aclAction($id = null)
- {
- if (!$this->admin->isAclEnabled()) {
- throw new NotFoundHttpException('ACL are not enabled for this admin');
- }
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('MASTER', $object)) {
- throw new AccessDeniedException();
- }
- $this->admin->setSubject($object);
- $aclUsers = $this->getAclUsers();
- $adminObjectAclManipulator = $this->get('sonata.admin.object.manipulator.acl.admin');
- $adminObjectAclData = new AdminObjectAclData(
- $this->admin,
- $object,
- $aclUsers,
- $adminObjectAclManipulator->getMaskBuilderClass()
- );
- $form = $adminObjectAclManipulator->createForm($adminObjectAclData);
- $request = $this->getRequest();
- if ($request->getMethod() === 'POST') {
- $form->submit($request);
- if ($form->isValid()) {
- $adminObjectAclManipulator->updateAcl($adminObjectAclData);
- $this->addFlash('sonata_flash_success', 'flash_acl_edit_success');
- return new RedirectResponse($this->admin->generateObjectUrl('acl', $object));
- }
- }
- return $this->render($this->admin->getTemplate('acl'), array(
- 'action' => 'acl',
- 'permissions' => $adminObjectAclData->getUserPermissions(),
- 'object' => $object,
- 'users' => $aclUsers,
- 'form' => $form->createView()
- ));
- }
- /**
- * Adds a flash message for type.
- *
- * @param string $type
- * @param string $message
- */
- protected function addFlash($type, $message)
- {
- $this->get('session')
- ->getFlashBag()
- ->add($type, $message);
- }
- /**
- * Validate CSRF token for action without form
- *
- * @param string $intention
- *
- * @throws HttpException
- */
- protected function validateCsrfToken($intention)
- {
- if (!$this->container->has('form.csrf_provider')) {
- return;
- }
- if (!$this->container->get('form.csrf_provider')->isCsrfTokenValid(
- $intention,
- $this->get('request')->request->get('_sonata_csrf_token', false)
- )) {
- throw new HttpException(400, 'The csrf token is not valid, CSRF attack?');
- }
- }
- /**
- * Get CSRF token
- *
- * @param string $intention
- *
- * @return string|false
- */
- protected function getCsrfToken($intention)
- {
- if (!$this->container->has('form.csrf_provider')) {
- return false;
- }
- return $this->container->get('form.csrf_provider')->generateCsrfToken($intention);
- }
- }
|