/**
  * Return array with all data
  *
  * @param Ticket $ticket
  * @param type $type 'user' or 'group'
  * @param type $withblank option to fill blank zones
  *
  * @return type
  */
 static function getDetails(Ticket $ticket, $type, $withblank = 1)
 {
     if ($type == 'group') {
         $palette = array(array('250', '151', '186'), array('255', '211', '112'), array('183', '210', '118'), array('117', '199', '187'), array('188', '168', '208'), array('186', '213', '118'), array('124', '169', '0'), array('168', '208', '49'), array('239', '215', '113'), array('235', '155', '0'), array('235', '249', '255'), array('193', '228', '250'), array('164', '217', '250'), array('88', '195', '240'), array('0', '156', '231'), array('198', '229', '111'), array('234', '38', '115'), array('245', '122', '160'), array('255', '208', '220'));
     } else {
         if ($type == 'user') {
             $palette = array(array('164', '53', '86'), array('137', '123', '78'), array('192', '114', '65'), array('143', '102', '98'), array('175', '105', '93'), array('186', '127', '61'), array('174', '104', '92'), array('213', '113', '63'), array('185', '168', '122'), array('233', '168', '112'), array('199', '133', '99'), array('80', '24', '69'), array('133', '39', '65'), array('120', '22', '61'), array('114', '59', '82'), array('245', '229', '195'));
         }
     }
     $ptState = new PluginTimelineticketState();
     $a_ret = PluginTimelineticketDisplay::getTotaltimeEnddate($ticket);
     $totaltime = $a_ret['totaltime'];
     if ($type == 'group') {
         $ptItem = new PluginTimelineticketAssignGroup();
     } else {
         if ($type == 'user') {
             $ptItem = new PluginTimelineticketAssignUser();
         }
     }
     $a_states = array();
     $a_item_palette = array();
     $a_dbstates = $ptState->find("`tickets_id`='" . $ticket->getField('id') . "'", "date");
     $end_previous = 0;
     foreach ($a_dbstates as $a_dbstate) {
         $end_previous += $a_dbstate['delay'];
         if ($a_dbstate['old_status'] == '') {
             $a_dbstate['old_status'] = 0;
         }
         if (isset($a_states[$end_previous])) {
             $end_previous++;
         }
         $a_states[$end_previous] = $a_dbstate['old_status'];
     }
     if (isset($a_dbstate['new_status']) && $a_dbstate['new_status'] != Ticket::CLOSED) {
         $a_states[$totaltime] = $a_dbstate['new_status'];
     }
     $a_itemsections = array();
     $a_dbitems = $ptItem->find("`tickets_id`='" . $ticket->getField('id') . "'", "`date`");
     foreach ($a_dbitems as $a_dbitem) {
         if ($type == 'group') {
             $items_id = 'groups_id';
         } else {
             if ($type == 'user') {
                 $items_id = 'users_id';
             }
         }
         if (!isset($a_itemsections[$a_dbitem[$items_id]])) {
             $a_itemsections[$a_dbitem[$items_id]] = array();
             $last_statedelay = 0;
         } else {
             foreach ($a_itemsections[$a_dbitem[$items_id]] as $data) {
                 $last_statedelay = $data['End'];
             }
         }
         if (!isset($a_item_palette[$a_dbitem[$items_id]])) {
             $a_item_palette[$a_dbitem[$items_id]] = array_shift($palette);
         }
         $color_R = $a_item_palette[$a_dbitem[$items_id]][0];
         $color_G = $a_item_palette[$a_dbitem[$items_id]][1];
         $color_B = $a_item_palette[$a_dbitem[$items_id]][2];
         $gbegin = $a_dbitem['begin'];
         if ($a_dbitem['delay'] == '') {
             $gdelay = $totaltime;
         } else {
             $gdelay = $a_dbitem['begin'] + $a_dbitem['delay'];
         }
         $mem = 0;
         $old_delay = 0;
         foreach ($a_states as $delay => $statusname) {
             if ($mem == 1) {
                 if ($gdelay > $delay) {
                     // all time of the state
                     $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $gbegin, 'End' => $delay, "Caption" => "", "Status" => $statusname, "R" => $color_R, "G" => $color_G, "B" => $color_B);
                     $gbegin = $delay;
                 } else {
                     if ($gdelay == $delay) {
                         // end of status = end of group
                         $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $gbegin, 'End' => $delay, "Caption" => "", "Status" => $statusname, "R" => $color_R, "G" => $color_G, "B" => $color_B);
                         $mem = 2;
                     } else {
                         // end of status is after end of group
                         $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $gbegin, 'End' => $gdelay, "Caption" => "", "Status" => $statusname, "R" => $color_R, "G" => $color_G, "B" => $color_B);
                         $mem = 2;
                     }
                 }
             } else {
                 if ($mem == 0 && $gbegin < $delay) {
                     if ($withblank && $gbegin != $last_statedelay) {
                         $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $last_statedelay, 'End' => $gbegin, "Caption" => "", "Status" => "", "R" => 235, "G" => 235, "B" => 235);
                     }
                     if ($gdelay > $delay) {
                         // all time of the state
                         $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $gbegin, 'End' => $delay, "Caption" => "", "Status" => $statusname, "R" => $color_R, "G" => $color_G, "B" => $color_B);
                         $gbegin = $delay;
                         $mem = 1;
                     } else {
                         if ($gdelay == $delay) {
                             // end of status = end of group
                             $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $gbegin, 'End' => $delay, "Caption" => "", "Status" => $statusname, "R" => $color_R, "G" => $color_G, "B" => $color_B);
                             $mem = 2;
                         } else {
                             // end of status is after end of group
                             $a_itemsections[$a_dbitem[$items_id]][] = array('Start' => $gbegin, 'End' => $gdelay, "Caption" => "", "Status" => $statusname, "R" => $color_R, "G" => $color_G, "B" => $color_B);
                             $mem = 2;
                         }
                     }
                 }
             }
             $old_delay = $delay;
         }
     }
     if ($withblank) {
         end($a_states);
         $verylastdelayStateDB = key($a_states);
         foreach ($a_itemsections as $items_id => $data_f) {
             $last = 0;
             $R = 235;
             $G = 235;
             $B = 235;
             $statusname = '';
             $a_end = end($data_f);
             $last = $a_end['End'];
             if ($ticket->fields['status'] != Ticket::CLOSED && $last == $verylastdelayStateDB) {
                 $R = $a_end['R'];
                 $G = $a_end['G'];
                 $B = $a_end['B'];
                 $statusname = $a_end['Status'];
             }
             if ($last < $totaltime) {
                 $a_itemsections[$items_id][] = array('Start' => $last, 'End' => $totaltime, "Caption" => "", "Status" => $statusname, "R" => $R, "G" => $G, "B" => $B);
             }
         }
     }
     return $a_itemsections;
 }
 static function showForTicket(Ticket $ticket)
 {
     global $DB, $CFG_GLPI;
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr><th>" . __('Summary') . "</th></tr>";
     echo "<tr class='tab_bg_1 center'><td>" . _n('Time range', 'Time ranges', 2) . "&nbsp;: ";
     $calendar = new Calendar();
     $calendars_id = Entity::getUsedConfig('calendars_id', $ticket->fields['entities_id']);
     if ($calendars_id > 0 && $calendar->getFromDB($calendars_id)) {
         echo $calendar->getLink();
     } else {
         echo NOT_AVAILABLE;
     }
     echo "</td></tr>";
     PluginTimelineticketState::showHistory($ticket);
     // Display ticket have Due date
     if ($ticket->fields['due_date'] && $ticket->fields['status'] != CommonITILObject::WAITING && strtotime(date('Y-m-d H:i:s')) - strtotime($ticket->fields['due_date']) > 0) {
         $calendar = new Calendar();
         $calendars_id = Entity::getUsedConfig('calendars_id', $ticket->fields['entities_id']);
         if ($calendars_id > 0 && $calendar->getFromDB($calendars_id)) {
             if ($ticket->fields['closedate']) {
                 $dateend = $calendar->getActiveTimeBetween($ticket->fields['due_date'], $ticket->fields['closedate']);
             } else {
                 $dateend = $calendar->getActiveTimeBetween($ticket->fields['due_date'], date('Y-m-d H:i:s'));
             }
         } else {
             // cas 24/24 - 7/7
             if ($ticket->fields['closedate']) {
                 $dateend = strtotime($ticket->fields['closedate']) - strtotime($ticket->fields['due_date']);
             } else {
                 $dateend = strtotime(date('Y-m-d H:i:s')) - strtotime($ticket->fields['due_date']);
             }
         }
         echo "<tr>";
         echo "<th>" . __('Late') . "</th>";
         echo "</tr>";
         echo "<tr>";
         echo "<td align='center' class='tab_bg_2_2'>" . Html::timestampToString($dateend, true) . "</td>";
         echo "</tr>";
     }
     echo "</table>";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     echo "<th colspan='2'>" . __('Status') . "</th>";
     echo "</tr>";
     /* pChart library inclusions */
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pData.class.php";
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pDraw.class.php";
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pImage.class.php";
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pIndicator.class.php";
     $a_data = PluginTimelineticketDisplay::getTotaltimeEnddate($ticket);
     $totaltime = $a_data['totaltime'];
     $end_date = $a_data['end_date'];
     $params = array('totaltime' => $totaltime, 'end_date' => $end_date);
     $ptState = new PluginTimelineticketState();
     $ptState->showTimeline($ticket, $params);
     $ptAssignGroup = new PluginTimelineticketAssignGroup();
     $ptAssignGroup->showTimeline($ticket, $params);
     $ptAssignUser = new PluginTimelineticketAssignUser();
     $ptAssignUser->showTimeline($ticket, $params);
     echo "</table>";
     PluginTimelineticketToolbox::ShowDetail($ticket, 'group');
     PluginTimelineticketToolbox::ShowDetail($ticket, 'user');
     if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) {
         echo "<br><table class='tab_cadre_fixe'>";
         echo "<tr>";
         echo "<th colspan='5'>" . __('DEBUG') . " " . __('Group') . "</th>";
         echo "</tr>";
         echo "<tr>";
         echo "<th>" . __('ID') . "</th>";
         echo "<th>" . __('Date') . "</th>";
         echo "<th>" . __('Group') . "</th>";
         echo "<th>" . __('Begin') . "</th>";
         echo "<th>" . __('Delay', 'timelineticket') . "</th>";
         echo "</tr>";
         $query = "SELECT *\n                         FROM `glpi_plugin_timelineticket_assigngroups`\n                         WHERE `tickets_id` = '" . $ticket->getID() . "'";
         $result = $DB->query($query);
         while ($data = $DB->fetch_assoc($result)) {
             echo "<tr class='tab_bg_1'>";
             echo "<td>" . $data['id'] . "</td>";
             echo "<td>" . Html::convDateTime($data['date']) . "</td>";
             echo "<td>" . Dropdown::getDropdownName("glpi_groups", $data['groups_id']) . "</td>";
             echo "<td>" . Html::timestampToString($data['begin']) . "</td>";
             echo "<td>" . Html::timestampToString($data['delay']) . "</td>";
             echo "</tr>";
         }
         echo "</table>";
         echo "<br><table class='tab_cadre_fixe'>";
         echo "<tr>";
         echo "<th colspan='5'>" . __('DEBUG') . " " . __('Technician') . "</th>";
         echo "</tr>";
         echo "<tr>";
         echo "<th>" . __('ID') . "</th>";
         echo "<th>" . __('Date') . "</th>";
         echo "<th>" . __('Technician') . "</th>";
         echo "<th>" . __('Begin') . "</th>";
         echo "<th>" . __('Delay', 'timelineticket') . "</th>";
         echo "</tr>";
         $query = "SELECT *\n                         FROM `glpi_plugin_timelineticket_assignusers`\n                         WHERE `tickets_id` = '" . $ticket->getID() . "'";
         $result = $DB->query($query);
         while ($data = $DB->fetch_assoc($result)) {
             echo "<tr class='tab_bg_1'>";
             echo "<td>" . $data['id'] . "</td>";
             echo "<td>" . Html::convDateTime($data['date']) . "</td>";
             echo "<td>" . getUserName($data['users_id']) . "</td>";
             echo "<td>" . Html::timestampToString($data['begin']) . "</td>";
             echo "<td>" . Html::timestampToString($data['delay']) . "</td>";
             echo "</tr>";
         }
         echo "</table>";
     }
 }
 static function showForTicket(Ticket $ticket)
 {
     global $DB, $CFG_GLPI;
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr><th>" . __('Summary') . "</th></tr>";
     echo "<tr class='tab_bg_1 center'><td>" . _n('Time range', 'Time ranges', 2) . "&nbsp;: ";
     $calendar = new Calendar();
     $calendars_id = Entity::getUsedConfig('calendars_id', $ticket->fields['entities_id']);
     if ($calendars_id > 0 && $calendar->getFromDB($calendars_id)) {
         echo $calendar->getLink();
     } else {
         echo NOT_AVAILABLE;
     }
     echo "</td></tr>";
     PluginTimelineticketState::showHistory($ticket);
     // Display ticket have Due date
     if ($ticket->fields['due_date'] && $ticket->fields['status'] != CommonITILObject::WAITING && strtotime(date('Y-m-d H:i:s')) - strtotime($ticket->fields['due_date']) > 0) {
         $calendar = new Calendar();
         $calendars_id = Entity::getUsedConfig('calendars_id', $ticket->fields['entities_id']);
         if ($calendars_id > 0 && $calendar->getFromDB($calendars_id)) {
             if ($ticket->fields['closedate']) {
                 $dateend = $calendar->getActiveTimeBetween($ticket->fields['due_date'], $ticket->fields['closedate']);
             } else {
                 $dateend = $calendar->getActiveTimeBetween($ticket->fields['due_date'], date('Y-m-d H:i:s'));
             }
         } else {
             // cas 24/24 - 7/7
             if ($ticket->fields['closedate']) {
                 $dateend = strtotime($ticket->fields['closedate']) - strtotime($ticket->fields['due_date']);
             } else {
                 $dateend = strtotime(date('Y-m-d H:i:s')) - strtotime($ticket->fields['due_date']);
             }
         }
         echo "<tr>";
         echo "<th>" . __('Late') . "</th>";
         echo "</tr>";
         echo "<tr>";
         echo "<td align='center' class='tab_bg_2_2'>" . Html::timestampToString($dateend, true) . "</td>";
         echo "</tr>";
     }
     echo "</table>";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     echo "<th colspan='2'>" . __('Status') . "</th>";
     echo "</tr>";
     /* pChart library inclusions */
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pData.class.php";
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pDraw.class.php";
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pImage.class.php";
     include GLPI_ROOT . "/plugins/timelineticket/lib/pChart2.1.4/class/pIndicator.class.php";
     $a_data = PluginTimelineticketDisplay::getTotaltimeEnddate($ticket);
     $totaltime = $a_data['totaltime'];
     $end_date = $a_data['end_date'];
     $params = array('totaltime' => $totaltime, 'end_date' => $end_date);
     $ptState = new PluginTimelineticketState();
     $ptState->showTimeline($ticket, $params);
     $ptAssignGroup = new PluginTimelineticketAssignGroup();
     $ptAssignGroup->showTimeline($ticket, $params);
     $ptAssignUser = new PluginTimelineticketAssignUser();
     $ptAssignUser->showTimeline($ticket, $params);
     echo "</table>";
     PluginTimelineticketToolbox::ShowDetail($ticket, 'group');
     PluginTimelineticketToolbox::ShowDetail($ticket, 'user');
 }
 private function totaltimeEnddateOfTicket()
 {
     global $DB;
     $DB->connect();
     $ticket = new Ticket();
     //      $a_storedate_temp = self::$storedate;
     $ticket->getFromDB(1);
     $a_data = PluginTimelineticketDisplay::getTotaltimeEnddate($ticket);
     $totaltime_ref = 0;
     $a_states = getAllDatasFromTable('glpi_plugin_timelineticket_states', '', FALSE, 'id');
     foreach ($a_states as $data) {
         $totaltime_ref += $data['delay'];
     }
     $this->assertEquals($totaltime_ref, $a_data['totaltime'], 'Totaltime of ticket (without hours of calendar');
 }