Browse Source

add batch support, add batch delete

Thomas 14 years ago
parent
commit
9949121fc0

+ 70 - 1
Controller/CRUDController.php

@@ -59,6 +59,14 @@ class CRUDController extends Controller
         return $query_buidler;
     }
 
+    public function getBatchActions()
+    {
+        
+        return array(
+            'delete' => 'Delete'
+        );
+    }
+
     public function getUrls()
     {
         return array(
@@ -81,6 +89,10 @@ class CRUDController extends Controller
             'edit'   => array(
                 'url'       => $this->base_route.'_edit',
                 'params'    => array()
+            ),
+            'batch'   => array(
+                'url'       => $this->base_route.'_batch',
+                'params'    => array()
             )
         );
     }
@@ -114,7 +126,8 @@ class CRUDController extends Controller
             'pager'             => $pager,
             'fields'            => $this->getListFields(),
             'class_meta_data'   => $this->getClassMetaData(),
-            'urls'              => $this->getUrls()
+            'urls'              => $this->getUrls(),
+            'batch_actions'     => $this->getBatchActions(),
         ));
 
     }
@@ -174,6 +187,11 @@ class CRUDController extends Controller
         return $fields;
     }
 
+    /**
+     * Construct and build the form field definitions
+     *
+     * @return list form field definition
+     */
     public function getFormFields()
     {
         $this->form_fields = $this->getBaseFields($this->form_fields);
@@ -210,9 +228,38 @@ class CRUDController extends Controller
             }
         }
 
+        if(!isset($this->list_fields['_batch'])) {
+            $this->list_fields = array('_batch' => array(
+                'template' => 'BaseApplicationBundle:CRUD:list__batch.twig'
+            ) ) + $this->list_fields;
+        }
+        
         return $this->list_fields;
     }
 
+    public function batchActionDelete($idx)
+    {
+        $em = $this->getEntityManager();
+
+        $query_builder = $em->createQueryBuilder();
+        $objects = $query_builder
+            ->select('o')
+            ->from($this->getClass(), 'o')
+            ->add('where', $query_builder->expr()->in('o.id', $idx))
+            ->getQuery()
+            ->execute();
+
+        foreach($objects as $object) {
+            $em->remove($object);
+        }
+
+        $em->flush();
+
+        // todo : add confirmation flash var
+        $url = $this->getUrl('list');
+        return $this->redirect($this->generateUrl($url['url']));
+    }
+
     public function deleteAction($id)
     {
 
@@ -346,6 +393,28 @@ class CRUDController extends Controller
         ));
     }
 
+    public function batchAction()
+    {
+        if($this->get('request')->getMethod() != 'POST') {
+           throw new \RuntimeException('invalid request type, POST expected');
+        }
+
+        $action = $this->get('request')->get('action');
+        $idx    = $this->get('request')->get('idx');
+
+        if(count($idx) == 0) { // no item selected
+            // todo : add flash information
+        }
+
+        // execute the action, batchActionXxxxx
+        $final_action = sprintf('batchAction%s', ucfirst($action));
+        if(!method_exists($this, $final_action)) {
+            throw new \RuntimeException(sprintf('A %s::%s method must be created', get_class($this), $final_action));
+        }
+
+        return call_user_func(array($this, $final_action), $idx);
+    }
+
     public function createAction($form = null)
     {
         $this->get('session')->start();

+ 44 - 34
Resources/views/CRUD/base_list.twig

@@ -20,42 +20,52 @@ file that was distributed with this source code.
     </div>
 {% endblock %}
 
-<table>
+<form action="{{ url(urls.batch.url) }}" method="POST" >
+    <table>
+        {% block table_header %}
+            <tr>
+                {% for field_name, field_description in fields %}
+                    <td>{{ field_name }}</td>
+                {% endfor %}
+            </tr>
+        {% endblock %}
 
-    {% block table_header %}
-        <tr>
-            {% for field_name, field_description in fields %}
-                <td>{{ field_name }}</td>
-            {% endfor %}
-        </tr>
-    {% endblock %}
-    
-    {% block table_body %}{% endblock %}
+        {% block table_body %}{% endblock %}
 
-    {% block table_footer %}
-        {% if pager.haveToPaginate() %}
-            <tr>
-                <td colspan="{{ fields|length }}">
-
-                    {% if pager.page != pager.previouspage %}
-                        <li><a href="{{ pager.renderLink(pager.previouspage) }}">{% trans 'link_previous_pager' from 'BaseApplicationBundle' %}</a></li>
-                    {% endif %}
-
-                    {# Set the number of pages to display in the pager #}
-                    {% for page in pager.getLinks(5) %}
-                        {% if page == pager.page %}
-                            <li>{{ page }}</li>
-                        {% else %}
-                            <li><a href="{{ pager.renderLink(page) }}">{{ page }}</a></li>
+        {% block table_footer %}
+            {% if pager.haveToPaginate() %}
+                <tr>
+                    <td colspan="{{ fields|length }}">
+
+                        {% if pager.page != pager.previouspage %}
+                            <li><a href="{{ pager.renderLink(pager.previouspage) }}">{% trans 'link_previous_pager' from 'BaseApplicationBundle' %}</a></li>
                         {% endif %}
-                    {% endfor %}
 
-                    {% if pager.page != pager.nextpage %}
-                        <li><a href="{{ pager.renderLink(pager.nextpage) }}">{% trans 'link_previous_pager' from 'BaseApplicationBundle' %}</a></li>
-                    {% endif %}
-                </td>
-            </tr>
-        {% endif %}
-    {% endblock %}
+                        {# Set the number of pages to display in the pager #}
+                        {% for page in pager.getLinks(5) %}
+                            {% if page == pager.page %}
+                                <li>{{ page }}</li>
+                            {% else %}
+                                <li><a href="{{ pager.renderLink(page) }}">{{ page }}</a></li>
+                            {% endif %}
+                        {% endfor %}
 
-</table>
+                        {% if pager.page != pager.nextpage %}
+                            <li><a href="{{ pager.renderLink(pager.nextpage) }}">{% trans 'link_previous_pager' from 'BaseApplicationBundle' %}</a></li>
+                        {% endif %}
+                    </td>
+                </tr>
+            {% endif %}
+        {% endblock %}
+    </table>
+
+    <div>
+        <select name="action" >
+            {% for action, label in batch_actions %}
+                <option value="{{ action }}">{{ label }}</option>
+            {% endfor %}
+        </select>
+        <input type="submit" value="{% trans 'btn_batch' from 'BaseApplicationBundle' %}" />
+
+    </div>
+</form>

+ 11 - 0
Resources/views/CRUD/list__batch.twig

@@ -0,0 +1,11 @@
+{#
+
+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.
+
+#}
+<input type="checkbox" name="idx" value="{{ object.id }}" />