浏览代码

Merge pull request #1981 from BenoitLeveque/execute-pre-batch-action

Allow user to alter query, idx before executre a batch actions
Thomas 11 年之前
父节点
当前提交
dbb07f74d6
共有 4 个文件被更改,包括 44 次插入0 次删除
  1. 8 0
      Admin/Admin.php
  2. 11 0
      Admin/AdminInterface.php
  3. 2 0
      Controller/CRUDController.php
  4. 23 0
      Resources/doc/reference/batch_actions.rst

+ 8 - 0
Admin/Admin.php

@@ -11,6 +11,7 @@
 
 
 namespace Sonata\AdminBundle\Admin;
 namespace Sonata\AdminBundle\Admin;
 
 
+use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 use Symfony\Component\Form\Form;
 use Symfony\Component\Form\Form;
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\PropertyAccess\PropertyPath;
 use Symfony\Component\PropertyAccess\PropertyPath;
@@ -675,6 +676,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     public function postRemove($object)
     public function postRemove($object)
     {}
     {}
 
 
+    /**
+     * {@inheritdoc}
+     */
+    public function preBatchAction($actionName, ProxyQueryInterface $query, array & $idx, $allElements)
+    {
+    }
+
     /**
     /**
      * build the view FieldDescription array
      * build the view FieldDescription array
      *
      *

+ 11 - 0
Admin/AdminInterface.php

@@ -16,6 +16,7 @@ use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Builder\ListBuilderInterface;
 use Sonata\AdminBundle\Builder\ListBuilderInterface;
 use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
 use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
+use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
 use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
 use Sonata\AdminBundle\Builder\RouteBuilderInterface;
 use Sonata\AdminBundle\Builder\RouteBuilderInterface;
 use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
 use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
@@ -716,6 +717,16 @@ interface AdminInterface
      */
      */
     public function postRemove($object);
     public function postRemove($object);
 
 
+    /**
+     * Call before the batch action, allow you to alter the query and the idx
+     *
+     * @param string              $actionName
+     * @param ProxyQueryInterface $query
+     * @param array               $idx
+     * @param bool                $allElements
+     */
+    public function preBatchAction($actionName, ProxyQueryInterface $query, array & $idx, $allElements);
+
     /**
     /**
      * Return array of filter parameters.
      * Return array of filter parameters.
      *
      *

+ 2 - 0
Controller/CRUDController.php

@@ -474,6 +474,8 @@ class CRUDController extends Controller
         $query->setFirstResult(null);
         $query->setFirstResult(null);
         $query->setMaxResults(null);
         $query->setMaxResults(null);
 
 
+        $this->admin->preBatchAction($action, $query, $idx, $allElements);
+
         if (count($idx) > 0) {
         if (count($idx) > 0) {
             $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
             $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
         } elseif (!$allElements) {
         } elseif (!$allElements) {

+ 23 - 0
Resources/doc/reference/batch_actions.rst

@@ -136,6 +136,29 @@ This method may return three different values:
         return count($selectedIds) > 0;
         return count($selectedIds) > 0;
     }
     }
 
 
+(Optional) Executing a pre batch hook
+-------------------------------------
+
+In your admin class you can create a ``preBacthAction`` method to execute something before doing the batch action.
+The main purpose of this method is to alter the query or the list of selected id.
+
+.. code-block:: php
+
+    <?php
+
+    // In your Admin class
+
+    public function preBatchAction($actionName, ProxyQueryInterface $query, array & $idx, $allElements)
+    {
+        // altering the query or the idx array
+        $foo = $query->getParameter('foo')->getValue();
+
+        // Doing something with the foo object
+        // ...
+
+        $query->setParameter('foo', $bar);
+    }
+
 
 
 Define the core action logic
 Define the core action logic
 ----------------------------
 ----------------------------