[%#
  # 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.
  #
  # Contributor(s): Frédéric Buclin <LpSolit@gmail.com>
  #%]

[%# INTERFACE:
  # field: Bugzila::Field; the current field being edited
  #%]

[% title = BLOCK %]
  [% IF field.id %]
    Edit the [% "Custom" IF field.custom %] field '[% field.name | html %]' ([% field.description | html %])
  [% ELSE %]
    Add a new Custom Field
  [% END %]
[% END %]

[% javascript = BLOCK %]
var constants = {
  FIELD_TYPE_SINGLE_SELECT: [% constants.FIELD_TYPE_SINGLE_SELECT %],
  FIELD_TYPE_MULTI_SELECT: [% constants.FIELD_TYPE_MULTI_SELECT %],
  FIELD_TYPE_BUG_ID: [% constants.FIELD_TYPE_BUG_ID %],
  FIELD_TYPE_EXTURL: [% constants.FIELD_TYPE_EXTURL %],
  FIELD_TYPE_BUG_ID_REV: [% constants.FIELD_TYPE_BUG_ID_REV %]
};
[% END %]

[% PROCESS
  global/header.html.tmpl
  title = title
  doc_section = "custom-fields.html#" _ (field.id ? "edit" : "add") _ "-custom-fields"
  javascript_urls = [ "js/cf-edit.js" ]
%]

[% IF !field.id %]
<p style="font-size: 120%">
  Adding custom fields can make the interface of [% terms.Bugzilla %] very
  complicated. Many admins who are new to [% terms.Bugzilla %] start off
  adding many custom fields, and then their users complain that the interface
  is "too complex". Please think carefully before adding any custom fields.
  It may be the case that [% terms.Bugzilla %] already does what you need,
  and you just haven't enabled the correct feature yet.
</p>

<ul style="font-size: 120%">
  <li>Custom field names must begin with "cf_" to distinguish them from
      standard fields. If you omit "cf_" from the beginning of the name, it
      will be added for you.</li>
  <li>Descriptions are a very short string describing the field and will be
      used as the label for this field in the user interface.</li>
</ul>
[% END %]

[% SET has_help = { 'classification' => 1, 'votes' => 1, 'alias' => 1, 'see_also' => 1 } %]
[% IF field.id && !field.custom && has_help.${field.name} %]
<table><tr>
  <td><img style="margin: 0 8px 0 0" src="images/info.png" /></td>
  <td>
    [% SWITCH field.name %]
      [% CASE 'classification' %]
      If this field is enabled, [% terms.Bugzilla %] will associate each product with a
      specific classification.<br /> But you must have 'editclassification'
      permissions enabled in order to edit classifications.
      [% CASE 'votes' %]
      If this field is enabled, users will be allowed to vote for [% terms.bugs %].<br />
      Note that in order for this to be effective, you will have to change the maximum
      votes allowed in a product to be non-zero in <a href="editproducts.cgi">the product edit page</a>.
      [% CASE 'alias' %]
      Enabling this field allows you to assign [% terms.bugs %] "aliases", which are
      easy-to-remember names by which you can refer to [% terms.bugs %].
      [% CASE 'see_also' %]
      Enabling See Also field allows you to refer to [% terms.bugs %] in other installations.<br />
      Even if you disable this field, [% terms.bug %] relationships (URLs) which are already set
      will still appear and can be removed.
    [% END %]
  </td>
</tr></table>
[% END %]

<form id="edit_field" action="editfields.cgi" method="POST">

<table border="0" cellspacing="0" cellpadding="0">
  <tr valign="top"><td>
    <table border="0" cellspacing="0" cellpadding="5">
      <tr>
        <th align="left">Name:</th>
        <td>
          [% IF field.id %]
            [% field.name | html %]<input type="hidden" name="name" value="[% field.name | html %]" />
          [% ELSE %]
            <input type="text" id="name" name="name" value="cf_" size="40" maxlength="64" />
          [% END %]
        </td>
      </tr>
      <tr>
        <th align="left"><label for="description">Title:</label></th>
        <td><input type="text" id="description" name="description" style="width: 400px" value="[% field.description | html %]" /></td>
      </tr>
      <tr>
        <th align="left"><label for="sortkey">Sortkey:</label></th>
        <td>
          <input type="text" id="sortkey" name="sortkey" size="6"
            maxlength="6" value="[% field.sortkey | html %]" onchange="onChangeSortkey()" />
          [% IF !field.id || field.custom %]
          <span id="sortkey_col1"[% IF !(field.sortkey < 1000) %] style="display: none"[% END %]>< 1000: first column</span>
          <span id="sortkey_col2"[% IF !(field.sortkey >= 1000 AND field.sortkey < 2000) %] style="display: none"[% END %]>1000-1999: second column</span>
          <span id="sortkey_col3"[% IF !(field.sortkey >= 2000 AND field.sortkey < 3000) %] style="display: none"[% END %]>2000-2999: third column</span>
          <span id="sortkey_col4"[% IF !(field.sortkey >= 3000) %] style="display: none"[% END %]>>= 3000: 4th column</span>
          [% END %]
        </td>
      </tr>
      <tr>
        <th align="left">Type:</th>
        <td valign="top">
          [% IF field.id %]
            [% field_types.${field.type} | html %]
          [% ELSE %]
            <select id="type" name="type" onchange="onChangeType()">
              [% FOREACH type = field_types.sort %]
                [%# Types "Bug URLs" and "Keywords" are rudiments from original Bugzilla %]
                [% NEXT IF type == constants.FIELD_TYPE_UNKNOWN ||
                  type == constants.FIELD_TYPE_BUG_URLS ||
                  type == constants.FIELD_TYPE_KEYWORDS %]
                <option value="[% type | html %]">[% field_types.$type | html %]</option>
              [% END %]
            </select>
          [% END %]
        </td>
      </tr>
      [% IF !field.id || field.type && field.type != constants.FIELD_TYPE_BUG_ID_REV %]
      <tr valign="top" id="add_to_deps_row">
        [% IF !field.id || field.type == constants.FIELD_TYPE_BUG_ID %]
        <th align="left"><label for="add_to_deps">Deps:</label></th>
        <td valign="top">
          <select name="add_to_deps" id="add_to_deps">
            <option value="no" [% ' selected="selected"' IF !field.add_to_deps %]>Do not add</option>
            <option value="blocked" [% ' selected="selected"' IF field.add_to_deps == constants.BUG_ID_ADD_TO_BLOCKED %]>Add field value to blocked</option>
            <option value="dependson" [% ' selected="selected"' IF field.add_to_deps == constants.BUG_ID_ADD_TO_DEPENDSON %]>Add field value to blockers</option>
          </select>
        </td>
        [% ELSIF field.is_select AND field.name != 'product' AND field.id %]
        <td></td>
        <td><a href="editvalues.cgi?field=[% field.name | url_quote %]">Edit legal values for this field</a></td>
        [% END %]
      </tr>
      [% END %]
      [% IF !field.id || field.type == constants.FIELD_TYPE_EXTURL %]
      <tr valign="top" id="url_row">
        <th align="left">
          <label for="url">
            URL:
          </label>
        </th>
        <td>
          <input name="url" type="text" size="40" value="[% field.url | html %]" /><br />
          <i>This field will link to this URL with $1<br />replaced by the field value.</i>
        </td>
      </tr>
      [% END %]
    </table></td>
    <td><table border="0" cellspacing="0" cellpadding="5">
      [% IF !field.id || field.can_tweak('mailhead') %]
      <tr>
        <th align="left">
          <label for="mailhead">Displayed in [% terms.bug %]mail for new [% terms.bugs %]:</label>
        </th>
        <td><input type="checkbox" id="mailhead" name="mailhead" value="1" [%- ' checked="checked"' IF field.in_new_bugmail %] /></td>
      </tr>
      [% END %]
      [% IF !field.id || field.can_tweak('clone_bug') %]
      <tr>
        <th align="left"><label for="clone_bug">Is copied into the cloned [% terms.bug %]:</label></th>
        <td><input type="checkbox" id="clone_bug" name="clone_bug" value="1" [%- ' checked="checked"' IF field.clone_bug %] onclick="onChangeCloned()" /></td>
      </tr>
      [% END %]
      [% IF !field.id || field.can_tweak('obsolete') %]
      <tr>
        <th align="left"><label for="obsolete">Is disabled:</label></th>
        <td>
          <input type="checkbox" id="obsolete" name="obsolete" value="1"
            [%- ' checked="checked"' IF field.obsolete %] />
        </td>
      </tr>
      [% END %]
      [% IF !field.id || field.can_tweak('default_value') %]
      <tr id="default_value_row">
        <th align="left">Default value:</th>
        <td>
          [% IF field.is_select %]
          <select name="default_value" style="width: 400px" [% "multiple size=3" IF field.type == constants.FIELD_TYPE_MULTI_SELECT %]>
            <option value="">---</option>
            [% FOREACH v = field.legal_values %]
            <option value="[% v.id | html %]"[% ' selected="selected"' IF field.default_value_hash.${v.id} %]>[% v.name | html %]</option>
            [% END %]
          </select>
          [% ELSE %]
          <input type="text" name="default_value" value="[% field.default_value | html %]" style="width: 400px" />
          [% END %]
        </td>
      </tr>
      [% END %]
      [% IF !field.id || field.can_tweak('nullable') %]
      <tr>
        <th align="left"><label for="nullable">Allow empty value:</label></th>
        <td>
          <input type="checkbox" id="nullable" name="nullable" onclick="onChangeNullable()"
            value="1" [%- ' checked="checked"' IF field.nullable %] />
        </td>
      </tr>
      [% END %]
    </table>
  </td></tr>

  [% IF !field.id || field.can_tweak('visibility_field_id') %]
  <tr valign="top">
    <th align="left" style="padding: 5px"><label for="visibility_field_id">
      Show/hide the field depending on the value of:
    </label></th>
    <td style="padding: 5px">
      <select style="width: 400px" name="visibility_field_id" id="visibility_field_id">
        <option value="">---</option>
        [% FOREACH sel_field = Bugzilla.get_fields({ is_select => 1 }) %]
          <option value="[% sel_field.id | html %]"
            [% ' selected="selected"' IF sel_field.id == field.visibility_field.id %]>
            [% sel_field.description | html %]
            ([% sel_field.name | html %])
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF !field.id || field.can_tweak('null_field_id') %]
  <tr valign="top" id="null_field_id_row">
    <th style="padding: 5px" align="left"><label for="null_field_id">
      Allow empty value depending on the value of:
    </label></th>
    <td style="padding: 5px">
      <select style="width: 400px" name="null_field_id" id="null_field_id">
        <option value="">---</option>
        [% FOREACH sel_field = Bugzilla.get_fields({ is_select => 1 }) %]
          <option value="[% sel_field.id | html %]"
            [% ' selected="selected"' IF sel_field.id == field.null_field.id %]>
            [% sel_field.description | html %]
            ([% sel_field.name | html %])
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF !field.id || field.can_tweak('clone_field_id') %]
  <tr valign="top" id="clone_field_id_row">
    <th style="padding: 5px" align="left"><label for="clone_field_id">
      Clone field depending on the value of:
    </label></th>
    <td style="padding: 5px">
      <select style="width: 400px" name="clone_field_id" id="clone_field_id">
        <option value="">---</option>
        [% FOREACH sel_field = Bugzilla.get_fields({ is_select => 1 }) %]
          <option value="[% sel_field.id | html %]"
            [% ' selected="selected"' IF sel_field.id == field.clone_field.id %]>
            [% sel_field.description | html %]
            ([% sel_field.name | html %])
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF !field.id || field.can_tweak('default_field_id') %]
  <tr valign="top">
    <th style="padding: 5px" align="left"><label for="default_field_id">
      Make default value dependent on the value of:
    </label></th>
    <td style="padding: 5px">
      <select style="width: 400px" name="default_field_id" id="default_field_id">
        <option value="">---</option>
        [% FOREACH sel_field = Bugzilla.get_fields({ is_select => 1 }) %]
          [% NEXT IF sel_field.id == field.id %]
          <option value="[% sel_field.id | html %]"
            [% ' selected="selected"' IF sel_field.id == field.default_field.id %]>
            [% sel_field.description | html %]
            ([% sel_field.name | html %])
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF !field.id || field.can_tweak('value_field_id') %]
  <tr valign="top" id="value_field_row">
    <th style="padding: 5px" align="left"><label for="value_field_id">
      Field that controls the values that appear in this field:
    </label></th>
    <td style="padding: 5px">
      <select style="width: 400px" name="value_field_id" id="value_field_id">
        <option value="">---</option>
        [% FOREACH sel_field = Bugzilla.get_fields({ is_select => 1 }) %]
          [% NEXT IF sel_field.id == field.id %]
          <option value="[% sel_field.id | html %]"
            [% ' selected="selected"' IF sel_field.id == field.value_field.id %]>
            [% sel_field.description | html %]
            ([% sel_field.name | html %])
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF !field.id || field.type == constants.FIELD_TYPE_BUG_ID_REV %]
  <tr valign="top" id="bug_id_rev_row">
    <th style="padding: 5px" align="left"><label for="bug_id_rev_value_field_id">
      Direct Bug ID field for this reverse one:
    </label></th>
    <td style="padding: 5px">
      [%# Duplicate name will be cleared by JS on creation form %]
      <select style="width: 400px" name="value_field_id" id="bug_id_rev_value_field_id">
        <option value="">---</option>
        [% FOREACH sel_field = Bugzilla.get_fields({ custom => 1, type => constants.FIELD_TYPE_BUG_ID }) %]
          [% NEXT IF sel_field.id == field.id %]
          <option value="[% sel_field.id | html %]"
            [% ' selected="selected"' IF sel_field.id == field.value_field.id %]>
            [% sel_field.description | html %]
            ([% sel_field.name | html %])
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF field.visibility_field %]
  <tr valign="top">
    <th style="padding: 5px" align="left">
      <label for="visibility_value_id">Show the field only if [% field.visibility_field.description | html %] is set to:</label>
    </th>
    <td style="padding: 5px">
      [% SET vis = field.visibility_values %]
      <select style="width: 400px" id="visibility_value_id"
        name="visibility_value_id" size="7" multiple="multiple">
        [% IF field.visibility_field.nullable %]
        <option value="0"[% IF vis.0 %] selected="selected"[% END %]>---</option>
        [% END %]
        [% FOREACH value = field.visibility_field.legal_values %]
          <option value="[% value.id | html %]"[% ' selected="selected"' IF vis && vis.${value.id} %]>
            [% value.name | html %]
          </option>
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF field.null_field %]
  <tr valign="top" id="allow_null_in_row">
    <th style="padding: 5px" align="left">
      <label for="null_visibility_values">Allow empty value only if [% field.null_field.description | html %] is set to:</label>
    </th>
    <td style="padding: 5px">
      [% SET null = field.null_visibility_values %]
      <select style="width: 400px" id="null_visibility_values"
        name="null_visibility_values" size="7" multiple="multiple">
        [% IF field.null_field.nullable %]
        <option value="0"[% IF null.0 %] selected="selected"[% END %]>---</option>
        [% END %]
        [% FOREACH value = field.null_field.legal_values %]
          [% IF field.null_field_id != field.visibility_field_id || !vis || vis.${value.id} %]
          <option value="[% value.id | html %]"[% ' selected="selected"' IF null && null.${value.id} %]>
            [% value.name | html %]
          </option>
          [% END %]
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

  [% IF field.clone_field %]
  <tr valign="top" id="allow_clone_in_row">
    <th style="padding: 5px" align="left">
      <label for="clone_visibility_values">Clone field only if [% field.clone_field.description | html %] of the cloned [% terms.bug %] is set to:</label>
    </th>
    <td style="padding: 5px">
      [% SET clone = field.clone_visibility_values %]
      <select style="width: 400px" id="clone_visibility_values"
        name="clone_visibility_values" size="7" multiple="multiple">
        [% IF field.clone_field.nullable %]
        <option value="0"[% IF clone.0 %] selected="selected"[% END %]>---</option>
        [% END %]
        [% FOREACH value = field.clone_field.legal_values %]
          [% IF field.clone_field_id != field.visibility_field_id || !vis || vis.${value.id} %]
          <option value="[% value.id | html %]"[% ' selected="selected"' IF clone && clone.${value.id} %]>
            [% value.name | html %]
          </option>
          [% END %]
        [% END %]
      </select>
    </td>
  </tr>
  [% END %]

</table>

<input type="hidden" id="action" name="action" value="[% field.id ? "update" : "new" %]" />
<input type="hidden" name="token" value="[% token | html %]" />
<input type="submit" id="edit" value="[% IF field.id %]Save[% ELSE %]Create[% END %]" />

</form>

[% IF field.obsolete AND field.custom %]
<p>
  <a href="editfields.cgi?action=del&amp;name=[% field.name | html %]">Remove this custom field from the database.</a><br />
  This action will only be successful if the custom field is cleared in all [% terms.bugs %].
</p>
[% END %]

<p>
  <a href="editfields.cgi">Back to the list of existing fields</a>
</p>

<script type="text/javascript">
<!--
onChangeNullable();
onChangeCloned();
onChangeSortkey();
[% IF !field.id %]
onChangeType();
[% END %]
//-->
</script>

[% PROCESS global/footer.html.tmpl %]
