12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace WorkflowBundle\Admin;
- use Base\AdminBundle\Admin\BaseAdmin;
- class WorkflowBaseAdmin extends BaseAdmin
- {
- /**
- * @return array
- */
- public function getBatchActions()
- {
- $actions = parent::getBatchActions();
- if (!($this->checkRole('ROLE_SONATA_WORKFLOW_ADMIN') || $this->checkRole('ROLE_SONATA_WORKFLOW_EDITOR') || $this->checkRole('ROLE_SUPER_ADMIN'))) {
- return $actions;
- }
- asort($actions);
- $actionsBase = [];
- $actionsBase['Default'] = array(
- 'label' => $this->trans('Default', array(), 'WorkflowLabel'),
- 'group' => $actions
- );
- $actions = [];
- $workflows = array();
- $registry = $this->get('workflow.registry');
- $class = $this->getClass();
- $tmpEntity = new $class();
- try {
- $workflows = $this->getRepository('WorkflowBundle:Workflow')->findAllByClass($this->getClass());
- // No hay workflows en db, pruebo traer desde el workflow registry
- if (count($workflows) == 0) {
- $workflows[] = $registry->get($tmpEntity);
- }
- $actions += $this->addActions($workflows, 'Entity');
- } catch (\Exception $ex) {
- }
- if (isset($this->batchWorkflows)) {
- // No hay workflows en db, pruebo traer desde el workflow registry
- // con los names definidos en el admin $batchWorkflows
- foreach ($this->batchWorkflows as $name) {
- $workflows[] = $registry->get($tmpEntity, $name);
- }
- $actions += $this->addActions($workflows, 'File');
- }
- asort($actions);
- return $actionsBase + $actions;
- }
- private function addActions($workflows, $place)
- {
- foreach ($workflows as $workflow) {
- $definition = method_exists($workflow, 'getSubject')
- ? $workflow->getDefinition($workflow->getSubject())
- : $workflow->getDefinition();
- $transitions = $definition ? $definition->getTransitions() : array();
- $tmp = array();
- foreach ($transitions as $transition) {
- $id = 'workflow.' . $workflow->getName() . '.transitions.' . $transition->getName();
- $label = $this->trans($id, array(), 'WorkflowLabel');
- if ($label == $id) {
- $label = $transition->getName();
- }
- $label .= " ( {$transition->getName()} )";
- $tmp["workflow:" . $workflow->getName() . ':' . $transition->getName()] = array(
- 'label' => $label,
- 'ask_confirmation' => true,
- 'workflow' => array(
- 'name' => $workflow->getName(),
- 'transition' => $transition->getName(),
- ),
- );
- }
- asort($tmp);
- $actions[$workflow->getName()] = array(
- 'label' => $this->trans($workflow->getName(), array(), 'WorkflowLabel'),
- 'group' => $tmp
- );
- }
- return $actions;
- }
- }
|