Skip to content

Commit

Permalink
Merge pull request sonata-project#2918 from pulzarraider/preaction
Browse files Browse the repository at this point in the history
Added preCreate, preEdit, preDelete, preShow, preList methods in the CRUDController
  • Loading branch information
rande committed May 5, 2015
2 parents dd1ef41 + 03392c2 commit afe81c0
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 58 deletions.
115 changes: 99 additions & 16 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ public function listAction(Request $request = null)
throw new AccessDeniedException();
}

$preResponse = $this->preList($request);
if ($preResponse !== null) {
return $preResponse;
}

if ($listMode = $request->get('_list_mode')) {
$this->admin->setListMode($listMode);
}
Expand Down Expand Up @@ -284,7 +289,6 @@ public function batchActionDelete(ProxyQueryInterface $query)
$modelManager->batchDelete($this->admin->getClass(), $query);
$this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
} catch (ModelManagerException $e) {

$this->handleModelManagerException($e);
$this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
}
Expand Down Expand Up @@ -320,12 +324,17 @@ public function deleteAction($id, Request $request = null)
throw new AccessDeniedException();
}

if ($this->getRestMethod($request) == 'DELETE') {
$preResponse = $this->preDelete($request, $object);
if ($preResponse !== null) {
return $preResponse;
}

if ($this->getRestMethod($request) === 'DELETE') {
// check the csrf token
$this->validateCsrfToken('sonata.delete', $request);

$objectName = $this->admin->toString($object);

try {
$this->admin->delete($object);

Expand All @@ -341,7 +350,6 @@ public function deleteAction($id, Request $request = null)
'SonataAdminBundle'
)
);

} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);

Expand All @@ -365,7 +373,7 @@ public function deleteAction($id, Request $request = null)
return $this->render($this->admin->getTemplate('delete'), array(
'object' => $object,
'action' => 'delete',
'csrf_token' => $this->getCsrfToken('sonata.delete')
'csrf_token' => $this->getCsrfToken('sonata.delete'),
), null, $request);
}

Expand Down Expand Up @@ -397,27 +405,31 @@ public function editAction($id = null, Request $request = null)
throw new AccessDeniedException();
}

$preResponse = $this->preEdit($request, $object);
if ($preResponse !== null) {
return $preResponse;
}

$this->admin->setSubject($object);

/** @var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);

if ($this->getRestMethod($request) == 'POST') {
if ($this->getRestMethod($request) === 'POST') {
$form->submit($request);

$isFormValid = $form->isValid();

// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode($request) || $this->isPreviewApproved($request))) {

try {
$object = $this->admin->update($object);

if ($this->isXmlHttpRequest($request)) {
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
'objectId' => $this->admin->getNormalizedIdentifier($object),
), 200, array(), $request);
}

Expand All @@ -432,7 +444,6 @@ public function editAction($id = null, Request $request = null)

// redirect to edit mode
return $this->redirectTo($object, $request);

} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);

Expand Down Expand Up @@ -500,7 +511,7 @@ protected function redirectTo($object, Request $request = null)
$url = $this->admin->generateUrl('create', $params);
}

if ($this->getRestMethod($request) == 'DELETE') {
if ($this->getRestMethod($request) === 'DELETE') {
$url = $this->admin->generateUrl('list');
}

Expand Down Expand Up @@ -661,20 +672,24 @@ public function createAction(Request $request = null)

$object = $this->admin->getNewInstance();

$preResponse = $this->preCreate($request, $object);
if ($preResponse !== null) {
return $preResponse;
}

$this->admin->setSubject($object);

/** @var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);

if ($this->getRestMethod($request) == 'POST') {
if ($this->getRestMethod($request) === 'POST') {
$form->submit($request);

$isFormValid = $form->isValid();

// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode($request) || $this->isPreviewApproved($request))) {

if (false === $this->admin->isGranted('CREATE', $object)) {
throw new AccessDeniedException();
}
Expand All @@ -685,7 +700,7 @@ public function createAction(Request $request = null)
if ($this->isXmlHttpRequest($request)) {
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
'objectId' => $this->admin->getNormalizedIdentifier($object),
), 200, array(), $request);
}

Expand All @@ -700,7 +715,6 @@ public function createAction(Request $request = null)

// redirect to edit mode
return $this->redirectTo($object, $request);

} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);

Expand Down Expand Up @@ -827,6 +841,11 @@ public function showAction($id = null, Request $request = null)
throw new AccessDeniedException();
}

$preResponse = $this->preShow($request, $object);
if ($preResponse !== null) {
return $preResponse;
}

$this->admin->setSubject($object);

return $this->render($this->admin->getTemplate('show'), array(
Expand Down Expand Up @@ -1022,7 +1041,7 @@ public function historyCompareRevisionsAction($id = null, $base_revision = null,
'action' => 'show',
'object' => $base_object,
'object_compare' => $compare_object,
'elements' => $this->admin->getShow()
'elements' => $this->admin->getShow(),
), null, $request);
}

Expand Down Expand Up @@ -1204,7 +1223,7 @@ public function aclAction($id = null, Request $request = null)
'users' => $aclUsers,
'roles' => $aclRoles,
'aclUsersForm' => $aclUsersForm->createView(),
'aclRolesForm' => $aclRolesForm->createView()
'aclRolesForm' => $aclRolesForm->createView(),
), null, $request);
}

Expand Down Expand Up @@ -1273,6 +1292,70 @@ protected function getCsrfToken($intention)
return $this->container->get('form.csrf_provider')->generateCsrfToken($intention);
}

/**
* This method can be overloaded in your custom CRUD controller.
* It's called from createAction.
*
* @param Request $request
* @param mixed $object
*
* @return Response|null
*/
protected function preCreate(Request $request, $object)
{
}

/**
* This method can be overloaded in your custom CRUD controller.
* It's called from editAction.
*
* @param Request $request
* @param mixed $object
*
* @return Response|null
*/
protected function preEdit(Request $request, $object)
{
}

/**
* This method can be overloaded in your custom CRUD controller.
* It's called from deleteAction.
*
* @param Request $request
* @param mixed $object
*
* @return Response|null
*/
protected function preDelete(Request $request, $object)
{
}

/**
* This method can be overloaded in your custom CRUD controller.
* It's called from showAction.
*
* @param Request $request
* @param mixed $object
*
* @return Response|null
*/
protected function preShow(Request $request, $object)
{
}

/**
* This method can be overloaded in your custom CRUD controller.
* It's called from listAction.
*
* @param Request $request
*
* @return Response|null
*/
protected function preList(Request $request)
{
}

/**
* To keep backwards compatibility with older Sonata Admin code.
*
Expand Down
12 changes: 12 additions & 0 deletions Resources/doc/reference/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ a set of automatically injected dependencies that are useful when implementing s
scenarios. Refer to the existing ``CRUDController`` actions for examples of how to get
the best out of them.

In your overloaded CRUDController you can overload also these methods to limit
the number of duplicated code from SonataAdmin:
* ``preCreate``: called from ``createAction``
* ``preEdit``: called from ``editAction``
* ``preDelete``: called from ``deleteAction``
* ``preShow``: called from ``showAction``
* ``preList``: called from ``listAction``

These methods are called after checking the access rights and after retrieving the object
from database. You can use them if you need to redirect user to some other page under certain conditions.


Fields Definition
-----------------

Expand Down
Loading

0 comments on commit afe81c0

Please sign in to comment.