[%# The contents of this file are subject to the Mozilla Public
  # License Version 1.1 (the "License"); you may not use this file
  # except in compliance with the License. You may obtain a copy of
  # the License at http://www.mozilla.org/MPL/
  #
  # Software distributed under the License is distributed on an "AS
  # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  # implied. See the License for the specific language governing
  # rights and limitations under the License.
  #
  # The Original Code is the Bugzilla Bug Tracking System.
  #
  # The Initial Developer of the Original Code is Netscape Communications
  # Corporation. Portions created by Netscape are
  # Copyright (C) 1998 Netscape Communications Corporation. All
  # Rights Reserved.
  #
  # Contributor(s): Jouni Heikniemi <jouni@heikniemi.net>
  #
  #%]

[%# INTERFACE:
  #
  # columns:
  #   array of hashes representing the columns in the table.
  #   Each hash contains data for a single column. Hash keys:
  #     name: Name of the field in the data param.
  #     heading: The text to print at the header cell.
  #     contentlink:
  #       URI to which the content of a data cell shall be linked to.
  #       Expressions of format %%xxx%% are replaced with value
  #       with the key xxx in data hash of the current row.
  #     content:
  #       If specified, the content of this variable is used
  #       instead of the data pulled from the current row.
  #       NOTE: This value is only partially HTML filtered!
  #     content_values:
  #       If specified, then each value of the column is translated
  #       using content_values hash.
  #       This content WILL be HTML-filtered in this case.
  #     align:
  #       left/center/right. Controls the horizontal alignment of the text in the column.
  #     allow_html_content:
  #       If defined, then this column allows some html content
  #       and so it will be only partially filtered.
  #     yesno_field: Turn the data from 0/!0 into Yes/No
  #
  # data:
  #   array of hashes representing the data for the table.
  #   Each hash contains data for a single row of data. The
  #   keys are column names from columns subhashes name field.
  #
  # overrides:
  # Example:
  #     overrides {                           # first hash
  #       column_name_to_be_overwriten => {   # second hash
  #         name_of_row_to_match_against => { # third hash
  #           value_to_match_against => {     # fourth hash
  #             content => "some contents"
  #             override_content => 1
  #           }
  #         }
  #       }
  #     }
  #
  #   Provides a method for overriding individual table cells. This is a hash
  #   (1), whose key is the column name, so the column must be named for
  #   one of it's cells to be overwritten. The hash value is another hash
  #   (2). The keys of that second hash are the name of the row to match
  #   against. The second hash then again points to another hash. Within this
  #   third hash (3), the keys represent values to match against. The item
  #   contains a fourth hash (4) specifying overridden values.
  #
  #   Each column value mentioned in the 'columns' documentation above
  #   can be overwritten (apart from name and heading). To override a
  #   table-cell value 'xxx', specify a new 'xxx' value, and specify a
  #   'override_xxx' value as well. See
  #   admin/milestones/list.html.tmpl for example
  #
  #%]

[%# FIXME move away from templates %]

[%###################  TABLE HEADER   ######################%]

<table id="admin_table" cellpadding="4" cellspacing="0">
  <tr>
    [% FOREACH c = columns %]
      [%# Default to align left for headers %]
      <th align="[% (c.align || 'left') FILTER html %]">
        [% c.heading FILTER html %]
      </th>
    [% END %]
  </tr>

[%###################  TABLE CONTENT  ######################%]

[% count = 0 %]
[% FOREACH row = data %]

  [% count = count + 1 %]
  <tr class="[%+ count % 2 == 1 ? 'odd' : 'even' +%]_row">
    [% FOREACH c = columns %]

      [%# Copy to local variables, as we may update these %]
      [% contentlink = c.contentlink
         content = c.content
         content_values = c.content_values
         align = c.align
         class = c.class
         allow_html_content = c.allow_html_content
         yesno_field = c.yesno_field
       %]

      [%# Get any specific "important" overrides for this c.name and row.name ? %]
      [% SET important = overrides.${c.name}.name.${row.name} %]

      [% IF important %]
        [% FOREACH key IN important.keys %]
          [% SET ${key} = important.${key} %]
        [% END %]
      [% ELSE %]
        [%# Are there any specific overrides for this column? %]
        [% FOREACH match_field = overrides.${c.name}.keys %]
          [% override = overrides.${c.name}.${match_field}.${row.$match_field} %]
          [% NEXT UNLESS override %]

          [% FOREACH key IN override.keys %]
            [% SET ${key} = override.${key} %]
          [% END %]
          [% LAST %]
        [% END %]
      [% END %]

      <td [% IF align %] align="[% align FILTER html %]" [% END %]
          [% IF class %] class="[% class FILTER html %]" [% END %]>

        [% IF contentlink %]
          [% link_uri = contentlink %]
          [% WHILE link_uri.search('%%(.+?)%%')%]
            [% FOREACH m = link_uri.match('%%(.+?)%%') %]
              [% IF row.$m.defined %]
                [% replacement_value = FILTER url_quote; row.$m; END %]
              [% ELSE %]
                [% replacement_value = "" %]
              [% END %]
              [% link_uri = link_uri.replace("%%$m%%", replacement_value) %]
            [% END %]
          [% END %]
          <a href="[% link_uri %]">
        [% END %]

        [% IF content_values %]
          [% colname = row.${c.name} %]
          [% content_values.${colname} FILTER html %]
        [% ELSIF content %]
          [% content FILTER html_light %]
        [% ELSE %]
          [% IF yesno_field %]
            [% IF row.${c.name} %]
              Yes
            [% ELSE %]
              No
            [% END %]
          [% ELSE %]
            [% IF allow_html_content %]
              [% row.${c.name} FILTER html_light %]
            [% ELSE %]
              [% row.${c.name} FILTER html %]
            [% END %]
          [% END %]
        [% END %]

        [% IF contentlink %]
          </a>
        [% END %]

      </td>
    [% END %]
  </tr>
[% END %]

[% IF data.size == 0 %]
  <tr><td colspan="[% columns.size %]" align="center"><i>&lt;none&gt;</i></td></tr>
[% END %]

[%###################  TABLE FOOTER   ######################%]

</table>
