<div id="section_parent" tal:define="
    user_folder python:here.getUserFolder();
    req_role python:request.get('req_role', '');
    req_location python:request.get('req_location', '');
    source_id python:request.get('id', '');
    source_title python:here.getAuthenticationTool().getSourceObj(source_id).title_or_id();
    ">

    <div class="cleaner">&nbsp;</div>

    <h3 i18n:translate="">
        Assign roles to <tal:block i18n:name="source-title" replace="source_title"></tal:block> users
    </h3>

    <div class="description-text">
        <p i18n:translate="">
            Use this form to find users from the <tal:block i18n:name="source-title" replace="source_title" /> directory, view their
            details and assign them roles in the current portal.
        </p>
    </div>

    <br />

    <form method="post" tal:attributes="action python:request.get('URL', '')" name="frmRoles" id="frmRoles">
        <input type="hidden" name="s" value="assign_to_users" />
        <input type="hidden" name="search" value="" />
        <input type="hidden" name="id" tal:attributes="value source_id" />
        <input type="hidden" name="dn" />

        <table border="0" cellspacing="4" cellpadding="0">
            <tr class="tr-hover">
                <td width="20">
                    <div style="white-space: nowrap; ">
                        <label for="params" i18n:translate="">Matching criteria</label>
                    </div>
                </td>

                <td width="1">
                    <select id="params" name="params" tal:define="
                            ldap_schema python:here.getLDAPSchema(user_folder)">
                        <option tal:repeat="item ldap_schema"
                            tal:attributes="value python:item[0]"
                            tal:content="python:'%s (%s)' % (item[1], item[0])"/>
                    </select>
                </td>

                <td width="1">
                    <label for="term" i18n:translate="">containing</label>
                </td>

                <td width="1">
                    <input type="text" id="term" name="term" value="" />
                </td>

                <td>
                    <input type="submit" name="search_user" value="Search" i18n:attributes="value" />
                </td>
            </tr>

            <tr>
                <td>
                    <strong i18n:translate="">OR</strong>
                </td>
            </tr>

            <tr class="tr-hover">
                <td width="20">
                    <div style="white-space: nowrap; ">
                        <label for="role" i18n:translate="">which have the role</label>
                    </div>
                </td>

                <td width="1">
                    <input type="text" id="role" name="role" size="15" />
                </td>

                <td width="1">
                    <input type="button" name="pickroles2" value="Pick"
            i18n:attributes="value"
                        tal:define="
                            url string:${here/getPluginPath}/pickroles_html;
                            js_code string:fPickRoles('${url}', 'dn');"
                        tal:attributes="onclick string:javascript:${js_code}" />
                </td>

                <td width="1">&nbsp;</td>

                <td>
                    <input type="submit" name="search_role" value="Search" i18n:attributes="value" />
                </td>
            </tr>
        </table>
    </form>


    <div id="waiting_for_search_results" class="loader">
        <img class="ajax-loader" src='misc_/Naaya/ajax-loader.gif' title="Loading" alt="Search results" i18n:attributes="alt; title" />
        <span i18n:translate="">&nbsp;Loading... please wait!</span>
    </div>

    <div id="error_on_search_results" class="message-error"
        style="display:none">
        <p i18n:translate="">
            Error searching for users.
        </p>
    </div>

    <div id="search_results_parent">
        <br />
    <tal:block define="
                       is_user_search python:request.has_key('search_user');
                       is_role_search python:request.has_key('search_role');
                       search_param python:request.get('params', '');
                       search_term python:request.get('term', '');
                       role python:request.get('role', '');
                       dn python:request.get('dn', '');
                       ldap_users python:here.findLDAPUsers(user_folder,
                                                            search_param,
                                                            search_term,
                                                            role,
                                                            dn);"
               condition="python:is_user_search or is_role_search">

    <tal:block condition="ldap_users">
    <div class="separator"></div>
    <h3 tal:condition="is_user_search" i18n:translate="">
    Users found for '<strong i18n:name="search-text" tal:content="search_term" />' search term
    </h3>
    <p tal:condition="is_role_search" i18n:translate="">
        Users found with <strong i18n:name="role-name" tal:content="role" /> role
    </p>
    </tal:block>

    <p tal:condition="not:ldap_users" class="message-empty" i18n:translate="">
       No <tal:block i18n:name="source" replace="source_title" /> users matching the selected criteria.
    </p>

    <tal:block condition="python:len(ldap_users) > 10">
    <a href="#assign_roles" i18n:translate="">Jump to assign roles to selected users</a>
    </tal:block>

    <form id="search_results_form" name="user-roles" method="post"
        tal:condition="ldap_users"
        tal:attributes="action string:${here/absolute_url}/addUserRoles">
        <table cellspacing="0" cellpadding="4" id="search_results" class="datatable sortable">
            <tr>
                <th width="5%" align="left" class="checkbox">
                    <input type="checkbox" class="select-all" checked="checked" onclick="selectAll(name='zope_perm_list:list');" />
                </th>
                <th i18n:translate="">User ID</th>
                <th i18n:translate="">Canonical name</th>
                <th i18n:translate="">Email</th>
                <th i18n:translate="" width="60%">Distinguished Name</th>
            </tr>
            <tr tal:repeat="item ldap_users"
                tal:attributes="class python:test(path('repeat/item/odd'),
                                                     'row-odd', 'row-even')">
                <td class="checkbox">
                    <input type="checkbox" name="name:list"
                        tal:attributes="value item/user_id" CHECKED/>
                </td>
                <td tal:define="uid item/user_id">
                    <tal:block condition="python: source_title.lower()=='eionet'"
                      define="user_quoted python:here.utUrlEncode(uid)">
                      <a rel="external" tal:content="user_quoted"
                      tal:attributes="href string:https://www.eionet.europa.eu/directory/user?uid=${user_quoted}" />
                    </tal:block>
                    <tal:block condition="python: source_title.lower()!='eionet'"
                      define="user_quoted python:here.utUrlEncode(uid)"
                      content="user_quoted" />
                    <span tal:condition="python: uid in here.user_names() and here.getUserSource(uid) != 'n/a'">
                        <img src="misc_/Naaya/conflict_users.png" alt="Conflicting usernames"
                             i18n:attributes="alt" tal:define="global any_username_conflict python:True" />
                    </span>
                </td>
                <td tal:content="item/full_name">cn</td>
                <td><a tal:condition="item/email" tal:attributes="href string:mailto:${item/email}"
                    tal:content="item/email" />
                </td>
                <td tal:content="item/dn">dn</td>
            </tr>
        </table>

        <div id="portal_users_empty" class="message-error" style="display:none">
            <p i18n:translate="">
                No users selected.
            </p>
        </div>

        <br />

        <div class="tip cleaner" tal:condition="any_username_conflict|nothing">
            <p i18n:translate="">
        <img src="misc_/Naaya/conflict_users.png" alt="Conflicting usernames"
        title="Conflicting usernames" i18n:attributes="alt; title" i18n:name="image" /> local and LDAP accounts for the same username
            </p>
        </div>
        <tal:block tal:condition="python:len(ldap_users) > 10">
        &middot;&nbsp;<a href="#section_parent" i18n:translate="">Go back to top</a>
        </tal:block>

        <br />

        <h3 i18n:translate="">
            Assign roles to selected users
        </h3>

        <div id="assign_roles" class="assign-form">
            <div class="field">
                <label for="portal_roles" i18n:translate="">
                    Roles
                </label>

                <select id="portal_roles" name="roles:list" size="6" multiple="multiple">
                    <tal:block repeat="item here/list_valid_roles">
                    <option tal:condition="python:item=='Manager' and request.AUTHENTICATED_USER.has_role('Manager')"
                            tal:attributes="value item; selected python:req_role and req_role == item" tal:content="item" />
                    <option tal:condition="python:item!='Manager'" tal:attributes="value item; selected python:req_role and req_role == item" tal:content="item" />
                    </tal:block>
                </select>
            </div>

            <div id="portal_roles_empty" class="message-error" style="display:none">
                <p i18n:translate="">
                    No role selected.
                </p>
            </div>

            <div class="field">
                <label for="location">
                    <span tal:omit-tag="" i18n:translate=""><span tal:omit-tag="" i18n:translate="">Location (folder):</span></span>
                    <br />
                    <noscript>
                        <small i18n:translate="">e.g. folderUrl</small>
                    </noscript>
                </label>

                <input name="location" class="sitemap_tree_target" id="location" type="text" size="50" value="" />
                <small i18n:translate="">(choose a location from the tree below)</small>

                <div id="sitemap_tree" class="sitemap_tree"></div>
                <div class="cleaner"></div>
                <tal:block define="tree_get_url string:${here/absolute_url}/getNavigationSiteMap/?only_folders=1">
                    <metal:block use-macro="here/macro_utils/macros/ajaxtree" />
                </tal:block>
            </div>

            <div class="field-inline">
                <input type="checkbox" id="send_mail" name="send_mail" checked="checked" />
                <label for="send_mail" i18n:translate="">Send notification email to selected user(s)</label>
            </div>
        </div>

        <br />

        <div>
            <input type="hidden" name="uf" tal:attributes="value here/obj_path" />
            <input type="hidden" name="user_location" tal:attributes="value python:test(role, role, 'Users')" />
            <input type="submit" name="add" value="Assign role" i18n:attributes="value" />
        </div>
    </form>

    <div id="assign_users_waiting_response" style="display:none" class="cleaner">
        <img src="misc_/Naaya/ajax-loader.gif" align="left"/>
        <p i18n:translate="">
            Waiting
        </p>
    </div>

    <div id="assign_users_error_response" class="message-error cleaner"
        style="display:none">
        <p i18n:translate="">
            Error assigning user roles.
        </p>
    </div>


    </tal:block>
    </div>
</div>
