. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * ITILTemplateMandatoryField Class * * Predefined fields for ITIL template class * * @since 9.5.0 **/ abstract class ITILTemplateMandatoryField extends ITILTemplateField { static function getTypeName($nb = 0) { return _n('Mandatory field', 'Mandatory fields', $nb); } 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 ''; } 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::$itiltype], 'num' => $items_id_id ] ]); if (count($iterator)) { $result = $iterator->next(); $a = new static(); $a->delete(['id' => $result['id']]); } } } /** * Get mandatory fields for a template * * @since 0.83 * * @param integer $ID the template ID * @param boolean $withtypeandcategory with type and category (true by default) * * @return array of mandatory fields **/ function getMandatoryFields($ID, $withtypeandcategory = true) { 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); $fields = []; while ($rule = $iterator->next()) { if (isset($allowed_fields[$rule['num']])) { $fields[$allowed_fields[$rule['num']]] = $rule['num']; } } 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 [ 175 => 175, // ticket's tasks ]; } /** * Print the mandatory 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; $ID = $tt->fields['id']; if (!$tt->getFromDB($ID) || !$tt->can($ID, READ)) { return false; } $canedit = $tt->canEdit($ID); $ttm = new static(); $fields = $ttm->getAllFields($tt); $simplified_fields = $tt->getSimplifiedInterfaceFields(); $both_interfaces = sprintf(__('%1$s + %2$s'), __('Simplified interface'), __ ('Standard interface')); $rand = mt_rand(); $iterator = $DB->request([ 'FROM' => static::getTable(), 'WHERE' => [static::$items_id => $ID] ]); $numrows = count($iterator); $mandatoryfields = []; $used = []; while ($data = $iterator->next()) { $mandatoryfields[$data['id']] = $data; $used[$data['num']] = $data['num']; } if ($canedit) { echo "
"; echo "
"; echo ""; echo ""; echo ""; echo "
".__('Add a mandatory field')."
"; echo ""; $select_fields = $fields; foreach ($select_fields as $key => $val) { if (static::$itiltype == Ticket::getType()) { if (in_array($key, $simplified_fields)) { $select_fields[$key] = sprintf(__('%1$s (%2$s)'), $val, $both_interfaces); } else { $select_fields[$key] = sprintf(__('%1$s (%2$s)'), $val, __('Standard interface')); } } else { $select_fields[$key] = $val; } } Dropdown::showFromArray('num', $select_fields, ['used' => $used]); echo ""; echo " "; echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && $numrows) { Html::openMassiveActionsForm('mass'.$ttm->getType().$rand); $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $numrows), 'container' => 'mass'.$ttm->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 ($mandatoryfields as $data) { echo ""; if ($canedit) { echo ""; } echo ""; echo ""; echo ""; } echo $header_begin.$header_bottom.$header_end; } else { echo ""; } echo "
"; echo static::getTypeName(count($iterator)); echo "
"; $header_top .= Html::getCheckAllAsCheckbox('mass'.$ttm->getType().$rand).""; $header_bottom .= Html::getCheckAllAsCheckbox('mass'.$ttm->getType().$rand)."".__('Name')."".__("Profile's interface")."
".Html::getMassiveActionCheckBox($ttm->getType(), $data["id"])."".$fields[$data['num']].""; if (in_array($data['num'], $simplified_fields)) { echo $both_interfaces; } else { echo __('Standard interface'); } echo "
".__('No item found')."
"; if ($canedit && $numrows) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } }