diff --git a/Generator/Action.php b/Generator/Action.php index 331d859c..80b92b16 100644 --- a/Generator/Action.php +++ b/Generator/Action.php @@ -40,6 +40,8 @@ class Action protected $credentials = 'AdmingenAllowed'; + protected $workflows = array(); + public function __construct($name, $type = 'custom') { $this->name = $name; @@ -180,11 +182,6 @@ public function getCsrfProtected() return $this->csrfProtected; } - public function setCredentials($credentials) - { - $this->credentials = $credentials; - } - public function setForceIntermediate($forceIntermediate) { $this->forceIntermediate = $forceIntermediate; @@ -195,11 +192,26 @@ public function getForceIntermediate() return $this->forceIntermediate; } + public function setCredentials($credentials) + { + $this->credentials = $credentials; + } + public function getCredentials() { return $this->credentials; } + public function setWorkflows(array $workflows) + { + $this->workflows = $workflows; + } + + public function getWorkflows() + { + return $this->workflows; + } + public function getParams() { return $this->params; diff --git a/Resources/doc/customization/actions.md b/Resources/doc/customization/actions.md index e5144e5f..763af57d 100644 --- a/Resources/doc/customization/actions.md +++ b/Resources/doc/customization/actions.md @@ -58,6 +58,10 @@ params: pk: "{{ User.id }}" action: lock csrfProtected: true + workflows: + # if set, wraps action link with a `workflow_can` check + # see Symfony 3.2 workflow component + - lock options: # this is the title for intermediate page # if JS is available then intermediate page will not be used @@ -151,6 +155,39 @@ For each custom object action there are four methods generated: > **Note:** The only method you **have to** overwrite is `executeObject{{ ActionName }}`. +#### Workflow transition actions + +If you use the [Symfony Workflow Component](http://symfony.com/blog/new-in-symfony-3-2-workflow-component), you can use the `workflows` option to wrap object actions with a `workflow_can` check. Example config and controller: + +```yaml +# config +params: + object_actions: + lock: + label: Lock account + icon: glyphicon-lock + route: Acme_SecurityBundle_User_object + params: + pk: "{{ User.id }}" + action: lock + csrfProtected: true + workflows: + - lock +``` + +```php + + /** + * This function is for you to customize what action actually does + */ + protected function executeObjectLock($User) + { + // this service is provieded by Workflow Component + $this->get('worflow.user_management')->apply($User, 'lock'); + } + +``` + ### Custom batch action example #### Batch actions configuration @@ -309,6 +346,12 @@ it here. For more documenation about credentials, check our [security documentat > __NOTE__ Credentials given here are valid for the whole admin, but can be overridden in specific builders or even specific fields. +##### Workflows + +`workflows` __type__: `array` + +This parameter is implemented only for **object actions** as Workflow Component transition checks can be only made given entity context. Empty by default, if set - the button link will be wrapped in a `workflow_can` check. + ##### CSRF protected `crsfProtected` __type__: `bool` __default__: `false` @@ -358,8 +401,7 @@ Set the action of the button. When this is set, there will be no controller STUB rendered as simple URL. ##### Submit -`submit` __type -__: `bool` +`submit` __type__: `bool` If set to true, the button will behave as a submit button for the form on that page. diff --git a/Resources/templates/CommonAdmin/object_actions.php.twig b/Resources/templates/CommonAdmin/object_actions.php.twig index 45d36e4e..72f24028 100644 --- a/Resources/templates/CommonAdmin/object_actions.php.twig +++ b/Resources/templates/CommonAdmin/object_actions.php.twig @@ -14,7 +14,13 @@ {% if action.credentials %} {{ echo_if_granted(action.credentials, builder.ModelClass) }} {% endif -%} + {% if action.workflows is not empty %} + {{ echo_if_workflow(builder.ModelClass, action.workflows) }} + {% endif -%} {{ block('object_action_block') }} + {%- if action.workflows is not empty %} + {{ echo_endif() }} + {% endif %} {%- if action.credentials %} {{ echo_endif() }} {% endif %} @@ -63,4 +69,4 @@ {{ echo_endblock() }} {{ echo_endblock() }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/Twig/Extension/EchoExtension.php b/Twig/Extension/EchoExtension.php index 291c119d..07b888ac 100644 --- a/Twig/Extension/EchoExtension.php +++ b/Twig/Extension/EchoExtension.php @@ -26,6 +26,7 @@ public function getFunctions() { return array( 'echo_if_granted' => new \Twig_SimpleFunction('echo_if_granted', array($this, 'getEchoIfGranted')), + 'echo_if_workflow' => new \Twig_SimpleFunction('echo_if_workflow', array($this, 'getEchoIfWorkflow')), 'echo_path' => new \Twig_SimpleFunction('echo_path', array($this, 'getEchoPath')), 'echo_trans' => new \Twig_SimpleFunction('echo_trans', array($this, 'getEchoTrans')), 'echo_render' => new \Twig_SimpleFunction('echo_render', array($this, 'getEchoRender')) @@ -188,6 +189,27 @@ public function getEchoIfGranted($credentials, $modelName = null) ); } + /** + * Print "if" tag with condition workflow_can() + * + * @param string $modelName + * @param array $workflows + * @return string + */ + public function getEchoIfWorkflow($modelName, $workflows = array()) + { + if (empty($workflows)) { + return "{% if (true) %}"; + } + + return sprintf( + "{%% if %s(%s, '%s') %%}", + 'workflow_can', + $modelName, + implode("', '", $workflows) + ); + } + /** * Print "echo tag with render call" to the controller $controller * with $params parameters.