<?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 PluginEventlogDefaultType extends CommonDBTM {

	function getFromDBbyType($type) {
		global $DB;
		
		$query = "SELECT * FROM `".$this->getTable()."` " .
			"WHERE `types_id` = '" . $type . "' ";
		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 addDefaultType($type) {

      if ($this->getFromDBbyType($type)) {

         $this->update(array(
           'id'=>$obj->fields['id'],
           'types_id'=>$type));
      } else {

         $this->add(array(
           'types_id'=>$type));
      }
   }
   
   static function EventlogFormChooseType($target, $eventlogservers_id) {
      global $LANG, $DB, $DBeventLog;
    
      $type = new PluginEventlogEventlogType();
    
      $query = "SELECT *
					FROM `glpi_plugin_eventlog_defaulttypes`
					ORDER BY `types_id` ASC;";
      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_type$rand' id='form_type$rand'  action=\"$target\">";
            echo "<table class='tab_cadre_fixe' cellpadding='5'>";
            echo "<tr>";
            echo "<th></th><th>".$LANG['plugin_eventlog']['setup'][4]."</th>";
            echo "</tr>";
            while($ligne= mysql_fetch_array($result)) {
               $types_id=$ligne["id"];
               echo "<tr class='tab_bg_1'>";
               echo "<td class='center' width='10'>";
               echo "<input type='hidden' name='id' value='$types_id'>";
               echo "<input type='checkbox' name='item[$types_id]' value='1'>";
               echo "</td>";
               echo "<td>".$type->getDropdownName($ligne["types_id"],$eventlogservers_id)."</td>";
               echo "</tr>";
            }

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

         }
         echo "<div align='center'><form method='post'  action=\"$target\">";
         echo "<table class='tab_cadre_fixe' cellpadding='5'><tr ><th colspan='2'>";
         echo $LANG['plugin_eventlog']['setup'][4]." : </th></tr>";
         echo "<tr class='tab_bg_1'><td>";

         $type->selectType('types_id',$eventlogservers_id);
         echo "</td>";
         echo "<td>";
         echo "<div align='center'>";
         echo "<input type='submit' name='add_type' value=\"".$LANG['buttons'][2]."\" class='submit' >";
         echo "</div>";
         echo "</td></tr>";
         echo "</table>";
         echo "</form></div>";

      }
	}
}

?>