item_ticket.class.php 51 KB
Newer Older
moyooo's avatar
moyooo committed
1
<?php
2 3 4
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
5
 * Copyright (C) 2015-2018 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')) {
korial29's avatar
korial29 committed
34
   die("Sorry. You can't access directly to this file");
moyooo's avatar
moyooo committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
}

/**
 * Item_Ticket Class
 *
 *  Relation between Tickets and Items
**/
class Item_Ticket extends CommonDBRelation{


   // From CommonDBRelation
   static public $itemtype_1          = 'Ticket';
   static public $items_id_1          = 'tickets_id';

   static public $itemtype_2          = 'itemtype';
   static public $items_id_2          = 'items_id';
   static public $checkItem_2_Rights  = self::HAVE_VIEW_RIGHT_ON_ITEM;



   /**
Anael Mobilia's avatar
Anael Mobilia committed
56
    * @since 0.84
moyooo's avatar
moyooo committed
57 58 59 60 61 62 63 64
   **/
   function getForbiddenStandardMassiveAction() {

      $forbidden   = parent::getForbiddenStandardMassiveAction();
      $forbidden[] = 'update';
      return $forbidden;
   }

yllen's avatar
CS  
yllen committed
65 66

   /**
Anael Mobilia's avatar
Anael Mobilia committed
67
    * @since 0.85.5
yllen's avatar
CS  
yllen committed
68 69
    * @see CommonDBRelation::canCreateItem()
   **/
70
   function canCreateItem() {
yllen's avatar
CS  
yllen committed
71

72 73 74
      $ticket = new Ticket();
      // Not item linked for closed tickets
      if ($ticket->getFromDB($this->fields['tickets_id'])
75
          && in_array($ticket->fields['status'], $ticket->getClosedStatusArray())) {
76
         return false;
77
      }
yllen's avatar
CS  
yllen committed
78

79 80 81 82
      if ($ticket->canUpdateItem()) {
         return true;
      }

83 84
      return parent::canCreateItem();
   }
yllen's avatar
CS  
yllen committed
85 86


moyooo's avatar
moyooo committed
87 88 89
   function post_addItem() {

      $ticket = new Ticket();
90
      $input  = ['id'            => $this->fields['tickets_id'],
moyooo's avatar
moyooo committed
91
                      'date_mod'      => $_SESSION["glpi_currenttime"],
92
                      '_donotadddocs' => true];
moyooo's avatar
moyooo committed
93 94 95 96 97 98 99 100 101 102 103

      if (!isset($this->input['_do_notif']) || $this->input['_do_notif']) {
         $input['_forcenotif'] = true;
      }
      if (isset($this->input['_disablenotif']) && $this->input['_disablenotif']) {
         $input['_disablenotif'] = true;
      }

      $ticket->update($input);
      parent::post_addItem();
   }
yllen's avatar
CS  
yllen committed
104 105


moyooo's avatar
moyooo committed
106 107 108
   function post_purgeItem() {

      $ticket = new Ticket();
109
      $input = ['id'            => $this->fields['tickets_id'],
moyooo's avatar
moyooo committed
110
                     'date_mod'      => $_SESSION["glpi_currenttime"],
111
                     '_donotadddocs' => true];
moyooo's avatar
moyooo committed
112 113 114 115 116 117 118 119

      if (!isset($this->input['_do_notif']) || $this->input['_do_notif']) {
         $input['_forcenotif'] = true;
      }
      $ticket->update($input);

      parent::post_purgeItem();
   }
yllen's avatar
CS  
yllen committed
120 121


moyooo's avatar
moyooo committed
122 123 124 125
   /**
    * @see CommonDBTM::prepareInputForAdd()
   **/
   function prepareInputForAdd($input) {
yllen's avatar
CS  
yllen committed
126

moyooo's avatar
moyooo committed
127
      // Avoid duplicate entry
128 129 130
      if (countElementsInTable($this->getTable(), ['tickets_id' => $input['tickets_id'],
                                                   'itemtype'   => $input['itemtype'],
                                                   'items_id'   => $input['items_id']]) > 0) {
moyooo's avatar
moyooo committed
131
         return false;
yllen's avatar
CS  
yllen committed
132 133
      }

moyooo's avatar
moyooo committed
134 135
      $ticket = new Ticket();
      $ticket->getFromDB($input['tickets_id']);
yllen's avatar
CS  
yllen committed
136

moyooo's avatar
moyooo committed
137 138 139 140 141 142 143
      // Get item location if location is not already set in ticket
      if (empty($ticket->fields['locations_id'])) {
         if (($input["items_id"] > 0) && !empty($input["itemtype"])) {
            if ($item = getItemForItemtype($input["itemtype"])) {
               if ($item->getFromDB($input["items_id"])) {
                  if ($item->isField('locations_id')) {
                     $ticket->fields['items_locations'] = $item->fields['locations_id'];
yllen's avatar
CS  
yllen committed
144

moyooo's avatar
moyooo committed
145 146 147 148 149
                     // Process Business Rules
                     $rules = new RuleTicketCollection($ticket->fields['entities_id']);

                     $ticket->fields = $rules->processAllRules(Toolbox::stripslashes_deep($ticket->fields),
                                                Toolbox::stripslashes_deep($ticket->fields),
150
                                                ['recursive' => true]);
yllen's avatar
CS  
yllen committed
151

moyooo's avatar
moyooo committed
152
                     unset($ticket->fields['items_locations']);
153
                     $ticket->updateInDB(['locations_id']);
moyooo's avatar
moyooo committed
154 155 156 157 158 159 160 161 162
                  }
               }
            }
         }
      }

      return parent::prepareInputForAdd($input);
   }

korial29's avatar
korial29 committed
163

korial29's avatar
korial29 committed
164 165 166 167 168 169 170 171 172 173 174
   /**
    * Print the HTML ajax associated item add
    *
    * @param $ticket Ticket object
    * @param $options   array of possible options:
    *    - id                  : ID of the ticket
    *    - _users_id_requester : ID of the requester user
    *    - items_id            : array of elements (itemtype => array(id1, id2, id3, ...))
    *
    * @return Nothing (display)
   **/
175
   static function itemAddForm(Ticket $ticket, $options = []) {
korial29's avatar
korial29 committed
176
      global $CFG_GLPI;
korial29's avatar
korial29 committed
177

178
      $params = ['id'                  => (isset($ticket->fields['id'])
179 180 181
                                                && $ticket->fields['id'] != '')
                                                   ? $ticket->fields['id']
                                                   : 0,
korial29's avatar
korial29 committed
182
                      '_users_id_requester' => 0,
183
                      'items_id'            => [],
184
                      'itemtype'            => '',
185
                      '_canupdate'          => false];
korial29's avatar
korial29 committed
186

187
      $opt = [];
korial29's avatar
korial29 committed
188

korial29's avatar
korial29 committed
189 190 191 192 193
      foreach ($options as $key => $val) {
         if (!empty($val)) {
            $params[$key] = $val;
         }
      }
moyooo's avatar
moyooo committed
194

korial29's avatar
korial29 committed
195 196 197
      if (!$ticket->can($params['id'], READ)) {
         return false;
      }
korial29's avatar
korial29 committed
198

199 200
      $canedit = ($ticket->can($params['id'], UPDATE)
                  && $params['_canupdate']);
korial29's avatar
korial29 committed
201

korial29's avatar
korial29 committed
202 203 204 205 206
      // Ticket update case
      if ($params['id'] > 0) {
         // Get requester
         $class        = new $ticket->userlinkclass();
         $tickets_user = $class->getActors($params['id']);
207 208
         if (isset($tickets_user[CommonITILActor::REQUESTER])
             && (count($tickets_user[CommonITILActor::REQUESTER]) == 1)) {
korial29's avatar
korial29 committed
209 210 211 212
            foreach ($tickets_user[CommonITILActor::REQUESTER] as $user_id_single) {
               $params['_users_id_requester'] = $user_id_single['users_id'];
            }
         }
korial29's avatar
korial29 committed
213

korial29's avatar
korial29 committed
214 215
         // Get associated elements for ticket
         $used = self::getUsedItems($params['id']);
216
         $usedcount = 0;
korial29's avatar
korial29 committed
217 218
         foreach ($used as $itemtype => $items) {
            foreach ($items as $items_id) {
219 220
               if (!isset($params['items_id'][$itemtype])
                   || !in_array($items_id, $params['items_id'][$itemtype])) {
korial29's avatar
korial29 committed
221 222
                  $params['items_id'][$itemtype][] = $items_id;
               }
223
               ++$usedcount;
korial29's avatar
korial29 committed
224 225
            }
         }
korial29's avatar
korial29 committed
226 227
      }

korial29's avatar
korial29 committed
228 229 230 231
      // Get ticket template
      $tt = new TicketTemplate();
      if (isset($options['_tickettemplate'])) {
         $tt                  = $options['_tickettemplate'];
232 233 234
         if (isset($tt->fields['id'])) {
            $opt['templates_id'] = $tt->fields['id'];
         }
yllen's avatar
CS  
yllen committed
235
      } else if (isset($options['templates_id'])) {
korial29's avatar
korial29 committed
236
         $tt->getFromDBWithDatas($options['templates_id']);
237 238 239
         if (isset($tt->fields['id'])) {
            $opt['templates_id'] = $tt->fields['id'];
         }
korial29's avatar
korial29 committed
240 241 242 243
      }

      $rand  = mt_rand();
      $count = 0;
korial29's avatar
korial29 committed
244

korial29's avatar
korial29 committed
245 246 247 248 249
      echo "<div id='itemAddForm$rand'>";

      // Show associated item dropdowns
      if ($canedit) {
         echo "<div style='float:left'>";
250
         $p = ['used'       => $params['items_id'],
korial29's avatar
korial29 committed
251
                    'rand'       => $rand,
252
                    'tickets_id' => $params['id']];
korial29's avatar
korial29 committed
253 254 255 256 257 258
         // My items
         if ($params['_users_id_requester'] > 0) {
            Item_Ticket::dropdownMyDevices($params['_users_id_requester'], $ticket->fields["entities_id"], $params['itemtype'], 0, $p);
         }
         // Global search
         Item_Ticket::dropdownAllDevices("itemtype", $params['itemtype'], 0, 1, $params['_users_id_requester'], $ticket->fields["entities_id"], $p);
259
         echo "<span id='item_ticket_selection_information$rand'></span>";
korial29's avatar
korial29 committed
260
         echo "</div>";
korial29's avatar
korial29 committed
261

korial29's avatar
korial29 committed
262 263 264 265 266 267
         // Add button
         echo "<a href='javascript:itemAction$rand(\"add\");' class='vsubmit' style='float:left'>"._sx('button', 'Add')."</a>";
      }

      // Display list
      echo "<div style='clear:both;'>";
korial29's avatar
korial29 committed
268

korial29's avatar
korial29 committed
269 270
      if (!empty($params['items_id'])) {
         // No delete if mandatory and only one item
271
         $delete = $ticket->canAddItem(__CLASS__);
korial29's avatar
korial29 committed
272 273
         $cpt = 0;
         foreach ($params['items_id'] as $itemtype => $items) {
274
            $cpt += count($items);
korial29's avatar
korial29 committed
275 276 277 278 279 280 281 282
         }

         if ($cpt == 1 && isset($tt->mandatory['items_id'])) {
            $delete = false;
         }
         foreach ($params['items_id'] as $itemtype => $items) {
            foreach ($items as $items_id) {
               $count++;
283 284 285 286 287 288 289 290 291 292
               echo self::showItemToAdd(
                  $params['id'],
                  $itemtype,
                  $items_id,
                  [
                     'rand'      => $rand,
                     'delete'    => $delete,
                     'visible'   => ($count <= 5)
                  ]
               );
korial29's avatar
korial29 committed
293 294
            }
         }
korial29's avatar
korial29 committed
295
      }
korial29's avatar
korial29 committed
296 297 298 299

      if ($count == 0) {
         echo "<input type='hidden' value='0' name='items_id'>";
      }
korial29's avatar
korial29 committed
300

301 302 303 304 305
      if ($params['id'] > 0 && $usedcount != $count) {
         $count_notsaved = $count - $usedcount;
         echo "<i>" . sprintf(_n('%1$s item not saved', '%1$s items not saved', $count_notsaved), $count_notsaved)  . "</i>";
      }
      if ($params['id'] > 0 && $usedcount > 5) {
306
         echo "<i><a href='".$ticket->getFormURLWithID($params['id'])."&amp;forcetab=Item_Ticket$1'>"
307
                  .__('Display all items')." (".$usedcount.")</a></i>";
korial29's avatar
korial29 committed
308 309 310
      }
      echo "</div>";

311
      foreach (['id', '_users_id_requester', 'items_id', 'itemtype', '_canupdate'] as $key) {
korial29's avatar
korial29 committed
312 313 314 315 316 317 318
         $opt[$key] = $params[$key];
      }

      $js  = " function itemAction$rand(action, itemtype, items_id) {";
      $js .= "    $.ajax({
                     url: '".$CFG_GLPI['root_doc']."/ajax/itemTicket.php',
                     dataType: 'html',
korial29's avatar
korial29 committed
319
                     data: {'action'     : action,
korial29's avatar
korial29 committed
320 321 322
                            'rand'       : $rand,
                            'params'     : ".json_encode($opt).",
                            'my_items'   : $('#dropdown_my_items$rand').val(),
korial29's avatar
korial29 committed
323
                            'itemtype'   : (itemtype === undefined) ? $('#dropdown_itemtype$rand').val() : itemtype,
korial29's avatar
korial29 committed
324
                            'items_id'   : (items_id === undefined) ? $('#dropdown_add_items_id$rand').val() : items_id},
korial29's avatar
korial29 committed
325
                     success: function(response) {";
326
      $js .= "          $(\"#itemAddForm$rand\").replaceWith(response);";
korial29's avatar
korial29 committed
327 328 329 330 331 332
      $js .= "       }";
      $js .= "    });";
      $js .= " }";
      echo Html::scriptBlock($js);
      echo "</div>";
   }
korial29's avatar
korial29 committed
333 334


korial29's avatar
korial29 committed
335
   static function showItemToAdd($tickets_id, $itemtype, $items_id, $options) {
336
      global $CFG_GLPI;
korial29's avatar
korial29 committed
337

338 339 340 341 342
      $params = [
         'rand'      => mt_rand(),
         'delete'    => true,
         'visible'   => true
      ];
korial29's avatar
korial29 committed
343

Anael Mobilia's avatar
Anael Mobilia committed
344
      foreach ($options as $key => $val) {
korial29's avatar
korial29 committed
345 346
         $params[$key] = $val;
      }
korial29's avatar
korial29 committed
347

348 349 350
      $result = "";

      if ($item = getItemForItemtype($itemtype)) {
351 352 353
         if ($params['visible']) {
            $item->getFromDB($items_id);
            $result =  "<div id='{$itemtype}_$items_id'>";
354
            $result .= $item->getTypeName(1)." : ".$item->getLink(['comments' => true]);
355 356
            $result .= Html::hidden("items_id[$itemtype][$items_id]", ['value' => $items_id]);
            if ($params['delete']) {
Johan Cwiklinski's avatar
Fix CS  
Johan Cwiklinski committed
357
               $result .= " <span class='fa fa-times-circle pointer' onclick=\"itemAction".$params['rand']."('delete', '$itemtype', '$items_id');\"></span>";
358 359 360 361
            }
            $result .= "</div>";
         } else {
            $result .= Html::hidden("items_id[$itemtype][$items_id]", ['value' => $items_id]);
362
         }
korial29's avatar
korial29 committed
363
      }
korial29's avatar
korial29 committed
364

korial29's avatar
korial29 committed
365 366
      return $result;
   }
moyooo's avatar
moyooo committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

   /**
    * Print the HTML array for Items linked to a ticket
    *
    * @param $ticket Ticket object
    *
    * @return Nothing (display)
   **/
   static function showForTicket(Ticket $ticket) {
      global $DB, $CFG_GLPI;

      $instID = $ticket->fields['id'];

      if (!$ticket->can($instID, READ)) {
         return false;
      }

384
      $canedit = $ticket->canAddItem($instID);
moyooo's avatar
moyooo committed
385 386
      $rand    = mt_rand();

387 388
      $types_iterator = self::getDistinctTypes($instID);
      $number = count($types_iterator);
moyooo's avatar
moyooo committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

      if ($canedit) {
         echo "<div class='firstbloc'>";
         echo "<form name='ticketitem_form$rand' id='ticketitem_form$rand' method='post'
                action='".Toolbox::getItemTypeFormURL(__CLASS__)."'>";

         echo "<table class='tab_cadre_fixe'>";
         echo "<tr class='tab_bg_2'><th colspan='2'>".__('Add an item')."</th></tr>";

         echo "<tr class='tab_bg_1'><td>";
         // Select hardware on creation or if have update right
         $class        = new $ticket->userlinkclass();
         $tickets_user = $class->getActors($instID);
         $dev_user_id = 0;
         if (isset($tickets_user[CommonITILActor::REQUESTER])
                 && (count($tickets_user[CommonITILActor::REQUESTER]) == 1)) {
            foreach ($tickets_user[CommonITILActor::REQUESTER] as $user_id_single) {
               $dev_user_id = $user_id_single['users_id'];
            }
         }

         if ($dev_user_id > 0) {
411
            self::dropdownMyDevices($dev_user_id, $ticket->fields["entities_id"], null, 0, ['tickets_id' => $instID]);
moyooo's avatar
moyooo committed
412
         }
yllen's avatar
CS  
yllen committed
413

414
         $used = self::getUsedItems($instID);
415 416
         self::dropdownAllDevices("itemtype", null, 0, 1, $dev_user_id, $ticket->fields["entities_id"], ['tickets_id' => $instID, 'used' => $used, 'rand' => $rand]);
         echo "<span id='item_ticket_selection_information$rand'></span>";
moyooo's avatar
moyooo committed
417 418 419 420 421 422 423 424 425 426 427 428
         echo "</td><td class='center' width='30%'>";
         echo "<input type='submit' name='add' value=\""._sx('button', 'Add')."\" class='submit'>";
         echo "<input type='hidden' name='tickets_id' value='$instID'>";
         echo "</td></tr>";
         echo "</table>";
         Html::closeForm();
         echo "</div>";
      }

      echo "<div class='spaced'>";
      if ($canedit && $number) {
         Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
429
         $massiveactionparams = ['container' => 'mass'.__CLASS__.$rand];
moyooo's avatar
moyooo committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
         Html::showMassiveActions($massiveactionparams);
      }
      echo "<table class='tab_cadre_fixehov'>";
      $header_begin  = "<tr>";
      $header_top    = '';
      $header_bottom = '';
      $header_end    = '';
      if ($canedit && $number) {
         $header_top    .= "<th width='10'>".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
         $header_top    .= "</th>";
         $header_bottom .= "<th width='10'>".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand);
         $header_bottom .= "</th>";
      }
      $header_end .= "<th>".__('Type')."</th>";
      $header_end .= "<th>".__('Entity')."</th>";
      $header_end .= "<th>".__('Name')."</th>";
      $header_end .= "<th>".__('Serial number')."</th>";
447 448
      $header_end .= "<th>".__('Inventory number')."</th>";
      echo "<tr>";
moyooo's avatar
moyooo committed
449 450 451
      echo $header_begin.$header_top.$header_end;

      $totalnb = 0;
452 453
      while ($row = $types_iterator->next()) {
         $itemtype = $row['itemtype'];
moyooo's avatar
moyooo committed
454 455 456 457
         if (!($item = getItemForItemtype($itemtype))) {
            continue;
         }

korial29's avatar
korial29 committed
458
         if (in_array($itemtype, $_SESSION["glpiactiveprofile"]["helpdesk_item_type"])) {
459 460
            $iterator = self::getTypeItems($instID, $itemtype);
            $nb = count($iterator);
moyooo's avatar
moyooo committed
461

462 463
            $prem = true;
            while ($data = $iterator->next()) {
korial29's avatar
korial29 committed
464 465 466 467 468
               $name = $data["name"];
               if ($_SESSION["glpiis_ids_visible"]
                   || empty($data["name"])) {
                  $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
               }
469
               if ((Session::getCurrentInterface() != 'helpdesk') && $item::canView()) {
korial29's avatar
korial29 committed
470 471 472 473 474
                  $link     = $itemtype::getFormURLWithID($data['id']);
                  $namelink = "<a href=\"".$link."\">".$name."</a>";
               } else {
                  $namelink = $name;
               }
moyooo's avatar
moyooo committed
475

korial29's avatar
korial29 committed
476 477 478
               echo "<tr class='tab_bg_1'>";
               if ($canedit) {
                  echo "<td width='10'>";
479
                  Html::showMassiveActionCheckBox(__CLASS__, $data["linkid"]);
korial29's avatar
korial29 committed
480 481 482 483 484 485
                  echo "</td>";
               }
               if ($prem) {
                  $typename = $item->getTypeName($nb);
                  echo "<td class='center top' rowspan='$nb'>".
                         (($nb > 1) ? sprintf(__('%1$s: %2$s'), $typename, $nb) : $typename)."</td>";
486
                  $prem = false;
korial29's avatar
korial29 committed
487 488 489 490 491 492 493 494 495 496 497
               }
               echo "<td class='center'>";
               echo Dropdown::getDropdownName("glpi_entities", $data['entity'])."</td>";
               echo "<td class='center".
                        (isset($data['is_deleted']) && $data['is_deleted'] ? " tab_bg_2_2'" : "'");
               echo ">".$namelink."</td>";
               echo "<td class='center'>".(isset($data["serial"])? "".$data["serial"]."" :"-").
                    "</td>";
               echo "<td class='center'>".
                      (isset($data["otherserial"])? "".$data["otherserial"]."" :"-")."</td>";
               echo "</tr>";
moyooo's avatar
moyooo committed
498
            }
korial29's avatar
korial29 committed
499
            $totalnb += $nb;
moyooo's avatar
moyooo committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
         }
      }

      if ($number) {
         echo $header_begin.$header_bottom.$header_end;
      }

      echo "</table>";
      if ($canedit && $number) {
         $massiveactionparams['ontop'] = false;
         Html::showMassiveActions($massiveactionparams);
         Html::closeForm();
      }
      echo "</div>";
   }


517
   function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
moyooo's avatar
moyooo committed
518 519

      if (!$withtemplate) {
yllen's avatar
cleanup  
yllen committed
520
         $nb = 0;
moyooo's avatar
moyooo committed
521 522
         switch ($item->getType()) {
            case 'Ticket' :
yllen's avatar
cleanup  
yllen committed
523 524
               if (($_SESSION["glpiactiveprofile"]["helpdesk_hardware"] != 0)
                   && (count($_SESSION["glpiactiveprofile"]["helpdesk_item_type"]) > 0)) {
525
                  if ($_SESSION['glpishow_count_on_tabs']) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
526
                     //$nb = self::countForMainItem($item);
527
                     $nb = countElementsInTable('glpi_items_tickets',
Cédric Anne's avatar
Cédric Anne committed
528 529
                                                ['tickets_id' => $item->getID(),
                                                 'itemtype' => $_SESSION["glpiactiveprofile"]["helpdesk_item_type"]]);
530
                  }
yllen's avatar
cleanup  
yllen committed
531
                  return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb);
532
               }
moyooo's avatar
moyooo committed
533 534 535 536 537 538
         }
      }
      return '';
   }


539
   static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
moyooo's avatar
moyooo committed
540 541 542 543 544 545 546 547

      switch ($item->getType()) {
         case 'Ticket' :
            self::showForTicket($item);
            break;
      }
      return true;
   }
yllen's avatar
CS  
yllen committed
548

moyooo's avatar
moyooo committed
549 550 551 552 553 554 555 556 557
   /**
    * Make a select box for Tracking All Devices
    *
    * @param $myname             select name
    * @param $itemtype           preselected value.for item type
    * @param $items_id           preselected value for item ID (default 0)
    * @param $admin              is an admin access ? (default 0)
    * @param $users_id           user ID used to display my devices (default 0
    * @param $entity_restrict    Restrict to a defined entity (default -1)
korial29's avatar
korial29 committed
558 559 560 561 562
    * @param $options   array of possible options:
    *    - tickets_id : ID of the ticket
    *    - used       : ID of the requester user
    *    - multiple   : allow multiple choice
    *    - rand       : random number
moyooo's avatar
moyooo committed
563 564 565
    *
    * @return nothing (print out an HTML select box)
   **/
566 567
   static function dropdownAllDevices($myname, $itemtype, $items_id = 0, $admin = 0, $users_id = 0,
                                      $entity_restrict = -1, $options = []) {
moyooo's avatar
moyooo committed
568 569
      global $CFG_GLPI, $DB;

570 571
      $params = ['tickets_id' => 0,
                      'used'       => [],
korial29's avatar
korial29 committed
572
                      'multiple'   => 0,
573
                      'rand'       => mt_rand()];
korial29's avatar
korial29 committed
574

korial29's avatar
korial29 committed
575 576 577
      foreach ($options as $key => $val) {
         $params[$key] = $val;
      }
moyooo's avatar
moyooo committed
578

korial29's avatar
korial29 committed
579
      $rand = $params['rand'];
korial29's avatar
korial29 committed
580

moyooo's avatar
moyooo committed
581 582 583 584 585 586 587 588
      if ($_SESSION["glpiactiveprofile"]["helpdesk_hardware"] == 0) {
         echo "<input type='hidden' name='$myname' value=''>";
         echo "<input type='hidden' name='items_id' value='0'>";

      } else {
         echo "<div id='tracking_all_devices$rand'>";
         if ($_SESSION["glpiactiveprofile"]["helpdesk_hardware"]&pow(2,
                                                                     Ticket::HELPDESK_ALL_HARDWARE)) {
korial29's avatar
korial29 committed
589
            // Display a message if view my hardware
moyooo's avatar
moyooo committed
590 591 592 593 594 595 596 597
            if ($users_id
                &&($_SESSION["glpiactiveprofile"]["helpdesk_hardware"]&pow(2,
                                                                           Ticket::HELPDESK_MY_HARDWARE))) {
               echo __('Or complete search')."&nbsp;";
            }

            $types = Ticket::getAllTypesForHelpdesk();
            $emptylabel = __('General');
korial29's avatar
korial29 committed
598
            if ($params['tickets_id'] > 0) {
moyooo's avatar
moyooo committed
599 600
               $emptylabel = Dropdown::EMPTY_VALUE;
            }
korial29's avatar
korial29 committed
601
            Dropdown::showItemTypes($myname, array_keys($types),
602
                                    ['emptylabel' => $emptylabel,
yllen's avatar
CS  
yllen committed
603
                                          'value'      => $itemtype,
604
                                          'rand'       => $rand, 'display_emptychoice' => true]);
moyooo's avatar
moyooo committed
605 606
            $found_type = isset($types[$itemtype]);

607
            $p = ['itemtype'        => '__VALUE__',
korial29's avatar
korial29 committed
608 609 610 611 612
                       'entity_restrict' => $entity_restrict,
                       'admin'           => $admin,
                       'used'            => $params['used'],
                       'multiple'        => $params['multiple'],
                       'rand'            => $rand,
613
                       'myname'          => "add_items_id"];
moyooo's avatar
moyooo committed
614

615
            Ajax::updateItemOnSelectEvent("dropdown_$myname$rand", "results_$myname$rand",
moyooo's avatar
moyooo committed
616 617
                                          $CFG_GLPI["root_doc"].
                                             "/ajax/dropdownTrackingDeviceType.php",
korial29's avatar
korial29 committed
618
                                          $p);
moyooo's avatar
moyooo committed
619 620 621 622 623
            echo "<span id='results_$myname$rand'>\n";

            // Display default value if itemtype is displayed
            if ($found_type
                && $itemtype) {
624
               if (($item = getItemForItemtype($itemtype))
moyooo's avatar
moyooo committed
625 626
                    && $items_id) {
                  if ($item->getFromDB($items_id)) {
627 628
                     Dropdown::showFromArray('items_id', [$items_id => $item->getName()],
                                             ['value' => $items_id]);
moyooo's avatar
moyooo committed
629 630
                  }
               } else {
korial29's avatar
korial29 committed
631
                  $p['itemtype'] = $itemtype;
moyooo's avatar
moyooo committed
632
                  echo "<script type='text/javascript' >\n";
Johan Cwiklinski's avatar
Johan Cwiklinski committed
633
                  echo "$(function() {";
moyooo's avatar
moyooo committed
634 635 636
                  Ajax::updateItemJsCode("results_$myname$rand",
                                         $CFG_GLPI["root_doc"].
                                            "/ajax/dropdownTrackingDeviceType.php",
korial29's avatar
korial29 committed
637
                                         $p);
Johan Cwiklinski's avatar
Johan Cwiklinski committed
638
                  echo '});</script>';
moyooo's avatar
moyooo committed
639 640 641 642 643 644 645 646
               }
            }
            echo "</span>\n";
         }
         echo "</div>";
      }
      return $rand;
   }
yllen's avatar
CS  
yllen committed
647

moyooo's avatar
moyooo committed
648 649 650 651 652 653 654
   /**
    * Make a select box for Ticket my devices
    *
    * @param $userID          User ID for my device section (default 0)
    * @param $entity_restrict restrict to a specific entity (default -1)
    * @param $itemtype        of selected item (default 0)
    * @param $items_id        of selected item (default 0)
korial29's avatar
korial29 committed
655 656 657
    * @param $options   array of possible options:
    *    - used     : ID of the requester user
    *    - multiple : allow multiple choice
korial29's avatar
korial29 committed
658
    *
moyooo's avatar
moyooo committed
659 660
    * @return nothing (print out an HTML select box)
   **/
661
   static function dropdownMyDevices($userID = 0, $entity_restrict = -1, $itemtype = 0, $items_id = 0, $options = []) {
moyooo's avatar
moyooo committed
662 663
      global $DB, $CFG_GLPI;

664 665
      $params = ['tickets_id' => 0,
                      'used'       => [],
korial29's avatar
korial29 committed
666
                      'multiple'   => false,
667
                      'rand'       => mt_rand()];
korial29's avatar
korial29 committed
668

korial29's avatar
korial29 committed
669 670 671
      foreach ($options as $key => $val) {
         $params[$key] = $val;
      }
yllen's avatar
CS  
yllen committed
672

moyooo's avatar
moyooo committed
673 674 675 676
      if ($userID == 0) {
         $userID = Session::getLoginUserID();
      }

korial29's avatar
korial29 committed
677 678
      $rand        = $params['rand'];
      $already_add = $params['used'];
moyooo's avatar
moyooo committed
679 680

      if ($_SESSION["glpiactiveprofile"]["helpdesk_hardware"]&pow(2, Ticket::HELPDESK_MY_HARDWARE)) {
681
         $my_devices = ['' => Dropdown::EMPTY_VALUE];
682
         $devices    = [];
moyooo's avatar
moyooo committed
683 684 685 686 687 688 689

         // My items
         foreach ($CFG_GLPI["linkuser_types"] as $itemtype) {
            if (($item = getItemForItemtype($itemtype))
                && Ticket::isPossibleToAssignType($itemtype)) {
               $itemtable = getTableForItemType($itemtype);

Johan Cwiklinski's avatar
Johan Cwiklinski committed
690 691 692 693 694 695 696 697
               $criteria = [
                  'FROM'   => $itemtable,
                  'WHERE'  => [
                     'users_id' => $userID
                  ] + getEntitiesRestrictCriteria($itemtable, '', $entity_restrict, $item->maybeRecursive()),
                  'ORDER'  => 'name'
               ];

moyooo's avatar
moyooo committed
698
               if ($item->maybeDeleted()) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
699
                  $criteria['WHERE']['is_deleted'] = 0;
moyooo's avatar
moyooo committed
700 701
               }
               if ($item->maybeTemplate()) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
702
                  $criteria['WHERE']['is_template'] = 0;
moyooo's avatar
moyooo committed
703 704
               }
               if (in_array($itemtype, $CFG_GLPI["helpdesk_visible_types"])) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
705
                  $criteria['WHERE']['is_helpdesk_visible'] = 1;
moyooo's avatar
moyooo committed
706 707
               }

Johan Cwiklinski's avatar
Johan Cwiklinski committed
708 709 710
               $iterator = $DB->request($criteria);
               $nb = count($iterator);
               if ($nb > 0) {
moyooo's avatar
moyooo committed
711 712
                  $type_name = $item->getTypeName($nb);

Johan Cwiklinski's avatar
Johan Cwiklinski committed
713
                  while ($data = $iterator->next()) {
moyooo's avatar
moyooo committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
                     if (!isset($already_add[$itemtype]) || !in_array($data["id"], $already_add[$itemtype])) {
                        $output = $data["name"];
                        if (empty($output) || $_SESSION["glpiis_ids_visible"]) {
                           $output = sprintf(__('%1$s (%2$s)'), $output, $data['id']);
                        }
                        $output = sprintf(__('%1$s - %2$s'), $type_name, $output);
                        if ($itemtype != 'Software') {
                           if (!empty($data['serial'])) {
                              $output = sprintf(__('%1$s - %2$s'), $output, $data['serial']);
                           }
                           if (!empty($data['otherserial'])) {
                              $output = sprintf(__('%1$s - %2$s'), $output, $data['otherserial']);
                           }
                        }
                        $devices[$itemtype."_".$data["id"]] = $output;

                        $already_add[$itemtype][] = $data["id"];
                     }
                  }
               }
            }
         }
yllen's avatar
CS  
yllen committed
736

moyooo's avatar
moyooo committed
737 738 739 740
         if (count($devices)) {
            $my_devices[__('My devices')] = $devices;
         }
         // My group items
741
         if (Session::haveRight("show_group_hardware", "1")) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
            $iterator = $DB->request([
               'SELECT'    => [
                  'glpi_groups_users.groups_id',
                  'glpi_groups.name'
               ],
               'FROM'      => 'glpi_groups_users',
               'LEFT JOIN' => [
                  'glpi_groups'  => [
                     'ON' => [
                        'glpi_groups_users'  => 'groups_id',
                        'glpi_groups'        => 'id'
                     ]
                  ]
               ],
               'WHERE'     => [
                  'glpi_groups_users.users_id'  => $userID
               ] + getEntitiesRestrictCriteria('glpi_groups', '', $entity_restrict, true)
            ]);

761
            $devices = [];
Johan Cwiklinski's avatar
Johan Cwiklinski committed
762 763 764
            $groups  = [];
            if (count($iterator)) {
               while ($data = $iterator->next()) {
moyooo's avatar
moyooo committed
765 766
                  $a_groups                     = getAncestorsOf("glpi_groups", $data["groups_id"]);
                  $a_groups[$data["groups_id"]] = $data["groups_id"];
Johan Cwiklinski's avatar
Johan Cwiklinski committed
767
                  $groups = array_merge($groups, $a_groups);
moyooo's avatar
moyooo committed
768 769 770 771 772 773
               }

               foreach ($CFG_GLPI["linkgroup_types"] as $itemtype) {
                  if (($item = getItemForItemtype($itemtype))
                      && Ticket::isPossibleToAssignType($itemtype)) {
                     $itemtable  = getTableForItemType($itemtype);
Johan Cwiklinski's avatar
Johan Cwiklinski committed
774 775 776 777 778 779 780
                     $criteria = [
                        'FROM'   => $itemtable,
                        'WHERE'  => [
                           'groups_id' => $groups
                        ] + getEntitiesRestrictCriteria($itemtable, '', $entity_restrict, $item->maybeRecursive()),
                        'ORDER'  => 'name'
                     ];
moyooo's avatar
moyooo committed
781 782

                     if ($item->maybeDeleted()) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
783
                        $criteria['WHERE']['is_deleted'] = 0;
moyooo's avatar
moyooo committed
784 785
                     }
                     if ($item->maybeTemplate()) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
786
                        $criteria['WHERE']['is_template'] = 0;
moyooo's avatar
moyooo committed
787 788
                     }

Johan Cwiklinski's avatar
Johan Cwiklinski committed
789 790
                     $iterator = $DB->request($criteria);
                     if (count($iterator)) {
moyooo's avatar
moyooo committed
791 792
                        $type_name = $item->getTypeName();
                        if (!isset($already_add[$itemtype])) {
793
                           $already_add[$itemtype] = [];
moyooo's avatar
moyooo committed
794
                        }
Johan Cwiklinski's avatar
Johan Cwiklinski committed
795
                        while ($data = $iterator->next()) {
moyooo's avatar
moyooo committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
                           if (!in_array($data["id"], $already_add[$itemtype])) {
                              $output = '';
                              if (isset($data["name"])) {
                                 $output = $data["name"];
                              }
                              if (empty($output) || $_SESSION["glpiis_ids_visible"]) {
                                 $output = sprintf(__('%1$s (%2$s)'), $output, $data['id']);
                              }
                              $output = sprintf(__('%1$s - %2$s'), $type_name, $output);
                              if (isset($data['serial'])) {
                                 $output = sprintf(__('%1$s - %2$s'), $output, $data['serial']);
                              }
                              if (isset($data['otherserial'])) {
                                 $output = sprintf(__('%1$s - %2$s'), $output, $data['otherserial']);
                              }
                              $devices[$itemtype."_".$data["id"]] = $output;

                              $already_add[$itemtype][] = $data["id"];
                           }
                        }
                     }
                  }
               }
               if (count($devices)) {
                  $my_devices[__('Devices own by my groups')] = $devices;
               }
            }
         }
         // Get linked items to computers
         if (isset($already_add['Computer']) && count($already_add['Computer'])) {
826
            $devices = [];
moyooo's avatar
moyooo committed
827 828

            // Direct Connection
829
            $types = ['Monitor', 'Peripheral', 'Phone', 'Printer'];
moyooo's avatar
moyooo committed
830
            foreach ($types as $itemtype) {
831
               if (in_array($itemtype, $_SESSION["glpiactiveprofile"]["helpdesk_item_type"])
moyooo's avatar
moyooo committed
832 833 834
                   && ($item = getItemForItemtype($itemtype))) {
                  $itemtable = getTableForItemType($itemtype);
                  if (!isset($already_add[$itemtype])) {
835
                     $already_add[$itemtype] = [];
moyooo's avatar
moyooo committed
836
                  }
Johan Cwiklinski's avatar
Johan Cwiklinski committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
                  $criteria = [
                     'SELECT DISTINCT' => "$itemtable.*",
                     'FROM'            => 'glpi_computers_items',
                     'LEFT JOIN'       => [
                        $itemtable  => [
                           'ON' => [
                              'glpi_computers_items'  => 'items_id',
                              $itemtable              => 'id'
                           ]
                        ]
                     ],
                     'WHERE'           => [
                        'glpi_computers_items.itemtype'     => $itemtype,
                        'glpi_computers_items.computers_id' => $already_add['Computer']
                     ] + getEntitiesRestrictCriteria($itemtable, '', $entity_restrict),
                     'ORDERBY'         => "$itemtable.name"
                  ];

moyooo's avatar
moyooo committed
855
                  if ($item->maybeDeleted()) {
856
                     $criteria['WHERE']["$itemtable.is_deleted"] = 0;
moyooo's avatar
moyooo committed
857 858
                  }
                  if ($item->maybeTemplate()) {
859
                     $criteria['WHERE']["$itemtable.is_template"] = 0;
moyooo's avatar
moyooo committed
860 861
                  }

Johan Cwiklinski's avatar
Johan Cwiklinski committed
862 863
                  $iterator = $DB->request($criteria);
                  if (count($iterator)) {
moyooo's avatar
moyooo committed
864
                     $type_name = $item->getTypeName();
Johan Cwiklinski's avatar
Johan Cwiklinski committed
865
                     while ($data = $iterator->next()) {
866
                        if (!in_array($data["id"], $already_add[$itemtype])) {
moyooo's avatar
moyooo committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
                           $output = $data["name"];
                           if (empty($output) || $_SESSION["glpiis_ids_visible"]) {
                              $output = sprintf(__('%1$s (%2$s)'), $output, $data['id']);
                           }
                           $output = sprintf(__('%1$s - %2$s'), $type_name, $output);
                           if ($itemtype != 'Software') {
                              $output = sprintf(__('%1$s - %2$s'), $output, $data['otherserial']);
                           }
                           $devices[$itemtype."_".$data["id"]] = $output;

                           $already_add[$itemtype][] = $data["id"];
                        }
                     }
                  }
               }
            }
            if (count($devices)) {
               $my_devices[__('Connected devices')] = $devices;
            }

            // Software
            if (in_array('Software', $_SESSION["glpiactiveprofile"]["helpdesk_item_type"])) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
               $iterator = $DB->request([
                  'SELECT DISTINCT' => 'glpi_softwareversions.name AS version',
                  'FIELDS'          => [
                     'glpi_softwares.name AS name',
                     'glpi_softwares.id'
                  ],
                  'FROM'            => 'glpi_computers_softwareversions',
                  'LEFT JOIN'       => [
                     'glpi_softwareversions'  => [
                        'ON' => [
                           'glpi_computers_softwareversions'   => 'softwareversions_id',
                           'glpi_softwareversions'             => 'id'
                        ]
                     ],
                     'glpi_softwares'        => [
                        'ON' => [
                           'glpi_softwareversions' => 'softwares_id',
                           'glpi_softwares'        => 'id'
                        ]
908 909 910 911 912 913 914
                     ]
                  ],
                  'WHERE'        => [
                     'glpi_computers_softwareversions.computers_id'   => $already_add['Computer'],
                     'glpi_softwares.is_helpdesk_visible'   => 1
                  ] + getEntitiesRestrictCriteria('glpi_softwares', '', $entity_restrict),
                  'ORDERBY'      => 'glpi_softwares.name'
Johan Cwiklinski's avatar
Johan Cwiklinski committed
915 916
               ]);

917
               $devices = [];
Johan Cwiklinski's avatar
Johan Cwiklinski committed
918
               if (count($iterator)) {
moyooo's avatar
moyooo committed
919 920 921 922
                  $tmp_device = "";
                  $item       = new Software();
                  $type_name  = $item->getTypeName();
                  if (!isset($already_add['Software'])) {
923
                     $already_add['Software'] = [];
moyooo's avatar
moyooo committed
924
                  }
Johan Cwiklinski's avatar
Johan Cwiklinski committed
925
                  while ($data = $iterator->next()) {
moyooo's avatar
moyooo committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
                     if (!in_array($data["id"], $already_add['Software'])) {
                        $output = sprintf(__('%1$s - %2$s'), $type_name, $data["name"]);
                        $output = sprintf(__('%1$s (%2$s)'), $output,
                                          sprintf(__('%1$s: %2$s'), __('version'),
                                                  $data["version"]));
                        if ($_SESSION["glpiis_ids_visible"]) {
                           $output = sprintf(__('%1$s (%2$s)'), $output, $data["id"]);
                        }
                        $devices["Software_".$data["id"]] = $output;

                        $already_add['Software'][] = $data["id"];
                     }
                  }
                  if (count($devices)) {
                     $my_devices[__('Installed software')] = $devices;
                  }
               }
            }
         }
         echo "<div id='tracking_my_devices'>";
946
         echo __('My devices')."&nbsp;";
947
         Dropdown::showFromArray('my_items', $my_devices, ['rand' => $rand]);
moyooo's avatar
moyooo committed
948 949 950
         echo "</div>";

         // Auto update summary of active or just solved tickets
951
         $params = ['my_items' => '__VALUE__'];
moyooo's avatar
moyooo committed
952

953
         Ajax::updateItemOnSelectEvent("dropdown_my_items$rand", "item_ticket_selection_information$rand",
moyooo's avatar
moyooo committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
                                       $CFG_GLPI["root_doc"]."/ajax/ticketiteminformation.php",
                                       $params);
      }
   }

   /**
    * Make a select box with all glpi items
    *
    * @param $options array of possible options:
    *    - name         : string / name of the select (default is users_id)
    *    - value
    *    - comments     : boolean / is the comments displayed near the dropdown (default true)
    *    - entity       : integer or array / restrict to a defined entity or array of entities
    *                      (default -1 : no restriction)
    *    - entity_sons  : boolean / if entity restrict specified auto select its sons
    *                      only available if entity is a single value not an array(default false)
    *    - rand         : integer / already computed rand value
    *    - toupdate     : array / Update a specific item on select change on dropdown
    *                      (need value_fieldname, to_update, url
    *                      (see Ajax::updateItemOnSelectEvent for information)
    *                      and may have moreparams)
    *    - used         : array / Already used items ID: not to display in dropdown (default empty)
    *    - on_change    : string / value to transmit to "onChange"
    *    - display      : boolean / display or get string (default true)
    *    - width        : specific width needed (default 80%)
    *
   **/
981
   static function dropdown($options = []) {
moyooo's avatar
moyooo committed
982
      global $DB;
yllen's avatar
CS  
yllen committed
983

moyooo's avatar
moyooo committed
984 985 986 987 988 989 990 991 992
      // Default values
      $p['name']           = 'items';
      $p['value']          = '';
      $p['all']            = 0;
      $p['on_change']      = '';
      $p['comments']       = 1;
      $p['width']          = '80%';
      $p['entity']         = -1;
      $p['entity_sons']    = false;
993
      $p['used']           = [];
moyooo's avatar
moyooo committed
994 995 996
      $p['toupdate']       = '';
      $p['rand']           = mt_rand();
      $p['display']        = true;
yllen's avatar
CS  
yllen committed
997

moyooo's avatar
moyooo committed
998 999 1000 1001 1002
      if (is_array($options) && count($options)) {
         foreach ($options as $key => $val) {
            $p[$key] = $val;
         }
      }
yllen's avatar
CS  
yllen committed
1003

1004
      $itemtypes = ['Computer', 'Monitor', 'NetworkEquipment', 'Peripheral', 'Phone', 'Printer'];
yllen's avatar
CS  
yllen committed
1005

Johan Cwiklinski's avatar
Johan Cwiklinski committed
1006
      $union = new \QueryUnion();
moyooo's avatar
moyooo committed
1007
      foreach ($itemtypes as $type) {
Johan Cwiklinski's avatar
Johan Cwiklinski committed
1008
         //TODO: migrate when iterator usuports UNION
moyooo's avatar
moyooo committed
1009
         $table = getTableForItemType($type);
Johan Cwiklinski's avatar
Johan Cwiklinski committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
         $union->addQuery([
            'SELECT' => [
               'id',
               new \QueryExpression("$type AS " . $DB->quoteName('itemtype')),
               "name"
            ],
            'FROM'   => $table,
            'WHERE'  => [
               'NOT'          => ['id' => null],
               'is_deleted'   => 0,
               'is_template'  => 0
            ]
         ]);
moyooo's avatar
moyooo committed
1023 1024
      }

Johan Cwiklinski's avatar
Johan Cwiklinski committed
1025
      $iterator = $DB->request(['FROM' => $union]);