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

	function getFromDBbyLogName($name) {
		global $DB;
		
		$query = "SELECT * FROM `".$this->getTable()."` " .
			"WHERE `logs_name` = '" . $name . "' ";
		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 addDefaultLog($name) {

      if ($this->getFromDBbyLogName($name)) {

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

      $this->add(array(
        'logs_name'=>$name));
      }
   }
   
   static function selectLog($eventlogservers_id) {
      global $DBeventLog;

      PluginEventlogEventlog::checkConnection($eventlogservers_id);

      $query = "SELECT `log`
        FROM `events`
        GROUP BY `log`
        ORDER BY `log` ASC";
      echo "<select name='logs_name'>";
      echo "<option value='0'>".Dropdown::EMPTY_VALUE."</option>\n";
      if ($result = $DBeventLog->query($query)) {
         $numrows =  $DBeventLog->numrows($result);
         while ($data=$DBeventLog->fetch_array($result)) {
            echo "<option value='".$data["log"]."'>".$data["log"]."</option>";
         }
      }
      echo "</select>";
   }
   
   static function EventlogFormChooseLog($target, $eventlogservers_id) {
      global $LANG, $DB, $DBeventLog;
      
      $query = "SELECT *
					FROM `glpi_plugin_eventlog_defaultlogs`
					ORDER BY `logs_name` 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_log$rand' id='form_log$rand'  action=\"$target\">";
            echo "<table class='tab_cadre_fixe' cellpadding='5'>";
            echo "<tr>";
            echo "<th></th><th>".$LANG['plugin_eventlog']['setup'][5]."</th>";
            echo "</tr>";
            while($ligne= mysql_fetch_array($result)) {
               $logID=$ligne["id"];
               echo "<tr class='tab_bg_1'>";
               echo "<td class='center' width='10'>";
               echo "<input type='hidden' name='id' value='$logID'>";
               echo "<input type='checkbox' name='item[$logID]' value='1'>";
               echo "</td>";
               echo "<td>".$ligne["logs_name"]."</td>";
               echo "</tr>";
            }

            Html::openArrowMassives("form_log$rand", true);
            Html::closeArrowMassives(array('delete_log' => $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'][5]." : </th></tr>";
         echo "<tr class='tab_bg_1'><td>";

         self::selectLog($eventlogservers_id);
         echo "</td>";
         echo "<td>";
         echo "<div align='center'>";
         echo "<input type='submit' name='add_log' value=\"".$LANG['buttons'][2]."\" class='submit' >";
         echo "</div>";
         echo "</td></tr>";
         echo "</table>";
         echo "</form></div>";

      }
   }
}

?>