change_ticket.class.php 16.2 KB
Newer Older
1
<?php
2
3
4
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
5
 * Copyright (C) 2015-2021 Teclib' and contributors.
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
 *
 * http://glpi-project.org
 *
 * based on GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2003-2014 by the INDEPNET Development Team.
 *
 * ---------------------------------------------------------------------
 *
 * LICENSE
 *
 * This file is part of GLPI.
 *
 * GLPI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GLPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GLPI. If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
31
32
33
 */

if (!defined('GLPI_ROOT')) {
Stefan Weil's avatar
Stefan Weil committed
34
   die("Sorry. You can't access this file directly");
35
36
}

moyooo's avatar
moyooo committed
37
38
39
40
41
/**
 * Change_Ticket Class
 *
 * Relation between Changes and Tickets
**/
42
43
44
class Change_Ticket extends CommonDBRelation{

   // From CommonDBRelation
yllen's avatar
CS    
yllen committed
45
46
   static public $itemtype_1   = 'Change';
   static public $items_id_1   = 'changes_id';
47

yllen's avatar
CS    
yllen committed
48
49
   static public $itemtype_2   = 'Ticket';
   static public $items_id_2   = 'tickets_id';
50

51

yllen's avatar
CS    
yllen committed
52

moyooo's avatar
moyooo committed
53
   function getForbiddenStandardMassiveAction() {
yllen's avatar
CS    
yllen committed
54
55

      $forbidden   = parent::getForbiddenStandardMassiveAction();
moyooo's avatar
moyooo committed
56
      $forbidden[] = 'update';
moyooo's avatar
moyooo committed
57
      return $forbidden;
yllen's avatar
CS    
yllen committed
58
59
   }

yllen's avatar
yllen committed
60

61
   static function getTypeName($nb = 0) {
62
      return _n('Link Ticket/Change', 'Links Ticket/Change', $nb);
63
64
   }

yllen's avatar
CS    
yllen committed
65

66
   function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
moyooo's avatar
moyooo committed
67
68

      if (static::canView()) {
yllen's avatar
cleanup    
yllen committed
69
         $nb = 0;
moyooo's avatar
moyooo committed
70
71
72
73
         switch ($item->getType()) {
            case 'Change' :
               if ($_SESSION['glpishow_count_on_tabs']) {
                  $nb = countElementsInTable('glpi_changes_tickets',
74
                                             ['changes_id' => $item->getID()]);
moyooo's avatar
moyooo committed
75
               }
76
               return self::createTabEntry(Ticket::getTypeName(Session::getPluralNumber()), $nb);
moyooo's avatar
moyooo committed
77
78
79
80

            case 'Ticket' :
               if ($_SESSION['glpishow_count_on_tabs']) {
                  $nb = countElementsInTable('glpi_changes_tickets',
81
                                            ['tickets_id' => $item->getID()]);
moyooo's avatar
moyooo committed
82
               }
83
               return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb);
moyooo's avatar
moyooo committed
84
85
86
87
88
         }
      }
      return '';
   }

89

90
   static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
moyooo's avatar
moyooo committed
91
92
93
94
95

      switch ($item->getType()) {
         case 'Change' :
            self::showForChange($item);
            break;
yllen's avatar
CS    
yllen committed
96

moyooo's avatar
moyooo committed
97
98
99
100
101
102
         case 'Ticket' :
            self::showForTicket($item);
            break;
      }
      return true;
   }
yllen's avatar
CS    
yllen committed
103
104


moyooo's avatar
moyooo committed
105
106
107
   static function showMassiveActionsSubForm(MassiveAction $ma) {

      switch ($ma->getAction()) {
108
109
110
         case 'add_task' :
            $tasktype = 'TicketTask';
            if ($ttype = getItemForItemtype($tasktype)) {
111
               $ttype->showMassiveActionAddTaskForm();
112
113
114
               return true;
            }
            return false;
115

moyooo's avatar
moyooo committed
116
117
118
119
         case "solveticket" :
            $change = new Change();
            $input = $ma->getInput();
            if (isset($input['changes_id']) && $change->getFromDB($input['changes_id'])) {
120
121
               $change->showMassiveSolutionForm($change);
               echo "<br>";
122
               echo Html::submit(_x('button', 'Post'), ['name' => 'massiveaction']);
moyooo's avatar
moyooo committed
123
124
125
126
127
128
129
130
131
132
133
134
               return true;
            }
            return false;
      }
      return parent::showMassiveActionsSubForm($ma);
   }


   static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item,
                                                       array $ids) {

      switch ($ma->getAction()) {
135
136
137
138
139
140
141
142
143
144
145
146
147
         case 'add_task' :
            if (!($task = getItemForItemtype('TicketTask'))) {
               $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
               break;
            }
            $ticket = new Ticket();
            $field = $ticket->getForeignKeyField();

            $input = $ma->getInput();

            foreach ($ids as $id) {
               if ($item->can($id, READ)) {
                  if ($ticket->getFromDB($item->fields['tickets_id'])) {
148
                     $input2 = [$field              => $item->fields['tickets_id'],
149
150
                                  'taskcategories_id' => $input['taskcategories_id'],
                                  'actiontime'        => $input['actiontime'],
151
                                  'content'           => $input['content']];
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
                     if ($task->can(-1, CREATE, $input2)) {
                        if ($task->add($input2)) {
                           $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                        } else {
                           $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                           $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                        }
                     } else {
                        $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                        $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                     }
                  } else {
                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                     $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                  }
               }
            }
            return;
         case 'solveticket' :
moyooo's avatar
moyooo committed
171
172
173
174
175
176
            $input  = $ma->getInput();
            $ticket = new Ticket();
            foreach ($ids as $id) {
               if ($item->can($id, READ)) {
                  if ($ticket->getFromDB($item->fields['tickets_id'])
                      && $ticket->canSolve()) {
177
178
179
180
181
182
183
184
185
                     $solution = new ITILSolution();
                     $added = $solution->add([
                        'itemtype'  => $ticket->getType(),
                        'items_id'  => $ticket->getID(),
                        'solutiontypes_id'   => $input['solutiontypes_id'],
                        'content'            => $input['content']
                     ]);

                     if ($added) {
moyooo's avatar
moyooo committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
                        $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                     } else {
                        $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                        $ma->addMessage($ticket->getErrorMessage(ERROR_ON_ACTION));
                     }
                  } else {
                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                     $ma->addMessage($ticket->getErrorMessage(ERROR_RIGHT));
                  }
               } else {
                  $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                  $ma->addMessage($ticket->getErrorMessage(ERROR_RIGHT));
               }
            }
            return;
      }
      parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
   }
204

yllen's avatar
CS    
yllen committed
205

206
207
208
209
210
211
   /**
    * Show tickets for a change
    *
    * @param $change Change object
   **/
   static function showForChange(Change $change) {
Cédric Anne's avatar
Cédric Anne committed
212
      global $DB;
213
214

      $ID = $change->getField('id');
moyooo's avatar
moyooo committed
215
      if (!$change->can($ID, READ)) {
216
217
218
         return false;
      }

moyooo's avatar
moyooo committed
219
      $canedit = $change->canEdit($ID);
yllen's avatar
yllen committed
220
      $rand    = mt_rand();
221

Johan Cwiklinski's avatar
Johan Cwiklinski committed
222
      $iterator = $DB->request([
223
         'SELECT' => [
224
            'glpi_changes_tickets.id AS linkid',
225
226
227
            'glpi_tickets.*'
         ],
         'DISTINCT'        => true,
Johan Cwiklinski's avatar
Johan Cwiklinski committed
228
229
230
231
232
233
234
235
236
237
238
239
         'FROM'            => 'glpi_changes_tickets',
         'LEFT JOIN'       => [
            'glpi_tickets' => [
               'ON' => [
                  'glpi_changes_tickets'  => 'tickets_id',
                  'glpi_tickets'          => 'id'
               ]
            ]
         ],
         'WHERE'           => [
            'glpi_changes_tickets.changes_id'   => $ID
         ],
cconard96's avatar
cconard96 committed
240
         'ORDERBY'          => [
Johan Cwiklinski's avatar
Johan Cwiklinski committed
241
242
243
            'glpi_tickets.name'
         ]
      ]);
moyooo's avatar
moyooo committed
244

245
246
      $tickets = [];
      $used    = [];
Johan Cwiklinski's avatar
Johan Cwiklinski committed
247
248
249
250
251
      $numrows = count($iterator);

      while ($data = $iterator->next()) {
         $tickets[$data['id']] = $data;
         $used[$data['id']]    = $data['id'];
moyooo's avatar
moyooo committed
252
253
      }

yllen's avatar
yllen committed
254
255
256
      if ($canedit
          && !in_array($change->fields['status'], array_merge($change->getClosedStatusArray(),
                                                              $change->getSolvedStatusArray()))) {
257
         echo "<div class='firstbloc'>";
moyooo's avatar
moyooo committed
258
         echo "<form name='changeticket_form$rand' id='changeticket_form$rand' method='post'
yllen's avatar
yllen committed
259
                action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";
moyooo's avatar
moyooo committed
260
261

         echo "<table class='tab_cadre_fixe'>";
262
         echo "<tr class='tab_bg_2'><th colspan='2'>".__('Add a ticket')."</th></tr>";
moyooo's avatar
moyooo committed
263

264
         echo "<tr class='tab_bg_2'><td>";
moyooo's avatar
moyooo committed
265
         echo "<input type='hidden' name='changes_id' value='$ID'>";
266
267
268
269
270
271
272
         Ticket::dropdown([
            'used'        => $used,
            'entity'      => $change->getEntityID(),
            'entity_sons' => $change->isRecursive(),
            'displaywith' => ['id'],
            'condition'   => Ticket::getOpenCriteria()
         ]);
moyooo's avatar
moyooo committed
273
         echo "</td><td class='center'>";
moyooo's avatar
moyooo committed
274
         echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
moyooo's avatar
moyooo committed
275
         echo "</td></tr>";
moyooo's avatar
moyooo committed
276

moyooo's avatar
moyooo committed
277
278
         echo "</table>";
         Html::closeForm();
279
         echo "</div>";
moyooo's avatar
moyooo committed
280
      }
yllen's avatar
CS    
yllen committed
281

moyooo's avatar
moyooo committed
282
      echo "<div class='spaced'>";
moyooo's avatar
moyooo committed
283
284
      if ($canedit && $numrows) {
         Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
yllen's avatar
CS    
yllen committed
285
         $massiveactionparams
286
287
            = ['num_displayed'    => min($_SESSION['glpilist_limit'], $numrows),
                    'specific_actions' => ['purge' => _x('button', 'Delete permanently'),
yllen's avatar
CS    
yllen committed
288
                                                 __CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'solveticket'
289
                                                        => __('Solve tickets'),
290
                                                 __CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'add_task'
291
                                                        => __('Add a new task')],
yllen's avatar
CS    
yllen committed
292
                     'container'        => 'mass'.__CLASS__.$rand,
293
                     'extraparams'      => ['changes_id' => $change->getID()],
yllen's avatar
CS    
yllen committed
294
                     'width'            => 1000,
295
                     'height'           => 500];
moyooo's avatar
moyooo committed
296
         Html::showMassiveActions($massiveactionparams);
moyooo's avatar
moyooo committed
297
298
299
      }

      echo "<table class='tab_cadre_fixehov'>";
moyooo's avatar
moyooo committed
300
      echo "<tr class='noHover'><th colspan='12'>".Ticket::getTypeName($numrows)."</th>";
moyooo's avatar
moyooo committed
301
302
      echo "</tr>";
      if ($numrows) {
303
         Ticket::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
remi's avatar
gettext    
remi committed
304
         Session::initNavigateListItems('Ticket',
yllen's avatar
yllen committed
305
306
                                 //TRANS : %1$s is the itemtype name,
                                 //        %2$s is the name of the item (used for headings of a list)
moyooo's avatar
moyooo committed
307
                                         sprintf(__('%1$s = %2$s'), Change::getTypeName(1),
moyooo's avatar
moyooo committed
308
                                                 $change->fields["name"]));
309

moyooo's avatar
moyooo committed
310
         $i = 0;
moyooo's avatar
moyooo committed
311
         foreach ($tickets as $data) {
yllen's avatar
yllen committed
312
            Session::addToNavigateListItems('Ticket', $data["id"]);
Cédric Anne's avatar
Cédric Anne committed
313
314
315
316
317
318
319
320
            Ticket::showShort(
               $data['id'],
               [
                  'row_num'                => $i,
                  'type_for_massiveaction' => __CLASS__,
                  'id_for_massiveaction'   => $data['linkid']
               ]
            );
moyooo's avatar
moyooo committed
321
            $i++;
322
         }
323
         Ticket::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
324
      }
moyooo's avatar
moyooo committed
325
      echo "</table>";
moyooo's avatar
moyooo committed
326
      if ($canedit && $numrows) {
moyooo's avatar
moyooo committed
327
         $massiveactionparams['ontop'] = false;
moyooo's avatar
moyooo committed
328
         Html::showMassiveActions($massiveactionparams);
moyooo's avatar
moyooo committed
329
         Html::closeForm();
330
      }
moyooo's avatar
moyooo committed
331
      echo "</div>";
332
333
334
335
336
337
338
339
340
   }


   /**
    * Show changes for a ticket
    *
    * @param $ticket Ticket object
   **/
   static function showForTicket(Ticket $ticket) {
Cédric Anne's avatar
Cédric Anne committed
341
      global $DB;
342
343

      $ID = $ticket->getField('id');
moyooo's avatar
moyooo committed
344
      if (!$ticket->can($ID, READ)) {
345
346
347
         return false;
      }

moyooo's avatar
moyooo committed
348
      $canedit = $ticket->canEdit($ID);
yllen's avatar
yllen committed
349
      $rand    = mt_rand();
yllen's avatar
yllen committed
350

Johan Cwiklinski's avatar
Johan Cwiklinski committed
351
      $iterator = $DB->request([
352
         'SELECT'          => [
353
            'glpi_changes_tickets.id AS linkid',
354
355
356
            'glpi_changes.*'
         ],
         'DISTINCT'        => true,
Johan Cwiklinski's avatar
Johan Cwiklinski committed
357
358
359
360
361
362
363
364
365
366
         'FROM'            => 'glpi_changes_tickets',
         'LEFT JOIN'       => [
            'glpi_changes' => [
               'ON' => [
                  'glpi_changes_tickets'  => 'changes_id',
                  'glpi_changes'          => 'id'
               ]
            ]
         ],
         'WHERE'           => [
Johan Cwiklinski's avatar
Johan Cwiklinski committed
367
            'glpi_changes_tickets.tickets_id'   => $ID
Johan Cwiklinski's avatar
Johan Cwiklinski committed
368
         ],
cconard96's avatar
cconard96 committed
369
         'ORDERBY'          => [
Johan Cwiklinski's avatar
Johan Cwiklinski committed
370
371
372
            'glpi_changes.name'
         ]
      ]);
yllen's avatar
yllen committed
373

374
      $changes = [];
Johan Cwiklinski's avatar
Johan Cwiklinski committed
375
376
377
378
379
380
      $used    = [];
      $numrows = count($iterator);

      while ($data = $iterator->next()) {
         $changes[$data['id']] = $data;
         $used[$data['id']]    = $data['id'];
moyooo's avatar
moyooo committed
381
      }
Johan Cwiklinski's avatar
Johan Cwiklinski committed
382

yllen's avatar
yllen committed
383
384
385
      if ($canedit
          && !in_array($ticket->fields['status'], array_merge($ticket->getClosedStatusArray(),
                                                              $ticket->getSolvedStatusArray()))) {
386
         echo "<div class='firstbloc'>";
moyooo's avatar
moyooo committed
387
         echo "<form name='changeticket_form$rand' id='changeticket_form$rand' method='post'
moyooo's avatar
moyooo committed
388
               action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";
389

moyooo's avatar
moyooo committed
390
391
392
393
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr class='tab_bg_2'><th colspan='3'>".__('Add a change')."</th></tr>";
         echo "<tr class='tab_bg_2'><td>";
         echo "<input type='hidden' name='tickets_id' value='$ID'>";
394
395
396
397
398
         Change::dropdown([
            'used'      => $used,
            'entity'    => $ticket->getEntityID(),
            'condition' => Change::getOpenCriteria(),
         ]);
moyooo's avatar
moyooo committed
399
         echo "</td><td class='center'>";
400
         echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
moyooo's avatar
moyooo committed
401
         echo "</td><td>";
yllen's avatar
yllen committed
402
403
         if (Session::haveRight('change', CREATE)) {
            echo "<a href='".Toolbox::getItemTypeFormURL('Change')."?tickets_id=$ID'>";
404
            echo __('Create a change from this ticket');
yllen's avatar
yllen committed
405
406
            echo "</a>";
         }
moyooo's avatar
moyooo committed
407
408
         echo "</td></tr></table>";
         Html::closeForm();
409
         echo "</div>";
moyooo's avatar
moyooo committed
410
      }
yllen's avatar
yllen committed
411

412
      echo "<div class='spaced'>";
moyooo's avatar
moyooo committed
413
414
      if ($canedit && $numrows) {
         Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
415
416
         $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $numrows),
                                      'container'     => 'mass'.__CLASS__.$rand];
moyooo's avatar
moyooo committed
417
         Html::showMassiveActions($massiveactionparams);
moyooo's avatar
moyooo committed
418
      }
moyooo's avatar
moyooo committed
419

moyooo's avatar
moyooo committed
420
      echo "<table class='tab_cadre_fixehov'>";
moyooo's avatar
moyooo committed
421
      echo "<tr class='noHover'><th colspan='12'>".Change::getTypeName($numrows)."</th>";
422
      echo "</tr>";
moyooo's avatar
moyooo committed
423
      if ($numrows) {
424
         Change::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
remi's avatar
gettext    
remi committed
425
         Session::initNavigateListItems('Change',
moyooo's avatar
moyooo committed
426
427
428
429
                                 //TRANS : %1$s is the itemtype name,
                                 //        %2$s is the name of the item (used for headings of a list)
                                         sprintf(__('%1$s = %2$s'), Ticket::getTypeName(1),
                                                 $ticket->fields["name"]));
remi's avatar
gettext    
remi committed
430

moyooo's avatar
moyooo committed
431
         $i = 0;
moyooo's avatar
moyooo committed
432
         foreach ($changes as $data) {
yllen's avatar
yllen committed
433
            Session::addToNavigateListItems('Change', $data["id"]);
434
            Change::showShort($data['id'], ['row_num'                => $i,
moyooo's avatar
moyooo committed
435
                                                 'type_for_massiveaction' => __CLASS__,
436
                                                 'id_for_massiveaction'   => $data['linkid']]);
moyooo's avatar
moyooo committed
437
            $i++;
438
         }
439
         Change::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand);
440
      }
moyooo's avatar
moyooo committed
441
      echo "</table>";
moyooo's avatar
moyooo committed
442

moyooo's avatar
moyooo committed
443
      if ($canedit && $numrows) {
moyooo's avatar
moyooo committed
444
         $massiveactionparams['ontop'] = false;
moyooo's avatar
moyooo committed
445
         Html::showMassiveActions($massiveactionparams);
moyooo's avatar
moyooo committed
446
         Html::closeForm();
447
      }
moyooo's avatar
moyooo committed
448
449
      echo "</div>";

450
451
452
453
   }


}