{#
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#}
{% extends "mediagoblin/base.html" %}
{% import "/mediagoblin/utils/wtforms.html" as wtforms_util %}

{% block title %}
  {%- if user -%}
    {%- trans username=user.username -%}
      User: {{ username }}
    {%- endtrans %} &mdash; {{ super() }}
  {%- else -%}
    {{ super() }}
  {%- endif -%}
{% endblock %}

{%- block mediagoblin_head %}
 <script src="{{ request.staticdirect('/js/setup_report_forms.js') }}">
 </script>
{% endblock %}

{% block mediagoblin_content -%}
    <div class="row">
      <div class="sixteen columns">
        <a href="{{ request.urlgen('mediagoblin.moderation.users') }}"
           class="return_to_panel button_action"
           title="Return to Users Panel">
          {% trans %}Return to Users Panel{% endtrans %}</a>
      </div>
    </div>
    <div class="row">
      <div class="six columns">
  {# If no user... #}
  {% if not user %}
    <p>{% trans %}Sorry, no such user found.{% endtrans %}</p>
  {# User exists, but needs verification #}
  {% elif not user.has_privilege('active') %}
    <div class="profile_sidebar empty_space">
    <h1>{% trans %}Email verification needed{% endtrans %}</h1>
    <p>
      {% trans -%}
        Someone has registered an account with this username, but it still has
        to be activated.
      {%- endtrans %}
    </p>

    </div>

  {# Active(?) (or at least verified at some point) user, horray! #}
  {% else %}
        <h1>
          {%- trans username=user.username %}{{ username }}'s profile{% endtrans -%}
        {% if user_banned and user_banned.expiration_date %}
          &mdash; {% trans expiration_date=user_banned.expiration_date -%}
            BANNED until {{ expiration_date }}
          {%- endtrans %}
        {% elif user_banned %}
          &mdash; {% trans %}Banned Indefinitely{% endtrans %}
        {% endif %}
        </h1>
        {% if not user.url and not user.bio %}
            <div class="profile_sidebar empty_space">
              <p>
                {% trans -%}
                  This user hasn't filled in their profile (yet).
                {%- endtrans %}
              </p>
        {% else %}
          <div class="profile_sidebar">
            {% include "mediagoblin/utils/profile.html" %}
            {% if request.user and
                  (request.user.id == user.id or request.user.has_privilege('admin')) %}
              <a href="{{ request.urlgen('mediagoblin.edit.profile',
                       user=user.username) }}">
                {%- trans %}Edit profile{% endtrans -%}
              </a>
            {% endif %}
        {% endif %}
          <p>
            <a href="{{ request.urlgen('mediagoblin.user_pages.collection_list',
                                               user=user.username) }}">
              {%- trans %}Browse collections{% endtrans -%}
            </a>
          </p>
        </div>
  {% endif %}
    </div>
  {% if user %}
      <div class="ten columns">
        <h2>
          {%- trans username=user.username -%}
            Active Reports on {{ username }}
          {%- endtrans -%}
        </h2>
        {% if reports.count() %}
        <table class="admin_side_panel">
          <tr>
            <th>{%- trans %}Report ID{% endtrans -%}</th>
            <th>{%- trans %}Reported Content{% endtrans -%}</th>
            <th>{%- trans %}Description of Report{% endtrans -%}</th>
          </tr>
          {% for report in reports %}
            <tr>
              <td>
                <img src="{{ request.staticdirect('/images/icon_clipboard.png') }}" />
                <a href="{{ request.urlgen('mediagoblin.moderation.reports_detail',
                                                   report_id=report.id) }}">
                  {%- trans report_number=report.id -%}
                    Report #{{ report_number }}
                  {%- endtrans -%}
                </a>
              </td>
              <td>
                {% if report.is_comment_report() %}
                  <a>{%- trans %}Reported Comment{% endtrans -%}</a>
                {% elif report.is_media_entry_report() %}
                  <a>{%- trans %}Reported Media Entry{% endtrans -%}</a>
                {% endif %}
              </td>
              <td>{{ report.report_content[:21] }}
                    {% if report.report_content|count >20 %}...{% endif %}</td>
              <td>{%- trans %}Resolve{% endtrans -%}</td>
            </tr>
          {% endfor %}
            <tr><td></td><td></td>
        </table>
        {% else %}
          {%- trans username=user.username -%}
            No active reports filed on {{ username }}
          {%- endtrans -%}
        {% endif %}
        <p>
          <span>
              <a href="{{ request.urlgen(
                  'mediagoblin.moderation.reports') }}?reported_user={{user.id}}">
              {%- trans
                  username=user.username %}All reports on {{ username }}{% endtrans %}</a>
          &middot;
              <a href="{{ request.urlgen(
                      'mediagoblin.moderation.reports') }}?reporter={{user.id}}">
              {%- trans username=user.username -%}
                All reports that {{ username }} has filed
              {%- endtrans %}</a>
          </span>
          <span class=clear></span>
        </p>
      </div>
    </div>
    <div class="row foot">
    <h2>{% trans username=user.username -%}
          {{ username }}'s Privileges{% endtrans %}</h2>
    <div class="six columns">
    <form action="{{ request.urlgen('mediagoblin.moderation.give_or_take_away_privilege',
               user=user.username) }}"
          method=post >
    <table class="admin_side_panel">
        <tr>
          <th>{% trans %}Privilege{% endtrans %}</th>
          <th>{% trans %}Granted{% endtrans %}</th>
        </tr>
        {% for privilege in privileges %}
          <tr>
            <td>{{ privilege.privilege_name }}</td>
            {% if user.has_privilege(privilege.privilege_name) %}
              <td class="user_with_privilege">
                {% trans %}Yes{% endtrans %}{% else %}
              <td class="user_without_privilege">
                {% trans %}No{% endtrans %}{% endif %}
            </td>
            {% if request.user.has_privilege('admin') %}
              <td>
                {% if user.has_privilege(privilege.privilege_name) %}
                <input type=submit id="{{ privilege.privilege_name }}"
                       class="submit_button button_action"
                       value =" -" />
                {% else %}
                <input type=submit id="{{ privilege.privilege_name }}"
                       class="submit_button button_action"
                       value ="+" />
                {% endif %}
              </td>
            {% endif %}
          </tr>
        {% endfor %}
    </table>
    {{ csrf_token }}
    <input type=hidden name=privilege_name id=hidden_privilege_name />
    </form>
    </div>
    <div class="five columns">
      <form method=POST action="{{ request.urlgen(
        'mediagoblin.moderation.ban_or_unban',
        user=user.username) }}">
        {{ csrf_token }}
        {% if request.user.has_privilege('admin') and not user_banned and
            not user.id == request.user.id %}
          {{ wtforms_util.render_divs(ban_form) }}
          <input type=submit class="button_action"
               value="{% trans %}Ban User{% endtrans %}"
               id="ban_user_submit" />
        {% elif request.user.has_privilege('admin') and
            not user.id == request.user.id %}
          <input type=submit class="button_action"
                 value="{% trans %}UnBan User{% endtrans %}" />
        {% endif %}
      </form>
    </div>
  {% endif %}
    <script>
$(document).ready(function(){
    $('.submit_button').click(function(){
        $('#hidden_privilege_name').val($(this).attr('id'));
    });
    init_user_banned_form();
    $('#ban_user_submit').click(function(){
        submit_user_banned_form()
    });
});
    </script>
</div><!--whoami-->
{% endblock %}
