Pārlūkot izejas kodu

Merge remote-tracking branch 'simplethings/FilterActions' into FilterActions

Thomas Rabaix 14 gadi atpakaļ
vecāks
revīzija
2b38d60b41
3 mainītis faili ar 74 papildinājumiem un 14 dzēšanām
  1. 31 14
      Admin/Admin.php
  2. 32 0
      Controller/CRUDController.php
  3. 11 0
      Resources/views/CRUD/base_list.html.twig

+ 31 - 14
Admin/Admin.php

@@ -534,6 +534,30 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         return $this->listFieldDescriptions;
     }
+    
+    /**
+     * Get parameters that are currently bound to the filter.
+     * 
+     * @return array
+     */
+    public function getFilterParameters()
+    {
+        $parameters = array();
+        // build the values array
+        if ($this->hasRequest()) {
+            $parameters = array_merge(
+                $this->getModelManager()->getDefaultSortValues($this->getClass()),
+                $this->datagridValues,
+                $this->request->query->all()
+            );
+
+            // always force the parent value
+            if ($this->isChild() && $this->getParentAssociationMapping()) {
+                $parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
+            }
+        }
+        return $parameters;
+    }
 
     /**
      * build the filter FieldDescription array
@@ -560,20 +584,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             $this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
         }
 
-        $parameters = array();
-        // build the values array
-        if ($this->hasRequest()) {
-            $parameters = array_merge(
-                $this->getModelManager()->getDefaultSortValues($this->getClass()),
-                $this->datagridValues,
-                $this->request->query->all()
-            );
-
-            // always force the parent value
-            if ($this->isChild() && $this->getParentAssociationMapping()) {
-                $parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
-            }
-        }
+        $parameters = $this->getFilterParameters();
 
         // initialize the datagrid
         $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
@@ -757,6 +768,11 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         return $actions;
     }
+    
+    public function getFilterActions()
+    {
+        return array();
+    }
 
     /**
      * Returns the list of available urls
@@ -813,6 +829,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         $collection->add('list');
         $collection->add('create');
         $collection->add('batch');
+        $collection->add('filter');
         $collection->add('edit', $this->getRouterIdParameter().'/edit');
         $collection->add('delete', $this->getRouterIdParameter().'/delete');
         $collection->add('view', $this->getRouterIdParameter().'/view');

+ 32 - 0
Controller/CRUDController.php

@@ -245,6 +245,38 @@ class CRUDController extends Controller
 
         return new RedirectResponse($url);
     }
+    
+    /**
+     * Execute a filter action, an action performed on all filter entries of a datagrid.
+     * 
+     * @return Response
+     */
+    public function filterAction()
+    {
+        if ($this->get('request')->getMethod() != 'POST') {
+           throw new \RuntimeException('invalid request type, POST expected');
+        }        
+        
+        $action = $this->get('request')->get('action');
+        
+        if (!array_key_exists($action, $this->admin->getFilterActions())) {
+            throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
+        }
+        
+        // execute the action, filterActionXxxxx
+        $action = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);
+
+        $final_action = sprintf('filterAction%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));
+        }
+        
+        $grid = $this->admin->getDatagrid();
+        $grid->buildPager();
+        $query = $grid->getQuery();
+
+        return call_user_func(array($this, $final_action), $query);
+    }
 
     /**
      * return the Response object associated to the batch action

+ 11 - 0
Resources/views/CRUD/base_list.html.twig

@@ -143,6 +143,17 @@ file that was distributed with this source code.
 
             <a href="{{ admin.generateUrl('list') }}">{% trans from 'SonataAdminBundle' %}link_reset_filter{% endtrans %}</a>
         </form>
+    
+        {% if admin.filterActions %}
+        <form class="sonata-filter-action-form" action="{{ admin.generateUrl('filter') }}?{% for k,v in admin.filterParameters %}&{{k}}={{v}}{% endfor %}" method="POST">
+            <select name="action">
+                {% for action, label in admin.filterActions %}
+                    <option value="{{ action }}">{{ label }}</option>
+                {% endfor %}
+            </select>
+            <input type="submit" value="{% trans from 'SonataAdminBundle' %}btn_batch{% endtrans %}" />
+        </form>
+        {% endif %}
     {% endif %}
 {% endblock %}