{#
 This file is deprecated. Put macros in "macros" directory
#}

{% macro deprecated(name, newfile) %}
<small class="text-warning"><strong>This block is generated from macro "{{ name
}}" in deprecated file "macros.html". Import from "macros/{{ newfile }}.html" instead.
</strong></small>
 {% endmacro %}

{# Generic renderer for boxes / portlets #}
{% macro m_box(title, color="blue", with_toolbar=False) %}
  <div class="box">
  {{ deprecated("m_box", "box") }}
    
    {% if title %}
      <div class="box-header box-header-small box-header-color-{{ color }}">
        <h5>{{ title }}</h5>

        {# LATER
      {% if with_toolbar %}
        <div class="box-toolbar">
          <a href="#" data-action="collapse">
            <i class="icon-chevron-up"></i>
          </a>
        </div>
      {% endif %}
      #}
      </div>
    {% endif %}

    <div class="box-body">
      <div class="box-main">
        {{ caller() }}
      </div>
    </div>
  </div>
{% endmacro %}

{% macro m_box_menu(title=None, color="green", with_toolbar=False) %}
  {{ deprecated("m_box_menu", "box") }}
  <div class="box">
    <div class="box-header box-header-small box-header-color-{{ color }}">
      <h5>{{ title or _('Menu') }}</h5>
    </div>

    <div class="box-body" style="padding-top: 10px; padding-bottom: 10px;">
      {{ caller() }}
    </div>
  </div>
{% endmacro %}

{% macro m_box_content(title=None, color="blue", with_toolbar=False) %}
  <div class="box">
  {{ deprecated("m_box_content", "box") }}
    {% if title %}
      <div class="box-header box-header-small box-header-color-{{ color }}">
        <h5>{{ title }}</h5>
      </div>
    {% endif %}

    <div class="box-body">
      <div class="box-main">
        {{ caller() }}
      </div>
    </div>
    <div class="clearfix"></div>
  </div>
{% endmacro %}

{# Generic renderer for activity streams #}
{% macro m_activities(entries, ignore_communities=False) %}
  {{ deprecated("m_activities", "activity") }}
  <ul class="activities">
    {% for entry in entries %}
      {% set actor_href = url_for("social.user", user_id=entry.actor.id) %}
      {% set actor_img = url_for("social.user_mugshot", user_id=entry.actor.id, s=45) %}

      <li>
        <a href="{{ actor_href }}"><img class="avatar" alt=""
            src="{{ actor_img }}"></a>

        <div class="entry">
          <span class="arrow"></span>
          <a href="{{ actor_href }}" class="name">{{ entry.actor }}</a>,
          <span class="datetime">{{ entry.happened_at|datetimeformat }}</span>
          <span class="message">{{ entry.message(ignore_communities)|safe }}</span>

          {% set body = entry.body() %}
          {% if body %}
          <span class="body">"<a href="{{ entry.object_url }}">{{ body }}"</a></span>
          {% endif %}
        </div>
      </li>
    {% endfor %}
  </ul>
{% endmacro %}


{% macro _m_audit_field_changes(changes) %}
  {{ deprecated("_m_audit_field_changes", "audit") }}
  <ul>
    {%- for key, values in changes.items() %}
      <li>
        {%- if values.items %}
          Field <b>{{ key }}</b> changed:
          {{ _m_audit_field_changes(values) }}
        {%- else %}
          {%- set old_value, new_value = values %}
          {%- if old_value and old_value != NO_VALUE %}
            Field <b>{{ key }}</b> changed from <em>{{ old_value }}</em>
            to <em>{{ new_value }}</em>
          {%- else %}
            Field <b>{{ key }}</b> set to <em>{{ new_value }}</em>
          {%- endif %}
        {%- endif %}
      </li>
    {%- endfor %}
  </ul>
{% endmacro %}

{% macro m_audit_log(entries) %}
  {{ deprecated("m_audit_log", "audit") }}
  {%- if entries %}
    <h3 class="main collapsed" data-toggle="collapse"
        href="#audit_log_entries">{{ _("Audit Log") }}</h3>
    <div id="audit_log_entries" class="collapse" style="margin-bottom: 25px;">
      {%- for entry in entries %}
        {%- if entry.related or entry.op == 1 %}
          <div>
            <img
                src="{{ url_for("social.user_mugshot", user_id=entry.user.id, s=16) }}"
                alt=""/>
            <a href="{{ url_for("social.user", user_id=entry.user.id) }}">{{ entry.user.name }}</a>
            made changes on {{ entry.happened_at.strftime('%Y-%m-%d %H:%M') }}
            {{ _m_audit_field_changes(entry.changes) }}
          </div>
        {%- elif entry.type == 0 %}
          <div>
            <img
                src="{{ url_for("social.user_mugshot", user_id=entry.user.id, s=16) }}"
                alt=""/>
            <a href="{{ url_for("social.user", user_id=entry.user.id) }}">{{ entry.user.name }}</a>
            created this record
            on {{ entry.happened_at.strftime('%Y-%m-%d %H:%M') }}
          </div>
        {%- endif %}
      {%- endfor %}
    </div>
  {%- endif %}
{% endmacro %}

{% macro m_breadcrumbs(breadcrumbs) %}
  <small class="text-warning">
    <strong>Breadcrumb is no more generated from a macro. Just include
"breadcrumb.html" if you are overriding "body" block from "base.html".</strong>
  </small>
 {% endmacro %}

{% macro m_flashed_messages() %}
  <small class="text-warning">
    <strong>Flash message block is no more generated from a macro. Just include
"flash-messages.html" if you are overriding "body" block from
"base.html".</strong>
  </small>
{% endmacro %}


{% macro m_recent_items(recent_items) %}
  {{ deprecated("m_recent_items", "recent") }}

  {%- if recent_items %}
    <div class="well" style="padding: 8px 0;">
      <ul class="nav nav-list">
        <li class="nav-header">{{ _("Recent items") }}</li>
        {%- for item in recent_items %}
          <li class="recent-item">
            <a href="{{ item.url }}">
              <img alt=""
                  src="/static/icons/{{ item.type }}-12.png"/>&nbsp;{{ item.name }}
            </a>
          </li>
        {%- endfor %}
      </ul>
    </div>
  {%- endif %}
{% endmacro %}


{# NOTE: there was a m_docs_table macro here, it has moved to the
 # documents app. Maybe a global macroa is needed, but probably a different one.
 #}

<!-- table for file properties -->
{% macro m_table(lines) %}
  {{ deprecated("m_table", "?") }}

  <table class="table table-striped table-condensed"
      style="margin: 10px 0 15px 0;">
    <tbody>
    {%- for line in lines %}
      <tr class="row">
        <td class="span3">{{ line[0] }}</td>
        {%- if line[0] == 'Size' %} <!-- ugly hack -->
          <td class="span9">{{ line[1]|filesize }}</td>
        {%- else %}
          <td class="span9">{{ line[1] }}</td>
        {%- endif %}
      </tr>
    {%- endfor %}
    </tbody>
  </table>
{%- endmacro %}


{% macro m_renderfield(field) -%}
  {{ deprecated("m_renderfield", "form") }}
  {% set with_label = kwargs.pop('with_label', True) %}
  {% set placeholder = '' %}
  {% if not with_label %}
    {% set placeholder = field.label.text %}
  {% endif %}

  <div class="form-group {% if field.errors %}has-error{% endif %}">
    {% if with_label and not field.type in ('BooleanField', 'CSRFTokenField') %}
      <label for="{{ field.id }}" class="control-label">
        {{ field.label.text }}{% if field.flags.required %} *{% endif %}:
      </label>
    {% endif %}

    {% set class_ = kwargs.pop('class_', '') %}
    {% if field.flags.required %}
      {% set class_ = class_ + ' required' %}
    {% endif %}

    {% if field.type == 'CSRFTokenField' %}
      {{ field() }}
    {% elif field.type == 'BooleanField' %}
      <label class="checkbox">
        {{ field(class_=class_, **kwargs) }}
        {{ field.label.text|safe }}
      </label>
    {% else %}
      {% if field.type in ('TextField', 'TextAreaField', 'PasswordField') %}
        {% set class_ = class_ + ' input-xlarge' %}
      {% elif field.type == 'FileField' %}
        {% set class_ = class_ + ' input-file' %}
      {% endif %}
      {{ field(class_=class_, placeholder=placeholder, **kwargs)|safe }}
    {% endif %}

    {% if field.errors %}
      <span class="error help-inline">{{ field.errors|join(', ') }}</span>
    {% endif %}

    {% if field.description %}
      <span class="help-block">{{ field.description|safe }}</span>
    {% endif %}

  </div>
{%- endmacro %}
