Skip to content

Commit

Permalink
Adding user flash notifications for CRUD actions
Browse files Browse the repository at this point in the history
Translations for En and fr
  • Loading branch information
Max Teneur committed Jul 29, 2011
1 parent e8dfbbb commit 1aafd83
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ public function batchActionDelete($query)

$modelManager = $this->admin->getModelManager();
$modelManager->batchDelete($this->admin->getClass(), $query);
$this->get('session')->setFlash('sonata_flash_success', 'sonata_flash_batch_delete_success');


// todo : add confirmation flash var
return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
}

Expand All @@ -165,7 +166,7 @@ public function deleteAction($id)
}

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

$this->get('session')->setFlash('sonata_flash_success', 'sonata_flash_delete_success');
return new RedirectResponse($this->admin->generateUrl('list'));
}

Expand Down Expand Up @@ -194,10 +195,11 @@ public function editAction($id)

if ($this->get('request')->getMethod() == 'POST') {
$form->bindRequest($this->get('request'));

if ($form->isValid()) {
$this->admin->update($object);

$this->get('session')->setFlash('sonata_flash_success', 'sonata_flash_edit_success');

if ($this->isXmlHttpRequest()) {
return $this->renderJson(array(
'result' => 'ok',
Expand All @@ -208,6 +210,7 @@ public function editAction($id)
// redirect to edit mode
return $this->redirectTo($object);
}
$this->get('session')->setFlash('sonata_flash_error', 'sonata_flash_edit_error');
}

return $this->render($this->admin->getEditTemplate(), array(
Expand Down Expand Up @@ -263,7 +266,7 @@ public function batchAction()
$all_elements = $this->get('request')->get('all_elements', false);

if (count($idx) == 0 && !$all_elements) { // no item selected
// todo : add flash information
$this->get('session')->setFlash('sonata_flash_notice', 'sonata_flash_batch_empty');

return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
}
Expand All @@ -290,7 +293,6 @@ public function batchAction()
if (count($idx) > 0) {
$this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
}

return call_user_func(array($this, $final_action), $query);
}

Expand Down Expand Up @@ -322,10 +324,11 @@ public function createAction()
'objectId' => $this->admin->getNormalizedIdentifier($object)
));
}

$this->get('session')->setFlash('sonata_flash_success','sonata_flash_create_success');
// redirect to edit mode
return $this->redirectTo($object);
}
$this->get('session')->setFlash('sonata_flash_error', 'sonata_flash_create_error');
}

return $this->render($this->admin->getEditTemplate(), array(
Expand Down
36 changes: 36 additions & 0 deletions Resources/translations/SonataAdminBundle.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,42 @@
<source>all_elements</source>
<target>All elements</target>
</trans-unit>
<trans-unit id='sonata_flash_batch_empty'>
<source>sonata_flash_batch_empty</source>
<target>Action aborted. No items where selected.</target>
</trans-unit>
<trans-unit id='sonata_flash_create_success'>
<source>sonata_flash_create_success</source>
<target>Item has been successfully created.</target>
</trans-unit>
<trans-unit id='sonata_flash_create_error'>
<source>sonata_flash_create_error</source>
<target>An error has occurred during item creation.</target>
</trans-unit>
<trans-unit id='sonata_flash_edit_success'>
<source>sonata_flash_edit_success</source>
<target>Item has been successfully updated.</target>
</trans-unit>
<trans-unit id='sonata_flash_edit_error'>
<source>sonata_flash_edit_error</source>
<target>An error has occurred during item update.</target>
</trans-unit>
<trans-unit id='sonata_flash_batch_delete_success'>
<source>sonata_flash_batch_delete_success</source>
<target>Selected items have been successfully deleted.</target>
</trans-unit>
<trans-unit id='sonata_flash_batch_delete_error'>
<source>sonata_flash_batch_delete_error</source>
<target>An Error has occurred during selected items deletion.</target>
</trans-unit>
<trans-unit id='sonata_flash_delete_error'>
<source>sonata_flash_delete_error</source>
<target>An Error has occurred during item deletion.</target>
</trans-unit>
<trans-unit id='sonata_flash_delete_success'>
<source>sonata_flash_delete_success</source>
<target>Item has been deleted successfully.</target>
</trans-unit>
</body>
</file>
</xliff>
36 changes: 36 additions & 0 deletions Resources/translations/SonataAdminBundle.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,42 @@
<source>all_elements</source>
<target>Tous les éléments</target>
</trans-unit>
<trans-unit id='sonata_flash_batch_empty'>
<source>sonata_flash_batch_empty</source>
<target>Action interrompue. Aucun élément n'a été séléctionné.</target>
</trans-unit>
<trans-unit id='sonata_flash_create_success'>
<source>sonata_flash_create_success</source>
<target>L'élément a été crée avec succès.</target>
</trans-unit>
<trans-unit id='sonata_flash_create_error'>
<source>sonata_flash_create_error</source>
<target>Une erreur est intervenue lors de la création de l'élément.</target>
</trans-unit>
<trans-unit id='sonata_flash_edit_success'>
<source>sonata_flash_edit_success</source>
<target>L'élément a été mis à jour avec succès.</target>
</trans-unit>
<trans-unit id='sonata_flash_edit_error'>
<source>sonata_flash_edit_error</source>
<target>Une erreur est intervenue lors de la mise à jour de l'élément.</target>
</trans-unit>
<trans-unit id='sonata_flash_batch_delete_success'>
<source>sonata_flash_batch_delete_success</source>
<target>Les éléments séléctionnés ont été supprimés avec succès.</target>
</trans-unit>
<trans-unit id='sonata_flash_batch_delete_error'>
<source>sonata_flash_batch_delete_error</source>
<target>Une erreur est intervenue lors de la suppression des éléments séléctionnés.</target>
</trans-unit>
<trans-unit id='sonata_flash_delete_error'>
<source>sonata_flash_delete_error</source>
<target>Une erreur est intervenue lors de la suppression de l'élément.</target>
</trans-unit>
<trans-unit id='sonata_flash_delete_success'>
<source>sonata_flash_delete_success</source>
<target>Les éléments ont été supprimés avec succès.</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions Resources/views/standard_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ file that was distributed with this source code.
{% endblock %}
</h1>
</div>

{# flash notice defined in CRUDController after actions- 3 levels #}
{% for notice_level in ['success','error','notice'] %}
{%set session_var = ( ['sonata_flash_',notice_level]|join)%}
{% if app.session.hasFlash(session_var) %}
<div class="{{notice_level}}">
{{ app.session.flash(session_var) | trans([],'SonataAdminBundle') }}
</div>
{%endif%}
{% endfor %}

</div>

{% if preview is not empty %}
Expand Down

0 comments on commit 1aafd83

Please sign in to comment.