
{% extends app['request'].isXmlHttpRequest ? "admin/common/ajax_wrap.html.twig" : "admin/common/iframe_wrap.html.twig" %}

{% block content %}

{% if app['request'].query.get('user-updated') is not none%}
    <div class="alert alert-success">
    <button class="close" data-dismiss="alert" type="button">×</button>
    {% set user_count = app['request'].get('user-updated') %}
    {% if user_count == 1 %}
        {% trans %}The user has been created.{% endtrans %}
    {% else %}
        {% trans with {'%user_count%' : user_count} %}%user_count% users have been created.{% endtrans %}
    {% endif %}
    </div>
{% endif %}

<div class="page-header">
    <h1>{{ 'Users' | trans }}</h1>
</div>

<div id="users_page">
        <p class="admin_head_opts">
            <a class="user_adder" href="#">{{ 'admin::user: nouvel utilisateur' | trans }}</a>
            <a class="template_adder" href="#">{{ 'admin::user: nouveau template' | trans }}</a>
            <a href="{{ path('users_display_import_file') }}" target='right'>{{ 'admin::user: import d\'utilisateurs' | trans }}</a> /
            <a href="#"  onclick="exportlist();return(false);">{{ 'admin::user: export d\'utilisateurs' | trans }}</a>
            <a class="invite_modifier" href="#">{{ 'Reglages:: reglages d acces guest' | trans }}</a>
            <input type="hidden" value="{{invite_user.getId()}}" name="invite_user_id"/>
            <a class="autoregister_modifier" href="#">{{ 'Reglages:: reglages d inscitpition automatisee' | trans }}</a>
            <input type="hidden" value="{{autoregister_user.getId()}}" name="autoregister_user_id"/>
        </p>

        <form id="users_page_search" method="post" action="{{ path('admin_users_search') }}" target="_self" class="form-inline">
            <fieldset class="admin_head_opts">
                <span class="checkbox">
                    {{ 'admin::userlist: filterguestuser' | trans }}
                    <input type="checkbox"  name="filter_guest_user" {% if parm['filter_guest_user'] %} checked="checked" {% endif %} style="margin-left:10px" />
                </span>
                <br/>
                <span>{{ 'Filter' | trans }}</span>
                <select name="like_field" class="input-medium">
                    <option {% if parm['like_field'] == "login" %}selected="selected"{% endif %} value="login">{{ 'Push::filter on login' | trans }}</option>
                    <option {% if parm['like_field'] == "name" %}selected="selected"{% endif %} value="name">{{ 'First/Last Name' | trans }}</option>
                    <option {% if parm['like_field'] == "country" %}selected="selected"{% endif %} value="country">{{ 'Push::filter on countries' | trans }}</option>
                    <option {% if parm['like_field'] == "company" %}selected="selected"{% endif %} value="company">{{ 'Push::filter on companies' | trans }}</option>
                    <option {% if parm['like_field'] == "email" %}selected="selected"{% endif %} value="email">{{ 'Push::filter on emails' | trans }}</option>
                </select>
                <span>{{ 'Push::filter starts' | trans }}</span>
                <input type="text" value="{{parm['like_value']}}" name="like_value" class="input-medium">
                <span>{{ 'Last applied template' | trans }}</span>
                <select name="last_model" class="input-medium">
                    <option value="">{{ 'None' | trans }}</option>
                    {% for template in templates %}
                        <option {% if parm['last_model'] == template.getId() %}selected="selected"{% endif %} value="{{ template.getId() }}">{{ template.getLogin() }}</option>
                    {% endfor %}
                </select>
                <button type="submit" class="btn">{{ 'boutton::chercher' | trans }}</button>
            </fieldset>
        </form>

        <button class="user_modifier btn">{{ 'boutton::modifier' | trans }}</button>
        <button class="user_deleter btn">{{ 'Supprimer' | trans }}</button>

        <form id="users_apply_template" method="post" action="{{ path('admin_users_apply_template') }}" target="_self" class="form-inline pull-left no-ajax" style="margin:0">
            <fieldset class="admin_head_opts">
                <span>{{ 'Apply a template' | trans }}</span>
                <select name="template_chooser" class="input-medium">
                    <option value="">{{ 'boutton::choisir' | trans }}</option>
                    {% for template in templates %}
                        <option value="{{ template.getId() }}">{{ template.getLogin() }}</option>
                    {% endfor %}
                </select>
                <button type="submit" class="btn">{{ 'boutton::appliquer' | trans }}</button>
            </fieldset>
        </form>

        <form id="users_reset_rights" method="post" action="{{ path('admin_users_rights_reset') }}" target="_self" class="form-inline pull-right" style="margin:0">
            <fieldset class="admin_head_opts">
                <input type="hidden" value="" name="users">
                <button type='button' class="btn btn-primary">{{ 'Delete all users rights' | trans }}</button>
            </fieldset>
        </form>

        <table id="users" cellspacing="0"  cellpadding="0" border="0" class="admintable">
            <thead>
                <tr>
                    <th style="text-align: center;" class="sortable {{ parm.srt == 'id' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur id utilisateur' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="id" />
                    </th>
                    <th class="sortable {{ parm.srt == 'login' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur identifiant' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="login" />
                    </th>
                    <th class="sortable {{ parm.srt == 'first_name' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'First/Last Name' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="first_name" />
                    </th>
                    <th class="sortable {{ parm.srt == 'company' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur societe' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="company" />
                    </th>
                    <th class="sortable {{ parm.srt == 'email' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur email' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="email" />
                    </th>
                    <th class="sortable {{ parm.srt == 'country' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur pays' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="country" />
                    </th>
                    <th class="sortable {{ parm.srt == 'last_model' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur dernier modele applique' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="last_model" />
                    </th>
                    <th class="sortable {{ parm.srt == 'created' ? 'sorted' :  '' }} {{ parm.ord == 'ASC' ? 'sort_asc' :  'sort_desc' }}">
                        <span>{{ 'admin::compte-utilisateur date de creation' | trans }}</span>
                        <span class="ord_notifier">{{ parm.ord == 'ASC' ? '&#x25bc;' :  '&#x25B2;' }}</span>
                        <input type="hidden" name="srt" value="created" />
                    </th>
                </tr>
            </thead>
            <tbody>
                {% for usr in users.get_results %}
                    <tr class="{% if loop.index is odd %}odd{% else %}even{% endif %}" id="user_{{usr.getId()}}">
                        <td style="text-align: center;">
                            {% if usr.isTemplate() %}
                                <img title="{{ 'This is a template' | trans }}" src="/assets/common/images/icons/template.png"/>
                            {% else %}
                                {% if app.getAclForUser(usr).is_phantom() %}
                                    <img title="{{ 'This user has no rights' | trans }}" src="/assets/admin/images/ghost.png"/>
                                {% endif %}
                                {{usr.getId()}}
                            {% endif %}
                        </td>
                        <td>
                            {{usr.getLogin()}}
                        </td>
                        <td>
                            {{usr.getFirstName()}} {{usr.getLastName()}}
                        </td>
                        <td>
                            {{usr.getCompany()}}
                        </td>
                        <td>
                            {{usr.getEmail()}}
                        </td>
                        <td>
                            {{usr.getCountry()}}
                        </td>
                        <td>
                        {% if usr.getLastAppliedTemplate() is not none %}
                            {{ usr.getLastAppliedTemplate().getLogin() }}
                        {% endif %}
                        </td>
                        <td>
                            {{ app['date-formatter'].getDate(usr.getCreated()) }}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

        <form id="export_form" method="post" target="_blank" class="no-ajax" action="{{ path('admin_users_search_export') }}" class="inline-form">
            <input name="srt" value="{{parm['srt']}}" type="hidden" />
            <input name="ord" value="{{parm.ord}}" type="hidden" />
            {% for sbas_id in parm.sbas_id %}
                <input name="sbas_id[]" value="{{sbas_id}}" type="hidden" />
            {% endfor %}
            {% for base_id in parm.base_id %}
                <input name="base_id[]" value="{{base_id}}" type="hidden" />
            {% endfor %}
            {% if parm['usr_ids'] is defined %}
                <input name="usr_ids" value="{{parm.usr_ids}}" type="hidden" />
            {% endif %}
            <input name="like_value" value="{{parm.like_value}}" type="hidden" />
            <input name="like_field" value="{{parm.like_field}}" type="hidden" />
            <input name="last_model" value="{{parm.last_model}}" type="hidden" />
            <input name="inactives" value="{{parm.inactives}}" type="hidden" />
        </form>

        <form id="users_page_form" method="post" target="_self" action="{{ path('admin_users_search') }}" class="inline-form pull-left">
            <fieldset>
                {% set pages = users.get_total() / parm['per_page'] %}
                {% set modulo = users.get_total() % parm['per_page'] %}

                {% if modulo > 0 %}{% set pages = pages + 1 %}{% endif %}
                <span>{{users.get_total}} resultats, {{pages|floor}} pages</span>

                {% if users.get_page > 2 %}
                    <button class="pager first btn"><<</button>
                {% endif %}
                {% if users.get_page > 1 %}
                    <button class="pager prev btn"><</button>
                {% endif %}
                    <input type="text" value="{{users.get_page}}" class="input-mini" style="margin:0"/>
                {% if users.get_page < pages -1 %}
                    <button class="pager next btn">></button>
                {% endif %}
                {% if users.get_page < pages - 2 %}
                    <button class="pager last btn">>></button>
                {% endif %}
                <select name="per_page" class="input-medium">
                    {% set n_par_page = 10 %}
                    <option value="10" {% if parm['per_page'] == 10%}selected="selected"{% endif %}>{% trans with {'%n_par_page%' : n_par_page} %}%n_par_page% par page{% endtrans %}</option>
                    {% set n_par_page = 20 %}
                    <option value="20" {% if parm['per_page'] == 20%}selected="selected"{% endif %}>{% trans with {'%n_par_page%' : n_par_page} %}%n_par_page% par page{% endtrans %}</option>
                    {% set n_par_page = 50 %}
                    <option value="50" {% if parm['per_page'] == 50%}selected="selected"{% endif %}>{% trans with {'%n_par_page%' : n_par_page} %}%n_par_page% par page{% endtrans %}</option>
                </select>

                <input type="hidden" name="total_results" value="{{users.get_total()}}"/>
                <input type="hidden" name="page" value="{{users.get_page()}}"/>
                <input name="srt" value="{{parm['srt']}}" type="hidden" />
                <input name="ord" value="{{parm.ord}}" type="hidden" />
                {% if parm['act'] is defined %}
                    <input name="act" value="{{parm.act}}" type="hidden" />
                {% endif %}
                {% for sbas_id in parm.sbas_id %}
                    <input name="sbas_id[]" value="{{sbas_id}}" type="hidden" />
                {% endfor %}
                {% for base_id in parm.base_id %}
                    <input name="base_id[]" value="{{base_id}}" type="hidden" />
                {% endfor %}
                {% if parm['usr_ids'] is defined %}
                    <input name="usr_ids" value="{{parm.usr_ids}}" type="hidden" />
                {% endif %}
                <input name="like_value" value="{{parm.like_value}}" type="hidden" />
                <input name="like_field" value="{{parm.like_field}}" type="hidden" />
                <input name="last_model" value="{{parm.last_model}}" type="hidden" />
                <input name="inactives" value="{{parm.inactives}}" type="hidden" />
                <input name="offset_start" value="{{parm.offset_start}}" type="hidden" />
            </fieldset>
        </form>

        <form method="post" action="{{ path('admin_users_export_csv') }}"  class="inline-form pull-right no-ajax">
            {% if parm.base_id is not empty %}
                {% for base_id in parm.base_id %}
                    <input name="base_id[]" value="{{base_id}}" type="hidden" />
                {% endfor %}
            {% endif %}
            {% if parm.sbas_id is not empty %}
                {% for sbas_id in parm.sbas_id %}
                    <input name="sbas_id[]" value="{{sbas_id}}" type="hidden" />
                {% endfor %}
            {% endif %}
            <input name="like_value" value="{{parm.like_value}}" type="hidden" />
            <input name="like_field" value="{{parm.like_field}}" type="hidden" />
            <button class="btn btn-primary">{{ 'boutton::exporter' | trans }}</button>
        </form>

    <script type="text/javascript">
        $(document).ready(function () {
            function users_select_this(n, k) {
                if (p4.users.sel.length >= 800) {
                    alert(language.max_record_selected);
                    return false;
                }
                p4.users.sel.push(k);
                $(n).addClass('selected');
                return true;
            }

            $('#users th.sortable').on('click', function () {
                var $this = $(this);

                var sort = $('input', $this).val();

                if ((sort == $('#users_page_form input[name="srt"]').val())
                        && ($('#users_page_form input[name="ord"]').val() == 'ASC')) {
                    var ord = 'DESC';
                }
                else {
                    var ord = 'ASC';
                }

                $('#users_page_form input[name="srt"]').val(sort);
                $('#users_page_form input[name="ord"]').val(ord);

                $('#users_page_form').trigger('submit');
            }).on('mouseover', function () {
                $(this).addClass('hover');
            }).on('mouseout', function () {
                $(this).removeClass('hover');
            });

            var buttons = {};
            buttons[language.annuler] = function () {
                $('#user_add_dialog').dialog('close')
            };
            buttons[language.create_user] = function () {
                check_new_user(false);
            };

            var userAddDialog = $('#user_add_dialog').dialog({
                buttons: buttons,
                modal: true,
                resizable: false,
                draggable: false,
                width: 382,
            }).dialog('close');

            userAddDialog.closest('.ui-dialog').addClass('dialog_container');

            var buttons = {};
            buttons[language.annuler] = function () {
                $('#template_add_dialog').dialog('close');
            };
            buttons[language.create_template] = function () {
                check_new_user(true);
            };

            var templateAddDialog = $('#template_add_dialog').dialog({
                buttons: buttons,
                modal: true,
                resizable: false,
                draggable: false,
                width: 382,

            }).dialog('close');

            templateAddDialog.closest('.ui-dialog').addClass('dialog_container');

            function check_new_user(is_template) {
                var container = is_template ? $('#template_add_dialog') : $('#user_add_dialog');
                $('#new_user_loader').show();
                $.ajax({
                    type: 'POST',
                    url: '../admin/users/create/',
                    dataType: 'json',
                    data: {
                        act: 'CREATENEWUSER',
                        value: $('input[name="value"]', container).val(),
                        send_credentials: $('input[name="send_credentials"]', container).is(':checked') ? 1 : 0,
                        validate_mail: $('input[name="validate_mail"]', container).is(':checked') ? 1 : 0,
                        template: is_template ? '1' : '0'
                    },
                    success: function (data) {
                        $('.new_user_loader', container).hide();
                        if (!data.error) {
                            if (container.data("ui-dialog")) {
                                container.dialog('close');
                            }
                            $('input[name="value"]', container).val('');
                            $('#right-ajax').empty().addClass('loading');
                            p4.users.sel = [];
                            $.ajax({
                                type: 'POST',
                                url: '../admin/users/rights/',
                                data: {
                                    users: data.data
                                },
                                success: function (data) {
                                    $('#right-ajax').removeClass('loading').html(data);
                                }
                            });
                        }
                        else {
                            alert(data.message);
                        }

                    },
                    error: function () {
                        alert(language.serverError);
                    },
                    timeout: function () {
                        alert(language.serverTimeout);
                    }
                });
            }

            $('#users_page .user_adder').on('click', function () {
                if ($('#user_add_dialog').data("ui-dialog")) {
                    $('#user_add_dialog').dialog('open');
                }
            });

            $('#users_page .template_adder').on('click', function () {
                if ($('#template_add_dialog').data("ui-dialog")) {
                    $('#template_add_dialog').dialog('open');
                }
            });

            $('#users_page_form').on('submit', function () {
                var datas = $('#users_page_form').serializeArray();
                $('#right-ajax').empty().addClass('loading');
                $.ajax({
                    type: 'POST',
                    url: '../admin/users/search/',
                    data: datas,
                    success: function (data) {
                        $('#right-ajax').removeClass('loading').empty().html(data);
                    }
                });

                return false;
            });

            $('#users_page_search').on('submit', function () {
                var datas = $('#users_page_search').serializeArray();
                $('#right-ajax').empty().addClass('loading');
                $.ajax({
                    type: 'POST',
                    url: '../admin/users/search/',
                    data: datas,
                    success: function (data) {
                        $('#right-ajax').removeClass('loading').empty().html(data);
                    }
                });

                return false;
            });

            $('#users_page_form .pager').on('click', function () {
                var form = $('#users_page_form');

                var current_page = parseInt($('input[name="page"]', form).val());
                var perPage = parseInt($('select[name="per_page"]', form).val());
                current_page = isNaN(current_page) ? 1 : current_page;

                var offset_start = 0;

                if ($(this).hasClass('prev')) {
                    offset_start = (current_page - 2) * perPage;
                }
                if ($(this).hasClass('first')) {
                    offset_start = 0;
                }
                if ($(this).hasClass('next')) {
                    offset_start = current_page * perPage;
                }
                if ($(this).hasClass('last')) {
                    offset_start = (Math.floor(parseInt($('input[name=total_results]').val()) / perPage)) * perPage;
                }

                $('input[name="offset_start"]', form).val(offset_start);
            });

            $('#users tbody tr, #users tbody td').on('dblclick', function (evt) {
                $('.user_modifier').trigger('click');
            });

            $('#users tbody tr, #users tbody td').on('click', function (evt) {
                if (evt.stopPropagation)
                    evt.stopPropagation();
                evt.cancelBubble = true;
                evt.preventDefault();
                var el = $(this).closest('tr');
                var cont = $('#users');

                var k = el.attr('id').split('_').pop();

                if (utilsModule.is_shift_key(evt) && $('tr.last_selected', cont).length != 0) {
                    var lst = $('tr', cont);
                    var index1 = $.inArray($('tr.last_selected', cont)[0], lst);
                    var index2 = $.inArray($(el)[0], lst);
                    if (index2 < index1) {
                        var tmp = index1;
                        index1 = (index2 - 1) < 0 ? index2 : (index2 - 1);
                        index2 = tmp;
                    }

                    var stopped = false;
                    if (index2 != -1 && index1 != -1) {
                        var exp = 'tr:gt(' + index1 + '):lt(' + (index2 - index1) + ')';
                        $.each($(exp, cont), function (i, n) {

                            if (!$(n).hasClass('selected')) {

                                if (!users_select_this(n, $(n).attr('id').split('_').pop())) {
                                    stopped = true;
                                    return false;
                                }
                            }
                        });
                    }

                    if (!stopped && $.inArray(k, p4.users.sel) < 0) {
                        if (!users_select_this(el, k))
                            return false;
                    }
                }
                else {
                    if (!utilsModule.is_ctrl_key(evt)) {
                        if ($.inArray(k, p4.users.sel) < 0) {
                            p4.users.sel = new Array();
                            $('tr', cont).removeClass('selected');

                            if (!users_select_this(el, k))
                                return false;
                        }
                    }
                    else {
                        if ($.inArray(k, p4.users.sel) >= 0) {
                            p4.users.sel = $.grep(p4.users.sel, function (n) {
                                return (n != k);
                            });
                            $(el).removeClass('selected');
                        }
                        else {
                            if (!users_select_this(el, k))
                                return false;
                        }
                    }
                }
                $('.last_selected', cont).removeClass('last_selected');
                $(el).addClass('last_selected');
            }).on('mousedown', function (evt) {

                if (evt.stopPropagation)
                    evt.stopPropagation();
                evt.cancelBubble = true;
                evt.preventDefault();
            });

            $('#users_apply_template').on('submit', function () {
                var users = p4.users.sel.join(';');
                if (users === '') {
                    return false;
                }

                var $this = $(this);
                var template = $('select[name="template_chooser"]', $this).val();

                if (template === '') {
                    return false;
                }

                dialogUserTemplate(function (reset_before_apply) {
                    $('#right-ajax').empty().addClass('loading');
                    p4.users.sel = [];

                    $.ajax({
                        type: $this.attr('method'),
                        url: $this.attr('action'),
                        data: {
                            users: users,
                            template: template,
                            reset_before_apply: reset_before_apply
                        },
                        success: function (data) {
                            $('#right-ajax').removeClass('loading').html(data);
                        }
                    });
                });
                return false;
            });

            $('.user_modifier').on('click', function () {
                var users = p4.users.sel.join(';');
                if (users === '') {
                    return false;
                }

                $('#right-ajax').empty().addClass('loading');
                p4.users.sel = [];
                $.ajax({
                    type: 'POST',
                    url: '../admin/users/rights/',
                    data: {
                        users: users
                    },
                    success: function (data) {
                        $('#right-ajax').removeClass('loading').html(data);
                    }
                });
                return false;
            });

            $('.user_deleter').on('click', function () {
                var users = p4.users.sel.join(';');
                if (users === '') {
                    return false;
                }

                $('#right-ajax').empty().addClass('loading');
                p4.users.sel = [];
                $.ajax({
                    type: 'POST',
                    url: '../admin/users/delete/',
                    data: {
                        users: users
                    },
                    success: function (data) {
                        $('#right-ajax').removeClass('loading').html(data);
                    }
                });
                return false;
            });
            $('#users_page .invite_modifier, #users_page .autoregister_modifier').on('click', function () {
                var users = $(this).next('input').val();

                if ($.trim(users) === '') {
                    return false;
                }

                $('#right-ajax').empty().addClass('loading');
                p4.users.sel = [];
                $.ajax({
                    type: 'POST',
                    url: '../admin/users/rights/',
                    data: {
                        users: users
                    },
                    success: function (data) {
                        $('#right-ajax').removeClass('loading').html(data);
                    }
                });
                return false;
            });
            $.each(p4.users.sel, function (i, n) {
                var el = $('#user_' + n);
                if (el.length > 0) {
                    el.addClass('selected');
                }
            });
            $('#users_page_form select[name="per_page"]').bind('change', function () {
                $(this).closest('form').submit();
            });

            $('#users_reset_rights button').bind('click', function () {
                var selectedUsers = $("#users tr.selected");

                if (selectedUsers.length === 0) {
                    alert("{{ 'select at least one user' | trans }}");
                    return;
                }

                var users = '';
                $.each(selectedUsers, function (i, n) {
                    users += $(n).attr("id").split("_").pop() + ';';
                });

                if (confirm("{{ 'Are you sure you want delete users rights ?' | trans }}")) {
                    $("#users_reset_rights input[name='users']").val(users);

                    var form = $(this).closest('form');

                    $.ajax({
                        url: form.attr("action"),
                        dataType: 'json',
                        type: form.attr('method'),
                        data: form.serializeArray(),
                        success: function (data) {
                            if (data.error) {
                                alert("{{ 'An error occured' | trans }}");
                            }
                            else {
                                alert("{{ 'users rights have been reseted' | trans }}");
                            }
                        }
                    });
                }
            });

            $("#users_page_form .input-mini").keydown(function (e) {
                if (e.which == 13) {
                    //prevent form from being submitted
                    e.preventDefault();
                }
            });
            $("#users_page_form .input-mini").keyup(function (e) {
                if (e.which == 13) {
                    e.preventDefault();
                    var form = $('#users_page_form');
                    var current_page = parseInt($('.input-mini').val());
                    var perPage = parseInt($('select[name="per_page"]', form).val());
                    current_page = isNaN(current_page) || current_page < 1
                    || current_page > {{ pages|floor }} ? 1 : current_page;
                    var offset_start = (current_page-1) * perPage;
                    $('input[name="offset_start"]', form).val(offset_start);
                    $('#users_page_form').trigger('submit');
                }
            });
        });

  function exportlist()
  {
    $('#export_form').submit();
  }

    </script>
</div>

{% endblock %}
