<?php
/*
 * @version $Id: HEADER 15930 2011-10-30 15:47:55Z tsmr $
 -------------------------------------------------------------------------
 Manageentities plugin for GLPI
 Copyright (C) 2003-2011 by the Manageentities Development Team.

 https://forge.indepnet.net/projects/manageentities
 -------------------------------------------------------------------------

 LICENSE
		
 This file is part of Manageentities.

 Manageentities is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Manageentities is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Manageentities. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 */

if (!defined('GLPI_ROOT')) {
	die("Sorry. You can't access directly to this file");
}

class PluginManageentitiesCriDetail extends CommonDBTM {
	
	function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
      global $LANG;

      if ($item->getType()=='Ticket' && plugin_manageentities_haveRight("cri_create","r")) {
            return $LANG['plugin_manageentities']['report'][0];
      }
      return '';
   }


   static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
      global $CFG_GLPI, $DB, $LANG;

      if (get_class($item)=='Ticket') {
         $cpt=0;
         $query = "SELECT COUNT(*) AS cpt
                  FROM `glpi_tickettasks`
                  WHERE `glpi_tickettasks`.`tickets_id` = '".$item->getField('id')."' ";
         $result = $DB->query($query);
         while ($data = $DB->fetch_array($result)) {
            $cpt= $data["cpt"];
         }
         if ($cpt!=0) {

            self::addReports($item->getField('id'));
            self::showReports(get_class($item),$item->getField('id'));

         } else {
            echo $LANG['plugin_manageentities']['cri'][38];
            echo "<br>";
         }
      }
      return true;
   }
   
	//Shows CRI from check date - report.form.php function
   function showHelpdeskReports($usertype,$technum,$date1,$date2) {
      global $DB,$CFG_GLPI, $LANG;

      $config= new PluginManageentitiesConfig();
      $config->GetFromDB(1);

      $query = "SELECT `glpi_documents`.*,`glpi_tickets_users`.`users_id`, `glpi_entities`.`id` AS entity, `".$this->getTable()."`.`date`, `".$this->getTable()."`.`technicians`, `".$this->getTable()."`.`plugin_manageentities_critypes_id`, `".$this->getTable()."`.`withcontract`, `".$this->getTable()."`.`contracts_id`, `".$this->getTable()."`.`realtime` "
      ." FROM `glpi_documents` "
      ." LEFT JOIN `glpi_entities` ON (`glpi_documents`.`entities_id` = `glpi_entities`.`id`)"
      ." LEFT JOIN `glpi_tickets` ON (`glpi_documents`.`tickets_id` = `glpi_tickets`.`id`)"
      ." LEFT JOIN `glpi_tickets_users` ON (`glpi_tickets_users`.`tickets_id` = `glpi_tickets`.`id`)"
      ." LEFT JOIN `glpi_plugin_manageentities_cridetails` ON (`glpi_documents`.`id` = `".$this->getTable()."`.`documents_id`) "
      ." LEFT JOIN `glpi_plugin_manageentities_critechnicians` ON (`glpi_documents`.`tickets_id` = `glpi_plugin_manageentities_critechnicians`.`tickets_id`) "
      ." WHERE `glpi_tickets_users`.`type` = ".Ticket::ASSIGN." AND `documentcategories_id` = '".$config->fields["documentcategories_id"]."' AND `".$this->getTable()."`.`date` >= '". $date1 ."' AND `".$this->getTable()."`.`date` <= '". $date2 ."' ";
      if ($usertype!="group")
        $query.= " AND (`glpi_tickets_users`.`users_id` ='".$technum."' OR `glpi_plugin_manageentities_critechnicians`.`users_id` ='".$technum."') ";

      $query .= getEntitiesRestrictRequest(" AND","glpi_documents",'','',true);
      
      //if ($usertype=="group")
         $query.= " GROUP BY `glpi_documents`.`tickets_id` ";
      $query.= "ORDER BY `".$this->getTable()."`.`date` ASC";

      $result = $DB->query($query);
      $number = $DB->numrows($result);

      if (Session::isMultiEntitiesMode()) {
        $colsup=1;
      } else {
        $colsup=0;
      }

      if ($number !="0") {

        echo "<form method='post' action=\"./front/entity.php\">";
        echo "<div align='center'><table class='tab_cadre center' width='95%'>";
        echo "<tr><th colspan='".(12+$colsup)."'>".$LANG['plugin_manageentities']["onglet"][3];
        if ($usertype!="group")
            echo " -".getusername($technum);
        echo " ".$LANG['plugin_manageentities']['report'][4]." ".Html::convdate($date1)." ".$LANG['plugin_manageentities']['report'][5]." ".Html::convdate($date2)."</th></tr>";
        echo "<tr>";
        if (Session::isMultiEntitiesMode())
            echo "<th>".$LANG["entity"][0]."</th>";
        echo "<th>".$LANG["common"][27]."</th>";
        echo "<th>".$LANG['plugin_manageentities']['infoscompreport'][4]."</th>";
        echo "<th>".$LANG['plugin_manageentities'][14]."</th>";
        echo "<th>".$LANG['plugin_manageentities']['cri'][18]."</th>";
        echo "<th>".$LANG['plugin_manageentities']['infoscompreport'][0]."</th>";
        echo "<th>".$LANG['plugin_manageentities']['cri'][16]."</th>";
        echo "<th>".$LANG['plugin_manageentities']['report'][6]."</th>";
        echo "<th>".$LANG["common"][16]."</th>";
        echo "<th width='100px'>".$LANG["document"][2]."</th>";
        echo "</tr>";
        $i=0;
         while ($data=$DB->fetch_array($result)) {
            $i++;
            $class=" class='tab_bg_2 ";
            if ($i%2) {
               $class=" class='tab_bg_1 ";
            }
            echo "<tr $class".($data["is_deleted"]=='1'?"_2":"")."'>";

            if (Session::isMultiEntitiesMode())
               echo "<td class='center'>".Dropdown::getDropdownName("glpi_entities",$data['entity'])."</td>";
            echo "<td class='center'>".Html::convdate($data["date"])."</td>";
            echo "<td class='center'>".$data["technicians"]."</td>";
            echo "<td class='center'>".Dropdown::getDropdownName("glpi_plugin_manageentities_critypes",$data['plugin_manageentities_critypes_id'])."</td>";
            echo "<td class='center'>".$data["realtime"]."</td>";
            echo "<td class='center'>".Dropdown::getYesNo($data["withcontract"])."</td>";
            $num_contract="";
            if ($data["withcontract"]) {
               $contract = new Contract();
               $contract->getFromDB($data["contracts_id"]);
               $num_contract=$contract->fields["num"];
            }
            echo "<td class='center'>".$num_contract."</td>";
            echo "<td class='center'>";
            if ($data["tickets_id"]>0)
               echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/ticket.form.php?id=".$data["tickets_id"]."\">".$data["tickets_id"]."</a>";
            echo "</td>";
            echo "<td class='left'><a href='".$CFG_GLPI["root_doc"]."/front/document.form.php?id=".$data["id"]."'><b>".$data["name"];
            if ($_SESSION["glpiis_ids_visible"]) echo " (".$data["id"].")";
            echo "</b></a></td>";
            $doc = new Document();
            $doc->getFromDB($data["id"]);
            echo "<td class='center'  width='100px'>".$doc->getDownloadLink()."</td>";

            echo "</tr>";
         }
         echo "</table></div></form>";
      }
   }
   
   static function addReports($ID) {
      global $CFG_GLPI, $LANG;
      
      if (plugin_manageentities_haveRight("cri_create","w")) {
         echo "<br><div align='center'>";
         echo "<input type='button' name='submit' value=\"".$LANG['plugin_manageentities']['title'][2]."\" 
         class='submit' onClick=\"window.open('".$CFG_GLPI["root_doc"]."/plugins/manageentities/front/cri.form.php?popup=export&amp;job=".$ID."' ,
         'glpipopup', 'height=1000, width=1000, top=100, left=100, scrollbars=yes' )\">";
         echo "</div><br>";
      }
   }
   
   //shows CRI from ticket or from entity portal
   static function showReports($type,$instID,$entity=-1) {
      global $DB,$CFG_GLPI, $LANG;

      $config= new PluginManageentitiesConfig();
      if ($config->getFromDB(1)) {
         if ($config->fields["backup"]==1) {

            $query = "SELECT `glpi_documents`.*, 
                           `glpi_plugin_manageentities_cridetails`.`date`, 
                           `glpi_plugin_manageentities_cridetails`.`technicians`, 
                           `glpi_plugin_manageentities_cridetails`.`plugin_manageentities_critypes_id`, 
                           `glpi_plugin_manageentities_cridetails`.`withcontract`, 
                           `glpi_plugin_manageentities_cridetails`.`contracts_id`, 
                           `glpi_plugin_manageentities_cridetails`.`realtime`
              FROM `glpi_documents`
              LEFT JOIN `glpi_plugin_manageentities_cridetails` ON (`glpi_documents`.`id` = `glpi_plugin_manageentities_cridetails`.`documents_id`)
              WHERE `glpi_documents`.`documentcategories_id` = '".$config->fields["documentcategories_id"]."' ";
            if	($entity!=-1)
               $query .= " AND `glpi_documents`.`entities_id` = '".$entity."' ";
            else
               $query .= " AND `glpi_documents`.`tickets_id` = '".$instID."' ";
            $query .= " ORDER BY `glpi_documents`.`name` DESC LIMIT 10";

            $result = $DB->query($query);
            $number = $DB->numrows($result);

            if ($number !="0") {

               echo "<table class='tab_cadre_fixe'>";
               echo "<tr><th colspan='8'>".$LANG['plugin_manageentities']['cri'][0];
               echo " <a href='".$CFG_GLPI["root_doc"]."/front/document.php?contains%5B0%5D=cri&amp;field%5B0%5D=1&amp;sort=19&amp;deleted=0&amp;start=0'>";
               echo $LANG['plugin_manageentities'][9]."</a></th></tr>";
               echo "<tr>";
               echo "<th>".$LANG['common'][27]."</th>";
               echo "<th>".$LANG['plugin_manageentities']['infoscompreport'][4]."</th>";
               echo "<th>".$LANG['plugin_manageentities'][14]."</th>";
               echo "<th>".$LANG['plugin_manageentities']['cri'][18]."</th>";
               echo "<th>".$LANG['plugin_manageentities']['infoscompreport'][0]."</th>";
               echo "<th>".$LANG['plugin_manageentities']['cri'][16]."</th>";
               echo "<th>".$LANG["common"][16]."</th>";
               echo "<th width='100px'>".$LANG["document"][2]."</th>";
               echo "</tr>";

               while ($data=$DB->fetch_array($result)) {

                  echo "<tr class='tab_bg_1".($data["is_deleted"]=='1'?"_2":"")."'>";
                  echo "<td class='center'>".Html::convdate($data["date"])."</td>";
                  echo "<td class='center'>".$data["technicians"]."</td>";
                  echo "<td class='center'>".Dropdown::getDropdownName("glpi_plugin_manageentities_critypes",$data['plugin_manageentities_critypes_id'])."</td>";
                  echo "<td class='center'>".$data["realtime"]."</td>";
                  echo "<td class='center'>".Dropdown::getYesNo($data["withcontract"])."</td>";
                  $num_contract="";
                  if ($data["withcontract"]) {
                     $contract = new contract;
                     $contract->getFromDB($data["contracts_id"]);
                     $num_contract=$contract->fields["num"];
                  }
                  echo "<td class='center'>".$num_contract."</td>";
                  echo "<td class='center'><a href='".$CFG_GLPI["root_doc"]."/front/document.form.php?id=".$data["id"]."'><b>".$data["name"];
                  if ($_SESSION["glpiis_ids_visible"]) echo " (".$data["id"].")";
                  echo "</b></a></td>";
                  $doc = new Document();
                  $doc->getFromDB($data["id"]);
                  echo "<td class='center' width='100px'>".$doc->getDownloadLink()."</td>";

                  echo "</tr>";

               }
               echo "</table>";
            }
         }
      }
   }
}

?>