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

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

 LICENSE
		
 This file is part of Eventlog.

 Eventlog 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.

 Eventlog 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 Eventlog. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 */

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

class PluginEventlogTicketCategory extends CommonDBTM {
   
	function getFromDBbyCategory($category) {
		global $DB;
		
		$query = "SELECT * FROM `".$this->getTable()."` " .
			"WHERE `ticketcategories_id` = '" . $category . "' ";
		if ($result = $DB->query($query)) {
			if ($DB->numrows($result) != 1) {
				return false;
			}
			$this->fields = $DB->fetch_assoc($result);
			if (is_array($this->fields) && count($this->fields)) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
	
	function addTicketCategory($category) {
	
      if ($this->getFromDBbyCategory($category)) {

         $this->update(array(
           'id'=>$this->fields['id'],
           'ticketcategories_id'=>$category));
      } else {

         $this->add(array(
        'id'=>1,
        'ticketcategories_id'=>$category));
      }
   }
   
   function showForm() {
      global $LANG,$DB,$CFG_GLPI;
      
      $query = "SELECT *
            FROM `".$this->getTable()."` ";
      if ($result = $DB->query($query)) {
         $number = $DB->numrows($result);
         if ($number != 0) {
            $rand=mt_rand();
            echo "<div align='center'>";
            echo "<form method='post' name='form_ticket$rand' id='form_ticket$rand' 
                  action='".$CFG_GLPI["root_doc"] ."/plugins/eventlog/front/config.form.php'>";
            echo "<table class='tab_cadre_fixe' cellpadding='5'>";
            echo "<tr>";
            echo "<th></th><th>".$LANG['plugin_eventlog']['setup'][14]."</th>";
            echo "</tr>";
            while($ligne= mysql_fetch_array($result)) {
               $ID=$ligne["id"];
               echo "<tr class='tab_bg_1'>";
               echo "<td class='center' width='10'>";
               echo "<input type='hidden' name='id' value='$ID'>";
               echo "<input type='checkbox' name='item[$ID]' value='1'>";
               echo "</td>";
               echo "<td>";
               echo Dropdown::getDropdownName("glpi_itilcategories",$ligne["ticketcategories_id"]);
               echo "</td></tr>";

            }
            
            Html::openArrowMassives("form_ticket$rand", true);
            Html::closeArrowMassives(array('delete_ticket' => $LANG['buttons'][6]));
            echo "</table>";
            echo "</form></div>";

         } else {
            echo "<div align='center'>";
            echo "<form method='post' action='".$CFG_GLPI["root_doc"] ."/plugins/eventlog/front/config.form.php'>";
            echo "<table class='tab_cadre_fixe' cellpadding='5'><tr ><th colspan='2'>";
            echo $LANG['plugin_eventlog']['setup'][14]." : </th></tr>";
            echo "<tr class='tab_bg_1'><td>";
            Dropdown::show('ITILCategory', array('name' => "ticketcategories_id"));
            echo "</td>";
            echo "<td>";
            echo "<div align='center'>";
            echo "<input type='submit' name='add_ticket' value=\"".$LANG['buttons'][2]."\" class='submit' >";
            echo "</div>";
            echo "</td></tr>";
            echo "</table>";
            echo "</form></div>";
         }
      }
   }
}

?>