[%# Mass bug import from Excel and CSV files
  # License: Dual-license GPL 3.0+ or MPL 1.1+ %]

[% PROCESS global/header.html.tmpl title = 'Excel import' %]

[% IF form || show_error %]
<h2>Mass Bug Import from Excel files</h2>

[% IF show_error %]
<p style="color:red"><b>
[% IF error == "parse_error" %]
    Excel format parse error occurred.
[% ELSIF error == "empty" %]
    The supplied file does not contain any bug descriptions, or the sheet with selected name was not found.
[% ELSE %]
    Unknown error: "[% error | html %]".
[% END %]
</b></p>
[% END %]

<script language="JavaScript">
function addfield()
{
    fd = document.getElementById('flds');
    sel = document.getElementById('newfld');
    opt = sel.options[sel.selectedIndex];
    tr = document.createElement('TR');
    td = document.createElement('TD');
    td.innerHTML = opt.text + ' for all bugs:&nbsp;';
    tr.appendChild(td);
    td = document.createElement('TD');
    td.innerHTML = '<input type="text" name="f_' + opt.value + '" value="" />';
    tr.appendChild(td);
    fd.appendChild(tr);
}
</script>

<form action="importxls.cgi" method="post" enctype="multipart/form-data">
[% FOR f = name_tr.keys %]
<input type="hidden" name="t_[% f | html %]" value="[% name_tr.${f} | html %]" />
[% END %]
<table>
<tbody>
<tr><td>Select XLS/XLSX/CSV file to import:</td><td><input type="file" name="xls" /></td></tr>
<tr><td>Enter sheet name to process<span style="color:red">*</span>:</td><td><input type="text" name="listname" value="[% listname | html %]" /> or CSV delimiter: <input type="text" name="csv_delimiter" size="3" value="," /></td></tr>
<tr><td>Maximum bug duplicate age:</td><td><input type="text" size="5" name="bugdays" value="[% bugdays | html %]" /> days</td></tr>
</tbody>
<tbody id="flds">
[% FOR f = bug_tpl.keys %]
<tr><td>[% import_field_descs.${f} | html %] for all bugs:&nbsp;</td><td><input type="text" name="f_[% f | html %]" value="[% bug_tpl.${f} | html %]" /></td></tr>
[% END %]
</tbody>
</table>
<p>
<select id="newfld">
    [% FOR f = import_fields %]
    <option value="[% f | html %]">[% import_field_descs.${f} | html %]</option>
    [% END %]
</select> <input type="button" onclick="addfield()" value="Add field value for all bugs" />
</p>
<p><input type="submit" value="Parse File" /></p>
</form>

<span style="color:red">*</span> Empty sheet name means to process all sheets.
[% ELSIF show_result %]
[% IF result %]
<p><b>Successfully imported [% result | html %] bugs[% IF result > 0 %]:
[% i = 0 %]
[% FOR b = bug_id %]
    [% ", " IF i %]
    <a href="show_bug.cgi?id=[% b | html %]">[% b | html %]</a>
    [% i = i+1 %]
[% END %]
[% END %].</b></p>
[% ELSE %]
<p>An import error occurred, no bugs were imported.</p>
[% END %]
<p><a href="[% importnext | html %]">Import another Excel file</a> - You can bookmark this link as a template.</p>
[% ELSIF fields %]

<h2>Select worksheet items to import as bugs</h2>

<script language="JavaScript">
function toggleallbugz(chk)
{
  var bgz = document.getElementById("bugz");
  for (var i = 0; i < bgz.childNodes.length; i++)
    if (bgz.childNodes[i].id && bgz.childNodes[i].id != "")
      document.getElementById('b_enabled_'+(""+bgz.childNodes[i].id).substr(5)).checked = chk;
}
function fieldrename(field)
{
  document.getElementById('tp_'+field).style.display='none';
  document.getElementById('t_'+field).style.display='';
}
function checkColumns()
{
  var bgz = document.getElementById("bugz");
  var chk = false;
  for (var i = 0; i < bgz.childNodes.length; i++)
    if (bgz.childNodes[i].id && bgz.childNodes[i].id != "" && document.getElementById('b_enabled_'+(""+bgz.childNodes[i].id).substr(5)).checked)
    {
      chk = true;
      break;
    }
  if (!chk)
    alert('Для импорта не выбрано ни одного бага из списка!');
  return chk;
}
</script>

[% lens = {} %]
[% FOR bug = data %]
    [% FOR field = fields %]
        [% IF bug.${field}.length > lens.${field} %]
        [% lens.${field} = bug.${field}.length %]
        [% END %]
    [% END %]
[% END %]
[% FOR key = lens.keys %]
    [% IF lens.${key} > 20 %]
        [% lens.${key} = 20 %]
    [% ELSIF lens.${key} < 3 %]
        [% lens.${key} = 3 %]
    [% END %]
[% END %]

<form action="importxls.cgi" method="post" onsubmit="return checkColumns()" id="gridform">
<input type="hidden" name="token" value="[% token | html %]" />
<input type="hidden" name="commit" value="1" />
<input type="hidden" name="bugdays" value="[% bugdays | html %]" />
<input type="hidden" name="listname" value="[% listname | html %]" />
[% FOR key = bug_tpl.keys %]
<input type="hidden" name="f_[% key | html %]" value="[% bug_tpl.${key} | html %]" />
[% END %]
<table id="gridtable">
<tr>
    <td><input type="checkbox" onclick="toggleallbugz(this.checked)" /></td>
[% FOR field = fields %]
    <td style="white-space: nowrap">
        [% IF name_tr.${field} %]
        <p id="tp_[% field | html %]">
            <a href="javascript:void fieldrename('[% field.replace("['\"]", '\\$&') | html %]')">
            [% IF import_field_descs.${field} %]
                [% import_field_descs.${field} | html %]
            [% ELSE %]
                <span style="color: red">[% field | html %]</span>
                [% IF name_tr.$field %] (mapped to [% import_field_descs.${name_tr.$field} %])[% END %]
            [% END %]
            </a>
        </p>
        [% END %]
        <select id="t_[% field | html %]" name="t_[% field | html %]" style="[% IF name_tr.${field} %]display: none; width: 90px[% ELSE %]width: 150px[% END %]">
            <option value="">Don&apos;t map: [% field | html %]</option>
        [% FOR f = import_fields %]
            <option value="[% f | html %]" [% IF name_tr.$field == f %] selected[% SET found = 1 %][% END %]>[% import_field_descs.$f | html %]</option>
        [% END %]
        </select>
    </td>
[% END %]
</tr>
<tbody id="bugz">
[% FOR bug = data %]
<tr id="bugz_[% bug.num | html %]">
    <td><input type="checkbox" id="b_enabled_[% bug.num | html %]" name="b_enabled_[% bug.num | html %]" value="1" [% IF bug.enabled %]checked[% END %] /></td>
    [% FOR field = fields %]
    [% SET v = bug.${field} | html %]
    [% IF v.match("\n") %]
    <td><textarea style="font-family: monospace; font-size: 13px" rows="2" cols="[% lens.$field | html %]" name="b_[% field | html %]_[% bug.num | html %]">[% v | html %]</textarea></td>
    [% ELSE %]
    <td><input type="text" size="[% lens.$field | html %]" name="b_[% field | html %]_[% bug.num | html %]" value="[% v | html %]" /></td>
    [% END %]
    [% END %]
</tr>
[% END %]
</tbody>
</table>
<p><input type="submit" value="Import selected bugs / updates" /> &nbsp; <input type="checkbox" name="commentsilent" id="commentsilent" value="1" /> <label for="commentsilent">Silent</label></p>
</form>

<p><a href="javascript:history.back()">Back</a></p>
[% END %]

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