瀏覽代碼

Merge remote-tracking branch 'tyx/delete_list'

Thomas 14 年之前
父節點
當前提交
f7700f5f6c

+ 11 - 0
Admin/Admin.php

@@ -702,6 +702,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',

+ 36 - 0
Builder/ORM/ListBuilder.php

@@ -41,6 +41,10 @@ class ListBuilder implements ListBuilderInterface
      */
     public function fixFieldDescription(Admin $admin, FieldDescription $fieldDescription, array $options = array())
     {
+        if ($fieldDescription->getName() == '_action')
+        {
+          $this->buildActionFieldDescription($fieldDescription);
+        }
 
         $fieldDescription->mergeOptions($options);
         $fieldDescription->setAdmin($admin);
@@ -99,4 +103,36 @@ class ListBuilder implements ListBuilderInterface
             $admin->attachAdminClass($fieldDescription);
         }
     }
+    
+    public function buildActionFieldDescription(FieldDescription $fieldDescription)
+    {
+        if (null === $fieldDescription->getTemplate()) {
+            $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
+        }
+        
+        if (null === $fieldDescription->getType()) {
+            $fieldDescription->setType('action');
+        }
+        
+        if (null === $fieldDescription->getOption('name')) {
+            $fieldDescription->setOption('name', 'Action');
+        }
+        
+        if (null === $fieldDescription->getOption('code')) {
+            $fieldDescription->setOption('code', 'Action');
+        }
+        
+        if (null !== $fieldDescription->getOption('actions')) {
+            $actions = $fieldDescription->getOption('actions');
+            foreach ($actions as $k => $action) {
+                if (!isset($action['template'])) {
+                    $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action%s.html.twig', $k);
+                }
+            }
+            
+            $fieldDescription->setOption('actions', $actions);
+        }
+      
+        return $fieldDescription;
+    }
 }

+ 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'));
     }
 
     /**

+ 28 - 1
Resources/doc/reference/list_field_definition.rst

@@ -20,7 +20,7 @@ Example
             'title'   => array(),
             'enabled' => array('type' => 'boolean'),
             'tags'    => array(),
-            'summary' => array(),
+            'summary' => array()
         );
 
         protected function configureListFields(ListMapper $list) // optional
@@ -52,6 +52,33 @@ types include:
 If no type is set, the ``Admin`` class will use the type defined in the doctrine
 mapping definition.
 
+List Actions
+------------
+
+You can set actions for each items in list by adding in $list, the '_action' field :
+
+.. code-block:: php
+
+    '_action' => array(
+      'actions' => array(
+        '_delete' => array(),
+        '_edit' => array()
+      )
+    )
+
+Edit and delete actions are available in default configuration. You can add your own! Default template file is :
+    SonataAdminBundle:CRUD:list__action[ACTION_NAME].html.twig
+  
+But you can specify yours by setup 'template' option like :
+.. code-block:: php
+
+    '_action' => array(
+      'actions' => array(
+        '_delete' => array('template' => 'MyBundle:MyController:my_partial.html.twig'),
+        '_edit' => array()
+      )
+    )
+
 Tweak it!
 ---------
 

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

@@ -90,6 +90,14 @@
                 <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>
+            <trans-unit id="action_edit">
+                <source>action_edit</source>
+                <target>Edit</target>
+            </trans-unit>
         </body>
     </file>
 </xliff>

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

@@ -74,6 +74,14 @@
                 <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>
+            <trans-unit id="action_edit">
+                <source>action_edit</source>
+                <target>Editer</target>
+            </trans-unit>
         </body>
     </file>
 </xliff>

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

@@ -82,12 +82,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>
 

+ 5 - 0
Resources/views/CRUD/list__action.html.twig

@@ -0,0 +1,5 @@
+<td class="sonata-ba-list-field sonata-ba-list-field-action">
+    {% for actions in field_description.options.actions %}
+        {% include actions.template %}
+    {% endfor %}
+</td>

+ 3 - 0
Resources/views/CRUD/list__action_delete.html.twig

@@ -0,0 +1,3 @@
+<a href="{{ admin.generateUrl('delete', {'id': object.id}) }}" class="delete_link" title="{% trans 'action_delete' from 'AdminBundle' %}">
+    <img src="{{ asset('bundles/sonataadmin/famfamfam/delete.png') }}" alt="{% trans 'action_delete' from 'AdminBundle' %}" />
+</a>

+ 3 - 0
Resources/views/CRUD/list__action_edit.html.twig

@@ -0,0 +1,3 @@
+<a href="{{ admin.generateUrl('edit', {'id': object.id}) }}" class="edit_link" title="{% trans 'action_edit' from 'AdminBundle' %}">
+    <img src="{{ asset('bundles/sonataadmin/famfamfam/page_white_edit.png') }}" alt="{% trans 'action_edit' from 'AdminBundle' %}" />
+</a>