. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * ITILTemplatePredefinedField Class * * Predefined fields for ITIL template class * * @since 9.5.0 **/ abstract class ITILTemplatePredefinedField extends ITILTemplateField { static function getTypeName($nb = 0) { return _n('Predefined field', 'Predefined fields', $nb); } protected function computeFriendlyName() { $tt_class = static::$itemtype; $tt = new $tt_class; $fields = $tt->getAllowedFieldsNames(true, true); if (isset($fields[$this->fields["num"]])) { return $fields[$this->fields["num"]]; } return ''; } function prepareInputForAdd($input) { // Use massiveaction system to manage add system. // Need to update data : value not set but if (!isset($input['value'])) { if (isset($input['field']) && isset($input[$input['field']])) { $input['value'] = $input[$input['field']]; unset($input[$input['field']]); unset($input['field']); } } return parent::prepareInputForAdd($input); } function post_purgeItem() { global $DB; parent::post_purgeItem(); $itil_class = static::$itiltype; $itil_object = new $itil_class; $itemtype_id = $itil_object->getSearchOptionIDByField('field', 'itemtype', $itil_object->getTable()); $items_id_id = $itil_object->getSearchOptionIDByField('field', 'items_id', $itil_object->getTable()); // Try to delete itemtype -> delete items_id if ($this->fields['num'] == $itemtype_id) { $iterator = $DB->request([ 'SELECT' => 'id', 'FROM' => $this->getTable(), 'WHERE' => [ static::$items_id => $this->fields[static::$items_id], 'num' => $items_id_id ] ]); if (count($iterator)) { $result = $iterator->next(); $a = new static(); $a->delete(['id' => $result['id']]); } } } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { // can exists for template if ($item instanceof ITILTemplate && Session::haveRight("itiltemplate", READ)) { $nb = 0; if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable($this->getTable(), [static::$items_id => $item->getID()]); } return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb); } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { self::showForITILTemplate($item, $withtemplate); return true; } /** * Get predefined fields for a template * * @since 0.83 * * @param integer $ID the template ID * @param boolean $withtypeandcategory with type and category (false by default) * * @return array of predefined fields **/ function getPredefinedFields($ID, $withtypeandcategory = false) { global $DB; $iterator = $DB->request([ 'FROM' => $this->getTable(), 'WHERE' => [static::$items_id => $ID], 'ORDER' => 'id' ]); $tt_class = static::$itemtype; $tt = new $tt_class; $allowed_fields = $tt->getAllowedFields($withtypeandcategory, true); $fields = []; $multiple = self::getMultiplePredefinedValues(); while ($rule = $iterator->next()) { if (isset($allowed_fields[$rule['num']])) { if (in_array($rule['num'], $multiple)) { if ($allowed_fields[$rule['num']] == 'items_id') { $item_itemtype = explode("_", $rule['value']); $fields[$allowed_fields[$rule['num']]][$item_itemtype[0]][$item_itemtype[1]] = $item_itemtype[1]; } else { $fields[$allowed_fields[$rule['num']]][] = $rule['value']; } } else { $fields[$allowed_fields[$rule['num']]] = $rule['value']; } } } return $fields; } /** * @since 0.85 **/ static function getMultiplePredefinedValues() { $itil_class = static::$itiltype; $itil_object = new $itil_class; $itemstable = null; switch ($itil_class) { case 'Change': $itemstable = 'glpi_changes_items'; break; case 'Problem': $itemstable = 'glpi_items_problems'; break; case 'Ticket': $itemstable = 'glpi_items_tickets'; break; default: throw new \RuntimeException('Unknown ITIL type ' . $itil_class); } $fields = [ $itil_object->getSearchOptionIDByField('field', 'name', 'glpi_documents'), $itil_object->getSearchOptionIDByField('field', 'items_id', $itemstable), $itil_object->getSearchOptionIDByField('field', 'name', 'glpi_tasktemplates') ]; return $fields; } /** * Return fields who doesn't need to be used for this part of template * * @since 9.2 * * @return array the excluded fields (keys and values are equals) */ static function getExcludedFields() { return [ -2 => -2, // validation request ]; } /** * Print the predefined fields * * @since 0.83 * * @param ITILTemplate $tt ITIL Template * @param boolean $withtemplate Template or basic item (default 0) * * @return void **/ static function showForITILTemplate(ITILTemplate $tt, $withtemplate = 0) { global $DB, $CFG_GLPI; $ID = $tt->fields['id']; if (!$tt->getFromDB($ID) || !$tt->can($ID, READ)) { return false; } $canedit = $tt->canEdit($ID); $fields = $tt->getAllowedFieldsNames(true, true); $fields = array_diff_key($fields, self::getExcludedFields()); $itil_class = static::$itiltype; $searchOption = Search::getOptions($itil_class); $itil_object = new $itil_class; $rand = mt_rand(); $iterator = $DB->request([ 'FROM' => static::getTable(), 'WHERE' => [static::$items_id => $ID], 'ORDER' => 'id' ]); $display_options = [ 'relative_dates' => true, 'comments' => true, 'html' => true ]; $predeffields = []; $used = []; $numrows = count($iterator); while ($data = $iterator->next()) { $predeffields[$data['id']] = $data; $used[$data['num']] = $data['num']; } if ($canedit) { echo "
| "; echo self::getTypeName($numrows); echo " | |||
|---|---|---|---|
| "; $header_top .= Html::getCheckAllAsCheckbox('mass'.static::getType().$rand)." | "; $header_bottom .= ""; $header_bottom .= Html::getCheckAllAsCheckbox('mass'.static::getType().$rand)." | "; } $header_end .= "".__('Name')." | "; $header_end .= "".__('Value')." | "; $header_end .= "
| ".Html::getMassiveActionCheckBox(static::getType(), $data["id"])." | "; } echo "".$fields[$data['num']]." | "; echo ""; $display_datas[$searchOption[$data['num']]['field']] = $data['value']; echo $itil_object->getValueToDisplay($searchOption[$data['num']], $display_datas, $display_options); echo " | "; echo "|
| ".__('No item found')." | |||