/** * 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) **/ static function itemAddForm(Ticket $ticket, $options = array()) { global $CFG_GLPI; $params = array('id' => isset($ticket->fields['id']) && $ticket->fields['id'] != '' ? $ticket->fields['id'] : 0, '_users_id_requester' => 0, 'items_id' => array(), 'itemtype' => ''); $opt = array(); foreach ($options as $key => $val) { if (!empty($val)) { $params[$key] = $val; } } if (!$ticket->can($params['id'], READ)) { return false; } $canedit = $ticket->can($params['id'], UPDATE); // Ticket update case if ($params['id'] > 0) { // Get requester $class = new $ticket->userlinkclass(); $tickets_user = $class->getActors($params['id']); if (isset($tickets_user[CommonITILActor::REQUESTER]) && count($tickets_user[CommonITILActor::REQUESTER]) == 1) { foreach ($tickets_user[CommonITILActor::REQUESTER] as $user_id_single) { $params['_users_id_requester'] = $user_id_single['users_id']; } } // Get associated elements for ticket $used = self::getUsedItems($params['id']); foreach ($used as $itemtype => $items) { foreach ($items as $items_id) { if (!isset($params['items_id'][$itemtype]) || !in_array($items_id, $params['items_id'][$itemtype])) { $params['items_id'][$itemtype][] = $items_id; } } } } // Get ticket template $tt = new TicketTemplate(); if (isset($options['_tickettemplate'])) { $tt = $options['_tickettemplate']; if (isset($tt->fields['id'])) { $opt['templates_id'] = $tt->fields['id']; } } else { if (isset($options['templates_id'])) { $tt->getFromDBWithDatas($options['templates_id']); if (isset($tt->fields['id'])) { $opt['templates_id'] = $tt->fields['id']; } } } $rand = mt_rand(); $count = 0; echo "<div id='itemAddForm{$rand}'>"; // Show associated item dropdowns if ($canedit) { echo "<div style='float:left'>"; $p = array('used' => $params['items_id'], 'rand' => $rand, 'tickets_id' => $params['id']); // 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); echo "<span id='item_ticket_selection_information'></span>"; echo "</div>"; // 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;'>"; if (!empty($params['items_id'])) { // No delete if mandatory and only one item $delete = true; $cpt = 0; foreach ($params['items_id'] as $itemtype => $items) { foreach ($items as $items_id) { $cpt++; } } if ($cpt == 1 && isset($tt->mandatory['items_id'])) { $delete = false; } foreach ($params['items_id'] as $itemtype => $items) { foreach ($items as $items_id) { if ($count < 5 && $params['id'] || $params['id'] == 0) { echo Item_Ticket::showItemToAdd($params['id'], $itemtype, $items_id, array('rand' => $rand, 'delete' => $delete)); } $count++; } } } if ($count == 0) { echo "<input type='hidden' value='0' name='items_id'>"; } if ($params['id'] > 0 && $count > 5) { echo "<i><a href='" . $ticket->getFormURL() . "?id=" . $params['id'] . "&forcetab=Item_Ticket\$1'>" . __('Display all items') . " (" . $count . ")</a></i>"; } echo "</div>"; foreach (array('id', '_users_id_requester', 'items_id', 'itemtype') as $key) { $opt[$key] = $params[$key]; } $js = " function itemAction{$rand}(action, itemtype, items_id) {"; $js .= " \$.ajax({\n url: '" . $CFG_GLPI['root_doc'] . "/ajax/itemTicket.php',\n dataType: 'html',\n data: {'action' : action,\n 'rand' : {$rand},\n 'params' : " . json_encode($opt) . ",\n 'my_items' : \$('#dropdown_my_items{$rand}').val(),\n 'itemtype' : (itemtype === undefined) ? \$('#dropdown_itemtype{$rand}').val() : itemtype,\n 'items_id' : (items_id === undefined) ? \$('#dropdown_add_items_id{$rand}').val() : items_id},\n success: function(response) {"; $js .= " \$(\"#itemAddForm{$rand}\").html(response);"; $js .= " }"; $js .= " });"; $js .= " }"; echo Html::scriptBlock($js); echo "</div>"; }