Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement symfony 3.2 workflows #297

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Generator/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Action

protected $credentials = 'AdmingenAllowed';

protected $workflows = array();

public function __construct($name, $type = 'custom')
{
$this->name = $name;
Expand Down Expand Up @@ -180,11 +182,6 @@ public function getCsrfProtected()
return $this->csrfProtected;
}

public function setCredentials($credentials)
{
$this->credentials = $credentials;
}

public function setForceIntermediate($forceIntermediate)
{
$this->forceIntermediate = $forceIntermediate;
Expand All @@ -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;
Expand Down
46 changes: 44 additions & 2 deletions Resources/doc/customization/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion Resources/templates/CommonAdmin/object_actions.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -63,4 +69,4 @@
{{ echo_endblock() }}
</script>
{{ echo_endblock() }}
{% endblock %}
{% endblock %}
22 changes: 22 additions & 0 deletions Twig/Extension/EchoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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.
Expand Down