<?php

/**

 * This file contains the base class for all addons to do their work.

 * Addons must extend this class as it contains basic functionality they will require.

 *

 * @package Interspire_Addons, Addons_add_addon_installer

 */



/**

 * Addons_add_addon_installer

 * This is the base class for all addons to work off.

 * It handles:

 * - installing & uninstalling addons

 * - enabling & disabling addons

 * - running addons

 * - finding/loading addons including their language files and description.xml files

 *

 * The description.xml file is used by the calling code to work out

 * - the name of the addon

 * - a short description

 * - the current version number

 * - the author

 * - license details

 *

 * @uses Interspire_Addons_Exception, Addons_add_addon_installer

 */



if (!class_exists('Interspire_Addons', false)) { require_once(dirname(dirname(__FILE__)) . '/interspire_addons.php'); }

require_once (dirname(__FILE__) . '/language/language.php');



class Addons_add_addon_installer extends Interspire_Addons	{

	

	private $AddonName = "add_addon_installer";

	

	// Installation of installer that's for global installer of Telesys Products

	public function Install(){

		$tables = $sequences = array();

		$this->Process("add_addon_installer", "GetApi", "add_addon_installer")->install($this->AddonName);

		$this->db->StartTransaction();

		require dirname(__FILE__) . '/schema.' . SENDSTUDIO_DATABASE_TYPE . '.php';

		foreach ($queries as $query) {

			if($query != ""){

				$DbQry = str_replace('%%TABLEPREFIX%%', $this->db->TablePrefix, $query);

				$result = $this->db->Query($DbQry);

				if (!$result) {

					$this->db->RollbackTransaction();

					throw new Interspire_Addons_Exception("Query Error: " . $DbQry . ": " . $this->db->GetErrorMsg(), Interspire_Addons_Exception::DatabaseError);

				}

			}

		}

		$this->enabled = true;

		$this->configured = true;

        try {

			$status = parent::Install();

		} catch (Interspire_Addons_Exception $e) {

			$this->db->RollbackTransaction();

			throw new Exception("ERROR: Unable to install addon {$this->GetId()} " . $e->getMessage());

		}

		$this->db->CommitTransaction();

		return true;

	}

	

	// UnInstall function for addon main installer	

	public function UnInstall(){

		$tables = $sequences = array();

 		$this->Process("add_addon_installer", "GetApi", "add_addon_installer")->uninstall($this->AddonName);

		$this->db->StartTransaction();

		try {

			$this->Disable();

		} catch (Interspire_Addons_Exception $e) {

			$this->db->RollbackTransaction();

			throw new Interspire_Addons_Exception($e->getMessage(), $e->getCode());

		}

		require dirname(__FILE__) . '/schema.' . SENDSTUDIO_DATABASE_TYPE . '.php';

	    try {

			$status = parent::UnInstall();

		} catch (Interspire_Addons_Exception $e) {

			$this->db->RollbackTransaction();

			throw new Interspire_Addons_Exception($e->getMessage(), $e->getCode());

		}

		$this->db->CommitTransaction();

		return true;

	}

	

	//Enable function for addon main installer

	public function Enable(){

		try {

			$status = parent::Enable();

		} catch (Interspire_Addons_Exception $e) {

			throw new Interspire_Addons_Exception($e->getMessage(), $e->getCode());

		}

		return true;

	}



	//Disable function for addon main installer

	public function Disable(){

		try {

			$status = parent::Disable();

		} catch (Interspire_Addons_Exception $e) {

			throw new Interspire_Addons_Exception($e->getMessage(), $e->getCode());

		}

		return true;

	}

	

	//Action default function for addon main installer

	public function Admin_Action_Default(){

		return;

	}



	//Get Event Listeners for IEM Addon

	function GetEventListeners(){

		$my_file = '{%IEM_ADDONS_PATH%}/add_addon_installer/add_addon_installer.php';

		$listeners = array();

		$listeners[] =

			array (

				'eventname' => 'IEM_USERAPI_GETPERMISSIONTYPES',

				'trigger_details' => array (

					'Interspire_Addons',

					'GetAddonPermissions',

				),

				'trigger_file' => $my_file

			);

		return $listeners;

	}



	//get permission for addon

	static function RegisterAddonPermissions(){

		return;

	}

	

	//GET API from installer

	protected function GetApi($AddApi='add_addon_installer'){

        $AddApi = (is_array($AddApi)) ? $AddApi[0] : $AddApi;

        $path = $this->addon_base_directory . $this->addon_id . '/oops/' . strtolower($AddApi) . '.php';

        if (! is_file($path)) {

            return false;

        }

        require_once $path;

        $class = $AddApi . '_API';

        $AddApi = new $class();

        $AddApi->template_system = $this->template_system;

        return $AddApi;

	}

	//GET API from installer

	protected function GetOops($AddApi='add_addon_installer'){

        $AddApi = (is_array($AddApi)) ? $AddApi[0] : $AddApi;

        $path = $this->addon_base_directory . $this->addon_id . '/oops/' . strtolower($AddApi) . '.php';

        if (! is_file($path)) {

            return false;

        }

        require_once $path;

        $class = $AddApi . '_API';

        $AddApi = new $class();

        $AddApi->template_system = $this->template_system;

        return $AddApi;

	}

	

}