<metal:block metal:use-macro="here/standard_template_macro">
<metal:block metal:fill-slot="header-title">
    <title tal:content="string:${here/title} | ${here/site_title}" />
</metal:block>

<metal:block fill-slot="body"
	tal:define="req_location request/form/location|string:;
							req_notif_type request/form/notif_type|nothing;
							my_subscriptions python:here.list_my_subscriptions(request);
							user_not_found python:here.user_not_found(request);
							location_subscription python:here.get_location_subscription(req_location, req_notif_type);
							first_location location_subscription/location|nothing;
							first_notif_type location_subscription/notif_type|nothing;
							first_content_types location_subscription/content_types|python:[];">

<script type="text/javascript">
    $(function() {
      if ($('#subscribe_me-all_content_types').is(':checked')){
        $('#content_types').hide();
      }
      if ($('#subscribe_me-notif_type').val() == 'administrative'){
          $('#subscribe_me-content_types option').removeProp('selected')
          $('#content_types').hide();
          $('#subscribe_me-all_content_types').prop('checked', 'checked');
          $('#subscribe_me-all_content_types').prop('disabled', 'disabled');
          $('#administrative_help').show()
      }
      else if ($('#subscribe_me-all_content_types').is(':checked')){
          $('#all_content_types_help').show()
      }

      $('#subscribe_me-notif_type').change(function(){
          if ($(this).val() == 'administrative'){
              if (!$('#subscribe_me-all_content_types').is(':checked')){
                  $('#subscribe_me-content_types option').removeProp('selected')
                  $('#content_types').hide();
                  $('#subscribe_me-all_content_types').prop('checked', 'checked');
              }
              $('#subscribe_me-all_content_types').prop('disabled', 'disabled');
              $('#all_content_types_help').hide()
              $('#administrative_help').show()
          } else {
              if ($('#subscribe_me-all_content_types').is(':disabled')){
                  $('#subscribe_me-all_content_types').removeProp('disabled');
              }
              $('#administrative_help').hide()
              $('#all_content_types_help').show()
          }
      });
      $('#subscribe_me-all_content_types').change(function(){
          if ($(this).is(':checked')){
              $('#subscribe_me-content_types option').removeProp('selected')
              $('#content_types').hide();
              $('#all_content_types_help').show()
          }
          else{
              $('#content_types').show();
              $('#all_content_types_help').hide()
          };
      });
    });
</script>

<h1 i18n:translate="">Subscribe to notifications</h1>

<tal:block define="can_subscribe_to python:list(here.available_notif_types())">
<p tal:condition="python:not(can_subscribe_to or here.checkPermissionPublishObjects())" i18n:translate="">
    Notifications are disabled.
</p>

<form tal:condition="python:can_subscribe_to or here.checkPermissionPublishObjects()"
      tal:attributes="action string:${here/absolute_url}/subscribe_me"
      name="subscribe_me" action="subscribe_me" method="post">
<fieldset>
<legend tal:condition="not:req_notif_type" i18n:translate="">New subscription</legend>
<legend tal:condition="req_notif_type" i18n:translate="">Edit subscription</legend>
<div class="field">
    <label for="subscribe_me-location" i18n:translate="">Location</label>
    <div id="subscribe_me-sitemap_tree" class="sitemap_tree"></div>
    <div style="clear: both"></div>
    <input type="text" size="50" id="subscribe_me-location" name="location"
           tal:attributes="value req_location"
           class="subscribe_me-sitemap_tree_target" />
</div>
<div class="field-inline">
    <label for="subscribe_me-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=""
                tal:attributes="selected python:test(first_notif_type=='administrative', 'selected', '')">Administrative notifications (uploaded objects)</option>
        <option value="instant"
                tal:condition="python:'instant' in can_subscribe_to"
                i18n:translate=""
                tal:attributes="selected python:test(first_notif_type=='instant', 'selected', '')">Instant notifications</option>
        <option value="daily"
                tal:condition="python:'daily' in can_subscribe_to"
                i18n:translate=""
                tal:attributes="selected python:test(first_notif_type=='daily', 'selected', '')">Daily digest</option>
        <option value="weekly"
                tal:condition="python:'weekly' in can_subscribe_to"
                i18n:translate=""
                tal:attributes="selected python:test(first_notif_type=='weekly', 'selected', '')">Weekly digest</option>
        <option value="monthly"
                tal:condition="python:'monthly' in can_subscribe_to"
                i18n:translate=""
                tal:attributes="selected python:test(first_notif_type=='monthly', 'selected', '')">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"
      tal:attributes="checked python:test(len(first_content_types)==0, 'checked', '')"/>
      <span id="all_content_types_help" style="display:none" i18n:translate="">(uncheck to select content types)</span>
      <span id="administrative_help" style="display:none" i18n:translate="">(content type filtering is disabled for administrative notifications)</span>
</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:content="item_label"
            tal:define="item_label python:here.get_meta_type_label(item);"
            tal:attributes="value item;
                            selected python:test(item in first_content_types, 'selected', '')" />
      </tal:block>
    </select>
</div>

<!-- Show select if there many languages -->
<tal:block condition="python:len(here.gl_get_languages_map()) > 1">
    <div class="field-inline">
        <label for="subscribe_me-lang" i18n:translate="">Language</label>
        <select name="lang" id="subscribe_me-lang"
                tal:define="gettext 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']+'] '+gettext(item['title'])" />
        </select>
    </div>
</tal:block>

<tal:block condition="context/config/enable_anonymous|nothing">
    <tal:block condition="not: python: request.AUTHENTICATED_USER.getId()">
        <div class="field-inline">
            <label for="subscribe_me-email" i18n:translate="">E-mail</label>
            <input type="text" id="subscribe_me-email" size="50" name="email"
                   tal:attributes="value request/form/email|string:"/>
        </div>
    </tal:block>
</tal:block>
<br/>
<input type="submit" value="Subscribe" i18n:attributes="value"/>
</fieldset>
</form>
<tal:block define="tree_get_url string:${here/absolute_url}/sitemap">
    <metal:block use-macro="here/macro_utils/macros/ajaxtree" />
</tal:block>
</tal:block>


<tal:block condition="my_subscriptions">

<h2 i18n:translate="">My subscriptions</h2>

<table class="datatable">
<thead>
    <th i18n:translate="">Location</th>
    <th i18n:translate="">Notification type</th>
    <th i18n:translate="">Language</th>
    <th i18n:translate="">Content types</th>
    <th>&nbsp;</th>
</thead>
<tbody>
    <tr tal:repeat="subscription my_subscriptions">
        <td tal:define="location subscription/location;
                        location_url python:here.get_location_link(location)">
            <a tal:condition="location"
               tal:attributes="href location_url;
                               title location_url"
               tal:content="location"></a>
            <a tal:condition="not:location"
               tal:attributes="href location_url;
                               title location_url"
               i18n:translate="">entire portal</a>
        </td>
        <td tal:content="subscription/notif_type"/>
        <td tal:define="lang subscription/lang"
            tal:content="python:here.gl_get_language_name(lang)"/>
        <td tal:condition="python:subscription['notif_type']!='administrative'">
          <tal:block content="python:', '.join(subscription['content_types']).replace('Naaya ', '') or 'ALL'"/>
          (<a i18n:translate=""
            tal:attributes="href string:${site_url}/portal_notification/my_subscriptions_html?location=${subscription/location}&notif_type=${subscription/notif_type}">click to edit</a>)
        </td>
        <td tal:condition="python:subscription['notif_type']=='administrative'"
          i18n:translate="">ALL
        </td>
        <td>
            <form method="post"
                  tal:define="url here/absolute_url"
                  tal:attributes="action string:${url}/unsubscribe_me">
                <input type="hidden" name="location"
                       tal:attributes="value subscription/location"/>
                <input type="hidden" name="notif_type"
                       tal:attributes="value subscription/notif_type"/>
                <input type="hidden" name="lang"
                       tal:attributes="value subscription/lang"/>
                <input type="submit" value="unsubscribe"
                       i18n:attributes="value"/>
            </form>
        </td>
    </tr>
</tbody>
</table>

</tal:block>

<tal:block condition="python:user_not_found and not my_subscriptions">

<h2 i18n:translate="">My subscriptions</h2>

<div class="message-error">
  <fieldset>
    <legend i18n:translate="">User not found</legend>
    <div class="fieldset_div">
      <p i18n:translate="">Your user was not found in the current user container. This may happen if your user is defined at a higher level in the server tree.</p>

      <p i18n:translate="">The subscription administration is only available for users defined within this site.</p>
    </div>
    </fieldset>
</div>

</tal:block>

</metal:block>
</metal:block>
