/**
  * Add a delay of a starting hour in a specific day
  *
  * @param $calendars_id id of the calendar
  * @param $day day number
  * @param $begin_time begin time
  * @param delay timestamp delay to add
  *
  * @return timestamp value
  **/
 static function addDelayInDay($calendars_id, $day, $begin_time, $delay)
 {
     global $DB;
     $sum = 0;
     // Do not check hour if day before the end day of after the begin day
     $query = "SELECT GREATEST(`begin`,'{$begin_time}') AS BEGIN,\n                       TIMEDIFF(`end`,GREATEST(`begin`,'{$begin_time}')) AS TDIFF\n                FROM `glpi_calendarsegments`\n                WHERE `calendars_id` = '{$calendars_id}'\n                     AND `day` = '{$day}'\n                     AND ('{$begin_time}' < `end`)\n                ORDER BY `begin`";
     if ($result = $DB->query($query)) {
         if ($DB->numrows($result)) {
             while ($data = $DB->fetch_assoc($result)) {
                 list($hour, $minute, $second) = explode(':', $data['TDIFF']);
                 $tstamp = $hour * HOUR_TIMESTAMP + $minute * MINUTE_TIMESTAMP + $second;
                 // Delay is completed
                 if ($delay <= $tstamp) {
                     list($begin_hour, $begin_minute, $begin_second) = explode(':', $data['BEGIN']);
                     $beginstamp = $begin_hour * HOUR_TIMESTAMP + $begin_minute * MINUTE_TIMESTAMP + $begin_second;
                     $endstamp = $beginstamp + $delay;
                     $units = getTimestampTimeUnits($endstamp);
                     return str_pad($units['hour'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($units['minute'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($units['second'], 2, '0', STR_PAD_LEFT);
                 } else {
                     $delay -= $tstamp;
                 }
             }
         }
     }
     return false;
 }
 /** form for Task
  *
  * @param $ID Integer : Id of the task
  * @param $options array
  *     -  ticket Object : the ticket
  **/
 function showForm($ID, $options = array())
 {
     global $DB, $LANG, $CFG_GLPI;
     if (isset($options['ticket']) && !empty($options['ticket'])) {
         $ticket = $options['ticket'];
     }
     if ($ID > 0) {
         $this->check($ID, 'r');
     } else {
         // Create item
         $input = array('tickets_id' => $ticket->getField('id'));
         $this->check(-1, 'w', $input);
     }
     $canplan = haveRight("show_planning", "1");
     $this->showFormHeader($options);
     echo "<tr class='tab_bg_1'>";
     echo "<td rowspan='5' class='middle right'>" . $LANG['joblist'][6] . "&nbsp;:</td>";
     echo "<td class='center middle' rowspan='5'>" . "<textarea name='content' cols='50' rows='6'>" . $this->fields["content"] . "</textarea></td>";
     if ($this->fields["date"]) {
         echo "<td>" . $LANG['common'][27] . "&nbsp;:</td>";
         echo "<td>" . convDateTime($this->fields["date"]);
     } else {
         echo "<td colspan='2'>&nbsp;";
     }
     echo "<input type='hidden' name='tickets_id' value='" . $this->fields["tickets_id"] . "'>";
     echo "</td></tr>\n";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . $LANG['common'][36] . "&nbsp;:</td><td>";
     Dropdown::show('TaskCategory', array('value' => $this->fields["taskcategories_id"]));
     echo "</td></tr>\n";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . $LANG['common'][77] . "&nbsp;:</td>";
     echo "<td><select name='is_private'>";
     echo "<option value='0' " . (!$this->fields["is_private"] ? " selected" : "") . ">" . $LANG['choice'][0] . "</option>";
     echo "<option value='1' " . ($this->fields["is_private"] ? " selected" : "") . ">" . $LANG['choice'][1] . "</option>";
     echo "</select></td>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . $LANG['job'][31] . "&nbsp;:</td><td>";
     $units = getTimestampTimeUnits($this->fields["actiontime"]);
     $hour = $units['hour'] + 24 * $units['day'];
     $minute = $units['minute'];
     Dropdown::showInteger('hour', $hour, 0, 100, 1, array($hour));
     echo "&nbsp;" . $LANG['job'][21] . "&nbsp;&nbsp;";
     Dropdown::showInteger('minute', $minute, 0, 59);
     echo "&nbsp;" . $LANG['job'][22];
     echo "</td></tr>\n";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . $LANG['job'][35] . "</td>";
     echo "<td>";
     $plan = new TicketPlanning();
     $plan->showFormForTask($ticket, $this);
     echo "</td></tr>";
     $this->showFormButtons($options);
     return true;
 }
/**
 * Make a good string from the unix timestamp $sec
 *
 * @param $time integer: timestamp
 * @param $display_sec boolean: display seconds ?
 *
 * @return string
**/
function timestampToString($time, $display_sec = true)
{
    global $LANG;
    $sign = '';
    if ($time < 0) {
        $sign = '- ';
        $time = abs($time);
    }
    $time = floor($time);
    // Force display seconds if time is null
    if ($time == 0) {
        $display_sec = true;
    }
    $units = getTimestampTimeUnits($time);
    $out = $sign;
    if ($units['day'] > 0) {
        $out .= " " . $units['day'] . "&nbsp;" . $LANG['stats'][31];
    }
    if ($units['hour'] > 0) {
        $out .= " " . $units['hour'] . "&nbsp;" . $LANG['job'][21];
    }
    if ($units['minute'] > 0) {
        $out .= " " . $units['minute'] . "&nbsp;" . $LANG['stats'][33];
    }
    if ($display_sec) {
        $out .= " " . $units['second'] . "&nbsp;" . $LANG['stats'][34];
    }
    return $out;
}