itilfollowup.class.php 42 KB
Newer Older
moyooo's avatar
moyooo committed
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/>.
 * ---------------------------------------------------------------------
moyooo's avatar
moyooo committed
31
32
33
 */

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

Cédric Anne's avatar
Cédric Anne committed
37
38
39
/**
 * @since 9.4.0
 */
cconard96's avatar
cconard96 committed
40
class ITILFollowup  extends CommonDBChild {
moyooo's avatar
moyooo committed
41
42

   // From CommonDBTM
43
   public $auto_message_on_action = false;
moyooo's avatar
moyooo committed
44
   static $rightname              = 'followup';
Cédric Anne's avatar
Cédric Anne committed
45
   private $item                  = null;
cconard96's avatar
cconard96 committed
46
47
48
49

   static public $log_history_add    = Log::HISTORY_LOG_SIMPLE_MESSAGE;
   static public $log_history_update = Log::HISTORY_LOG_SIMPLE_MESSAGE;
   static public $log_history_delete = Log::HISTORY_LOG_SIMPLE_MESSAGE;
moyooo's avatar
moyooo committed
50
51
52
53
54
55
56
57
58

   const SEEPUBLIC       =    1;
   const UPDATEMY        =    2;
   const ADDMYTICKET     =    4;
   const UPDATEALL       = 1024;
   const ADDGROUPTICKET  = 2048;
   const ADDALLTICKET    = 4096;
   const SEEPRIVATE      = 8192;

Cédric Anne's avatar
Cédric Anne committed
59
60
   static public $itemtype = 'itemtype';
   static public $items_id = 'items_id';
cconard96's avatar
cconard96 committed
61
62
63
64
65


   function getItilObjectItemType() {
      return str_replace('Followup', '', $this->getType());
   }
moyooo's avatar
moyooo committed
66

67

68
   static function getTypeName($nb = 0) {
moyooo's avatar
moyooo committed
69
      return _n('Followup', 'Followups', $nb);
remi's avatar
remi committed
70
71
72
   }


Cédric Anne's avatar
Cédric Anne committed
73
   /**
cconard96's avatar
cconard96 committed
74
75
76
    * can read the parent ITIL Object ?
    *
    * @return boolean
Cédric Anne's avatar
Cédric Anne committed
77
    */
cconard96's avatar
cconard96 committed
78
   function canReadITILItem() {
yllen's avatar
yllen committed
79

cconard96's avatar
cconard96 committed
80
81
82
83
84
85
      $itemtype = $this->getItilObjectItemType();
      $item     = new $itemtype();
      if (!$item->can($this->getField($item->getForeignKeyField()), READ)) {
         return false;
      }
      return true;
86
87
   }

yllen's avatar
yllen committed
88

89
   static function canView() {
90
      return (Session::haveRightsOr(self::$rightname, [self::SEEPUBLIC, self::SEEPRIVATE])
cconard96's avatar
cconard96 committed
91
92
              || Session::haveRight('ticket', Ticket::OWN))
              || Session::haveRight('ticket', READ)
Cédric Anne's avatar
Cédric Anne committed
93
94
              || Session::haveRight('change', READ)
              || Session::haveRight('problem', READ);
cconard96's avatar
cconard96 committed
95
96
97
98
   }


   static function canCreate() {
Cédric Anne's avatar
Cédric Anne committed
99
100
101
102
103
      return Session::haveRight('change', UPDATE)
             || Session::haveRight('problem', UPDATE)
             || (Session::haveRightsOr(self::$rightname,
                    [self::ADDALLTICKET, self::ADDMYTICKET, self::ADDGROUPTICKET])
             || Session::haveRight('ticket', Ticket::OWN));
yllen's avatar
yllen committed
104
105
106
   }


cconard96's avatar
cconard96 committed
107
   function canViewItem() {
yllen's avatar
yllen committed
108

cconard96's avatar
cconard96 committed
109
110
      $itilobject = new $this->fields['itemtype'];
      if (!$itilobject->can($this->getField('items_id'), READ)) {
yllen's avatar
yllen committed
111
112
         return false;
      }
cconard96's avatar
cconard96 committed
113
      if (Session::haveRight(self::$rightname, self::SEEPRIVATE)) {
yllen's avatar
yllen committed
114
115
         return true;
      }
cconard96's avatar
cconard96 committed
116
117
118
119
      if (!$this->fields['is_private']
          && Session::haveRight(self::$rightname, self::SEEPUBLIC)) {
         return true;
      }
120
      if ($itilobject instanceof Ticket) {
cconard96's avatar
cconard96 committed
121
122
123
124
125
126
         if ($this->fields["users_id"] === Session::getLoginUserID()) {
            return true;
         }
      } else {
         return Session::haveRight($itilobject::$rightname, READ);
      }
yllen's avatar
yllen committed
127
128
129
130
      return false;
   }


cconard96's avatar
cconard96 committed
131
   function canCreateItem() {
132
133
134
135
136
      if (!isset($this->fields['itemtype'])
          || strlen($this->fields['itemtype']) == 0) {
         return false;
      }

cconard96's avatar
cconard96 committed
137
      $itilobject = new $this->fields['itemtype'];
Adrien Clairembault's avatar
Adrien Clairembault committed
138

cconard96's avatar
cconard96 committed
139
      if (!$itilobject->can($this->getField('items_id'), READ)
Adrien Clairembault's avatar
Adrien Clairembault committed
140
141
142
143
         // No validation for closed tickets
         || in_array($itilobject->fields['status'], $itilobject->getClosedStatusArray())
         && !$itilobject->canReopen()
      ) {
144
145
         return false;
      }
cconard96's avatar
cconard96 committed
146
147
148
149
150
151
152
153
154
      return $itilobject->canAddFollowups();
   }


   function canPurgeItem() {

      $itilobject = new $this->fields['itemtype'];
      if (!$itilobject->can($this->getField('items_id'), READ)) {
         return false;
155
      }
cconard96's avatar
cconard96 committed
156
157

      if (Session::haveRight(self::$rightname, PURGE)) {
158
159
         return true;
      }
cconard96's avatar
cconard96 committed
160

161
162
163
      return false;
   }

yllen's avatar
yllen committed
164

165
   function canUpdateItem() {
yllen's avatar
yllen committed
166

yllen's avatar
yllen committed
167
      if (($this->fields["users_id"] != Session::getLoginUserID())
moyooo's avatar
moyooo committed
168
          && !Session::haveRight(self::$rightname, self::UPDATEALL)) {
169
170
         return false;
      }
yllen's avatar
yllen committed
171

cconard96's avatar
cconard96 committed
172
173
      $itilobject = new $this->fields['itemtype'];
      if (!$itilobject->can($this->getField('items_id'), READ)) {
174
175
         return false;
      }
yllen's avatar
yllen committed
176

177
178
179
180
      if ($this->fields["users_id"] === Session::getLoginUserID()) {
         if (!Session::haveRight(self::$rightname, self::UPDATEMY)) {
            return false;
         }
moyooo's avatar
moyooo committed
181
         return true;
yllen's avatar
yllen committed
182
      }
moyooo's avatar
moyooo committed
183

184
      // Only the technician
moyooo's avatar
moyooo committed
185
      return (Session::haveRight(self::$rightname, self::UPDATEALL)
cconard96's avatar
cconard96 committed
186
              || $itilobject->isUser(CommonITILActor::ASSIGN, Session::getLoginUserID())
187
              || (isset($_SESSION["glpigroups"])
cconard96's avatar
cconard96 committed
188
                  && $itilobject->haveAGroup(CommonITILActor::ASSIGN, $_SESSION['glpigroups'])));
moyooo's avatar
moyooo committed
189
   }
yllen's avatar
yllen committed
190

yllen's avatar
yllen committed
191

remi's avatar
remi committed
192
   function post_getEmpty() {
yllen's avatar
yllen committed
193

remi's avatar
remi committed
194
195
196
      if (isset($_SESSION['glpifollowup_private']) && $_SESSION['glpifollowup_private']) {
         $this->fields['is_private'] = 1;
      }
yllen's avatar
yllen committed
197

yllen's avatar
yllen committed
198
      if (isset($_SESSION["glpiname"])) {
199
         $this->fields['requesttypes_id'] = RequestType::getDefault('followup');
200
      }
remi's avatar
remi committed
201
   }
moyooo's avatar
moyooo committed
202

yllen's avatar
yllen committed
203

cconard96's avatar
cconard96 committed
204
   function post_addItem() {
Cédric Anne's avatar
Cédric Anne committed
205

206
207
      global $CFG_GLPI;

Thierry Bugier's avatar
Thierry Bugier committed
208
209
210
211
212
213
214
215
216
217
218
      // Add screenshots if needed, without notification
      $this->input = $this->addFiles($this->input, [
         'force_update'  => true,
         'name'          => 'content',
         'content_field' => 'content',
      ]);

      // Add documents if needed, without notification
      $this->input = $this->addFiles($this->input, [
         'force_update'  => true,
      ]);
moyooo's avatar
moyooo committed
219

cconard96's avatar
cconard96 committed
220
      $donotif = !isset($this->input['_disablenotif']) && $CFG_GLPI["use_notifications"];
remi's avatar
remi committed
221

222
223
224
      // Check if stats should be computed after this change
      $no_stat = isset($this->input['_do_not_compute_takeintoaccount']);

cconard96's avatar
cconard96 committed
225
      $parentitem = $this->input['_job'];
226
227
228
229
230
      $parentitem->updateDateMod(
         $this->input["items_id"],
         $no_stat,
         $this->input["users_id"]
      );
yllen's avatar
yllen committed
231

cconard96's avatar
cconard96 committed
232
233
234
      if (isset($this->input["_close"])
          && $this->input["_close"]
          && ($parentitem->fields["status"] == CommonITILObject::SOLVED)) {
moyooo's avatar
moyooo committed
235

Cédric Anne's avatar
Cédric Anne committed
236
237
238
239
240
241
         $update = [
            'id'        => $parentitem->fields['id'],
            'status'    => CommonITILObject::CLOSED,
            'closedate' => $_SESSION["glpi_currenttime"],
            '_accepted' => true,
         ];
moyooo's avatar
moyooo committed
242

cconard96's avatar
cconard96 committed
243
244
245
         // Use update method for history
         $this->input["_job"]->update($update);
         $donotif = false; // Done for ITILObject update (new status)
246
247
      }

cconard96's avatar
cconard96 committed
248
249
250
251
252
253
254
255
256
257
258
      //manage reopening of ITILObject
      $reopened = false;
      if (!isset($this->input['_status'])) {
         $this->input['_status'] = $parentitem->fields["status"];
      }
      // if reopen set (from followup form or mailcollector)
      // and status is reopenable and not changed in form
      if (isset($this->input["_reopen"])
          && $this->input["_reopen"]
          && in_array($parentitem->fields["status"], $parentitem::getReopenableStatusArray())
          && $this->input['_status'] == $parentitem->fields["status"]) {
moyooo's avatar
moyooo committed
259

260
         $needupdateparent = false;
cconard96's avatar
cconard96 committed
261
262
263
         if (($parentitem->countUsers(CommonITILActor::ASSIGN) > 0)
             || ($parentitem->countGroups(CommonITILActor::ASSIGN) > 0)
             || ($parentitem->countSuppliers(CommonITILActor::ASSIGN) > 0)) {
264
265

            //check if lifecycle allowed new status
266
267
268
            if (Session::isCron()
                || Session::getCurrentInterface() == "helpdesk"
                || $parentitem::isAllowedStatus($parentitem->fields["status"], CommonITILObject::ASSIGNED)) {
269
270
271
               $needupdateparent = true;
               $update['status'] = CommonITILObject::ASSIGNED;
            }
cconard96's avatar
cconard96 committed
272
         } else {
273
            //check if lifecycle allowed new status
274
275
276
            if (Session::isCron()
                || Session::getCurrentInterface() == "helpdesk"
                || $parentitem::isAllowedStatus($parentitem->fields["status"], CommonITILObject::INCOMING)) {
277
278
279
               $needupdateparent = true;
               $update['status'] = CommonITILObject::INCOMING;
            }
cconard96's avatar
cconard96 committed
280
281
         }

282
283
284
285
286
287
288
         if ($needupdateparent) {
            $update['id'] = $parentitem->fields['id'];

            // Use update method for history
            $parentitem->update($update);
            $reopened     = true;
         }
cconard96's avatar
cconard96 committed
289

290
      }
291

cconard96's avatar
cconard96 committed
292
293
294
      //change ITILObject status only if imput change
      if (!$reopened
          && $this->input['_status'] != $parentitem->fields['status']) {
moyooo's avatar
moyooo committed
295

cconard96's avatar
cconard96 committed
296
297
         $update['status'] = $this->input['_status'];
         $update['id']     = $parentitem->fields['id'];
moyooo's avatar
moyooo committed
298

cconard96's avatar
cconard96 committed
299
300
         // don't notify on ITILObject - update event
         $update['_disablenotif'] = true;
moyooo's avatar
moyooo committed
301

cconard96's avatar
cconard96 committed
302
303
304
         // Use update method for history
         $parentitem->update($update);
      }
moyooo's avatar
moyooo committed
305

cconard96's avatar
cconard96 committed
306
307
308
309
310
      if ($donotif) {
         $options = ['followup_id' => $this->fields["id"],
                          'is_private'  => $this->fields['is_private']];
         NotificationEvent::raiseEvent("add_followup", $parentitem, $options);
      }
moyooo's avatar
moyooo committed
311

cconard96's avatar
cconard96 committed
312
      // Add log entry in the ITILObject
Cédric Anne's avatar
Cédric Anne committed
313
314
315
316
317
      $changes = [
         0,
         '',
         $this->fields['id'],
      ];
cconard96's avatar
cconard96 committed
318
319
320
      Log::history($this->getField('items_id'), get_class($parentitem), $changes, $this->getType(),
                   Log::HISTORY_ADD_SUBITEM);
   }
yllen's avatar
yllen committed
321

322

cconard96's avatar
cconard96 committed
323
324
   function post_deleteFromDB() {
      global $CFG_GLPI;
325

cconard96's avatar
cconard96 committed
326
327
328
329
      $donotif = $CFG_GLPI["use_notifications"];
      if (isset($this->input['_disablenotif'])) {
         $donotif = false;
      }
330

331
      $job = new $this->fields['itemtype'];
cconard96's avatar
cconard96 committed
332
333
334
335
      $job->getFromDB($this->fields[self::$items_id]);
      $job->updateDateMod($this->fields[self::$items_id]);

      // Add log entry in the ITIL Object
Cédric Anne's avatar
Cédric Anne committed
336
337
338
339
340
      $changes = [
         0,
         '',
         $this->fields['id'],
      ];
341
      Log::history($this->getField(self::$items_id), $this->fields['itemtype'], $changes, $this->getType(),
cconard96's avatar
cconard96 committed
342
343
344
345
346
347
348
                   Log::HISTORY_DELETE_SUBITEM);

      if ($donotif) {
         $options = ['followup_id' => $this->fields["id"],
                           // Force is_private with data / not available
                          'is_private'  => $this->fields['is_private']];
         NotificationEvent::raiseEvent('delete_followup', $job, $options);
moyooo's avatar
moyooo committed
349
350
351
352
353
      }
   }


   function prepareInputForAdd($input) {
354

cconard96's avatar
cconard96 committed
355
      $input["_job"] = new $input['itemtype']();
moyooo's avatar
moyooo committed
356

yllen's avatar
yllen committed
357
      if (empty($input['content'])
moyooo's avatar
moyooo committed
358
359
          && !isset($input['add_close'])
          && !isset($input['add_reopen'])) {
360
361
         Session::addMessageAfterRedirect(__("You can't add a followup without description"),
                                          false, ERROR);
362
363
         return false;
      }
cconard96's avatar
cconard96 committed
364
      if (!$input["_job"]->getFromDB($input["items_id"])) {
moyooo's avatar
moyooo committed
365
366
367
368
369
         return false;
      }

      $input['_close'] = 0;

370
371
372
373
374
      if (!isset($input["users_id"])) {
         $input["users_id"] = 0;
         if ($uid = Session::getLoginUserID()) {
            $input["users_id"] = $uid;
         }
moyooo's avatar
moyooo committed
375
      }
376
      // if ($input["_isadmin"] && $input["_type"]!="update") {
yllen's avatar
yllen committed
377
378
      if (isset($input["add_close"])) {
         $input['_close'] = 1;
moyooo's avatar
moyooo committed
379
380
381
         if (empty($input['content'])) {
            $input['content'] = __('Solution approved');
         }
yllen's avatar
yllen committed
382
      }
yllen's avatar
yllen committed
383

yllen's avatar
yllen committed
384
385
      unset($input["add_close"]);

moyooo's avatar
moyooo committed
386
387
388
389
      if (!isset($input["is_private"])) {
         $input['is_private'] = 0;
      }

yllen's avatar
yllen committed
390
391
      if (isset($input["add_reopen"])) {
         if ($input["content"] == '') {
392
393
            if (isset($input["_add"])) {
               // Reopen using add form
cconard96's avatar
cconard96 committed
394
               Session::addMessageAfterRedirect(__('If you want to reopen this item, you must specify a reason'),
yllen's avatar
yllen committed
395
                                                false, ERROR);
396
397
398
399
400
            } else {
               // Refuse solution
               Session::addMessageAfterRedirect(__('If you reject the solution, you must specify a reason'),
                                                false, ERROR);
            }
yllen's avatar
yllen committed
401
            return false;
moyooo's avatar
moyooo committed
402
         }
yllen's avatar
yllen committed
403
404
405
         $input['_reopen'] = 1;
      }
      unset($input["add_reopen"]);
406
      // }
407
      unset($input["add"]);
yllen's avatar
yllen committed
408

cconard96's avatar
cconard96 committed
409
410
      $itemtype = $input['itemtype'];
      $input['timeline_position'] = $itemtype::getTimelinePosition($input["items_id"], $this->getType(), $input["users_id"]);
411

cconard96's avatar
cconard96 committed
412
413
414
      if (!isset($input['date'])) {
         $input["date"] = $_SESSION["glpi_currenttime"];
      }
moyooo's avatar
moyooo committed
415
416
417
418
      return $input;
   }


cconard96's avatar
cconard96 committed
419
420
421
422
423
424
425
426
   function prepareInputForUpdate($input) {
      if (!isset($this->fields['itemtype'])) {
         return false;
      }
      $input["_job"] = new $this->fields['itemtype']();
      if (!$input["_job"]->getFromDB($this->fields["items_id"])) {
         return false;
      }
427

cconard96's avatar
cconard96 committed
428
429
430
431
432
      // update last editor if content change
      if (($uid = Session::getLoginUserID())
          && isset($input['content']) && ($input['content'] != $this->fields['content'])) {
         $input["users_id_editor"] = $uid;
      }
moyooo's avatar
moyooo committed
433

cconard96's avatar
cconard96 committed
434
435
      return $input;
   }
moyooo's avatar
moyooo committed
436

yllen's avatar
yllen committed
437

cconard96's avatar
cconard96 committed
438
439
   function post_updateItem($history = 1) {
      global $CFG_GLPI;
moyooo's avatar
moyooo committed
440

cconard96's avatar
cconard96 committed
441
      $job      = new $this->fields['itemtype']();
moyooo's avatar
moyooo committed
442

443
444
445
      if (!$job->getFromDB($this->fields['items_id'])) {
         return;
      }
moyooo's avatar
moyooo committed
446

Thierry Bugier's avatar
Thierry Bugier committed
447
448
449
450
451
452
453
454
455
456
457
      // Add screenshots if needed, without notification
      $this->input = $this->addFiles($this->input, [
         'force_update' => true,
         'name'          => 'content',
         'content_field' => 'content',
      ]);

      // Add documents if needed, without notification
      $this->input = $this->addFiles($this->input, [
         'force_update' => true,
      ]);
yllen's avatar
yllen committed
458

459
460
461
462
463
464
465
      //Get user_id when not logged (from mailgate)
      $uid = Session::getLoginUserID();
      if ($uid === false) {
         if (isset($this->fields['users_id_editor'])) {
            $uid = $this->fields['users_id_editor'];
         } else {
            $uid = $this->fields['users_id'];
cconard96's avatar
cconard96 committed
466
         }
467
468
469
470
471
472
473
474
475
      }
      $job->updateDateMod($this->fields['items_id'], false, $uid);

      if (count($this->updates)) {
         if (!isset($this->input['_disablenotif'])
             && $CFG_GLPI["use_notifications"]
             && (in_array("content", $this->updates)
                 || isset($this->input['_need_send_mail']))) {
            //FIXME: _need_send_mail does not seems to be used
476

477
478
479
480
            $options = ['followup_id' => $this->fields["id"],
                             'is_private'  => $this->fields['is_private']];

            NotificationEvent::raiseEvent("update_followup", $job, $options);
cconard96's avatar
cconard96 committed
481
         }
482
      }
483

484
485
486
487
488
489
490
491
492
      // change ITIL Object status (from splitted button)
      if (isset($this->input['_status'])
          && ($this->input['_status'] != $this->input['_job']->fields['status'])) {
          $update = [
             'status'        => $this->input['_status'],
             'id'            => $this->input['_job']->fields['id'],
             '_disablenotif' => true,
          ];
          $this->input['_job']->update($update);
493
      }
494
495
496
497
498
499
500
501
502

      // Add log entry in the ITIL Object
      $changes = [
         0,
         '',
         $this->fields['id'],
      ];
      Log::history($this->getField('items_id'), $this->fields['itemtype'], $changes, $this->getType(),
                   Log::HISTORY_UPDATE_SUBITEM);
cconard96's avatar
cconard96 committed
503
   }
504

remi's avatar
remi committed
505

cconard96's avatar
cconard96 committed
506
   function post_getFromDB() {
Cédric Anne's avatar
Cédric Anne committed
507

cconard96's avatar
cconard96 committed
508
509
510
511
512
      $this->item = new $this->fields['itemtype'];
      $this->item->getFromDB($this->fields['items_id']);
   }


513
   protected function computeFriendlyName() {
yllen's avatar
yllen committed
514

515
516
517
      if (isset($this->fields['requesttypes_id'])) {
         if ($this->fields['requesttypes_id']) {
            return Dropdown::getDropdownName('glpi_requesttypes', $this->fields['requesttypes_id']);
moyooo's avatar
moyooo committed
518
         }
yllen's avatar
yllen committed
519
         return $this->getTypeName();
moyooo's avatar
moyooo committed
520
      }
521
      return '';
remi's avatar
remi committed
522
523
   }

yllen's avatar
yllen committed
524

cconard96's avatar
cconard96 committed
525
   function rawSearchOptions() {
Cédric Anne's avatar
Cédric Anne committed
526

cconard96's avatar
cconard96 committed
527
      $tab = [];
remi's avatar
remi committed
528

cconard96's avatar
cconard96 committed
529
530
531
532
      $tab[] = [
         'id'                 => 'common',
         'name'               => __('Characteristics')
      ];
533

cconard96's avatar
cconard96 committed
534
535
536
537
538
539
540
      $tab[] = [
         'id'                 => '1',
         'table'              => $this->getTable(),
         'field'              => 'content',
         'name'               => __('Description'),
         'datatype'           => 'text'
      ];
remi's avatar
remi committed
541

cconard96's avatar
cconard96 committed
542
543
544
545
      $tab[] = [
         'id'                 => '2',
         'table'              => 'glpi_requesttypes',
         'field'              => 'name',
546
         'name'               => RequestType::getTypeName(1),
cconard96's avatar
cconard96 committed
547
548
549
         'forcegroupby'       => true,
         'datatype'           => 'dropdown'
      ];
remi's avatar
remi committed
550

cconard96's avatar
cconard96 committed
551
552
553
554
      $tab[] = [
         'id'                 => '3',
         'table'              => $this->getTable(),
         'field'              => 'date',
555
         'name'               => _n('Date', 'Dates', 1),
cconard96's avatar
cconard96 committed
556
557
         'datatype'           => 'datetime'
      ];
remi's avatar
remi committed
558

cconard96's avatar
cconard96 committed
559
560
561
562
563
564
565
566
567
568
569
570
      $tab[] = [
         'id'                 => '4',
         'table'              => $this->getTable(),
         'field'              => 'is_private',
         'name'               => __('Private'),
         'datatype'           => 'bool'
      ];

      $tab[] = [
         'id'                 => '5',
         'table'              => 'glpi_users',
         'field'              => 'name',
571
         'name'               => User::getTypeName(1),
cconard96's avatar
cconard96 committed
572
573
574
575
         'datatype'           => 'dropdown',
         'right'              => 'all'
      ];

576
577
578
579
      $tab[] = [
         'id'                 => '6',
         'table'              => $this->getTable(),
         'field'              => 'itemtype',
580
         'name'               => RequestType::getTypeName(1),
581
582
583
         'datatype'           => 'dropdown'
      ];

cconard96's avatar
cconard96 committed
584
      return $tab;
remi's avatar
remi committed
585
   }
586

yllen's avatar
yllen committed
587

cconard96's avatar
cconard96 committed
588
   static function rawSearchOptionsToAdd($itemtype = null) {
Cédric Anne's avatar
Cédric Anne committed
589

cconard96's avatar
cconard96 committed
590
      $tab = [];
Cédric Anne's avatar
Cédric Anne committed
591

cconard96's avatar
cconard96 committed
592
593
594
595
      $tab[] = [
         'id'                 => 'followup',
         'name'               => _n('Followup', 'Followups', Session::getPluralNumber())
      ];
remi's avatar
remi committed
596

cconard96's avatar
cconard96 committed
597
598
599
600
601
      $followup_condition = '';
      if (!Session::haveRight('followup', self::SEEPRIVATE)) {
         $followup_condition = "AND (`NEWTABLE`.`is_private` = 0
                                     OR `NEWTABLE`.`users_id` = '".Session::getLoginUserID()."')";
      }
remi's avatar
remi committed
602

cconard96's avatar
cconard96 committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
      $tab[] = [
         'id'                 => '25',
         'table'              => static::getTable(),
         'field'              => 'content',
         'name'               => __('Description'),
         'forcegroupby'       => true,
         'splititems'         => true,
         'massiveaction'      => false,
         'joinparams'         => [
            'jointype'           => 'itemtype_item',
            'condition'          => $followup_condition
         ],
         'datatype'           => 'text',
         'htmltext'           => true
      ];
remi's avatar
remi committed
618

cconard96's avatar
cconard96 committed
619
620
621
622
      $tab[] = [
         'id'                 => '36',
         'table'              => static::getTable(),
         'field'              => 'date',
623
         'name'               => _n('Date', 'Dates', 1),
cconard96's avatar
cconard96 committed
624
625
626
627
628
629
630
631
         'datatype'           => 'datetime',
         'massiveaction'      => false,
         'forcegroupby'       => true,
         'joinparams'         => [
            'jointype'           => 'itemtype_item',
            'condition'          => $followup_condition
         ]
      ];
remi's avatar
remi committed
632

cconard96's avatar
cconard96 committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
      $tab[] = [
         'id'                 => '27',
         'table'              => static::getTable(),
         'field'              => 'id',
         'name'               => _x('quantity', 'Number of followups'),
         'forcegroupby'       => true,
         'usehaving'          => true,
         'datatype'           => 'count',
         'massiveaction'      => false,
         'joinparams'         => [
            'jointype'           => 'itemtype_item',
            'condition'          =>$followup_condition
         ]
      ];
yllen's avatar
yllen committed
647

cconard96's avatar
cconard96 committed
648
649
650
651
      $tab[] = [
         'id'                 => '29',
         'table'              => 'glpi_requesttypes',
         'field'              => 'name',
652
         'name'               => RequestType::getTypeName(1),
cconard96's avatar
cconard96 committed
653
654
655
656
657
658
659
660
661
662
663
664
665
         'datatype'           => 'dropdown',
         'forcegroupby'       => true,
         'massiveaction'      => false,
         'joinparams'         => [
            'beforejoin'         => [
               'table'              => static::getTable(),
               'joinparams'         => [
                  'jointype'           => 'itemtype_item',
                  'condition'          => $followup_condition
               ]
            ]
         ]
      ];
webmyster's avatar
webmyster committed
666

cconard96's avatar
cconard96 committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
      $tab[] = [
         'id'                 => '91',
         'table'              => static::getTable(),
         'field'              => 'is_private',
         'name'               => __('Private followup'),
         'datatype'           => 'bool',
         'forcegroupby'       => true,
         'splititems'         => true,
         'massiveaction'      => false,
         'joinparams'         => [
            'jointype'           => 'itemtype_item',
            'condition'          => $followup_condition
         ]
      ];
681

cconard96's avatar
cconard96 committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
      $tab[] = [
         'id'                 => '93',
         'table'              => 'glpi_users',
         'field'              => 'name',
         'name'               => __('Writer'),
         'datatype'           => 'itemlink',
         'right'              => 'all',
         'forcegroupby'       => true,
         'massiveaction'      => false,
         'joinparams'         => [
            'beforejoin'         => [
               'table'              => static::getTable(),
               'joinparams'         => [
                  'jointype'           => 'itemtype_item',
                  'condition'          => $followup_condition
               ]
            ]
         ]
      ];

      return $tab;
webmyster's avatar
webmyster committed
703
704
705
   }


Cédric Anne's avatar
Cédric Anne committed
706
707
   /**
    * form for soluce's approbation
webmyster's avatar
webmyster committed
708
    *
Cédric Anne's avatar
Cédric Anne committed
709
710
    * @param CommonITILObject $itilobject
    */
cconard96's avatar
cconard96 committed
711
   function showApprobationForm($itilobject) {
webmyster's avatar
webmyster committed
712

Cédric Anne's avatar
Cédric Anne committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
      if (($itilobject->fields["status"] == CommonITILObject::SOLVED)
          && $itilobject->canApprove()
          && $itilobject->isAllowedStatus($itilobject->fields['status'], CommonITILObject::CLOSED)) {
         echo "<form name='form' method='post' action='".$this->getFormURL()."'>";
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr><th colspan='4'>". __('Approval of the solution')."</th></tr>";

         echo "<tr class='tab_bg_1'>";
         echo "<td colspan='2'>".__('Comments')."<br>(".__('Optional when approved').")</td>";
         echo "<td class='center middle' colspan='2'>";
         echo "<textarea name='content' cols='70' rows='6'></textarea>";
         echo "<input type='hidden' name='itemtype' value='".$itilobject->getType()."'>";
         echo "<input type='hidden' name='items_id' value='".$itilobject->getField('id')."'>";
         echo "<input type='hidden' name='requesttypes_id' value='".
                RequestType::getDefault('followup')."'>";
         echo "</td></tr>\n";

         echo "<tr class='tab_bg_2'>";
         echo "<td class='tab_bg_2 center' colspan='2' width='200'>\n";
         echo "<input type='submit' name='add_reopen' value=\"".__('Refuse the solution')."\"
                class='submit'>";
         echo "</td>\n";
         echo "<td class='tab_bg_2 center' colspan='2'>\n";
         echo "<input type='submit' name='add_close' value=\"".__('Approve the solution')."\"
                class='submit'>";
         echo "</td></tr>\n";
         echo "</table>";
         Html::closeForm();
webmyster's avatar
webmyster committed
741
      }
Cédric Anne's avatar
Cédric Anne committed
742

cconard96's avatar
cconard96 committed
743
744
745
746
747
748
      return true;
   }


   static function getFormURL($full = true) {
      return Toolbox::getItemTypeFormURL("ITILFollowup", $full);
webmyster's avatar
webmyster committed
749
750
751
   }


752
753
   /** form for Followup
    *
yllen's avatar
yllen committed
754
755
    *@param $ID      integer : Id of the followup
    *@param $options array of possible options:
cconard96's avatar
cconard96 committed
756
    *     - item Object : the ITILObject parent
yllen's avatar
yllen committed
757
   **/
758
   function showForm($ID, $options = []) {
759
      global $CFG_GLPI;
760

cconard96's avatar
cconard96 committed
761
762
763
764
765
766
767
      if ($this->isNewItem()) {
         $this->getEmpty();
      }

      if (!isset($options['item']) && isset($options['parent'])) {
         //when we came from aja/viewsubitem.php
         $options['item'] = $options['parent'];
768
      }
cconard96's avatar
cconard96 committed
769
      $options['formoptions'] = ($options['formoptions'] ?? '') . ' data-track-changes=true';
yllen's avatar
yllen committed
770

cconard96's avatar
cconard96 committed
771
772
773
      $item = $options['item'];
      $this->item = $item;

774
      if ($ID > 0) {
moyooo's avatar
moyooo committed
775
         $this->check($ID, READ);
776
777
      } else {
         // Create item
cconard96's avatar
cconard96 committed
778
779
         $options['itemtype'] = $item->getType();
         $options['items_id'] = $item->getField('id');
moyooo's avatar
moyooo committed
780
         $this->check(-1, CREATE, $options);
781
      }
yllen's avatar
yllen committed
782
      $tech = (Session::haveRight(self::$rightname, self::ADDALLTICKET)
cconard96's avatar
cconard96 committed
783
               || $item->isUser(CommonITILActor::ASSIGN, Session::getLoginUserID())
yllen's avatar
yllen committed
784
               || (isset($_SESSION["glpigroups"])
cconard96's avatar
cconard96 committed
785
                   && $item->haveAGroup(CommonITILActor::ASSIGN, $_SESSION['glpigroups'])));
786

cconard96's avatar
cconard96 committed
787
      $requester = ($item->isUser(CommonITILActor::REQUESTER, Session::getLoginUserID())
788
                    || (isset($_SESSION["glpigroups"])
cconard96's avatar
cconard96 committed
789
                        && $item->haveAGroup(CommonITILActor::REQUESTER, $_SESSION['glpigroups'])));
790

791
      $reopen_case = false;
792
      if ($this->isNewID($ID)) {
cconard96's avatar
cconard96 committed
793
         if ($item->canReopen()) {
794
            $reopen_case = true;
795
796
            echo "<div class='center b'>".__('If you want to reopen the ticket, you must specify a reason')."</div>";
         }
797
798
799

         // the reqester triggers the reopening on close/solve/waiting status
         if ($requester
cconard96's avatar
cconard96 committed
800
             && in_array($item->fields['status'], $item::getReopenableStatusArray())) {
801
802
            $reopen_case = true;
         }
803
804
      }

805
806
      $cols    = 100;
      $rows    = 10;
807

808
      if ($tech) {
809
         $this->showFormHeader($options);
810

811
         $rand       = mt_rand();
812
813
         $content_id = "content$rand";

814
         echo "<tr class='tab_bg_1'>";
815
         echo "<td rowspan='3'>";
816
817
818

         Html::textarea(['name'              => 'content',
                         'value'             => $this->fields["content"],
819
                         'rand'              => $rand,
820
821
                         'editor_id'         => $content_id,
                         'enable_fileupload' => true,
822
                         'enable_richtext'   => true,
823
824
825
                         'cols'              => $cols,
                         'rows'              => $rows]);

826
         if ($this->fields["date"]) {
827
            echo "</td><td>"._n('Date', 'Dates', 1)."</td>";
yllen's avatar
yllen committed
828
            echo "<td>".Html::convDateTime($this->fields["date"]);
yllen's avatar
CS    
yllen committed
829
         } else {
830

yllen's avatar
yllen committed
831
            echo "</td><td colspan='2'>&nbsp;";
832
         }
cconard96's avatar
cconard96 committed
833
834
         echo Html::hidden('itemtype', ['value' => $item->getType()]);
         echo Html::hidden('items_id', ['value' => $item->getID()]);
835
         // Reopen case
836
         if ($reopen_case) {
837
838
            echo "<input type='hidden' name='add_reopen' value='1'>";
         }
yllen's avatar
yllen committed
839

840
841
         echo "</td></tr>\n";

842
843
844
         echo "<tr class='tab_bg_1'></tr>";
         echo "<tr class='tab_bg_1' style='vertical-align: top'>";
         echo "<td colspan='4'>";
845
846
         echo "<div class='fa-label'>
            <i class='fas fa-reply fa-fw'
847
               title='"._n('Followup template', 'Followup templates', Session::getPluralNumber())."'></i>";
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
         $this->fields['itilfollowuptemplates_id'] = 0;
         ITILFollowupTemplate::dropdown([
            'value'     => $this->fields['itilfollowuptemplates_id'],
            'entity'    => $this->getEntityID(),
            'on_change' => "itilfollowuptemplate_update$rand(this.value)"
         ]);
         echo "</div>";

         $ajax_url = $CFG_GLPI["root_doc"]."/ajax/itilfollowup.php";
         $JS = <<<JAVASCRIPT
            function itilfollowuptemplate_update{$rand}(value) {
               $.ajax({
                  url: '{$ajax_url}',
                  type: 'POST',
                  data: {
                     itilfollowuptemplates_id: value
                  }
               }).done(function(data) {
                  var requesttypes_id = isNaN(parseInt(data.requesttypes_id))
                     ? 0
                     : parseInt(data.requesttypes_id);

                  // set textarea content
                  if (tasktinymce = tinymce.get("{$content_id}")) {
                     tasktinymce.setContent(data.content);
                  }
                  // set category
                  $("#dropdown_requesttypes_id{$rand}").trigger("setValue", requesttypes_id);
                  // set is_private
                  $("#is_privateswitch{$rand}")
                     .prop("checked", data.is_private == "0"
                        ? false
                        : true);
               });
            }
JAVASCRIPT;
         echo Html::scriptBlock($JS);

886
887
888
         echo "<div class='fa-label'>
            <i class='fas fa-inbox fa-fw'
               title='".__('Source of followup')."'></i>";
889
890
         RequestType::dropdown([
            'value'     => $this->fields["requesttypes_id"],
891
892
            'condition' => ['is_active' => 1, 'is_itilfollowup' => 1],
            'rand'      => $rand,
893
         ]);
894
         echo "</div>";
895

896
897
898
899
900
901
902
903
904
905
906
907
908
909
         echo "<div class='fa-label'>
            <i class='fas fa-lock fa-fw' title='".__('Private')."'></i>";
         echo "<span class='switch pager_controls'>
            <label for='is_privateswitch$rand' title='".__('Private')."'>
               <input type='hidden' name='is_private' value='0'>
               <input type='checkbox' id='is_privateswitch$rand' name='is_private' value='1'".
                     ($this->fields["is_private"]
                        ? "checked='checked'"
                        : "")."
               >
               <span class='lever'></span>
            </label>
         </span>";
         echo "</div></td></tr>";
910

911
         $this->showFormButtons($options);
yllen's avatar
yllen committed
912

913
      } else {
yllen's avatar
yllen committed
914
         $options['colspan'] = 1;
915

916
         $this->showFormHeader($options);
917

918
919
920
         $rand = mt_rand();
         $rand_text = mt_rand();
         $content_id = "content$rand";
921
         echo "<tr class='tab_bg_1'>";
moyooo's avatar
moyooo committed
922
         echo "<td class='middle right'>".__('Description')."</td>";
yllen's avatar
yllen committed
923
         echo "<td class='center middle'>";
924
925
926
927
928
929

         Html::textarea(['name'              => 'content',
                         'value'             => $this->fields["content"],
                         'rand'              => $rand_text,
                         'editor_id'         => $content_id,
                         'enable_fileupload' => true,
930
                         'enable_richtext'   => true,
931
932
933
                         'cols'              => $cols,
                         'rows'              => $rows]);

cconard96's avatar
cconard96 committed
934
935
936
         echo Html::hidden('itemtype', ['value' => $item->getType()]);
         echo Html::hidden('items_id', ['value' => $item->getID()]);
         echo Html::hidden('requesttypes_id', ['value' => RequestType::getDefault('followup')]);
937
         // Reopen case
938
         if ($reopen_case) {
939
940
            echo "<input type='hidden' name='add_reopen' value='1'>";
         }
yllen's avatar
yllen committed
941

942
         echo "</td></tr>\n";
943

944
         $this->showFormButtons($options);
945
      }
946
947
948
      return true;
   }

yllen's avatar
yllen committed
949

950
   function showFormButtons($options = []) {
951
952
953
954
955
956
957

      // for single object like config
      $ID = 1;
      if (isset($this->fields['id'])) {
         $ID = $this->fields['id'];
      }

Cédric Anne's avatar
Cédric Anne committed
958
959
960
961
962
      $params = [
         'colspan'  => 2,
         'candel'   => true,
         'canedit'  => true,
      ];
963
964
965
966
967
968
969
970
971
972

      if (is_array($options) && count($options)) {
         foreach ($options as $key => $val) {
            $params[$key] = $val;
         }
      }

      if (!$this->isNewID($ID)) {
         echo "<input type='hidden' name='id' value='$ID'>";
      }
yllen's avatar
yllen committed
973

974
975
      Plugin::doHook("post_item_form", ['item' => $this, 'options' => &$params]);

976
977
978
979
      echo "<tr class='tab_bg_2'>";
      echo "<td class='center' colspan='".($params['colspan']*2)."'>";

      if ($this->isNewID($ID)) {
cconard96's avatar
cconard96 committed
980
         echo $params['item']::getSplittedSubmitButtonHtml($this->fields['items_id'], 'add');
981
982
983
984
985
986
987
988
      } else {
         if ($params['candel']
             && !$this->can($ID, DELETE)
             && !$this->can($ID, PURGE)) {
            $params['candel'] = false;
         }

         if ($params['canedit'] && $this->can($ID, UPDATE)) {
cconard96's avatar
cconard96 committed
989
            echo $params['item']::getSplittedSubmitButtonHtml($this->fields['items_id'], 'update');
990
991
992
993
994
995
            echo "</td></tr><tr class='tab_bg_2'>\n";
         }

         if ($params['candel']) {
            echo "<td class='right' colspan='".($params['colspan']*2)."' >\n";
            if ($this->can($ID, PURGE)) {
996
               echo Html::submit(_x('button', 'Delete permanently'),
997
998
                                 ['name'    => 'purge',
                                       'confirm' => __('Confirm the final deletion?')]);
yllen's avatar
yllen committed
999
            }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
         }

         if ($this->isField('date_mod')) {
            echo "<input type='hidden' name='_read_date_mod' value='".$this->getField('date_mod')."'>";
         }
      }

      echo "</td></tr></table></div>";
      Html::closeForm();
   }

1011

1012
   /**
cconard96's avatar
cconard96 committed
1013
1014
    * @param $ID  integer  ID of the ITILObject
    * @param $itemtype  string   parent itemtype
Cédric Anne's avatar
Cédric Anne committed
1015
1016
    *
    * @deprecated 9.5.6
cconard96's avatar
cconard96 committed
1017
1018
   **/
   static function showShortForITILObject($ID, $itemtype) {
Cédric Anne's avatar
Cédric Anne committed
1019
      Toolbox::deprecated();
Cédric Anne's avatar
Cédric Anne committed
1020

cconard96's avatar
cconard96 committed
1021
1022
1023
1024
1025
      global $DB, $CFG_GLPI;

      // Print Followups for a job
      $showprivate = Session::haveRight(self::$rightname, self::SEEPRIVATE);

Johan Cwiklinski's avatar
Johan Cwiklinski committed
1026
1027
1028
1029
      $where = [
         'itemtype'  => $itemtype,
         'items_id'  => $ID
      ];
cconard96's avatar
cconard96 committed
1030
      if (!$showprivate) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
1031
1032
1033
1034
         $where['OR'] = [
            'is_private'   => 0,
            'users_id'     => Session::getLoginUserID()
         ];
cconard96's avatar
cconard96 committed
1035
1036
      }

Johan Cwiklinski's avatar
Johan Cwiklinski committed
1037
1038
1039
1040
1041
1042
      // Get Followups
      $iterator = $DB->request([
         'FROM'   => 'glpi_itilfollowups',
         'WHERE'  => $where,
         'ORDER'  => 'date DESC'
      ]);
cconard96's avatar
cconard96 committed
1043
1044

      $out = "";
Johan Cwiklinski's avatar
Johan Cwiklinski committed
1045
      if (count($iterator)) {
cconard96's avatar
cconard96 committed
1046
         $out .= "<div class='center'><table class='tab_cadre' width='100%'>\n
1047
                  <tr><th>"._n('Date', 'Dates', 1)."</th><th>"._n('Requester', 'Requesters', 1)."</th>
cconard96's avatar
cconard96 committed
1048
1049
1050
1051
1052
1053
                  <th>".__('Description')."</th></tr>\n";

         $showuserlink = 0;
         if (Session::haveRight('user', READ)) {
            $showuserlink = 1;
         }
Johan Cwiklinski's avatar
Johan Cwiklinski committed
1054
         while ($data = $iterator->next()) {
cconard96's avatar
cconard96 committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
            $out .= "<tr class='tab_bg_3'>
                     <td class='center'>".Html::convDateTime($data["date"])."</td>
                     <td class='center'>".getUserName($data["users_id"], $showuserlink)."</td>
                     <td width='70%' class='b'>".Html::resume_text($data["content"],
                                                                   $CFG_GLPI["cut"])."
                     </td></tr>";
         }
         $out .= "</table></div>";
      }
      return $out;
   }


1068
   function getRights($interface = 'central') {
moyooo's avatar
moyooo committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

      $values = parent::getRights();
      unset($values[UPDATE], $values[CREATE], $values[READ]);

      if ($interface == 'central') {
         $values[self::UPDATEALL]      = __('Update all');
         $values[self::ADDALLTICKET]   = __('Add to all tickets');
         $values[self::SEEPRIVATE]     = __('See private ones');
      }

      $values[self::ADDGROUPTICKET]
1080
1081
                                 = ['short' => __('Add followup (associated groups)'),
                                         'long'  => __('Add a followup to tickets of associated groups')];
moyooo's avatar
moyooo committed
1082
      $values[self::UPDATEMY]    = __('Update followups (author)');
1083
1084
      $values[self::ADDMYTICKET] = ['short' => __('Add followup (requester)'),
                                         'long'  => __('Add a followup to tickets (requester)')];
moyooo's avatar
moyooo committed
1085
1086
1087
1088
1089
1090
1091
1092
      $values[self::SEEPUBLIC]   = __('See public ones');

      if ($interface == 'helpdesk') {
         unset($values[PURGE]);
      }

      return $values;
   }
1093
1094
1095