<metal:block use-macro="python:here.getFormsTool().getForm('site_admin_template').macros['page']">

<h1 metal:fill-slot="title" i18n:translate="">Notifications administration</h1>
<tal:block metal:fill-slot="section" tal:define="get_config python:here.get_config">
<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(){
        if ($('#notification_table').length){
            $('#notification_table').dataTable({
                'aaSorting': [[0, "desc"]],
                'sPaginationType': 'full_numbers',
                "aLengthMenu": [[10, 25, 50, -1],
                                [10, 25, 50, "All"]]
            });
        }
    });
</script>

<tal:block define="email_tool here/getEmailTool"
           content="structure email_tool/configuration_errors_report" />

<tal:block define="global days_of_week python:['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']" />

<p i18n:translate="">
  Notifications are sent to subscribed users when content is added or modified
  in the portal.
</p>

<form id="settingsForm" method="post" tal:attributes="action string:${here/absolute_url}/admin_settings">

<!--

<fieldset><legend i18n:translate="">Site administrators</legend>
<div class="field-inline">
    <label for="admin_on_edit" i18n:translate="">Notify on edit</label>
    <input type="checkbox" id="admin_on_edit" name="admin_on_edit:boolean"
        tal:attributes="checked python:test(get_config('admin_on_edit'), 'checked', None)" />
</div>
<div class="field-inline">
    <label for="admin_on_error" i18n:translate="">Notify on error</label>
    <input type="checkbox" id="admin_on_error" name="admin_on_error:boolean"
        tal:attributes="checked python:test(get_config('admin_on_error'), 'checked', None)" />
</div>
</fieldset>

-->

<fieldset><legend i18n:translate="">Users may subscribe to notifications</legend>
<div class="field-inline">
    <input type="checkbox" id="enable_instant" name="enable_instant:boolean"
        tal:attributes="checked python:test(get_config('enable_instant'), 'checked', None)" />
    <label for="enable_instant"
           i18n:translate="">Allow instant notification</label>
</div>
<div class="field-inline">
    <input type="checkbox" id="enable_daily" name="enable_daily:boolean"
        tal:attributes="checked python:test(get_config('enable_daily'), 'checked', None)" />
    <label for="enable_daily"
           i18n:translate="">Allow daily digest</label>
    <!--
    <label for="daily_hour" i18n:translate="">hour:</label>
    <select id="daily_hour" name="daily_hour:int">
        <option tal:repeat="hour python:range(24)" tal:content="python:'%02d' % hour"
          tal:attributes="value hour; selected python:hour==get_config('daily_hour')" />
    </select>
    -->
</div>
<div class="field-inline">
    <input type="checkbox" id="enable_weekly" name="enable_weekly:boolean"
        tal:attributes="checked python:test(get_config('enable_weekly'), 'checked', None)" />
    <label for="enable_weekly"
           i18n:translate="">Allow weekly digest</label>
    <!--
    <label for="weekly_day" i18n:translate="">day:</label>
    <select id="weekly_day" name="weekly_day:int">
        <option tal:repeat="day python:range(7)" tal:content="python:days_of_week[day-1]"
          tal:attributes="value day; selected python:day==get_config('weekly_day')" />
    </select>
    <label for="weekly_hour" i18n:translate="">hour:</label>
    <select id="weekly_hour" name="weekly_hour:int">
        <option tal:repeat="hour python:range(24)" tal:content="python:'%02d' % hour"
          tal:attributes="value hour; selected python:hour==get_config('weekly_hour')" />
    </select>
    -->
</div>
<div class="field-inline">
    <input type="checkbox" id="enable_monthly" name="enable_monthly:boolean"
        tal:attributes="checked python:test(get_config('enable_monthly'), 'checked', None)" />
    <label for="enable_monthly"
           i18n:translate="">Allow monthly digest</label>
    <!--
    <label for="monthly_day" i18n:translate="">day:</label>
    <select id="monthly_day" name="monthly_day:int">
        <option tal:repeat="day python:range(1, 32)" tal:content="python:'%d' % day"
          tal:attributes="value day; selected python:day==get_config('monthly_day')" />
    </select>
    <label for="monthly_hour" i18n:translate="">hour:</label>
    <select id="monthly_hour" name="monthly_hour:int">
        <option tal:repeat="hour python:range(24)" tal:content="python:'%02d' % hour"
          tal:attributes="value hour; selected python:hour==get_config('monthly_hour')" />
    </select>
    -->
</div>
<div class="field-inline" tal:condition="not: here/portal_is_restricted|nothing">
    <input type="checkbox" id="enable_anonymous" name="enable_anonymous:boolean"
        tal:attributes="checked python:test(get_config('enable_anonymous'), 'checked', None)" />
    <label for="enable_anonymous"
           i18n:translate="">Allow subscriptions with any e-mail address</label>
</div>
<!--
<div class="field">
    <label id="content_types" i18n:translate="">Content types</label>
    <select id="content_types" name="content_types:list" size="5" multiple="true">
        <option tal:repeat="item here/get_pluggable_metatypes"
                tal:attributes="value item; selected python:item in get_config('notif_content_types')"
                tal:content="item">Meta type</option>
    </select>
</div>
    <p>Granularity: users may subscribe to notifications for changes in the whole portal.</p>
<div class="field-inline">
    <label id="enable_top_folders" i18n:translate="">Also allow subscription to top-level folders</label>
    <input type="checkbox" id="enable_top_folders" name="enable_top_folders:boolean"
        tal:attributes="checked python:test(get_config('enable_top_folders'), 'checked', None)" />
</div>
-->
<div class="field">
    <input type="submit" value="Save changes" />
</div>
</fieldset>
</form>

<form id="skip_notifications" method="post" tal:attributes="action string:${here/absolute_url}/skip_notifications">
  <fieldset>
    <legend i18n:translate="">Administration actions</legend>
    <div class="field-inline">
      <input type="hidden" id="skip_notifications" name="skip_notifications:boolean"
          tal:attributes="value python:test(request.SESSION.get('skip_notifications', False), None, 1)" />
      <label
             i18n:translate="">Create/change/move objects without sending notifications to subscribers. This will affect only the actions made by the user: <tal:block replace="request/AUTHENTICATED_USER/getUserName" /></label>
      <br />
      <input name="submit-button" type="submit" i18n:attributes="value"
            tal:attributes="value python:test(request.SESSION.get('skip_notifications', False), 'Enable notifications', 'Disable notifications')"/>
    </div>
  </fieldset>
</form>

<h2 i18n:translate="">Current subscriptions</h2>
<tal:block tal:define="query python:request.get('query', '');
                       rkey python:request.get('rkey', '');
                       object_list python:list(here.admin_get_subscriptions(query));
                       page python:int(request.get('page', 1));
                       available_subscriptions python:list(here.available_notif_types())">
<form id="addSubscriptionForm" action="admin_add_account_subscription" method="post"
      tal:condition="available_subscriptions">
    <fieldset>
        <legend i18n:translate="">Add user subscription</legend>
        <div class="field">
            <label for="user_id" i18n:translate="">Search user by name, user ID or email</label>
            <input type="text" id="user_id" name="user_id"/>
            <span id="pick-user-ui" style="display:none;">
                <button i18n:translate="">Find user</button>
                <div id="uid_wakeup">
                </div>
                <div class="search" style="display:none;">
                    <fieldset>
                        <div id="user-search-results"></div>
                    </fieldset>
                </div>
            </span>
        </div>
        <div class="field" id="location_id">
            <label for="location" i18n:translate="">Location</label>
            <div id="sitemap_tree" class="sitemap_tree"></div>
            <div style="clear: both"></div>
            <input type="text" id="location" name="location" class="sitemap_tree_target" style="width:40%;"/>
        </div>
        <div class="field-inline" id="subscribe_to_id">
            <label for="notif_type" i18n:translate="">Subscribe to</label>
            <select name="notif_type" id="subscribe_me-notif_type">
                <option value="administrative"
                        tal:condition="here/checkPermissionPublishObjects"
                        i18n:translate="">Administrative notifications (uploaded objects)</option>
                <option value="instant" tal:condition="python:'instant' in available_subscriptions" i18n:translate="">Instant notifications</option>
                <option value="daily" tal:condition="python:'daily' in available_subscriptions" i18n:translate="">Daily digest</option>
                <option value="weekly" tal:condition="python:'weekly' in available_subscriptions" i18n:translate="">Weekly digest</option>
                <option value="monthly" tal:condition="python:'monthly' in available_subscriptions" i18n:translate="">Monthly digest</option>
            </select>
        </div>
        <div class="field-inline" id="all_content_types">
            <label for="subscribe_me-all_content_types" i18n:translate="">All content types</label>
            <input id="subscribe_me-all_content_types" type="checkbox" checked="checked"/>
        </div>
        <div class="field" id="content_types">
            <label for="subscribe_me-content_types" i18n:translate="">Select content types</label>
            <select size="15" multiple name="content_types"
                    id="subscribe_me-content_types"
                    tal:define="gettext python:here.getPortalI18n().get_translation;
                                meta_types python:here.get_meta_types(1)">
              <tal:block repeat="item meta_types">
                  <option tal:define="item_label python:here.get_meta_type_label(item);"
                    tal:attributes="value item" tal:content="item_label" />
              </tal:block>
            </select>
        </div>
        <div class="field-inline" id="language_id">
            <label for="lang" i18n:translate="">Language</label>
            <select name="lang" id="lang"
                    tal:define="translations python:here.getPortalI18n().get_translation">
                <option tal:repeat="item here/gl_get_languages_map"
                        tal:attributes="value python:item['id'];
                            selected python:item['selected']"
                        tal:content="python:'['+item['id']+'] '+translations(item['title'])" />
            </select>
        </div>
        <input type="submit" id="subscribe_button_id" value="Subscribe user" i18n:attributes="value"/>
    </fieldset>
</form>

<p i18n:translate="" tal:condition="not: available_subscriptions">
    Can not add subscriptions because no notification types are enabled.
</p>

<fieldset>
    <legend i18n:translate="">Subscribtions</legend>
<tal:block tal:condition="python: (not object_list) and (not query)"
    i18n:translate="">No users have yet subscribed to any notifications.
</tal:block>

<tal:block condition="python: object_list or query">
    <div class="floated-buttons">
        <span class="buttons">
            <a tal:attributes="href python:'%s/download?file_type=CSV' % here.absolute_url()" i18n:translate="">Download as CSV</a>
        </span>
        <span class="buttons">
            <a tal:attributes="href python:'%s/download?file_type=Excel' % here.absolute_url()" i18n:translate="">Download as Excel</a>
        </span>
    </div>
    <tal:block condition="not:object_list" i18n:translate="">
        No subscribed users matched the selected query.
    </tal:block>
    <tal:block condition="object_list">
    <table id="notification_table" class="datatable">
        <thead>
            <th i18n:translate="">User</th>
            <th i18n:translate="">Location</th>
            <th i18n:translate="">Notification type</th>
            <th i18n:translate="">Language</th>
            <th i18n:translate="">Content types</th>
            <th></th>
        </thead>
        <tbody>
            <tr tal:repeat="sub_data object_list">
                <td>
                    <span tal:content="sub_data/user"/>
                </td>
                <td tal:define="location sub_data/location">
                    <span tal:condition="location"><a tal:attributes="href python:here.get_location_link(location)" tal:content="location"></a></span>
                    <span tal:condition="not:location"><a tal:attributes="href python:here.get_location_link(location)">entire portal</a></span>
                </td>
                <td>
                    <span tal:content="sub_data/notif_type"/>
                </td>
                <td tal:define="lang sub_data/lang">
                    <span tal:content="python:here.gl_get_language_name(lang)"/>
                </td>
                <td tal:define="content_types sub_data/content_types;
                                content_types python:', '.join(content_types).replace('Naaya ', '') or 'ALL'">
                    <span tal:content="content_types"/>
                </td>
                <td>
                    <form id="delSubscriptionForm" method="post"
                          tal:attributes="action string:${here/absolute_url}/admin_remove_account_subscription">
                        <input type="hidden" name="location"
                               tal:attributes="value sub_data/location"/>
                        <input type="hidden" name="sub_id:int"
                               tal:attributes="value sub_data/sub_id"/>
                        <input type="submit" value="unsubscribe"
                               i18n:attributes="value"/>
                    </form>
                </td>
            </tr>
        </tbody>
    </table>
    </tal:block>
</tal:block>
</fieldset>

</tal:block>
<!-- scripts -->
<script type="text/javascript" src="/++resource++NaayaCore.NotificationTool/admin_user.js">
</script>
<tal:block define="tree_get_url string:${here/absolute_url}/sitemap">
  <metal:block use-macro="here/macro_utils/macros/ajaxtree" />
</tal:block>
<script tal:content="python:('var notif_tool_url = %s' %
                     here.rstk['json_dumps'](here.absolute_url()))"></script>
</tal:block>
</metal:block>
