Browse Source

Agregado al CRUDController el optgroup al select del batchAction

gabriel 7 năm trước cách đây
mục cha
commit
d0964f4926
1 tập tin đã thay đổi với 53 bổ sung30 xóa
  1. 53 30
      Controller/CRUDController.php

+ 53 - 30
Controller/CRUDController.php

@@ -56,16 +56,22 @@ class CRUDController extends BaseCRUDController
         $reflector = new \ReflectionMethod($this->admin, 'getBatchActions');
         if ($reflector->getDeclaringClass()->getName() === get_class($this->admin)) {
             @trigger_error('Override Sonata\AdminBundle\Admin\AbstractAdmin::getBatchActions method'
-                            . ' is deprecated since version 3.2.'
-                            . ' Use Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions instead.'
-                            . ' The method will be final in 4.0.', E_USER_DEPRECATED
+                . ' is deprecated since version 3.2.'
+                . ' Use Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions instead.'
+                . ' The method will be final in 4.0.', E_USER_DEPRECATED
             );
         }
         $batchActions = $this->admin->getBatchActions();
         if (!array_key_exists($action, $batchActions)) {
-            throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
-        }
+            // ahora cheque los groups
+            $actionGroup = $this->batchActionGroup($batchActions, $action);
+            if ($actionGroup == null) {
+                throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
+            }
 
+        } else {
+            $actionGroup = null;
+        }
         $camelizedAction = Inflector::classify($action);
         $isRelevantAction = sprintf('batchAction%sIsRelevant', $camelizedAction);
 
@@ -86,42 +92,59 @@ class CRUDController extends BaseCRUDController
             $this->addFlash('sonata_flash_info', $nonRelevantMessage);
 
             return new RedirectResponse(
-                    $this->admin->generateUrl(
-                            'list', array('filter' => $this->admin->getFilterParameters())
-                    )
+                $this->admin->generateUrl(
+                    'list', array('filter' => $this->admin->getFilterParameters())
+                )
             );
         }
 
-        $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ?
+        if ($actionGroup == null) {
+            $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ?
                 $batchActions[$action]['ask_confirmation'] :
                 true;
 
+        } else {
+            $askConfirmation = isset($actionGroup['ask_confirmation']) ?
+                $actionGroup['ask_confirmation'] :
+                true;
+        }
         if ($askConfirmation && $confirmation != 'ok') {
-            $actionLabel = $batchActions[$action]['label'];
-            $batchTranslationDomain = isset($batchActions[$action]['translation_domain']) ?
+            if ($actionGroup == null) {
+                $actionLabel = $batchActions[$action]['label'];
+                $batchTranslationDomain = isset($batchActions[$action]['translation_domain']) ?
                     $batchActions[$action]['translation_domain'] :
                     $this->admin->getTranslationDomain();
 
+            } else {
+                $actionLabel = $actionGroup['label'];
+                $batchTranslationDomain = isset($actionGroup['translation_domain']) ?
+                    $actionGroup['translation_domain'] :
+                    $this->admin->getTranslationDomain();
+            }
             $formView = $datagrid->getForm()->createView();
             $this->setFormTheme($formView, $this->admin->getFilterTheme());
 
             return $this->render($this->admin->getTemplate('batch_confirmation'), array(
-                        'action' => 'list',
-                        'action_label' => $actionLabel,
-                        'batch_translation_domain' => $batchTranslationDomain,
-                        'datagrid' => $datagrid,
-                        'form' => $formView,
-                        'data' => $data,
-                        'csrf_token' => $this->getCsrfToken('sonata.batch'),
-                            ), null);
+                'action' => 'list',
+                'action_label' => $actionLabel,
+                'batch_translation_domain' => $batchTranslationDomain,
+                'datagrid' => $datagrid,
+                'form' => $formView,
+                'data' => $data,
+                'csrf_token' => $this->getCsrfToken('sonata.batch'),
+            ), null);
         }
 
-        $workflowBatchAction = isset($batchActions[$action]['workflow']) ?: false;
-
         // execute the action, batchActionXxxxx
-        $finalAction = sprintf('batchAction%s', $camelizedAction);
-        if (!is_callable(array($this, $finalAction)) && !$workflowBatchAction) {
-            throw new \RuntimeException(sprintf('A `%s::%s` method must be callable', get_class($this), $finalAction));
+        $actionsParse = preg_split("/:/", $action);
+        $workflowBatchAction = count($actionsParse) > 0 && $actionsParse[0] == 'workflow';
+        if ($workflowBatchAction) {
+            // es una transicion de workflow
+        } else {
+            $finalAction = sprintf('batchAction%s', $camelizedAction);
+            if (!is_callable(array($this, $finalAction))) {
+                throw new \RuntimeException(sprintf('A `%s::%s` method must be callable', get_class($this), $finalAction));
+            }
         }
 
         $query = $datagrid->getQuery();
@@ -139,7 +162,7 @@ class CRUDController extends BaseCRUDController
         }
 
         if ($workflowBatchAction) {
-            return $this->runWorkflowBatchAction($batchActions[$action]['workflow'], $query);
+            return $this->runWorkflowBatchAction($actionsParse, $query);
         }
 
         return call_user_func(array($this, $finalAction), $query, $request);
@@ -148,7 +171,7 @@ class CRUDController extends BaseCRUDController
     /**
      * @param array $workflow
      * @param ProxyQueryInterface $selectedModelQuery
-     * 
+     *
      * @return RedirectResponse
      */
     public function runWorkflowBatchAction($workflow, ProxyQueryInterface $selectedModelQuery)
@@ -161,13 +184,13 @@ class CRUDController extends BaseCRUDController
         try {
             $workflowRegistry = $this->get('workflow.registry');
             $em = $this->get('doctrine.orm.entity_manager');
-            
+
             $selectedModels = $selectedModelQuery->execute();
             foreach ($selectedModels as $selectedModel) {
-                if ($workflowRegistry->get($selectedModel, $workflow['name'])->can($selectedModel, $workflow['transition'])) {
+                if ($workflowRegistry->get($selectedModel, $workflow[1])->can($selectedModel, $workflow[2])) {
                     $workflowName = "{$selectedModel->getWorkflowType()}.{$selectedModel->getWorkflowName()}";
-                    $this->get($workflowName)->apply($selectedModel, $workflow['transition']);
-                    
+                    $this->get($workflowName)->apply($selectedModel, $workflow[2]);
+
                     $em->persist($selectedModel);
                     $em->flush($selectedModel);
                 }