<!--!  Copyright (C) 2006-2014 Edgewall Software

  This software is licensed as described in the file COPYING, which
  you should have received as part of this distribution. The terms
  are also available at http://trac.edgewall.com/license.html.

  This software consists of voluntary contributions made by many
  individuals. For the exact contribution history, see the revision
  history and logs, available at http://trac.edgewall.org/.
-->
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:i18n="http://genshi.edgewall.org/i18n"
      xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="layout.html" />
  <head>
    <title>$title</title>
    <script type="text/javascript" src="${chrome.htdocs_location}js/folding.js"></script>
    <script type="text/javascript">
      jQuery(document).ready(function($) {
        initializeFilters();
        $("#group").change(function() {
          $("#groupdesc").enable(this.selectedIndex != 0)
        }).change();
        $("fieldset legend.foldable").enableFolding(false);
      /*<![CDATA[*/
        /* Hide the filters for saved queries. */
        if (window.location.href.search(/[?&]report=[0-9]+/) != -1)
          $("#filters").toggleClass("collapsed");
      /*]]>*/
        /* Hide the columns by default. */
        $("#columns").toggleClass("collapsed");
      });
    </script>
  </head>

  <body>
    <div id="content" class="query">
      <h1>$title <span class="numrows">(${ngettext('%(num)s match', '%(num)s matches', query.num_items)})</span></h1>

      <div py:if="description" id="description" xml:space="preserve">
        ${wiki_to_html(context.child(report_resource), description)}
      </div>

      <form id="query" method="post" action="${href.query()}"
            py:with="field_names = sorted(fields.iterkeys(), key=lambda name: fields[name].label.lower())">
        <fieldset id="filters">
          <legend class="foldable">Filters</legend>
          <table summary="Query filters">
            <tbody py:for="clause_num, constraints in enumerate(clauses or [{}])"
                   py:with="clause_pre = '%d_' % clause_num">
              <tr style="${'display: none' if clause_num == 0 else None}">
                <td>
                  <div class="trac-clause-lsep">&nbsp;<hr /></div>
                  <div class="trac-clause-msep">Or</div>
                  <div class="trac-clause-rsep">&nbsp;<hr /></div>
                </td>
              </tr>
              <tr><td class="trac-clause">
                <table class="trac-clause">
                  <tbody py:for="field_name in field_names" py:if="field_name in constraints"
                         py:with="field = fields[field_name]; n_field_name = clause_pre + field_name;
                                  constraint = constraints[field_name];
                                  multiline = field.type in ('select', 'text', 'textarea', 'time')">
                    <tr py:for="constraint_idx, constraint_value in enumerate(constraint['values'])"
                        class="${field_name}" py:if="multiline or constraint_idx == 0">
                      <td>
                        <div class="inlinebuttons">
                          <input type="submit" name="rm_filter_${n_field_name}_${constraint_idx}" value="&ndash;" />
                        </div>
                      </td>
                      <py:choose test="constraint_idx">
                        <py:when test="0">
                          <th scope="row"><label id="label_${n_field_name}">${fields[field_name].label}</label></th>
                          <td py:if="field.type not in ('radio', 'checkbox', 'time')" class="mode">
                            <select name="${n_field_name}_mode">
                              <option py:for="mode in modes[field.type]" value="$mode.value"
                                      selected="${mode.value == constraint.mode or None}">$mode.name</option>
                            </select>
                          </td>
                        </py:when>
                        <py:otherwise><!--! not the first line of a multiline constraint -->
                          <th colspan="${1 if field.type == 'time' else 2}"><label>or</label></th>
                        </py:otherwise>
                      </py:choose>

                      <td class="filter" colspan="${2 if field.type in ('radio', 'checkbox', 'time') else None}"
                          py:choose="">

                        <py:def function="render_options(field_name, options)"
                                py:choose="field_name">
                          <option py:when="'owner'"
                                  py:for="option in options"
                                  selected="${option.value == constraint_value or None}"
                                  value="$option.value" py:content="option.name"></option>
                          <option py:otherwise=""
                                  py:for="option in options"
                                  selected="${option == constraint_value or None}"
                                  value="$option" py:content="option"></option>
                        </py:def>

                        <py:when test="field.type == 'select'">
                          <select name="${n_field_name}">
                            <option></option>
                            ${render_options(field.name, field.options)}
                            <optgroup py:for="optgroup in field.optgroups"
                                      py:if="optgroup.options"
                                      label="${optgroup.label}">
                              ${render_options(field.name, optgroup.options)}
                            </optgroup>
                          </select>
                        </py:when>

                        <py:when test="field.type == 'radio'">
                          <py:for each="option in field.options">
                            <input type="checkbox" id="_${n_field_name}_$option" name="${n_field_name}"
                              value="$option"
                              checked="${((constraint['mode'] == '') == (option in constraint['values'])) or None}" />
                            <label for="_${n_field_name}_$option" class="control">${option or 'none'}</label>
                          </py:for>
                        </py:when>

                        <py:when test="field.type == 'checkbox'">
                          <input type="radio" id="_${n_field_name}_on" name="$n_field_name" value="1"
                                 checked="${constraint.mode != '!' or constraint_value == '1' or None}" />
                          <label for="_${n_field_name}_on" class="control">yes</label>
                          <input type="radio" id="_${n_field_name}_off" name="$n_field_name" value="0"
                                 checked="${constraint.mode == '!' or constraint_value != '1' or None}" />
                          <label for="_${n_field_name}_off" class="control">no</label>
                        </py:when>

                        <py:when test="field.type in ('text', 'textarea', 'id')">
                          <input type="text" name="${n_field_name}" value="$constraint_value" size="42" />
                        </py:when>

                        <py:when test="field.type == 'time'"
                                 py:with="(start, end) = '..' in constraint_value and constraint_value.split('..', 1)
                                                         or (constraint_value, '');
                                          format = field.get('format', 'datetime');
                                          date_or_datetime = 'date' if format == 'relative' else format;">
                          <i18n:msg>
                            <label>between</label>
                            <input type="text" name="${n_field_name}" value="$start" size="14" class="trac-${date_or_datetime}picker" />
                            <label>and</label>
                            <input type="text" name="${n_field_name}_end" value="$end" size="14" class="trac-${date_or_datetime}picker" />
                          </i18n:msg>
                        </py:when>
                      </td>
                    </tr>
                  </tbody>

                  <tbody py:with="last_clause = clause_num == (len(clauses) or 1) - 1">
                    <tr class="actions">
                      <td class="and" colspan="2">
                        &nbsp;<label for="add_filter_${clause_num}">And</label>&nbsp;
                        <select name="add_filter_${clause_num}" id="add_filter_${clause_num}">
                          <option></option>
                          <option py:for="field_name in field_names" py:with="field = fields[field_name]"
                                  value="$field_name"
                                  disabled="${(field.type in ('radio', 'checkbox', 'id') and
                                               field_name in constraints and
                                               len(constraints[field_name])) or None}">${field.label}</option>
                        </select>
                        <div class="inlinebuttons">
                          <input type="submit" name="add_${clause_num}" value="+" />
                        </div>
                      </td>
                      <td py:if="last_clause" class="or" colspan="2">
                        <label for="add_clause">Or</label>&nbsp;
                        <select name="add_clause_${clause_num + 1}" id="add_clause">
                          <option></option>
                          <option py:for="field_name in field_names" value="$field_name">${fields[field_name].label}</option>
                        </select>
                        <div class="inlinebuttons">
                          <input type="submit" name="add_${clause_num + 1}" value="+" />
                        </div>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </td></tr>
            </tbody>
          </table>
        </fieldset>

        <!--! Allow the user to decide what columns to include in the output of the query -->
        <fieldset id="columns">
          <legend class="foldable">Columns</legend>
          <div>
            <py:for each="column in all_columns">
              <label>
                <input type="checkbox" name="col" value="$column"
                       checked="${any(value == column for value in col) or None}" />
                ${fields.get(column, {'label': column or 'none'}).label}
              </label>
            </py:for>
          </div>
        </fieldset>

        <p class="option">
          <label for="group">Group results by</label>
          <select name="group" id="group">
            <option></option>
            <py:for each="field_name in field_names" py:with="field = fields[field_name]">
              <option py:if="field.type in ('select', 'radio') or field_name in ('owner', 'reporter')"
                      selected="${field_name == query.group or None}"
                      value="${field_name}">${field.label}</option>
            </py:for>
          </select>
          <input type="checkbox" name="groupdesc" id="groupdesc"
                 checked="${query.groupdesc or None}" />
          <label for="groupdesc">descending</label>
        </p>

        <p class="option">
          <!--! Allow the user to decide what rows to include in the output of the query -->
          Show under each result:
          <py:for each="column in all_textareas">
            <label><input type="checkbox" name="row" value="$column"
                          checked="${any(value == column for value in row) or None}" />
            ${fields.get(column, {'label': column or 'none'}).label}</label>
          </py:for>
        </p>

        <p class="option">
          <label for="max">Max items per page</label>
          <input type="text" name="max" id="max" size="10" value="${query.max}" />
        </p>

        <div class="buttons">
          <input py:if="report_resource" type="hidden" name="report" value="$report_resource.id" />
          <input type="hidden" name="order" value="$query.order" />
          <input py:if="query.desc" type="hidden" name="desc" value="1" />
          <input type="submit" name="update" value="${_('Update')}" />
        </div>
        <hr />
      </form>

      <xi:include href="query_results.html" />
      <xi:include py:if="tickets and batch_modify" href="batch_modify.html" />

      <div id="trac-report-buttons" class="buttons"
           py:with="edit = report_resource and 'REPORT_MODIFY' in perm(report_resource);
                    new = 'REPORT_CREATE' in perm('report');
                    delete = report_resource and 'REPORT_DELETE' in perm(report_resource)">
        <form py:if="edit" method="get" id="trac-report-edit"
              action="${url_of(report_resource)}">
          <div>
            <input type="hidden" name="action" value="edit" />
            <input type="submit"
                   title="${_('Edit report {%(id)s} corresponding to this query', id=report_resource.id)}"
                   value="${_('Edit query')}" />
          </div>
        </form>
        <form py:if="new or edit" method="get" id="trac-report-save"
              action="${url_of(report_resource) if edit else href.report()}">
          <div>
            <input type="hidden" name="action" value="${'edit' if edit else 'new'}" />
            <input type="hidden" name="query" value="${query.to_string()}" />
            <input type="submit" value="${_('Save query')}"
                   title="${_('Save updated query in report {%(id)s}', id=report_resource.id) if edit
                            else _('Create new report from current query')}" />
          </div>
        </form>
        <form py:if="delete" method="get" id="trac-report-delete"
              action="${url_of(report_resource)}">
          <div>
            <input type="hidden" name="action" value="delete" />
            <input type="submit"
                   title="${_('Delete report {%(id)s} corresponding to this query', id=report_resource.id)}"
                   value="${_('Delete query')}" />
          </div>
        </form>
      </div>

      <div id="help" i18n:msg="">
        <strong>Note:</strong> See <a href="${href.wiki('TracQuery')}">TracQuery</a>
        for help on using queries.
      </div>

    </div>
  </body>
</html>

