<?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/>.
 --------------------------------------------------------------------------
 */
 
function plugin_eventlog_install() {
   global $DB;
   
	include_once (GLPI_ROOT."/plugins/eventlog/inc/profile.class.php");
   
   $update = false;
	if (!TableExists("glpi_plugin_eventlog_profiles") && !TableExists("glpi_plugin_eventlog_ticketcategories")) {
      
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/empty-1.3.0.sql");

	} else if (TableExists("glpi_plugin_eventlog_display") && !TableExists("glpi_plugin_eventlog_ticket")) {
      
      $update = true;
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/update-1.1.sql");
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/update-1.2.0.sql");
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/update-1.3.0.sql");

	} else if (TableExists("glpi_plugin_eventlog_profiles") && FieldExists("glpi_plugin_eventlog_profiles","interface")) {
      
      $update = true;
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/update-1.2.0.sql");
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/update-1.3.0.sql");

	} else if (!TableExists("glpi_plugin_eventlog_ticketcategories")) {
      
      $update = true;
		$DB->runFile(GLPI_ROOT ."/plugins/eventlog/sql/update-1.3.0.sql");

	}
   
   if ($update) {
      //Do One time on 0.78
      $query_="SELECT *
            FROM `glpi_plugin_eventlog_profiles` ";
      $result_=$DB->query($query_);
      if ($DB->numrows($result_)>0) {

         while ($data=$DB->fetch_array($result_)) {
            $query="UPDATE `glpi_plugin_eventlog_profiles`
                  SET `profiles_id` = '".$data["id"]."'
                  WHERE `id` = '".$data["id"]."';";
            $result=$DB->query($query);

         }
      }
      
      $query="ALTER TABLE `glpi_plugin_eventlog_profiles`
               DROP `name` ;";
      $result=$DB->query($query);
   }
   PluginEventlogProfile::createFirstAccess($_SESSION['glpiactiveprofile']['id']);
	return true;
}

function plugin_eventlog_uninstall() {
	global $DB;

	$tables = array("glpi_plugin_eventlog_eventlogservers",
          "glpi_plugin_eventlog_ticketcategories",
					"glpi_plugin_eventlog_knowbaseitemcategories",
					"glpi_plugin_eventlog_defaulttypes",
					"glpi_plugin_eventlog_defaultlogs",
					"glpi_plugin_eventlog_profiles");

   foreach($tables as $table)
		$DB->query("DROP TABLE IF EXISTS `$table`;");
   
   //old versions	
   $tables = array("glpi_plugin_eventlog_config",
					"glpi_plugin_eventlog_default",
					"glpi_plugin_eventlog_display",
					"glpi_plugin_eventlog_ticket");

	foreach($tables as $table)
		$DB->query("DROP TABLE IF EXISTS `$table`;");
		
   $tables_glpi = array("glpi_displaypreferences");

	foreach($tables_glpi as $table_glpi)
		$DB->query("DELETE FROM `$table_glpi` WHERE `itemtype` = 'PluginEventlogEventlog' OR `itemtype` = 'PluginEventlogEventlogServer' ;");

	return true;
}

// Define dropdown relations
function plugin_eventlog_getDatabaseRelations() {

	$plugin = new Plugin();
	if ($plugin->isActivated("eventlog"))
		return array (
			"glpi_entities" => array (
				"glpi_plugin_eventlog_eventlogservers" => "entities_id"
			),
			"glpi_ticketcategories" => array (
				"glpi_plugin_eventlog_ticketcategories" => "ticketcategories_id"
			),
			"glpi_knowbaseitemcategories" => array (
				"glpi_plugin_eventlog_knowbaseitemcategories" => "knowbaseitemcategories_id"
			),
			"glpi_profiles" => array ("glpi_plugin_eventlog_profiles" => "profiles_id")
		);
	else
		return array ();

}

// Do special actions for dynamic report
function plugin_eventlog_dynamicReport($parm) {

   $eventlog=new PluginEventlogEventlog();
	if ($parm["item_type"]=='PluginEventlogEventlog') {
		$eventlog->showList($parm);

		return true;
	}

	// Return false if no specific display is done, then use standard display
	return false;
}

?>