<metal:block use-macro="python:here.getFormsTool().getForm('site_admin_users').macros['page']">
  <metal:block fill-slot="second-title" i18n:translate="">Local users</metal:block>

  <metal:block fill-slot="description-text">
    <div class="description-text">
      <p i18n:translate="">
        This page lists the local users defined in this portal along with their credentials.
      </p>
    </div>
  </metal:block>

  <metal:block fill-slot="section-title">
  </metal:block>

  <metal:block fill-slot="buttons"><metal:block define-macro="buttons">
      <div class="floated-buttons">
        <span class="buttons">
          <a tal:condition="python:user_tool.showBulkDownloadButton()"
            tal:attributes="href string:${user_tool/absolute_url}/downloadUsersCsv">
            <span i18n:translate="">Bulk download</span>
          </a>
        </span>
      </div>
    </metal:block>
  </metal:block>

  <metal:block fill-slot="second-tabs"><metal:block define-macro="second-tabs">
      <div class="second_tab_set">
        <ul>
          <li tal:define="url string:${site_url}/admin_local_users_html">
            <a tal:attributes="href url; class python: test('admin_local_users_html' in request.PATH_INFO or 'admin_users_html' in request.PATH_INFO, 'current_sub', '')"
              i18n:translate="">List users</a>
          </li>
          <li tal:define="url string:${site_url}/admin_assignroles_html">
            <a tal:attributes="href url; class python: test('admin_assignroles_html' in request.PATH_INFO, 'current_sub', '')"
              i18n:translate="">Assign roles to users</a>
          </li>
          <li tal:define="url string:${site_url}/admin_adduser_html">
            <a tal:attributes="href url; class python: test('admin_adduser_html' in request.PATH_INFO, 'current_sub', '')"
              i18n:translate="">Add new user</a>
          </li>
          <li tal:define="url string:${site_url}/admin_importusers_html">
            <a tal:attributes="href url; class python: test('admin_importusers_html' in request.PATH_INFO, 'current_sub', '')"
              i18n:translate="">Import users</a>
          </li>
        </ul>
      </div>
      <div class="cleaner"></div>
    </metal:block>
  </metal:block>

  <metal:block fill-slot="content">
    <metal:block define-macro="content" tal:define="
      disabled_type python:request.form.get('disabled', 'no_disabled');
      users python: user_tool.search_users(query, skey=skey, rkey=rkey, role=filter_role, location=filter_location, disabled_type=disabled_type)">

      <script type="text/javascript" src="/++resource++Products.Naaya/lib/datatables/jquery.dataTables.min.js"></script>
      <link rel="stylesheet" type="text/css" href="/++resource++Products.Naaya/lib/datatables/jquery.dataTables.css" />
      <script type="text/javascript">
        $(document).ready(function(){
          $('.users_listing').each(function(){
            $(this).dataTable({
            'aaSorting': [[0, "desc"]],
            'aoColumnDefs': [
              { 'bSortable': false, 'aTargets': [ 3 ] }
             ],
            'sPaginationType': 'full_numbers',
            "aLengthMenu": [[10, 25, 50, -1],
                    [10, 25, 50, "All"]],
            'iDisplayLength': 10
            });
          });
        });
      </script>
      <form method="post" action="" class="datatable-form">
        <div class="cleaner">&nbsp;</div>
        <div class="form-buttons">
          <span class="buttons">
            <input tal:condition="users" type="submit"
              value="Delete selected user(s)" i18n:attributes="value"
              class="deluser" name="../admin_deleteusers:method" />
            <input tal:condition="python: users and filter_role != 'noroles'"
              type="submit"
              value="Revoke searched role(s)" i18n:attributes="value"
              class="revoke_roles" name="../admin_revoke_searched_roles:method" />
          </span>
        </div>
        <div class="cleaner">&nbsp;</div>

        <input type="hidden" id="template" value="site_admin_local_users" />
        <input type="hidden" name="role" tal:attributes="value filter_role" />
        <input type="hidden" name="location" tal:attributes="value filter_location" />

        <metal:block define-macro="datatable">
          <div class="user-results">

            <div class="cleaner"></div>

            <table cellspacing="0" cellpadding="4" width="95%"
              tal:attributes="class python:len(users) and 'datatable users_listing' or 'empty'">
              <thead>
                <tr  tal:condition="python:len(users) != 0">
                  <th width="5%" align="left" class="checkbox">
                    <input type="checkbox" class="select-all" onclick="selectAll();" />
                  </th>
                  <tal:block define="sortdata python: ({'key': 'username', 'label': 'Username', 'width': '15%'},
                    {'key': 'name', 'label': 'Name/Email', 'width': '30%'},
                    {'key': '', 'label': 'Roles in Locations'},
                    {'key': '', 'label': 'Revoke roles'});
                    querystring python: 'query=%s&amp;role=%s&amp;location=%s' % (query, filter_role, filter_location);">
                    <th align="left" tal:repeat="item sortdata" tal:attributes="width item/width|nothing">
                      <tal:block condition="item/key">
                        <a class="sort-key"
                          tal:condition="python: (skey == item['key'] and rkey) or skey != item['key']"
                          tal:attributes="href string:${here/absolute_url}/admin_local_users_html?${querystring}&skey=${item/key}"
                          i18n:translate="" tal:content="item/label"></a>
                        <a class="sort-key"
                          tal:condition="python: skey == item['key'] and not rkey"
                          tal:attributes="href string:${here/absolute_url}/admin_local_users_html?${querystring}&skey=${item/key}&rkey=1"
                          i18n:translate="" tal:content="item/label"></a>
                        <img tal:condition="python: skey == item['key'] and not rkey" src="misc_/Naaya/sort_asc.gif" />
                        <img tal:condition="python: skey == item['key'] and rkey" src="misc_/Naaya/sort_desc.gif" />
                      </tal:block>
                      <tal:block condition="not: item/key"
                        i18n:translate="" tal:content="item/label">
                      </tal:block>
                    </th>
                  </tal:block>
                </tr>
              </thead>
              <tbody>
                <tal:block repeat="user users">
                  <tal:block define="user_roles user/roles;
                    row_class python:test(path('repeat/user/odd'), 'row-odd', 'row-even');
                    user_id user/user_id">
                      <tr>
                        <!-- checkbox -->
                        <td nowrap="nowrap" class="center checkbox">
                          <input type="checkbox" name="names"
                            tal:attributes="value user/user_id" />
                        </td>
                        <!-- end of checkbox -->

                        <!-- Username -->
                        <td>
                          <strong><a tal:define="user_quoted python:here.utUrlEncode(user_id)"
                              tal:attributes="href string:${site_url}/admin_edituser_html?name=${user_quoted}&from=local" tal:content="user_id">username</a></strong>
                          <span tal:condition="user/is_new_user">
                            <img src="misc_/Naaya/star.png" alt="New account"
                              tal:define="global any_new_account python:True" />
                          </span>
                          <span tal:define="source_title user/source_title"
                            tal:condition="python: user_id in user_tool.user_names() and source_title != 'Local'">
                            <img src="misc_/Naaya/conflict_users.png" alt="Conflicting usernames"
                              i18n:attributes="alt" tal:define="global any_username_conflict python:True" />
                          </span>
                        </td>
                        <!-- end of Username -->

                        <!-- Name/Email -->
                        <td>
                          <div class="datatable-username">
                            <tal:block content="user/full_name"></tal:block>
                          </div>
                          <div class="datatable-email" tal:define="email user/email">
                            <a tal:attributes="href string:mailto:${email}; title string:Click to open email client" i18n:attributes="title" tal:content="email"></a>
                          </div>
                        </td>
                        <!-- end of Name/Email -->

                        <!-- Roles in Locations -->
                        <td class="user-role">
                          <ul tal:condition="user_roles">
                            <tal:block repeat="role user_roles">
                            <li tal:define="obj python:role[1]">
                              <span class="roles-title" tal:content="python: ', '.join(role[0])"></span> in
                              <a class="roles-location"
                                  tal:attributes="href obj/absolute_url"
                                  tal:content="obj/title_or_id"></a>
                              <img tal:condition="python: here.getSite()!=obj.getSite()"
                                tal:attributes="alt python:('In %s subsite' % obj.getSite().title_or_id());
                                title python:('In %s subsite' % obj.getSite().title_or_id());"
                                src="misc_/Naaya/info.png" />
                            </li>
                            </tal:block>
                          </ul>
                          <tal:block condition="not:user_roles">-</tal:block>
                        </td>
                        <!-- end of Roles in Locations -->

                        <!-- Revoke roles -->
                          <td class="user-role-revoke">
                            <ul taL:condition="user_roles">
                              <tal:block repeat="role user_roles">
                                <li>
                                  <a class="revoke-role" i18n:attributes="title" title="Revoke role"
                                  tal:define="location python: here.rstk['relative_object_path'](role[1], here.getSite())"
                                  tal:attributes="href string:${site_url}/admin_revokerole?user=${user_id}&location=${location}">
                                    <img src="/misc_/Naaya/revoke_permission.png"
                                    alt="Revoke role"
                                    i18n:attributes="alt" />
                                  </a>
                                </li>
                              </tal:block>
                            </ul>
                          </td>
                        <!-- end of Revoke roles -->
                      </tr>
                  </tal:block>
                </tal:block>

                <tr tal:condition="not: users" class="tr-messages">
                  <tal:block define="has_filters python: query or filter_role or filter_location != '_all_'">
                    <tal:block condition="not: has_filters">
                      <td colspan="6">
                        <tal:block condition="user_sources">
                          <p class="table-message" tal:define="source_id python: user_sources[0].id" i18n:translate="">
                            No local users defined yet.
                            You can <a  i18n:name="add-user-link" tal:attributes="href string:${site_url}/admin_adduser_html" title="Add local user" i18n:attributes="title" i18n:translate="">add a local user</a>.
                          </p>
                        </tal:block>
                      </td>
                    </tal:block>
                    <tal:block condition="has_filters">
                      <td colspan="6">
                        <p class="table-message" i18n:translate="">
                          No users matched this search criteria.
                        </p>
                      </td>
                    </tal:block>
                  </tal:block>
                </tr>
              </tbody>
            </table>

            <div class="tip"
              tal:define="any_new_account any_new_account|nothing;
              any_username_conflict any_username_conflict|nothing"
              tal:condition="python:any_new_account or any_username_conflict">
              <p>
                <span i18n:translate="" tal:condition="any_new_account">
                  <img src="misc_/Naaya/star.png" alt="New account" title="New account" i18n:attributes="alt; title" i18n:name="image" /> local users added in the past 5 days
                </span>

                <tal:block condition="user_sources">
                  <br class="cleaner" />
                  <span i18n:translate="" tal:condition="any_username_conflict">
                    <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.
                  </span>
                </tal:block>
              </p>
            </div>

            <div class="cleaner">&nbsp;</div>
          </div>
        </metal:block>
      </form>
    </metal:block>
  </metal:block>
</metal:block>
