{% macro projects(view) %}
    {% import "macros/widgets.html.twig" as widgets %}
    
    {% set actions = {'search': {'class': 'search-toggle visible-xs-inline'}, 'visibility': '#modal_project_admin'} %}

    {% set actions = actions|merge({'download': {'url': path('project_export'), 'class': 'toolbar-action'}}) %}

    {% if is_granted('create_project') %}
        {% set actions = actions|merge({'create': path('admin_project_create')}) %}
    {% endif %}

    {% set actions = actions|merge({'help': {'url': 'project.html'|docu_link, 'target': '_blank'}}) %}

    {% set event = trigger('actions.projects', {'actions': actions, 'view': view}) %}
    {{ widgets.page_actions(event.payload.actions) }}
{% endmacro %}

{% macro project(project, view, options) %}
    {% import "macros/widgets.html.twig" as widgets %}
    {% set actions = {} %}

    {% if project.id is not empty %}
        {% if view != 'details' and is_granted('view', project) %}
            {% set actions = actions|merge({'details': path('project_details', {'id': project.id})}) %}
        {% endif %}
        {% if is_granted('edit', project) %}
            {% set class = '' %}
            {% if view != 'edit' %}
                {% set class = 'modal-ajax-form' %}
            {% endif %}
            {% set actions = actions|merge({'edit': {'url': path('admin_project_edit', {'id': project.id}), 'class': class}}) %}
            {% set actions = actions|merge({'copy': {'url': path('admin_project_duplicate', {'id': project.id})}}) %}
        {% endif %}
        {% if is_granted('permissions', project) %}
            {% set class = '' %}
            {% if view != 'permissions' %}
                {% set class = 'modal-ajax-form' %}
            {% endif %}
            {% set actions = actions|merge({'permissions': {'url': path('admin_project_permissions', {'id': project.id}), 'class': class}}) %}
        {% endif %}
        {% if actions|length > 0 %}
            {% set actions = actions|merge({'divider': null}) %}
        {% endif %}
        {% if is_granted('view_activity') %}
            {% set actions = actions|merge({'activity': path('admin_activity', {'customers[]': project.customer.id, 'projects[]': project.id})}) %}
        {% endif %}
        {% if is_granted('view_other_timesheet') %}
            {% set actions = actions|merge({'timesheet': path('admin_timesheet', {'customers[]': project.customer.id, 'projects[]': project.id})}) %}
        {% endif %}
        {% if project.visible and project.customer.visible and is_granted('create_activity') %}
            {% set actions = actions|merge({'create-activity': {'url': path('admin_activity_create_with_project', {'project': project.id}), 'class': 'modal-ajax-form'}}) %}
        {% endif %}
        {% if (view == 'index' or view == 'custom') and is_granted('delete', project) %}
            {% set actions = actions|merge({'trash': {'url': path('admin_project_delete', {'id': project.id}), 'class': 'modal-ajax-form'}}) %}
        {% endif %}
    {% endif %}

    {% if view != 'index' and view != 'custom' %}
        {% set actions = actions|merge({'back': options.back|default(path('admin_project'))}) %}
    {% endif %}

    {% set event = trigger('actions.project', {'actions': actions, 'view': view, 'project': project}) %}
    {% if view == 'index' or view == 'custom' %}
        {{ widgets.table_actions(event.payload.actions) }}
    {% else %}
        {{ widgets.entity_actions(event.payload.actions) }}
    {% endif %}
{% endmacro %}
