فهرست منبع

first implementation for delete in view list

Timothée Barray 14 سال پیش
والد
کامیت
5109228ab7

+ 11 - 0
Admin/Admin.php

@@ -703,6 +703,17 @@ abstract class Admin implements AdminInterface
                 'options' => array(),
                 'params'    => array(),
             ),
+            $this->baseCodeRoute . '.delete' => array(
+                'name'      => $this->getBaseRouteName().'_delete',
+                'pattern'   => $this->getBaseRoutePattern().'/'.$this->getRouterIdParameter().'/delete',
+                'defaults'  => array(
+                    '_controller' => $this->getBaseControllerName().':delete',
+                    '_sonata_admin' => $this->baseCodeRoute
+                ),
+                'requirements' => array(),
+                'options' => array(),
+                'params'    => array(),
+            ),
             $this->baseCodeRoute . '.batch' => array(
                 'name'      => $this->getBaseRouteName().'_batch',
                 'pattern'       => $this->getBaseRoutePattern().'/batch',

+ 12 - 1
Controller/CRUDController.php

@@ -165,7 +165,18 @@ class CRUDController extends Controller
 
     public function deleteAction($id)
     {
-        // todo
+        $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));
+        }
+        
+        $em = $this->admin->getModelManager();
+        $em->remove($object);
+        $em->flush();
+        
+        return new RedirectResponse($this->admin->generateUrl('list'));
     }
 
     /**

+ 4 - 0
Resources/translations/AdminBundle.en.xliff

@@ -90,6 +90,10 @@
                 <source>no_result</source>
                 <target>No result</target>
             </trans-unit>
+            <trans-unit id="confirm_msg">
+                <source>confirm_msg</source>
+                <target>Are you sure ?</target>
+            </trans-unit>
         </body>
     </file>
 </xliff>

+ 4 - 0
Resources/translations/AdminBundle.fr.xliff

@@ -74,6 +74,10 @@
                 <source>no_result</source>
                 <target>Aucun résultat</target>
             </trans-unit>
+            <trans-unit id="confirm_msg">
+                <source>confirm_msg</source>
+                <target>Êtes-vous sûr ?</target>
+            </trans-unit>
         </body>
     </file>
 </xliff>

+ 35 - 1
Resources/views/CRUD/base_list.html.twig

@@ -22,10 +22,36 @@ file that was distributed with this source code.
 {% block side_menu %}{% if side_menu %}{{ side_menu.render|raw }}{% endif %}{% endblock %}
 
 {% block list_table %}
+<<<<<<< HEAD
     {% if datagrid.results|length > 0 %}
         <form action="{{ admin.generateUrl('batch') }}" method="POST" >
             <table>
                 {% block table_header %}
+=======
+    <form action="{{ admin.generateUrl('batch') }}" method="POST" >
+        <table>
+            {% block table_header %}
+                <tr>
+                    {% for field_description in list.elements %}
+                        {% if field_description.getOption('code') == '_batch' %}
+                            <th class="sonata-ba-list-field-header sonata-ba-list-field-header-batch"><input type="checkbox" id="list_batch_checkbox" /></th>
+                        {% else %}
+                                {% spaceless %}<th class="sonata-ba-list-field-header sonata-ba-list-field-header-{{ field_description.type}}">
+                                    {% if field_description.options.name is defined %}
+                                        {% trans field_description.options.name from admin.translationdomain %}
+                                    {% else %}
+                                        {% trans field_description.name from admin.translationdomain %}
+                                    {% endif %}
+                                </th>{% endspaceless %}
+                        {% endif %}
+                    {% endfor %}
+                </tr>
+            {% endblock %}
+
+
+            {% block table_body %}
+                {% for object in datagrid.results %}
+>>>>>>> first implementation for delete in view list
                     <tr>
                         {% for field_description in list.elements %}
                             {% if field_description.getOption('code') == '_batch' %}
@@ -42,6 +68,11 @@ file that was distributed with this source code.
                                     </th>{% endspaceless %}
                             {% endif %}
                         {% endfor %}
+                        <td>
+                            <a href="{{ admin.generateUrl('delete', {'id': object.id}) }}" class="delete_link">
+                              <img src="{{ asset('bundles/sonataadmin/famfamfam/delete.png') }}" alt="{% trans 'list_delete' from 'AdminBundle' %}" title="{% trans 'list_delete' from 'AdminBundle' %}" />
+                            </a>
+                        </td>
                     </tr>
                 {% endblock %}
 
@@ -82,12 +113,15 @@ file that was distributed with this source code.
                     {% endif %}
                 {% endblock %}
             </table>
-        
+
             <script type="text/javascript">
                 jQuery(document).ready(function($){
                    $('#list_batch_checkbox').click(function(){
                        $(this).closest('table').find("td input[type='checkbox']").attr('checked', $(this).is(':checked'));
                    });
+                   $('.delete_link').click(function(e){
+                      if (!confirm('{% trans 'confirm_msg' from 'AdminBundle' %}')) e.preventDefault();
+                   });
                 });
             </script>