/**
  * Print the mandatory fields
  *
  * @since version 0.83
  *
  * @param $tt                       Ticket Template
  * @param $withtemplate    boolean  Template or basic item (default '')
  *
  * @return Nothing (call to classes members)
  **/
 static function showForTicketTemplate(TicketTemplate $tt, $withtemplate = '')
 {
     global $DB;
     $ID = $tt->fields['id'];
     if (!$tt->getFromDB($ID) || !$tt->can($ID, READ)) {
         return false;
     }
     $canedit = $tt->canEdit($ID);
     $ttm = new self();
     $used = $ttm->getMandatoryFields($ID);
     $fields = $tt->getAllowedFieldsNames(true);
     $simplified_fields = $tt->getSimplifiedInterfaceFields();
     $both_interfaces = sprintf(__('%1$s + %2$s'), __('Simplified interface'), __('Standard interface'));
     $rand = mt_rand();
     $query = "SELECT `glpi_tickettemplatemandatoryfields`.*\n                FROM `glpi_tickettemplatemandatoryfields`\n                WHERE (`tickettemplates_id` = '{$ID}')";
     if ($result = $DB->query($query)) {
         $mandatoryfields = array();
         $used = array();
         if ($numrows = $DB->numrows($result)) {
             while ($data = $DB->fetch_assoc($result)) {
                 $mandatoryfields[$data['id']] = $data;
                 $used[$data['num']] = $data['num'];
             }
         }
         if ($canedit) {
             echo "<div class='firstbloc'>";
             echo "<form name='changeproblem_form{$rand}' id='changeproblem_form{$rand}' method='post'\n                   action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
             echo "<table class='tab_cadre_fixe'>";
             echo "<tr class='tab_bg_2'><th colspan='2'>" . __('Add a mandatory field') . "</th></tr>";
             echo "<tr class='tab_bg_2'><td class='right'>";
             echo "<input type='hidden' name='tickettemplates_id' value='{$ID}'>";
             $select_fields = $fields;
             foreach ($select_fields as $key => $val) {
                 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'));
                 }
             }
             Dropdown::showFromArray('num', $select_fields, array('used' => $used));
             echo "</td><td class='center'>";
             echo "&nbsp;<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
             echo "</td></tr>";
             echo "</table>";
             Html::closeForm();
             echo "</div>";
         }
         echo "<div class='spaced'>";
         if ($canedit && $numrows) {
             Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
             $massiveactionparams = array('num_displayed' => $numrows, 'container' => 'mass' . __CLASS__ . $rand);
             Html::showMassiveActions($massiveactionparams);
         }
         echo "<table class='tab_cadre_fixehov'>";
         echo "<tr class='noHover'><th colspan='3'>";
         echo self::getTypeName($DB->numrows($result));
         echo "</th></tr>";
         if ($numrows) {
             $header_begin = "<tr>";
             $header_top = '';
             $header_bottom = '';
             $header_end = '';
             if ($canedit) {
                 $header_top .= "<th width='10'>";
                 $header_top .= Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
                 $header_bottom .= "<th width='10'>";
                 $header_bottom .= Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
             }
             $header_end .= "<th>" . __('Name') . "</th>";
             $header_end .= "<th>" . __("Profile's interface") . "</th>";
             $header_end .= "</tr>";
             echo $header_begin . $header_top . $header_end;
             foreach ($mandatoryfields as $data) {
                 echo "<tr class='tab_bg_2'>";
                 if ($canedit) {
                     echo "<td>" . Html::getMassiveActionCheckBox(__CLASS__, $data["id"]) . "</td>";
                 }
                 echo "<td>" . $fields[$data['num']] . "</td>";
                 echo "<td>";
                 if (in_array($data['num'], $simplified_fields)) {
                     echo $both_interfaces;
                 } else {
                     _e('Standard interface');
                 }
                 echo "</td>";
                 echo "</tr>";
             }
             echo $header_begin . $header_bottom . $header_end;
         } else {
             echo "<tr><th colspan='2'>" . __('No item found') . "</th></tr>";
         }
         echo "</table>";
         if ($canedit && $numrows) {
             $massiveactionparams['ontop'] = false;
             Html::showMassiveActions($massiveactionparams);
             Html::closeForm();
         }
         echo "</div>";
     }
 }
 /**
  * Print the predefined fields
  *
  * @since version 0.83
  *
  * @param $tt                       Ticket Template
  * @param $withtemplate    boolean  Template or basic item (default '')
  *
  * @return Nothing (call to classes members)
  **/
 static function showForTicketTemplate(TicketTemplate $tt, $withtemplate = '')
 {
     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);
     $searchOption = Search::getOptions('Ticket');
     $ticket = new Ticket();
     $rand = mt_rand();
     $query = "SELECT `glpi_tickettemplatepredefinedfields`.*\n                FROM `glpi_tickettemplatepredefinedfields`\n                WHERE (`tickettemplates_id` = '{$ID}')\n                ORDER BY 'id'";
     $display_options = array('relative_dates' => true, 'comments' => true, 'html' => true);
     if ($result = $DB->query($query)) {
         $predeffields = array();
         $used = array();
         if ($numrows = $DB->numrows($result)) {
             while ($data = $DB->fetch_assoc($result)) {
                 $predeffields[$data['id']] = $data;
                 $used[$data['num']] = $data['num'];
             }
         }
         if ($canedit) {
             echo "<div class='firstbloc'>";
             echo "<form name='changeproblem_form{$rand}' id='changeproblem_form{$rand}' method='post'\n                  action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
             echo "<table class='tab_cadre_fixe'>";
             echo "<tr class='tab_bg_2'><th colspan='3'>" . __('Add a predefined field') . "</th></tr>";
             echo "<tr class='tab_bg_2'><td class='right top' width='30%'>";
             echo "<input type='hidden' name='tickettemplates_id' value='{$ID}'>";
             $display_fields[-1] = Dropdown::EMPTY_VALUE;
             $display_fields += $fields;
             // Force validation request as used
             $used[-2] = -2;
             // Unset multiple items
             $multiple = self::getMultiplePredefinedValues();
             foreach ($multiple as $val) {
                 if (isset($used[$val])) {
                     unset($used[$val]);
                 }
             }
             $rand_dp = Dropdown::showFromArray('num', $display_fields, array('used' => $used, 'toadd'));
             echo "</td><td class='top'>";
             $paramsmassaction = array('id_field' => '__VALUE__', 'itemtype' => 'Ticket', 'inline' => true, 'submitname' => _sx('button', 'Add'), 'options' => array('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 "</td><td>";
             echo "<span id='show_massiveaction_field'>&nbsp;</span>\n";
             echo "</td></tr>";
             echo "</table>";
             Html::closeForm();
             echo "</div>";
         }
         echo "<div class='spaced'>";
         if ($canedit && $numrows) {
             Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
             $massiveactionparams = array('num_displayed' => $numrows, 'container' => 'mass' . __CLASS__ . $rand);
             Html::showMassiveActions($massiveactionparams);
         }
         echo "<table class='tab_cadre_fixehov'>";
         echo "<tr class='noHover'><th colspan='3'>";
         echo self::getTypeName($DB->numrows($result));
         echo "</th></tr>";
         if ($numrows) {
             $header_begin = "<tr>";
             $header_top = '';
             $header_bottom = '';
             $header_end = '';
             if ($canedit) {
                 $header_top .= "<th width='10'>";
                 $header_top .= Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
                 $header_bottom .= "<th width='10'>";
                 $header_bottom .= Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
             }
             $header_end .= "<th>" . __('Name') . "</th>";
             $header_end .= "<th>" . __('Value') . "</th>";
             $header_end .= "</tr>";
             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 "<tr class='tab_bg_2'>";
                 if ($canedit) {
                     echo "<td>" . Html::getMassiveActionCheckBox(__CLASS__, $data["id"]) . "</td>";
                 }
                 echo "<td>" . $fields[$data['num']] . "</td>";
                 echo "<td>";
                 $display_datas[$searchOption[$data['num']]['field']] = $data['value'];
                 echo $ticket->getValueToDisplay($searchOption[$data['num']], $display_datas, $display_options);
                 echo "</td>";
                 echo "</tr>";
             }
             echo $header_begin . $header_bottom . $header_end;
         } else {
             echo "<tr><th colspan='3'>" . __('No item found') . "</th></tr>";
         }
         echo "</table>";
         if ($canedit && $numrows) {
             $massiveactionparams['ontop'] = false;
             Html::showMassiveActions($massiveactionparams);
             Html::closeForm();
         }
         echo "</div>";
     }
 }
Esempio n. 3
0
 function prepareInputForAdd($input)
 {
     global $CFG_GLPI;
     // save value before clean;
     $title = ltrim($input['name']);
     // Standard clean datas
     $input = parent::prepareInputForAdd($input);
     // Do not check mandatory on auto import (mailgates)
     if (!isset($input['_auto_import'])) {
         if (isset($input['_tickettemplates_id']) && $input['_tickettemplates_id']) {
             $tt = new TicketTemplate();
             if ($tt->getFromDBWithDatas($input['_tickettemplates_id'])) {
                 if (count($tt->mandatory)) {
                     $mandatory_missing = array();
                     $fieldsname = $tt->getAllowedFieldsNames(true);
                     foreach ($tt->mandatory as $key => $val) {
                         // for title if mandatory (restore initial value)
                         if ($key == 'name') {
                             $input['name'] = $title;
                         }
                         // Check only defined values : Not defined not in form
                         if (isset($input[$key])) {
                             // If content is also predefined need to be different from predefined value
                             if ($key == 'content' && isset($tt->predefined['content'])) {
                                 // Clean new lines to be fix encoding
                                 if (strcmp(preg_replace("/\r?\n/", "", Html::cleanPostForTextArea($input[$key])), preg_replace("/\r?\n/", "", $tt->predefined['content'])) == 0) {
                                     $mandatory_missing[$key] = $fieldsname[$val];
                                 }
                             }
                             if (empty($input[$key]) || $input[$key] == 'NULL' || is_array($input[$key]) && $input[$key] === array(0 => "0")) {
                                 $mandatory_missing[$key] = $fieldsname[$val];
                             }
                         }
                         if ($key == '_add_validation' && !empty($input['users_id_validate']) && isset($input['users_id_validate'][0]) && $input['users_id_validate'][0] > 0) {
                             unset($mandatory_missing['_add_validation']);
                         }
                         // For due_date and time_to_own : check also slts
                         foreach (array(SLT::TTR, SLT::TTO) as $sltType) {
                             list($dateField, $sltField) = SLT::getSltFieldNames($sltType);
                             if ($key == $dateField && isset($input[$sltField]) && $input[$sltField] > 0 && isset($mandatory_missing[$dateField])) {
                                 unset($mandatory_missing[$dateField]);
                             }
                         }
                         // For document mandatory
                         if ($key == '_documents_id' && !isset($input['_filename']) && !isset($input['_tag_filename']) && !isset($input['_stock_image']) && !isset($input['_tag_stock_image'])) {
                             $mandatory_missing[$key] = $fieldsname[$val];
                         }
                     }
                     if (count($mandatory_missing)) {
                         //TRANS: %s are the fields concerned
                         $message = sprintf(__('Mandatory fields are not filled. Please correct: %s'), implode(", ", $mandatory_missing));
                         Session::addMessageAfterRedirect($message, false, ERROR);
                         return false;
                     }
                 }
             }
         }
     }
     if (!isset($input["requesttypes_id"])) {
         $input["requesttypes_id"] = RequestType::getDefault('helpdesk');
     }
     if (!isset($input['global_validation'])) {
         $input['global_validation'] = CommonITILValidation::NONE;
     }
     // Set additional default dropdown
     $dropdown_fields = array('users_locations', 'items_locations');
     foreach ($dropdown_fields as $field) {
         if (!isset($input[$field])) {
             $input[$field] = 0;
         }
     }
     if (!isset($input['itemtype']) || !isset($input['items_id']) || !($input['items_id'] > 0)) {
         $input['itemtype'] = '';
     }
     // Get first item location
     $item = NULL;
     if (isset($input["items_id"]) && is_array($input["items_id"]) && count($input["items_id"]) > 0) {
         foreach ($input["items_id"] as $itemtype => $items) {
             foreach ($items as $items_id) {
                 if ($item = getItemForItemtype($itemtype)) {
                     $item->getFromDB($items_id);
                     $input['items_locations'] = $item->fields['locations_id'];
                     $input['items_groups'] = $item->fields['groups_id'];
                     break 2;
                 }
             }
         }
     }
     // Business Rules do not override manual SLT
     $manual_slts_id = array();
     foreach (array(SLT::TTR, SLT::TTO) as $sltType) {
         list($dateField, $sltField) = SLT::getSltFieldNames($sltType);
         if (isset($input[$sltField]) && $input[$sltField] > 0) {
             $manual_slts_id[$sltType] = $input[$sltField];
         }
     }
     // Process Business Rules
     $rules = new RuleTicketCollection($input['entities_id']);
     // Set unset variables with are needed
     $user = new User();
     if (isset($input["_users_id_requester"]) && !is_array($input["_users_id_requester"]) && $user->getFromDB($input["_users_id_requester"])) {
         $input['users_locations'] = $user->fields['locations_id'];
         $tmprequester = $input["_users_id_requester"];
     } else {
         $tmprequester = 0;
     }
     // Clean new lines before passing to rules
     if (isset($input["content"])) {
         $input["content"] = preg_replace('/\\\\r\\\\n/', "\n", $input['content']);
         $input["content"] = preg_replace('/\\\\n/', "\n", $input['content']);
         if (!$CFG_GLPI['use_rich_text']) {
             $input["content"] = Html::entity_decode_deep($input["content"]);
             $input["content"] = Html::entity_decode_deep($input["content"]);
             $input["content"] = Html::clean($input["content"]);
         }
     }
     $input = $rules->processAllRules(Toolbox::stripslashes_deep($input), Toolbox::stripslashes_deep($input), array('recursive' => true), array('condition' => RuleTicket::ONADD));
     // Recompute default values based on values computed by rules
     $input = $this->computeDefaultValuesForAdd($input);
     if (isset($input['_users_id_requester']) && !is_array($input['_users_id_requester']) && $input['_users_id_requester'] != $tmprequester) {
         // if requester set by rule, clear address from mailcollector
         unset($input['_users_id_requester_notif']);
     }
     // Manage auto assign
     $auto_assign_mode = Entity::getUsedConfig('auto_assign_mode', $input['entities_id']);
     switch ($auto_assign_mode) {
         case Entity::CONFIG_NEVER:
             break;
         case Entity::AUTO_ASSIGN_HARDWARE_CATEGORY:
             if ($item != NULL) {
                 // Auto assign tech from item
                 if ((!isset($input['_users_id_assign']) || $input['_users_id_assign'] == 0) && $item->isField('users_id_tech')) {
                     $input['_users_id_assign'] = $item->getField('users_id_tech');
                 }
                 // Auto assign group from item
                 if ((!isset($input['_groups_id_assign']) || $input['_groups_id_assign'] == 0) && $item->isField('groups_id_tech')) {
                     $input['_groups_id_assign'] = $item->getField('groups_id_tech');
                 }
             }
             // Auto assign tech/group from Category
             if ($input['itilcategories_id'] > 0 && (!isset($input['_users_id_assign']) || !$input['_users_id_assign'] || (!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']))) {
                 $cat = new ITILCategory();
                 $cat->getFromDB($input['itilcategories_id']);
                 if ((!isset($input['_users_id_assign']) || !$input['_users_id_assign']) && $cat->isField('users_id')) {
                     $input['_users_id_assign'] = $cat->getField('users_id');
                 }
                 if ((!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']) && $cat->isField('groups_id')) {
                     $input['_groups_id_assign'] = $cat->getField('groups_id');
                 }
             }
             break;
         case Entity::AUTO_ASSIGN_CATEGORY_HARDWARE:
             // Auto assign tech/group from Category
             if ($input['itilcategories_id'] > 0 && (!isset($input['_users_id_assign']) || !$input['_users_id_assign'] || (!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']))) {
                 $cat = new ITILCategory();
                 $cat->getFromDB($input['itilcategories_id']);
                 if ((!isset($input['_users_id_assign']) || !$input['_users_id_assign']) && $cat->isField('users_id')) {
                     $input['_users_id_assign'] = $cat->getField('users_id');
                 }
                 if ((!isset($input['_groups_id_assign']) || !$input['_groups_id_assign']) && $cat->isField('groups_id')) {
                     $input['_groups_id_assign'] = $cat->getField('groups_id');
                 }
             }
             if ($item != NULL) {
                 // Auto assign tech from item
                 if ((!isset($input['_users_id_assign']) || $input['_users_id_assign'] == 0) && $item->isField('users_id_tech')) {
                     $input['_users_id_assign'] = $item->getField('users_id_tech');
                 }
                 // Auto assign group from item
                 if ((!isset($input['_groups_id_assign']) || $input['_groups_id_assign'] == 0) && $item->isField('groups_id_tech')) {
                     $input['_groups_id_assign'] = $item->getField('groups_id_tech');
                 }
             }
             break;
     }
     // Replay setting auto assign if set in rules engine or by auto_assign_mode
     if ((isset($input["_users_id_assign"]) && (!is_array($input['_users_id_assign']) && $input["_users_id_assign"] > 0 || is_array($input['_users_id_assign']) && count($input['_users_id_assign']) > 0) || isset($input["_groups_id_assign"]) && (!is_array($input['_groups_id_assign']) && $input["_groups_id_assign"] > 0 || is_array($input['_groups_id_assign']) && count($input['_groups_id_assign']) > 0) || isset($input["_suppliers_id_assign"]) && (!is_array($input['_suppliers_id_assign']) && $input["_suppliers_id_assign"] > 0 || is_array($input['_suppliers_id_assign']) && count($input['_suppliers_id_assign']) > 0)) && in_array($input['status'], $this->getNewStatusArray())) {
         $input["status"] = self::ASSIGNED;
     }
     // Manage SLT signment
     // Manual SLT defined : reset due date
     // No manual SLT and due date defined : reset auto SLT
     foreach (array(SLT::TTR, SLT::TTO) as $sltType) {
         $this->sltAffect($sltType, $input, $manual_slts_id);
     }
     // auto set type if not set
     if (!isset($input["type"])) {
         $input['type'] = Entity::getUsedConfig('tickettype', $input['entities_id'], '', Ticket::INCIDENT_TYPE);
     }
     return $input;
 }
 /**
  * Print the hidden fields
  *
  * @since version 0.83
  *
  * @param $tt                       Ticket Template
  * @param $withtemplate    boolean  Template or basic item (default '')
  *
  * @return Nothing (call to classes members)
  **/
 static function showForTicketTemplate(TicketTemplate $tt, $withtemplate = '')
 {
     global $DB;
     $ID = $tt->fields['id'];
     if (!$tt->getFromDB($ID) || !$tt->can($ID, "r")) {
         return false;
     }
     $ttm = new self();
     $used = $ttm->getHiddenFields($ID);
     $canedit = $tt->can($ID, "w");
     $fields = $tt->getAllowedFieldsNames(false, isset($used['itemtype']));
     $rand = mt_rand();
     $query = "SELECT `glpi_tickettemplatehiddenfields`.*\n                FROM `glpi_tickettemplatehiddenfields`\n                WHERE (`tickettemplates_id` = '{$ID}')";
     if ($result = $DB->query($query)) {
         $hiddenfields = array();
         $used = array();
         if ($numrows = $DB->numrows($result)) {
             while ($data = $DB->fetch_assoc($result)) {
                 $hiddenfields[$data['id']] = $data;
                 $used[$data['num']] = $data['num'];
             }
         }
         if ($canedit) {
             echo "<div class='firstbloc'>";
             echo "<form name='changeproblem_form{$rand}' id='changeproblem_form{$rand}' method='post'\n                   action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
             echo "<table class='tab_cadre_fixe'>";
             echo "<tr class='tab_bg_2'><th colspan='2'>" . __('Add a hidden field') . "</th></tr>";
             echo "<tr class='tab_bg_2'><td class='right'>";
             echo "<input type='hidden' name='tickettemplates_id' value='{$ID}'>";
             Dropdown::showFromArray('num', $fields, array('used' => $used));
             echo "</td><td class='center'>";
             echo "&nbsp;<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
             echo "</td></tr>";
             echo "</table>";
             Html::closeForm();
             echo "</div>";
         }
         echo "<div class='spaced'>";
         if ($canedit && $numrows) {
             Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
             $massiveactionparams = array('num_displayed' => $numrows);
             Html::showMassiveActions(__CLASS__, $massiveactionparams);
         }
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr><th colspan='2'>";
         echo self::getTypeName($DB->numrows($result));
         echo "</th></tr>";
         if ($numrows) {
             echo "<tr>";
             if ($canedit) {
                 echo "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
             }
             echo "<th>" . __('Name') . "</th>";
             echo "</tr>";
             foreach ($hiddenfields as $data) {
                 echo "<tr class='tab_bg_2'>";
                 if ($canedit) {
                     echo "<td>" . Html::getMassiveActionCheckBox(__CLASS__, $data["id"]) . "</td>";
                 }
                 echo "<td>" . $fields[$data['num']] . "</td>";
                 echo "</tr>";
             }
         } else {
             echo "<tr><th colspan='2'>" . __('No item found') . "</th></tr>";
         }
         echo "</table>";
         if ($canedit && $numrows) {
             $massiveactionparams['ontop'] = false;
             Html::showMassiveActions(__CLASS__, $massiveactionparams);
             Html::closeForm();
         }
         echo "</div>";
     }
 }