<?php

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

	require_once( dirname( __FILE__ ) . '/language/language.php' );
	class Addons_backup extends Interspire_Addons {
		protected $addon_name = 'backup';

		function Install() {
			$tables = $sequences = array();

			if (!$this->isEnabled( 'installer' )) {
				throw new Interspire_Addons_Exception( 'The Addon (Advanced Addons Installer) is required', AddonDoesntExist );
			} else {
				$this->Process( 'installer', 'GetApi', 'installer' )->install( $this->addon_name );
			}

			$tmp_path = IEM_ADDONS_PATH . '/backup/tmp/';

			if (!is_writable( $tmp_path )) {
				throw new Interspire_Addons_Exception( '' . 'The folder ' . $tmp_path . ' is not writable', AddonDoesntExist );
			}

			$this->db->StartTransaction();
			require( dirname( __FILE__ ) . '/schema.' . SENDSTUDIO_DATABASE_TYPE . '.php' );
			foreach ($queries as $query) {
				$qry = str_replace( '%%TABLEPREFIX%%', $this->db->TablePrefix, $query );
				$result = $this->db->Query( $qry );

				if (!$result) {
					$this->db->RollbackTransaction();
					throw new Interspire_Addons_Exception( 'There was a problem running query ' . $qry . ': ' . $this->db->GetErrorMsg(), DatabaseError );
					continue;
				}
			}

			$this->enabled = true;
			$this->configured = true;
			try {
				$status = parent::install();
			} catch (Interspire_Addons_Exception $e) {
				$this->db->RollbackTransaction();
				throw new Exception( '' . 'Unable to install addon ' . $this->GetId() . ' ' . $e->getMessage() );
			}
			$this->db->CommitTransaction();
			return true;
		}

		function UnInstall() {
			$tables = $sequences = array();

			if (!$this->isEnabled( 'installer' )) {
				throw new Interspire_Addons_Exception( 'The Addon (Advanced Addons Installer) is required', AddonDoesntExist );
			} else {
				$this->Process( 'installer', 'GetApi', 'installer' )->uninstall( $this->addon_name );
			}

			$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;
		}

		function Enable() {
			try {
				$status = parent::enable();
			} catch (Interspire_Addons_Exception $e) {
				throw new Interspire_Addons_Exception( $e->getMessage( ), $e->getCode( ) );
			}
			return true;
		}

		function Disable() {
			try {
				$status = parent::disable();
			} catch (Interspire_Addons_Exception $e) {
				throw new Interspire_Addons_Exception( $e->getMessage( ), $e->getCode( ) );
			}
			return true;
		}

		function Admin_Action_Default() {
			$api = $this->GetApi();
			$this->template_system->Assign( 'users', $api->users_subscribers(), false );
			$this->template_system->Assign( 'users_lists', $api->users_lists(), false );
			$this->template_system->Assign( 'users_campaigns', $api->users_campaigns(), false );
			$this->template_system->Assign( 'users_stats', $api->users_stats(), false );
			$this->template_system->Assign( 'users_forms', $api->users_forms(), false );
			$this->template_system->ParseTemplate( 'manage' );
		}

		function Admin_Action_Export() {
			if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				$api = $this->GetApi();
				$api->export( $_POST );
			}

		}

		function GetEventListeners() {
			$my_file = '' . '{%IEM_ADDONS_PATH%}/' . $this->addon_name . '/' . $this->addon_name . '.php';
			$listeners = array();
			$listeners[] = array( 'eventname' => 'IEM_SENDSTUDIOFUNCTIONS_GENERATETEXTMENULINKS', 'trigger_details' => array( 'Addons_backup', 'SetMenuItems' ), 'trigger_file' => $my_file );
			$listeners[] = array( 'eventname' => 'IEM_USERAPI_GETPERMISSIONTYPES', 'trigger_details' => array( 'Interspire_Addons', 'GetAddonPermissions' ), 'trigger_file' => $my_file );
			return $listeners;
		}

		function SetMenuItems($data) {
			$user = &GetUser();

			$self = new self();
			$menuItems = $data->data;

			if ($user->isAdmin()) {
				$menuItems['tools'][] = array( 'text' => GetLang( 'Addon_backup_backup' ), 'link' => 'index.php?Page=Addons&Addon=backup', 'show' => $user->isAdmin(), 'description' => '', 'image' => 'templates_view.gif' );
			}

			$data->data = $menuItems;
		}

		function RegisterAddonPermissions() {
		}

		function GetApi($api = 'Backup') {
			$api = (is_array( $api ) ? $api[0] : $api);
			$path = $this->addon_base_directory . $this->addon_id . '/api/' . strtolower( $api ) . '.php';

			if (!is_file( $path )) {
				return false;
			}

			require_once( $path );
			$class = $api . '_API';
			$api = new $class();
			$api->template_system = $this->template_system;
			return $api;
		}
	}

?>