<?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 PluginManageentitiesContract extends CommonDBTM {
   
   function canView() {
      return plugin_manageentities_haveRight("manageentities","r");
   }
   
   function canCreate() {
      return plugin_manageentities_haveRight("manageentities","w");
   }
   
	function addContractByDefault($contracts_id,$entities_id) {
      global $DB;

      $query = "SELECT *
        FROM `".$this->getTable()."`
        WHERE `entities_id` = '".$entities_id."' ";
      $result = $DB->query($query);
      $number = $DB->numrows($result);

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

            $query_nodefault = "UPDATE `".$this->getTable()."`
            SET `is_default` = '0' WHERE `id` = '".$data["id"]."' ";
            $result_nodefault = $DB->query($query_nodefault);
         }
      }

      $query_default = "UPDATE `".$this->getTable()."`
        SET `is_default` = '1' WHERE `id` = '".$contracts_id."' ";
      $result_default = $DB->query($query_default);
   }

   function addContract($contracts_id,$entities_id) {

      $this->add(array('contracts_id'=>$contracts_id,'entities_id'=>$entities_id));

   }

   function deleteContract($ID) {

      $this->delete(array('id'=>$ID));
   }
  
   function showContracts($instID) {
      global $DB,$CFG_GLPI, $LANG;

      $query = "SELECT `glpi_contracts`.*, `".$this->getTable()."`.`id` as contracts_id, `".$this->getTable()."`.`is_default`
        FROM `".$this->getTable()."`, `glpi_contracts`
        WHERE `".$this->getTable()."`.`contracts_id` = `glpi_contracts`.`id`
        AND `".$this->getTable()."`.`entities_id` = '$instID'
        ORDER BY `glpi_contracts`.`name`";
      $result = $DB->query($query);
      $number = $DB->numrows($result);

      if ($number) {
         echo "<form method='post' action=\"./entity.php\">";
         echo "<div align='center'><table class='tab_cadre_fixe center'>";
         echo "<tr><th colspan='5'>".$LANG['plugin_manageentities'][3]."</th></tr>";
         echo "<tr><th>".$LANG['common'][16]."</th>";
         echo "<th>".$LANG['financial'][4]."</th>";
         echo "<th>".$LANG['common'][25]."</th>";
         echo "<th>".$LANG['plugin_manageentities'][13]."</th>";
         if ($this->canCreate())
            echo "<th>&nbsp;</th>";
         echo "</tr>";

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

            echo "<tr class='tab_bg_1".($data["is_deleted"]=='1'?"_2":"")."'>";
            echo "<td><a href=\"".$CFG_GLPI["root_doc"]."/front/contract.form.php?id=".$data["id"]."\">".$data["name"]."";
            if ($_SESSION["glpiis_ids_visible"]||empty($data["name"])) echo " (".$data["id"].")";
               echo "</a></td>";
            echo "<td class='center'>".$data["num"]."</td>";
            echo "<td class='center'>".nl2br($data["comment"])."</td>";
            echo "<td class='center'>";
            if ($data["is_default"])
               echo $LANG['choice'][1];
            else
               echo "<a href='./entity.php?contractbydefault=contractbydefault&amp;contracts_id=".$data["contracts_id"]."&amp;entities_id=$instID'>".$LANG['choice'][0]."</a>";
            echo "</td>";

            if ($this->canCreate())
               echo "<td class='tab_bg_2 center'><a href=\"./entity.php?deletecontracts=deletecontracts&amp;id=".$data["contracts_id"]."&amp;manageentities=$instID\">".$LANG['buttons'][6]."</a></td>";
            else echo "";
            echo "</tr>";

         }

         if ($this->canCreate()) {
            echo "<tr class='tab_bg_1'><td colspan='4' class='center'>";
            echo "<input type='hidden' name='entities_id' value='$instID'>";
            Dropdown::show('Contract', array('name' => "contracts_id"));
            echo "</td><td class='center'><input type='submit' name='addcontracts' value=\"".$LANG['buttons'][8]."\" class='submit'></td>";
            echo "</tr>";
         }
         echo "</table></div>" ;
         echo"</form>";
         
      } else {
         echo "<form method='post' action=\"./entity.php\">";
         echo "<div align='center'><table class='tab_cadre_fixe center'>";
         echo "<tr><th colspan='3'>".$LANG['plugin_manageentities'][3].":</th></tr>";
         echo "<tr><th>".$LANG['common'][16]."</th>";
         echo "<th>".$LANG['financial'][4]."</th>";
         echo "<th>".$LANG['common'][25]."</th>";

         echo "</tr>";
         if ($this->canCreate()) {
            echo "<tr class='tab_bg_1'><td class='center'>";
            echo "<input type='hidden' name='entities_id' value='$instID'>";
            Dropdown::show('Contract', array('name' => "contracts_id"));
            echo "</td><td class='center'><input type='submit' name='addcontracts' value=\"".$LANG['buttons'][8]."\" class='submit'>";
            echo "</td><td></td>";
            echo "</tr>";
         }
         echo "</table></div></form>";
      }
   }
}

?>