. * --------------------------------------------------------------------- */ 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 "
"; echo ""; echo ""; echo ""; echo "
".__('Add a predefined field')."
"; echo ""; $display_fields[-1] = Dropdown::EMPTY_VALUE; $display_fields += $fields; // Unset multiple items $multiple = self::getMultiplePredefinedValues(); foreach ($multiple as $val) { if (isset($used[$val])) { unset($used[$val]); } } $rand_dp = Dropdown::showFromArray('num', $display_fields, ['used' => $used, 'toadd']); echo ""; $paramsmassaction = ['id_field' => '__VALUE__', 'itemtype' => static::$itiltype, 'inline' => true, 'submitname' => _sx('button', 'Add'), 'options' => ['relative_dates' => 1, 'with_time' => 1, 'with_days' => 0, 'with_specific_date' => 0, 'itemlink_as_string' => 1, 'entity' => $tt->getEntityID()]]; Ajax::updateItemOnSelectEvent("dropdown_num".$rand_dp, "show_massiveaction_field", $CFG_GLPI["root_doc"]."/ajax/dropdownMassiveActionField.php", $paramsmassaction); echo ""; echo " \n"; echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && $numrows) { Html::openMassiveActionsForm('mass'.static::getType().$rand); $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $numrows), 'container' => 'mass'.static::getType().$rand]; Html::showMassiveActions($massiveactionparams); } echo ""; echo ""; if ($numrows) { $header_begin = ""; $header_top = ''; $header_bottom = ''; $header_end = ''; if ($canedit) { $header_top .= ""; $header_bottom .= ""; } $header_end .= ""; $header_end .= ""; $header_end .= ""; echo $header_begin.$header_top.$header_end; foreach ($predeffields as $data) { if (!isset($fields[$data['num']])) { // could happen when itemtype removed and items_id present continue; } echo ""; if ($canedit) { echo ""; } echo ""; echo ""; echo ""; } echo $header_begin.$header_bottom.$header_end; } else { echo ""; } echo "
"; echo self::getTypeName($numrows); echo "
"; $header_top .= Html::getCheckAllAsCheckbox('mass'.static::getType().$rand).""; $header_bottom .= Html::getCheckAllAsCheckbox('mass'.static::getType().$rand)."".__('Name')."".__('Value')."
".Html::getMassiveActionCheckBox(static::getType(), $data["id"])."".$fields[$data['num']].""; $display_datas[$searchOption[$data['num']]['field']] = $data['value']; echo $itil_object->getValueToDisplay($searchOption[$data['num']], $display_datas, $display_options); echo "
".__('No item found')."
"; if ($canedit && $numrows) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } }