mtrack /web/admin/importcsv.php

Language PHP Lines 327
MD5 Hash e8876ee907f4395fca622466948ac12e Estimated Cost $5,274 (why?)
Repository https://bitbucket.org/yoander/mtrack View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php # vim:ts=2:sw=2:et:
include '../../inc/common.php';

MTrackACL::requireAllRights("Tickets", 'create');
session_start();

$field_aliases = array(
  'state' => 'status',
  'pri' => 'priority',
  'id' => 'ticket',
  'type' => 'classification',
);
$supported_fields = array(
  'classification',
  'ticket',
  'milestone',
  '-milestone',
  '+milestone',
  'summary',
  'status',
  'priority',
  'estimated',
  'owner',
  'type',
  'component',
  '-component',
  '+component',
  'description'
);
foreach ($supported_fields as $i => $f) {
  unset($supported_fields[$i]);
  $supported_fields[$f] = $f;
}

$C = MTrackTicket_CustomFields::getInstance();
foreach ($C->getFields() as $f) {
  $name = substr($f->name, 2);
  $supported_fields[$f->name] = $f->name;
  if (!isset($field_aliases[$name])) {
    $field_aliases[$name] = $f->name;
  }
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  if (isset($_FILES['csvfile']) && $_FILES['csvfile']['error'] == 0
      && is_uploaded_file($_FILES['csvfile']['tmp_name'])) {
    ini_set('auto_detect_line_endings', true);
    $fp = fopen($_FILES['csvfile']['tmp_name'], 'r');
    $header = fgetcsv($fp);
    $err = array();
    $output = array();
    foreach ($header as $i => $name) {
      $name = strtolower($name);
      if (isset($field_aliases[$name])) {
        $name = $field_aliases[$name];
      }
      if (!isset($supported_fields[$name])) {
        $err[] = "Unsupported field: $name";
      }
      $header[$i] = $name;
    }
    $db = MTrackDB::get();
    $db->beginTransaction();
    MTrackChangeset::$use_txn = false;
    $todo = array();
    do {
      $line = fgetcsv($fp);
      if ($line === false) break;

      $item = array();
      foreach ($header as $i => $name) {
        $item[$name] = $line[$i];
      }

      if (isset($item['ticket']) && strlen($item['ticket'])) {
        $id = $item['ticket'];
        if ($id[0] == '#') {
          $id = substr($id, 1);
        }
        try {
          $tkt = MTrackIssue::loadByNSIdent($id);
          if ($tkt == null) {
            $err[] = "No such ticket $id";
            continue;
          }
        } catch (Exception $e) {
          $err[] = $e->getMessage();
          continue;
        }
        $output[] = "<b>Updating ticket $tkt->nsident</b><br>\n";
      } else {
        $tkt = new MTrackIssue;
        $tkt->priority = 'normal';
        $tkt->get_next_nsident();
        $output[] = "<b>Creating ticket $tkt->nsident<b><br>\n";
      }
      $CS = MTrackChangeset::begin("ticket:X", $_POST['comment']);
      if (strlen(trim($_POST['comment']))) {
        $tkt->addComment($_POST['comment']);
      }
      foreach ($item as $name => $value) {
        if ($name == 'ticket') {
          continue;
        }
        $output[] = "$name => $value<br>\n";
        try {
          switch ($name) {
            case 'summary':
            case 'description':
            case 'classification':
            case 'priority':
            case 'severity':
            case 'changelog':
            case 'owner':
            case 'cc':
            case 'estimated':
            case 'status':
              $tkt->$name = strlen($value) ? $value : null;
              break;
            case 'milestone':
              if (strlen($value)) {
                foreach ($tkt->getMilestones() as $mid) {
                  $tkt->dissocMilestone($mid);
                }
                $tkt->assocMilestone($value);
              }
              break;
            case '+milestone':
              if (strlen($value)) {
                $tkt->assocMilestone($value);
              }
              break;
            case '-milestone':
              if (strlen($value)) {
                $tkt->dissocMilestone($value);
              }
              break;
            case 'component':
              if (strlen($value)) {
                foreach ($tkt->getComponents() as $mid) {
                  $tkt->dissocComponent($mid);
                }
                $tkt->assocComponent($value);
              }
              break;
            case '+component':
              if (strlen($value)) {
                $tkt->assocComponent($value);
              }
              break;
            case '-component':
              if (strlen($value)) {
                $tkt->dissocComponent($value);
              }
              break;
            default:
              if (!strncmp($name, 'x_', 2)) {
                $tkt->{$name} = $value;
              }
              break;
          }
        } catch (Exception $e) {
          $err[] = $e->getMessage();
        }
      }
      $tkt->save($CS);
      $CS->setObject("ticket:" . $tkt->tid);

    } while (true);
    $_SESSION['admin.import.result'] = array($err, $output);
    if (count($err)) {
      $db->rollback();
    } else {
      $db->commit();
    }
  }
  header("Location: {$ABSWEB}admin/importcsv.php");
  exit;
}

if (isset($_SESSION['admin.import.result'])) {
  list($err, $info) = $_SESSION['admin.import.result'];
  unset($_SESSION['admin.import.result']);

  mtrack_head(count($err) ? 'Import Failed' : 'Import Complete');

  foreach ($info as $line) {
    echo $line;
  }

  if (count($err)) {
    echo "The following errors were encountered:<br>\n";
    foreach ($err as $msg) {
      echo htmlentities($msg) . "<br>\n";
    }
    echo "<br><b>No changes were committed</b><br>\n";
  } else {
    echo "<br><b>Done!</b>\n";
  }

  mtrack_foot();
  exit;
}

mtrack_head('Import');

?>
<h1>Import/Update via CSV</h1>

<p>
You may use this facility to change ticket properties en-masse by uploading
a CSV file.
</p>

<ul>
  <li>If a ticket column is present and non-empty,
    that ticket will be updated</li>
  <li>If there is no ticket column, or the ticket column is empty,
    then a ticket will be created</li>
  <li>If any errors are detected, none of the changes from the CSV file
    will be applied</li>
</ul>

<p>
The input file must be a CSV file with the field names on the first line.
</p>

<p>
The following fields are supported:
</p>

<dl>
  <dt>ticket</dt>
  <dd>The ticket number</dd>

  <dt>milestone</dt>
  <dd>The value to use for the milestone.  If updating an existing ticket,
   this field will remove any other milestones in the ticket and set it to
   only this value.
  </dd>

  <dt>-milestone</dt>
  <dd>Removes a milestone; if the ticket is associated with the named milestone,
   it will be removed from that milestone.
  </dd>

  <dt>+milestone</dt>
  <dd>Associates the ticket with the named milestone, preserving any other
  milestones currently associated with the ticket.
  </dd>

  <dt>summary</dt>
  <dd>Sets the summary for the ticket</dd>

  <dt>status or state</dt>
  <dd>Sets the state of the ticket; can be one of the configured ticket states
  </dd>

  <dt>priority</dt>
  <dd>Sets the priority; can be one of the configured priorities</dd>

  <dt>owner</dt>
  <dd>Sets the owner</dd>

  <dt>type</dt>
  <dd>Sets the ticket type</dd>

  <dt>estimated</dt>
  <dd>Estimated time (hours)</dd>

  <dt>component</dt>
  <dd>Sets the component, replacing all other component associations</dd>

  <dt>-component</dt>
  <dd>Removes association with the named component</dd>

  <dt>+component</dt>
  <dd>Associates with the named component, preserving existing associations</dd>

  <dt>description</dt>
  <dd>Sets the description of the ticket</dd>

<?php

foreach ($C->getFields() as $f) {
  $name = substr($f->name, 2);
  if (!isset($field_aliases[$name]) || $field_aliases[$name] != $f->name) {
    $name = $f->name;
    echo "<dt>$name</dt>\n";
  } else {
    echo "<dt>$name</dt>\n";
    echo "<dt>$f->name</dt>\n";
  }
  echo "<dd>" . htmlentities($f->label, ENT_QUOTES, 'utf-8') . "\n";

  if ($f->type == 'select') {
    echo "<br>Value may be one of:<br>";
    $data = $f->ticketData();
    foreach ($data['options'] as $opt) {
      echo " <tt>" . htmlentities($opt, ENT_QUOTES, 'utf-8') . "</tt><br>";
    }
  }

  echo "</dd>\n";
}

?>

</dl>

<h2>Import</h2>

<p>Enter a comment in the box below; it will be added as a comment to
all affected tickets</p>

<form method='post' enctype='multipart/form-data'>
  <textarea name='comment' id='comment'
    class='code wiki' rows='4' cols='78'></textarea>
  <input type='file' name='csvfile'>
  <input type='submit' value='Import'>
</form>

<?php
mtrack_foot();
Back to Top