<commit_before>{% macro renderAvailableVariablesWidget(entityName, dependentFieldId) %}
    <script type="text/javascript">
        require(['jquery', 'oroemail/js/email/variable/view', 'oroemail/js/email/variable/model'],
        function($, View, Model){
            $(function() {
                'use strict';
                new View({
                    el:       $('#oro-email-available-variables'),
                    template: $('#available-variables-lists-template'),
                    target:   $('#{{ dependentFieldId }}'),
                    model:    new Model({{ render(path('oro_api_get_emailtemplate_available_variables', { 'entityName': entityName }))}})
                });
            });
        });
    </script>

    <script type="text/template" id="available-variables-lists-template">
        <% if (userVars.length > 0) { %>
        <div>
            <h4>{{ 'Logged in user:'|trans }}</h4>
            <ul class="nav nav-list">
                <% _.each(userVars, function(variable, i) { %>
                    <li><a href="javascript:void(0);" title="<%= title %>">{% verbatim %}{{ user.<%= variable %> }}{% endverbatim %}</a></li>
                <% }); %>
            </ul>
        </div>
        <% } %>
        <% if (entityVars.length > 0) { %>
        <div>
            <h4>{{ 'Related entity:'|trans }}</h4>
            <ul class="nav nav-list">
                <% _.each(entityVars, function(variable, i) { %>
                    <li><a href="javascript:void(0);" title="<%= title %>">{% verbatim %}{{ entity.<%= variable %> }}{% endverbatim %}</a></li>
                <% }); %>
            </ul>
        </div>
        <% } %>
    </script>

    <div class="well well-small" id="oro-email-available-variables"></div>
{% endmacro %}

{% macro renderPreviewDialog(formName, title) %}
    <script type="text/javascript">
        require(['jquery', 'underscore', 'orotranslation/js/translator', 'oroui/js/modal'],
        function($, _, __, Modal){
            $(function () {
                var showEmailPreviewDialog = function(el) {
                    var iframeId = 'preview-frame';
                    var iframe = $('<iframe />', {
                        name: iframeId,
                        id: iframeId,
                        'class': "scrollable-container",
                        frameborder: 0,
                        marginwidth: 0,
                        marginheight: 0,
                        allowfullscreen: true,
                        width: '100%',
                        height: '98%'
                    });

                    var form = $('#{{ formName }}');
                    var formAction = form.attr('action');

                    form.one('submit', function(e) {
                        var loader;
                        var confirmModal = new Modal({
                            title: __('Preview'),
                            content: '<div class="loading-content">' + __('Loading...') + '</div>',
                            okText: __('Ok'),
                            className: 'modal oro-modal-normal',
                            template: _.template('<% if (title) { %>' +
                                '<div class="modal-header">' +
                                    '<% if (allowCancel) { %>' +
                                    '<a class="close">×</a>' +
                                    '<% } %>' +
                                    '<h3><%- title %></h3>' +
                                '</div>' +
                                '<% } %>' +
                                '<div class="modal-body"><%= content %></div>'
                            )
                        });
                        confirmModal.open();
                        confirmModal.$content.append(iframe);
                        loader = confirmModal.$content.find('.loading-content');

                        loader.show();
                        form.attr('target', iframeId);
                        form.attr('action', $(el).attr('href'));

                        iframe.one('load', function() {
                            loader.hide();
                            form.removeAttr('target');
                            form.attr('action', formAction);
                        });

                        // prevent hash navigation form processing
                        e.stopImmediatePropagation();
                    });

                    form.submit();
                };

                $('.dialog-form-renderer').click(function (e) {
                    showEmailPreviewDialog(this);
                    e.preventDefault();
                });
            });
        });
    </script>
{% endmacro %}

{#
    Render email address name, owner name or a link to owner view page can be rendered depends on given parameters
    Parameters:
        emailAddress - email address entity Oro\Bundle\EmailBundle\Entity\EmailAddress
        emailAddressName - a string contains an email address. It is used if the email address has no owner
        noLink - determines whether the rendering of a link to the owner view page is forbidden or not. Default value is false
        knownOnly - if true renders only emails which have an owner; otherwise, all emails. Default value is false
#}
{% macro email_address(emailAddress, emailAddressName, noLink, knownOnly) -%}
    {% macro email_address_text(emailAddress) -%}
        {{ emailAddress.owner|oro_format_name|default('N/A')|escape }}
    {%- endmacro -%}
    {% macro email_address_link(emailAddress) -%}
        {#- TODO: we need EntityConfig to get view url for an entity -#}
        <a href="{{ path(emailAddress.owner.class|split('\\')|first|lower ~ '_' ~ emailAddress.owner.class|split('\\')|last|lower ~ '_view', {id:emailAddress.owner.id}) }}">{{ _self.email_address_text(emailAddress) }}</a>
    {%- endmacro -%}
    {% if emailAddress.owner is null -%}
        {% if not knownOnly|default(false) -%}
            {{ emailAddressName }}
        {%- endif %}
    {%- else -%}
        {% if noLink|default(false) -%}
            {{ _self.email_address_text(emailAddress) }}
        {%- else -%}
            {{ _self.email_address_link(emailAddress)|raw }}
        {%- endif %}
    {%- endif %}
{%- endmacro %}

{#
    Render email address with title
    Parameters:
        email - EmailInterface object or string
        title (optional) - email title
#}
{%- macro email_address_simple(email, title) -%}
    {% if email is not empty %}
        {% set emailAddress = null %}
        {# if email is an object implemented EmailInterface #}
        {% if email.email is defined %}
            {% if email.email is not empty %}
                {% set emailAddress = email.email %}
            {% endif %}
            {# if email is a string #}
        {% else %}
            {% set emailAddress = email %}
        {% endif %}

        {% if title is empty %}
            {% set title = emailAddress %}
        {% endif %}

        {% if emailAddress %}
            <a href="mailto:{{ emailAddress|raw }}" title="{{ title|escape('html_attr') }}">{{ title }}</a>
        {% endif %}
    {% endif %}
{%- endmacro -%}

{#
    Render the given email recipients
    Parameters:
        recipients - an array of Oro\Bundle\EmailBundle\Entity\EmailRecipients
        noLink - determines whether the rendering of a link to the owner view page is forbidden or not. Default value is false
        knownOnly - if true renders only emails which have an owner; otherwise, all emails. Default value is false
#}
{% macro recipient_email_addresses(recipients, noLink, knownOnly) -%}
    {% set addresses = {} -%}
    {% for recipient in recipients -%}
        {% set address = _self.email_address(recipient.emailAddress, recipient.name, noLink, knownOnly) %}
        {%- if address|length > 0 -%}
            {% set addresses = addresses|merge([address]) %}
        {%- endif %}
    {%- endfor -%}
    {% for address in addresses -%}
        {{ address }}
        {%- if not loop.last %}; {% endif %}
    {%- endfor %}
{%- endmacro %}

{#
    Render the given email attachments
    Parameters:
        attachments - an array of Oro\Bundle\EmailBundle\Entity\EmailAttachment
#}
{% macro attachments(attachments) -%}
    {% macro attachment_row_data(values) -%}
        {%- for val in values -%}
        <a class="no-hash" href="{{ path('oro_email_attachment', {id:val.id}) }}"><commit_msg>CRM-1134: User calls grid: recipients empty<commit_after>{% macro renderAvailableVariablesWidget(entityName, dependentFieldId) %}
    <script type="text/javascript">
        require(['jquery', 'oroemail/js/email/variable/view', 'oroemail/js/email/variable/model'],
        function($, View, Model){
            $(function() {
                'use strict';
                new View({
                    el:       $('#oro-email-available-variables'),
                    template: $('#available-variables-lists-template'),
                    target:   $('#{{ dependentFieldId }}'),
                    model:    new Model({{ render(path('oro_api_get_emailtemplate_available_variables', { 'entityName': entityName }))}})
                });
            });
        });
    </script>

    <script type="text/template" id="available-variables-lists-template">
        <% if (userVars.length > 0) { %>
        <div>
            <h4>{{ 'Logged in user:'|trans }}</h4>
            <ul class="nav nav-list">
                <% _.each(userVars, function(variable, i) { %>
                    <li><a href="javascript:void(0);" title="<%= title %>">{% verbatim %}{{ user.<%= variable %> }}{% endverbatim %}</a></li>
                <% }); %>
            </ul>
        </div>
        <% } %>
        <% if (entityVars.length > 0) { %>
        <div>
            <h4>{{ 'Related entity:'|trans }}</h4>
            <ul class="nav nav-list">
                <% _.each(entityVars, function(variable, i) { %>
                    <li><a href="javascript:void(0);" title="<%= title %>">{% verbatim %}{{ entity.<%= variable %> }}{% endverbatim %}</a></li>
                <% }); %>
            </ul>
        </div>
        <% } %>
    </script>

    <div class="well well-small" id="oro-email-available-variables"></div>
{% endmacro %}

{% macro renderPreviewDialog(formName, title) %}
    <script type="text/javascript">
        require(['jquery', 'underscore', 'orotranslation/js/translator', 'oroui/js/modal'],
        function($, _, __, Modal){
            $(function () {
                var showEmailPreviewDialog = function(el) {
                    var iframeId = 'preview-frame';
                    var iframe = $('<iframe />', {
                        name: iframeId,
                        id: iframeId,
                        'class': "scrollable-container",
                        frameborder: 0,
                        marginwidth: 0,
                        marginheight: 0,
                        allowfullscreen: true,
                        width: '100%',
                        height: '98%'
                    });

                    var form = $('#{{ formName }}');
                    var formAction = form.attr('action');

                    form.one('submit', function(e) {
                        var loader;
                        var confirmModal = new Modal({
                            title: __('Preview'),
                            content: '<div class="loading-content">' + __('Loading...') + '</div>',
                            okText: __('Ok'),
                            className: 'modal oro-modal-normal',
                            template: _.template('<% if (title) { %>' +
                                '<div class="modal-header">' +
                                    '<% if (allowCancel) { %>' +
                                    '<a class="close">×</a>' +
                                    '<% } %>' +
                                    '<h3><%- title %></h3>' +
                                '</div>' +
                                '<% } %>' +
                                '<div class="modal-body"><%= content %></div>'
                            )
                        });
                        confirmModal.open();
                        confirmModal.$content.append(iframe);
                        loader = confirmModal.$content.find('.loading-content');

                        loader.show();
                        form.attr('target', iframeId);
                        form.attr('action', $(el).attr('href'));

                        iframe.one('load', function() {
                            loader.hide();
                            form.removeAttr('target');
                            form.attr('action', formAction);
                        });

                        // prevent hash navigation form processing
                        e.stopImmediatePropagation();
                    });

                    form.submit();
                };

                $('.dialog-form-renderer').click(function (e) {
                    showEmailPreviewDialog(this);
                    e.preventDefault();
                });
            });
        });
    </script>
{% endmacro %}

{#
    Render email address name, owner name or a link to owner view page can be rendered depends on given parameters
    Parameters:
        emailAddress - email address entity Oro\Bundle\EmailBundle\Entity\EmailAddress
        emailAddressName - a string contains an email address. It is used if the email address has no owner
        noLink - determines whether the rendering of a link to the owner view page is forbidden or not. Default value is false
        knownOnly - if true renders only emails which have an owner; otherwise, all emails. Default value is false
#}
{% macro email_address(emailAddress, emailAddressName, noLink, knownOnly) -%}
    {% macro email_address_text(emailAddress) -%}
        {{ emailAddress.owner|oro_format_name|default('N/A')|escape }}
    {%- endmacro -%}
    {% macro email_address_link(emailAddress) -%}
        {#- TODO: we need EntityConfig to get view url for an entity -#}
        <a href="{{ path(emailAddress.owner.class|split('\\')|first|lower ~ '_' ~ emailAddress.owner.class|split('\\')|last|lower ~ '_view', {id:emailAddress.owner.id}) }}">{{ _self.email_address_text(emailAddress) }}</a>
    {%- endmacro -%}
    {% if emailAddress.owner is null -%}
        {% if not knownOnly|default(false) -%}
            {{ emailAddressName }}
        {%- endif %}
    {%- else -%}
        {% if noLink|default(false) -%}
            {{ _self.email_address_text(emailAddress) }}
        {%- else -%}
            {{ _self.email_address_link(emailAddress)|raw }}
        {%- endif %}
    {%- endif %}
{%- endmacro %}

{#
    Render email address with title
    Parameters:
        email - EmailInterface object or string
        title (optional) - email title
#}
{%- macro email_address_simple(email, title) -%}
    {% if email is not empty %}
        {% set emailAddress = null %}
        {# if email is an object implemented EmailInterface #}
        {% if email.email is defined %}
            {% if email.email is not empty %}
                {% set emailAddress = email.email %}
            {% endif %}
            {# if email is a string #}
        {% else %}
            {% set emailAddress = email %}
        {% endif %}

        {% if title is empty %}
            {% set title = emailAddress %}
        {% endif %}

        {% if emailAddress %}
            <a href="mailto:{{ emailAddress|raw }}" title="{{ title|escape('html_attr') }}">{{ title }}</a>
        {% endif %}
    {% endif %}
{%- endmacro -%}

{#
    Render the given email recipients
    Parameters:
        recipients - an array of Oro\Bundle\EmailBundle\Entity\EmailRecipients
        noLink - determines whether the rendering of a link to the owner view page is forbidden or not. Default value is false
        knownOnly - if true renders only emails which have an owner; otherwise, all emails. Default value is false
#}
{% macro recipient_email_addresses(recipients, noLink, knownOnly) -%}
    {% set addresses = {} -%}
    {% for recipient in recipients -%}
        {% set address = _self.email_address(recipient.emailAddress, recipient.name, noLink, knownOnly) %}
        {%- if address|length > 0 -%}
            {% set addresses = addresses|merge([address]) %}
        {% else %}
            {% set addresses = addresses|merge([recipient.emailAddress.email]) %}
        {%- endif %}
    {%- endfor -%}

    {% for address in addresses -%}
        {{ address }}
        {%- if not loop.last %}; {% endif %}
    {%- endfor %}
{%- endmacro %}

{#
    Render the given email attachments
    Parameters:
        attachments - an array of Oro\Bundle\EmailBundle\Entity\EmailAttachment
#}
{% macro attachments(attachments) -%}
    {% macro attachment_row_data(values) -%}
        {%- for val in values -%}
        <a class="no-hash" href="{{ path('oro_email_attachment', {id:val.id}) }}">
{'fraction_non_alphanumeric': 0.14723529411764705, 'fraction_numerical': 0.0024705882352941176, 'mean_word_length': 1.5755188607786699, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5626898', 'n_tokens_mistral': 4738, 'n_tokens_neox': 4296, 'n_words': 1039}