Преглед на файлове

CRUDController now uses a new API to set the session flash message.
It also allows you to set multiple flash messages for the same type of message.

imenem преди 12 години
родител
ревизия
40126a25c3
променени са 2 файла, в които са добавени 25 реда и са изтрити 12 реда
  1. 22 9
      Controller/CRUDController.php
  2. 3 3
      Resources/doc/reference/batch_actions.rst

+ 22 - 9
Controller/CRUDController.php

@@ -199,9 +199,9 @@ class CRUDController extends Controller
         $modelManager = $this->admin->getModelManager();
         try {
             $modelManager->batchDelete($this->admin->getClass(), $query);
-            $this->get('session')->setFlash('sonata_flash_success', 'flash_batch_delete_success');
+            $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
         } catch ( ModelManagerException $e ) {
-            $this->get('session')->setFlash('sonata_flash_error', 'flash_batch_delete_error');
+            $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
         }
 
         return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
@@ -235,7 +235,7 @@ class CRUDController extends Controller
                     return $this->renderJson(array('result' => 'ok'));
                 }
 
-                $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');
+                $this->addFlash('sonata_flash_success', 'flash_delete_success');
 
             } catch (ModelManagerException $e) {
 
@@ -243,7 +243,7 @@ class CRUDController extends Controller
                     return $this->renderJson(array('result' => 'error'));
                 }
 
-                $this->get('session')->setFlash('sonata_flash_error', 'flash_delete_error');
+                $this->addFlash('sonata_flash_error', 'flash_delete_error');
             }
 
             return new RedirectResponse($this->admin->generateUrl('list'));
@@ -297,7 +297,7 @@ class CRUDController extends Controller
             // persist if the form was valid and if in preview mode the preview was approved
             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
                 $this->admin->update($object);
-                $this->get('session')->setFlash('sonata_flash_success', 'flash_edit_success');
+                $this->addFlash('sonata_flash_success', 'flash_edit_success');
 
                 if ($this->isXmlHttpRequest()) {
                     return $this->renderJson(array(
@@ -313,7 +313,7 @@ class CRUDController extends Controller
             // show an error message if the form failed validation
             if (!$isFormValid) {
                 if (!$this->isXmlHttpRequest()) {
-                    $this->get('session')->setFlash('sonata_flash_error', 'flash_edit_error');
+                    $this->addFlash('sonata_flash_error', 'flash_edit_error');
                 }
             } elseif ($this->isPreviewRequested()) {
                 // enable the preview template if the form was valid and preview was requested
@@ -417,7 +417,7 @@ class CRUDController extends Controller
         $datagrid->buildPager();
 
         if (true !== $nonRelevantMessage) {
-            $this->get('session')->setFlash('sonata_flash_info', $nonRelevantMessage);
+            $this->addFlash('sonata_flash_info', $nonRelevantMessage);
 
             return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
         }
@@ -494,7 +494,7 @@ class CRUDController extends Controller
                     ));
                 }
 
-                $this->get('session')->setFlash('sonata_flash_success','flash_create_success');
+                $this->addFlash('sonata_flash_success','flash_create_success');
                 // redirect to edit mode
                 return $this->redirectTo($object);
             }
@@ -502,7 +502,7 @@ class CRUDController extends Controller
             // show an error message if the form failed validation
             if (!$isFormValid) {
                 if (!$this->isXmlHttpRequest()) {
-                    $this->get('session')->setFlash('sonata_flash_error', 'flash_create_error');
+                    $this->addFlash('sonata_flash_error', 'flash_create_error');
                 }
             } elseif ($this->isPreviewRequested()) {
                 // pick the preview template if the form was valid and preview was requested
@@ -716,4 +716,17 @@ class CRUDController extends Controller
 
         return $this->get('sonata.admin.exporter')->getResponse($format, $filename, $this->admin->getDataSourceIterator());
     }
+
+    /**
+     * Adds a flash message for type.
+     *
+     * @param string $type
+     * @param string $message
+     */
+    public function addFlash($type, $message)
+    {
+        $this->get('session')
+             ->getFlashBag()
+             ->add($type, $message);
+    }
 }

+ 3 - 3
Resources/doc/reference/batch_actions.rst

@@ -150,7 +150,7 @@ passed query is ``null``.
         $target = $modelManager->find($this->admin->getClass(), $request->get('targetId'));
 
         if( $target === null){
-            $this->get('session')->setFlash('sonata_flash_info', 'flash_batch_merge_no_target');
+            $this->addFlash('sonata_flash_info', 'flash_batch_merge_no_target');
 
             return new RedirectResponse($this->admin->generateUrl('list',$this->admin->getFilterParameters()));
         }
@@ -166,12 +166,12 @@ passed query is ``null``.
 
             $modelManager->update($selectedModel);
         } catch (\Exception $e) {
-            $this->get('session')->setFlash('sonata_flash_error', 'flash_batch_merge_error');
+            $this->addFlash('sonata_flash_error', 'flash_batch_merge_error');
 
             return new RedirectResponse($this->admin->generateUrl('list',$this->admin->getFilterParameters()));
         }
 
-        $this->get('session')->setFlash('sonata_flash_success', 'flash_batch_merge_success');
+        $this->addFlash('sonata_flash_success', 'flash_batch_merge_success');
 
         return new RedirectResponse($this->admin->generateUrl('list',$this->admin->getFilterParameters()));
     }