<?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 PluginEventlogEventlogServer extends CommonDBTM {
   
   public $dohistory = true;
   
   static function getTypeName() {
      global $LANG;

      return $LANG['plugin_eventlog']['config'][0];
   }
   
   function canCreate() {
      return plugin_eventlog_haveRight('eventlog', 'w');
   }

   function canView() {
      return plugin_eventlog_haveRight('eventlog', 'r');
   }
   
   function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
      global $LANG;

      if (!$withtemplate) {
         switch ($item->getType()) {
            case __CLASS__ :
               //If connection to the OCS DB  is ok, and all rights are ok too
               $ong = array();
               $ong[1] = self::getTypeName();
               
               if (PluginEventlogEventlog::checkConnection($item->getID())) {
                  $ong[2] = $LANG['plugin_eventlog']['setup'][5];
                  $ong[3] = $LANG['plugin_eventlog']['setup'][4];
               }
               return $ong;
         }
      }
      return '';
   }


   static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {

      if ($item->getType() == __CLASS__) {
         switch ($tabnum) {
            case 1 :
               self::showDBConnectionStatus($item->getID());
               break;

            case 2 :
               PluginEventlogDefaultLog::EventlogFormChooseLog($_POST['target'], $_POST["id"]);
               break;
               
            case 3 :
               PluginEventlogDefaultType::EventlogFormChooseType($_POST['target'], $_POST["id"]);
               break;
         }
      }
      return true;
   }
	
   function defineTabs($options=array()) {
      global $LANG;

      $ong = array();
      $this->addStandardTab(__CLASS__, $ong, $options);
      $this->addStandardTab('Log', $ong, $options);

      return $ong;
   }
  /**
   * Get Eventlog mode configuration
   *
   * Get all config of the Eventlog mode
   *
   * @param $id int : ID of the Eventlog config (default value 1)
   *@return Value of $confVar fields or false if unfound.
   *
   **/
   function getEventlogConf($id) {
      global $DB;

      $query = "SELECT *
      FROM `".$this->getTable()."`
      WHERE `id` = '$id'";
      $result = $DB->query($query);
      if ($result)
         $data = $DB->fetch_assoc($result);
      else
         $data = 0;

      return $data;

   }
  
   function getEvenlogDBByEntity() {
      global $DB;
      
      $result = $DB->query("SELECT `id` FROM `".$this->getTable()."` 
                           WHERE ".getEntitiesRestrictRequest('',$this->getTable()));
      if ($DB->numrows($result) > 0)
         return $DB->result($result,0,"id");
      else
         return -1;
   }
  
   function getSearchOptions() {
      global $LANG;

      $tab = array();
    
      $tab['common'] = $LANG['plugin_eventlog']['config'][0];

      $tab[1]['table']=$this->getTable();
      $tab[1]['field']='name';
      $tab[1]['name']=$LANG['common'][16];
      $tab[1]['datatype']='itemlink';
      
      $tab[2]['table']=$this->getTable();
      $tab[2]['field']='eventlog_db_name';
      $tab[2]['name']=$LANG['plugin_eventlog']['config'][4];
      
      $tab[3]['table']=$this->getTable();
      $tab[3]['field']='eventlog_db_user';
      $tab[3]['name']=$LANG['plugin_eventlog']['config'][1];

      $tab[30]['table']=$this->getTable();
      $tab[30]['field']='id';
      $tab[30]['name']=$LANG['common'][2];

      $tab[80]['table']='glpi_entities';
      $tab[80]['field']='completename';
      $tab[80]['name']=$LANG['entity'][0];
		
		return $tab;
   }
   

   function showForm ($ID, $options=array()) {
		global $LANG;

		if (!$this->canView())
			return false;

		$spotted = false;
		if (empty($ID)||$ID==-1) {
			if ($this->getEmpty()) $spotted = true;
		} else {
			if ($this->getFromDB($ID) 
            && Session::haveAccessToEntity($this->fields["entities_id"])) 
               $spotted = true;
		}

		$this->showTabs($options);
      $this->showFormHeader($options);
      
      echo "<tr class='tab_bg_2'>";
      
      echo "<td>".$LANG['common'][16].":</td>";
      echo "<td>";
      Html::autocompletionTextField($this,"name");
      echo "</td>";
      
      echo "<td></td>";
      echo "<td></td>";
      
      echo "</tr>";
      
      echo "<tr class='tab_bg_2'>";
      
      echo "<td>".$LANG['plugin_eventlog']['config'][4].":</td>";
      echo "<td>";
      Html::autocompletionTextField($this,"eventlog_db_name");
      echo "</td>";
      
      echo "<td>".$LANG['plugin_eventlog']['config'][2].":</td>";
      echo "<td>";
      Html::autocompletionTextField($this,"eventlog_db_host");
      echo "</td>";
      
      echo "</tr>";
      
      echo "<tr class='tab_bg_2'>";
      
      echo "<td>".$LANG['plugin_eventlog']['config'][1].":</td>";
      echo "<td>";
      Html::autocompletionTextField($this,"eventlog_db_user");
      echo "</td>";

      echo "<td>" . $LANG['plugin_eventlog']['config'][3].":</td>";
      echo "<td>";
      echo "<input type='password' autocomplete='off' name='eventlog_db_passwd' value='".$this->fields["eventlog_db_passwd"]."'>";
      echo "</td>";
      
      echo "</tr>";
      
      $this->showFormButtons($options);
      $this->addDivForTabs();

	}

	static function showDBConnectionStatus($ID) {
		global $LANG;
		
      echo "<div class='center'>";
      echo "<table class='tab_cadre'>";
      echo "<tr><th>" .$LANG['plugin_eventlog']['config'][0] . "</th></tr>";
      echo "<tr class='tab_bg_2'><td class='center'>";
      if (!empty($ID)||$ID!=-1) {
         if (!PluginEventlogEventlog::checkConnection($ID)) {
            echo $LANG['plugin_eventlog']['setup'][1];
         } else {
            echo $LANG['plugin_eventlog']['setup'][2];
         }
      }
      echo "</td></tr>";
      echo "</table>";
      echo "</div><br>";

	}
}

?>