/**
  * 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);
     $ttp = new self();
     $used_fields = $ttp->getPredefinedFields($ID, true);
     $itemtype_used = '';
     if (isset($used_fields['itemtype'])) {
         $itemtype_used = $used_fields['itemtype'];
     }
     $fields = $tt->getAllowedFieldsNames(true, isset($used_fields['itemtype']));
     $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_datas = array('itemtype' => $itemtype_used);
     $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', 'additionalvalues' => array('itemtype' => $itemtype_used), '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>";
     }
 }