. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Change_Ticket Class * * Relation between Changes and Tickets **/ class Change_Ticket extends CommonDBRelation{ // From CommonDBRelation static public $itemtype_1 = 'Change'; static public $items_id_1 = 'changes_id'; static public $itemtype_2 = 'Ticket'; static public $items_id_2 = 'tickets_id'; function getForbiddenStandardMassiveAction() { $forbidden = parent::getForbiddenStandardMassiveAction(); $forbidden[] = 'update'; return $forbidden; } static function getTypeName($nb = 0) { return _n('Link Ticket/Change', 'Links Ticket/Change', $nb); } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (static::canView()) { $nb = 0; switch ($item->getType()) { case 'Change' : if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable('glpi_changes_tickets', ['changes_id' => $item->getID()]); } return self::createTabEntry(Ticket::getTypeName(Session::getPluralNumber()), $nb); case 'Ticket' : if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable('glpi_changes_tickets', ['tickets_id' => $item->getID()]); } return self::createTabEntry(Change::getTypeName(Session::getPluralNumber()), $nb); } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { switch ($item->getType()) { case 'Change' : self::showForChange($item); break; case 'Ticket' : self::showForTicket($item); break; } return true; } static function showMassiveActionsSubForm(MassiveAction $ma) { switch ($ma->getAction()) { case 'add_task' : $tasktype = 'TicketTask'; if ($ttype = getItemForItemtype($tasktype)) { $ttype->showMassiveActionAddTaskForm(); return true; } return false; case "solveticket" : $change = new Change(); $input = $ma->getInput(); if (isset($input['changes_id']) && $change->getFromDB($input['changes_id'])) { $change->showMassiveSolutionForm($change); echo "
"; echo Html::submit(_x('button', 'Post'), ['name' => 'massiveaction']); return true; } return false; } return parent::showMassiveActionsSubForm($ma); } static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids) { switch ($ma->getAction()) { case 'add_task' : if (!($task = getItemForItemtype('TicketTask'))) { $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO); break; } $ticket = new Ticket(); $field = $ticket->getForeignKeyField(); $input = $ma->getInput(); foreach ($ids as $id) { if ($item->can($id, READ)) { if ($ticket->getFromDB($item->fields['tickets_id'])) { $input2 = [$field => $item->fields['tickets_id'], 'taskcategories_id' => $input['taskcategories_id'], 'actiontime' => $input['actiontime'], 'content' => $input['content']]; if ($task->can(-1, CREATE, $input2)) { if ($task->add($input2)) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION)); } } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); $ma->addMessage($item->getErrorMessage(ERROR_RIGHT)); } } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); $ma->addMessage($item->getErrorMessage(ERROR_RIGHT)); } } } return; case 'solveticket' : $input = $ma->getInput(); $ticket = new Ticket(); foreach ($ids as $id) { if ($item->can($id, READ)) { if ($ticket->getFromDB($item->fields['tickets_id']) && $ticket->canSolve()) { $solution = new ITILSolution(); $added = $solution->add([ 'itemtype' => $ticket->getType(), 'items_id' => $ticket->getID(), 'solutiontypes_id' => $input['solutiontypes_id'], 'content' => $input['content'] ]); if ($added) { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); $ma->addMessage($ticket->getErrorMessage(ERROR_ON_ACTION)); } } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); $ma->addMessage($ticket->getErrorMessage(ERROR_RIGHT)); } } else { $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); $ma->addMessage($ticket->getErrorMessage(ERROR_RIGHT)); } } return; } parent::processMassiveActionsForOneItemtype($ma, $item, $ids); } /** * Show tickets for a change * * @param $change Change object **/ static function showForChange(Change $change) { global $DB; $ID = $change->getField('id'); if (!$change->can($ID, READ)) { return false; } $canedit = $change->canEdit($ID); $rand = mt_rand(); $iterator = $DB->request([ 'SELECT' => [ 'glpi_changes_tickets.id AS linkid', 'glpi_tickets.*' ], 'DISTINCT' => true, 'FROM' => 'glpi_changes_tickets', 'LEFT JOIN' => [ 'glpi_tickets' => [ 'ON' => [ 'glpi_changes_tickets' => 'tickets_id', 'glpi_tickets' => 'id' ] ] ], 'WHERE' => [ 'glpi_changes_tickets.changes_id' => $ID ], 'ORDERBY' => [ 'glpi_tickets.name' ] ]); $tickets = []; $used = []; $numrows = count($iterator); while ($data = $iterator->next()) { $tickets[$data['id']] = $data; $used[$data['id']] = $data['id']; } if ($canedit && !in_array($change->fields['status'], array_merge($change->getClosedStatusArray(), $change->getSolvedStatusArray()))) { echo "
"; echo "
"; echo ""; echo ""; echo ""; echo "
".__('Add a ticket')."
"; echo ""; Ticket::dropdown([ 'used' => $used, 'entity' => $change->getEntityID(), 'entity_sons' => $change->isRecursive(), 'displaywith' => ['id'], 'condition' => Ticket::getOpenCriteria() ]); echo ""; echo ""; echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && $numrows) { Html::openMassiveActionsForm('mass'.__CLASS__.$rand); $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $numrows), 'specific_actions' => ['purge' => _x('button', 'Delete permanently'), __CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'solveticket' => __('Solve tickets'), __CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'add_task' => __('Add a new task')], 'container' => 'mass'.__CLASS__.$rand, 'extraparams' => ['changes_id' => $change->getID()], 'width' => 1000, 'height' => 500]; Html::showMassiveActions($massiveactionparams); } echo ""; echo ""; echo ""; if ($numrows) { Ticket::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand); Session::initNavigateListItems('Ticket', //TRANS : %1$s is the itemtype name, // %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), Change::getTypeName(1), $change->fields["name"])); $i = 0; foreach ($tickets as $data) { Session::addToNavigateListItems('Ticket', $data["id"]); Ticket::showShort($data['id'], ['followups' => false, 'row_num' => $i, 'type_for_massiveaction' => __CLASS__, 'id_for_massiveaction' => $data['linkid']]); $i++; } Ticket::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand); } echo "
".Ticket::getTypeName($numrows)."
"; if ($canedit && $numrows) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } /** * Show changes for a ticket * * @param $ticket Ticket object **/ static function showForTicket(Ticket $ticket) { global $DB; $ID = $ticket->getField('id'); if (!$ticket->can($ID, READ)) { return false; } $canedit = $ticket->canEdit($ID); $rand = mt_rand(); $iterator = $DB->request([ 'SELECT' => [ 'glpi_changes_tickets.id AS linkid', 'glpi_changes.*' ], 'DISTINCT' => true, 'FROM' => 'glpi_changes_tickets', 'LEFT JOIN' => [ 'glpi_changes' => [ 'ON' => [ 'glpi_changes_tickets' => 'changes_id', 'glpi_changes' => 'id' ] ] ], 'WHERE' => [ 'glpi_changes_tickets.tickets_id' => $ID ], 'ORDERBY' => [ 'glpi_changes.name' ] ]); $changes = []; $used = []; $numrows = count($iterator); while ($data = $iterator->next()) { $changes[$data['id']] = $data; $used[$data['id']] = $data['id']; } if ($canedit && !in_array($ticket->fields['status'], array_merge($ticket->getClosedStatusArray(), $ticket->getSolvedStatusArray()))) { echo "
"; echo ""; echo ""; echo ""; echo "
".__('Add a change')."
"; echo ""; Change::dropdown([ 'used' => $used, 'entity' => $ticket->getEntityID(), 'condition' => Change::getOpenCriteria(), ]); echo ""; echo ""; echo ""; if (Session::haveRight('change', CREATE)) { echo ""; echo __('Create a change from this ticket'); echo ""; } echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && $numrows) { Html::openMassiveActionsForm('mass'.__CLASS__.$rand); $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $numrows), 'container' => 'mass'.__CLASS__.$rand]; Html::showMassiveActions($massiveactionparams); } echo ""; echo ""; echo ""; if ($numrows) { Change::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand); Session::initNavigateListItems('Change', //TRANS : %1$s is the itemtype name, // %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), Ticket::getTypeName(1), $ticket->fields["name"])); $i = 0; foreach ($changes as $data) { Session::addToNavigateListItems('Change', $data["id"]); Change::showShort($data['id'], ['row_num' => $i, 'type_for_massiveaction' => __CLASS__, 'id_for_massiveaction' => $data['linkid']]); $i++; } Change::commonListHeader(Search::HTML_OUTPUT, 'mass'.__CLASS__.$rand); } echo "
".Change::getTypeName($numrows)."
"; if ($canedit && $numrows) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } }