{#
# This file is part of Invenio.
# Copyright (C) 2013, 2014, 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
#}

{%- extends config.COMMUNITIES_BASE_TEMPLATE -%}

{%- block css %}
  {{ super() }}
  {% assets "invenio_communities_css" %}<link href="{{ ASSET_URL }}" rel="stylesheet">{% endassets %}
{%- endblock css %}

{%- block javascript -%}
  {{ super() }}
  {% assets "invenio_communities_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript -%}

{% block page_body %}
<div class="container communities">
  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <h1>
          <i class="fa fa-group"></i>
          {{ _("Communities") }} <small>{{ _("created and curated by %(x_name)s users", x_name=config.CFG_SITE_NAME) }}</small>
        </h1>

    </div>
    <div class="col-sm-8 col-md-8">
      {%- set args = request.args.copy().to_dict() -%}
      <form action="." method="GET" id="search_form" role="form">
        {{ form.p(class_="form-control " + form.p.short_name, placeholder='Search communities', type='text') }}
      </form>
        <br />
        <div class="form-inline">
          <div class="form-group">
            <p class="help-block hidden-xs">
              {{ _('Showing %(x_from)d to %(x_to)d out of %(x_total)d communities.', x_from=r_from, x_to=r_to, x_total=r_total) }}
            </p>
          </div>
          <div class="form-group pull-right">
            <div class="btn-group">
              <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
                <i class="glyphicon glyphicon-random"></i> {{ _('Sort by') }}
                <span class="caret"></span>
              </a>
              <ul class="dropdown-menu">
              {%- for opt in ['title', 'ranking'] -%}
                {%- set new_args = args.copy() -%}
                {%- do new_args.update({'so': opt}) -%}
                <li>
                  <a href="{{ url_for('invenio_communities.index', **new_args) }}" class="active">
                    <i class="pull-right icon {{ 'glyphicon glyphicon-ok' if args.get('so', 'ranking')==opt }}"></i>
                    {{ opt }}
                  </a>
                </li>
              {%- endfor -%}
              </ul>
            </div>
          </div>
        </div>
      {% if communities %}
        {% if featured_community and not request.args.get('p') and pagination.page == 1 %}
        <br />
        <div class="wrapper">
          <div class="ribbon-wrapper-green">
            <div class="ribbon-green">{{ _("Featured") }}</div>
          </div>
          <a href="{{url_for('.detail', community_id=featured_community.id)}}">
          <h2>{{ featured_community.title }}</h2>
          </a>
          <div>
            {{ featured_community.description|striptags }}
          </div>
        </div>
        {% endif %}
        <br />
        {%- for row in communities|batch(2) %}
          <div class="row">
            {%- for obj in row %}
              {% if obj %}
                <div class="col-sm-12 col-md-6">
                  <div class="well">
                    <h4>
                      <div class="pull-right">
                        &nbsp;
                        <a href="{{url_for('.detail', community_id=obj.id)}}" class="btn btn-info">{{ _('View') }}</a>
                        {% if obj.id_user == current_user.id %}
                          <a href="{{url_for('.curate', community_id=obj.id)}}" class="btn btn-info">{{ _('Curate') }}</a>
                        {% endif %}
                      </div>
                      {{ obj.title }}
                    </h4><br />
                    <p>{{ obj.description|striptags|truncate }}</p>
                    <small class="text-muted">Curated by: {{ obj.owner.nickname }}</small>
                  </div>
                </div>
              {% endif %}
            {%- endfor %}
          </div>
        {%- endfor %}
        {% if pagination.total_count > pagination.per_page %}
          <div align="center">
            <ul class="pagination">
              <li{{ ' class="disabled"'|safe if not pagination.has_prev }}>
                {%- set new_args = args.copy() -%}
                {%- do new_args.update({'page': 1}) -%}
                <a title="first" href="{{ url_for('.index', **new_args) }}">&laquo;</a>
              </li>
              <li{{ ' class="disabled"'|safe if not pagination.has_prev }}>
                {%- do args.update({'page': pagination.page-1 }) -%}
                <a title="prev" href="{{ url_for('.index', **args) }}">&lsaquo;</a>
              </li>
              {%- for page_p in pagination.iter_pages() %}
                {%- if page_p -%}
                  {%- set new_args = args.copy() -%}
                  {%- do new_args.update({'page': page_p}) -%}
                  <li{{ ' class="active"'|safe if page_p == pagination.page }}>
                    <a href="{{ url_for('.index', **new_args) }}">{{ page_p }}</a>
                  </li>
                {%- else -%}
                  <li class="disabled"><a href="#">...</a></li>
                {%- endif -%}
              {%- endfor -%}
              <li{{ ' class="disabled"'|safe if not pagination.has_next }}>
                {%- set new_args = args.copy() -%}
                {%- do new_args.update({'page': pagination.page+1}) -%}
                <a href="{{ url_for('.index', **new_args) }}">&rsaquo;</a>
              </li>
              <li{{ ' class="disabled"'|safe if not pagination.has_next }}>
                {%- set new_args = args.copy() -%}
                {%- do new_args.update({'page': pagination.pages }) -%}
                <a title="last" href="{{ url_for('.index', **new_args) }}">&raquo;</a>
              </li>
            </ul>
          </div>
        {% endif %}
      {% else %}
        <p class="text-muted text-center">
          <strong>{{ _('Your search did not match any communities. Please try again.') }}</strong>
        </p>
      {% endif %}
    </div> <!-- /.col-sm-8 col-md-8 -->
    <div class="col-sm-4 col-md-4">
      {% include "invenio_communities/mycommunities.html" %}
    </div>
  </div>
</div>
{% endblock page_body %}
