Browse Source

Add a delete action

Thomas Rabaix 13 years ago
parent
commit
40d97a5087

+ 15 - 3
Controller/CRUDController.php

@@ -171,6 +171,11 @@ class CRUDController extends Controller
         return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
     }
 
+    /**
+     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Symfony\Component\Security\Core\Exception\AccessDeniedException
+     * @param $id
+     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response|\Symfony\Component\HttpFoundation\RedirectResponse
+     */
     public function deleteAction($id)
     {
         if (false === $this->admin->isGranted('DELETE')) {
@@ -184,10 +189,17 @@ class CRUDController extends Controller
             throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
         }
 
-        $this->admin->delete($object);
-        $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');
+        if ($this->getRequest()->getMethod() == 'DELETE') {
+            $this->admin->delete($object);
+            $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');
 
-        return new RedirectResponse($this->admin->generateUrl('list'));
+            return new RedirectResponse($this->admin->generateUrl('list'));
+        }
+
+        return $this->render('SonataAdminBundle:CRUD:delete.html.twig', array(
+            'object' => $object,
+            'action' => 'delete'
+        ));
     }
 
     /**

+ 4 - 0
Resources/views/CRUD/base_edit.html.twig

@@ -87,6 +87,10 @@ file that was distributed with this source code.
                 {% if admin.id(object) %}
                     <input type="submit" name="btn_update_and_edit" value="{% trans from 'SonataAdminBundle' %}btn_update_and_edit_again{% endtrans %}"/>
                     <input type="submit" name="btn_update_and_list" value="{% trans from 'SonataAdminBundle' %}btn_update_and_return_to_list{% endtrans %}"/>
+
+                    {% if admin.isGranted('DELETE') %}
+                        <a href="{{ admin.generateObjectUrl('delete', object) }}">{% trans from 'SonataAdminBundle' %}link_delete{% endtrans %}</a>
+                    {% endif %}
                 {% else %}
                     <input type="submit" name="btn_create_and_edit" value="{% trans from 'SonataAdminBundle' %}btn_create_and_edit_again{% endtrans %}"/>
                     <input type="submit" name="btn_create_and_create" value="{% trans from 'SonataAdminBundle' %}btn_create_and_create_a_new_one{% endtrans %}"/>

+ 44 - 0
Resources/views/CRUD/delete.html.twig

@@ -0,0 +1,44 @@
+{#
+
+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.
+
+#}
+
+{% extends base_template %}
+
+{% block actions %}
+    <div class="sonata-actions">
+        <ul>
+            {% if admin.hasRoute('edit') and admin.isGranted('EDIT')%}
+                <li class="sonata-action-element"><a href="{{ admin.generateObjectUrl('edit', object) }}">{% trans from 'SonataAdminBundle' %}link_action_edit{% endtrans %}</a></li>
+            {% endif %}
+
+            {% if admin.hasRoute('create') and admin.isGranted('CREATE')%}
+                <li class="sonata-action-element"><a href="{{ admin.generateUrl('create') }}">{% trans from 'SonataAdminBundle' %}link_action_create{% endtrans %}</a></li>
+            {% endif %}
+        </ul>
+    </div>
+{% endblock %}
+
+{% block side_menu %}{{ admin.sidemenu(action)|knp_menu_render('list') }}{% endblock %}
+
+{% block content %}
+    <div class="sonata-ba-delete">
+
+        <h1>{% trans from 'SonataAdminBundle' %}title_delete{% endtrans %}</h1>
+
+        {% trans with {'%object%': object} from 'SonataAdminBundle' %}message_delete_confirmation{% endtrans %}
+
+        <form method="POST" action="{{ admin.generateObjectUrl('delete', object) }}">
+            <input type="hidden" value="DELETE" name="_method" />
+
+            <input type="submit" value="{% trans from 'SonataAdminBundle' %}btn_delete{% endtrans %}" />
+        </form>
+
+    </div>
+{% endblock %}