<?php
/**
 * Mediaflex Sp. z O.O.
 * @author krystian.kuczek@mediaflex.pl
 * @package Moneybookers Payment Plugin
 */


include_once(DIR_FS_CATALOG . DIR_WS_LANGUAGES . $_SESSION['language'] . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'payment' . DIRECTORY_SEPARATOR . 'mb.php');

include_once(DIR_FS_CATALOG . DIR_WS_MODULES . 'payment' . DIRECTORY_SEPARATOR . 'moneybookers' . DIRECTORY_SEPARATOR . 'moneybookers_functions.php');
include_once(DIR_FS_CATALOG . DIR_WS_MODULES . 'payment' . DIRECTORY_SEPARATOR . 'moneybookers' . DIRECTORY_SEPARATOR . 'moneybookers_main.php');

class mb extends moneybookers_main {

	function mb() {
		global $messageStack;

		$this->code = __CLASS__;
		$this->codeVersion = '0.0.1.a';
		$this->description = MODULE_PAYMENT_MONEYBOOKERS_TEXT_WELCOME;
		$this->_prefix = 'MODULE_PAYMENT_MONEYBOOKERS_';
		$this->title = ($this->check() ? "<b>" : "") . MODULE_PAYMENT_MONEYBOOKERS_TEXT_MAIN_MODULE . (!$this->check() ? " - <b>" . MODULE_PAYMENT_MONEYBOOKERS_TEXT_REQUIRED : "") . "</b>";

		try {
			if ((int)$this->_get('ORDER_STATUS_ID') > 0) {
				$this->order_status = $this->_get('ORDER_STATUS_ID');
			}

			$this->sort_order = $this->_get('SORT_ORDER');

			if (!$this->checkConfigured()) {
				$this->title .= '<b style="color: red"> (Module is not configured)</b>';
			}
		} catch (Exception $e) {
			$this->enabled = false;
			$excp = true;
		}

		$this->moneybookers_main();

		if (!isset($excp) || $excp != true) {
			$this->enabled = true;
		}
	}

	function keys() {
		 $keys_list = array(
			$this->_prefix . 'STATUS',
			$this->_prefix . 'SORT_ORDER',
			$this->_prefix . 'CURRENCY',
			$this->_prefix . 'ORDER_STATUS_ID',
			$this->_prefix . 'PROCESSING_ORDER_STATUS_ID',
			$this->_prefix . 'EMAIL',
			$this->_prefix . 'ID',
			$this->_prefix . 'SECRET_WORD',
			$this->_prefix . 'INFO_MAIL'
			);

		 return $keys_list;
	}

	/**
	 * Instalacja modułu moneybookers.
	 * 
	 */
	function install() {
		global $db;

		//Czyścimy
		tep_db_query("DELETE from " . TABLE_CONFIGURATION . " where configuration_key LIKE '" . $this->_prefix . "%'");

		tep_db_query(" ALTER TABLE `configuration` CHANGE `configuration_value` `configuration_value` TEXT CHARACTER SET latin2 COLLATE latin2_general_ci NOT NULL");

		//Na razie nic nie wstawiamy.
		$s = 0;
//		$this->_toConf($key, $value, $title, $description, $sortOrder, $setFunction, $useFunction)
		$this->_toConf('STATUS', 'True', MODULE_PAYMENT_MONEYBOOKERS_TEXT_ENABLED, MODULE_PAYMENT_MONEYBOOKERS_TEXT_ENABLED_DESC, $s++, 'tep_cfg_select_option(array(\'True\', \'False\'), ');
		$this->_toConf('SORT_ORDER', 0, MODULE_PAYMENT_MONEYBOOKERS_TEXT_ORDEROFDISPLAY, MODULE_PAYMENT_MONEYBOOKERS_TEXT_ORDEROFDISPLAY_DESC, $s++);
		$this->_toConf('CURRENCY', 'USER', MODULE_PAYMENT_MONEYBOOKERS_TEXT_CURRENCY, MODULE_PAYMENT_MONEYBOOKERS_TEXT_CURRENCY_DESC, $s++, 'mb_cfg_currency(');
		$this->_toConf('PROCESSING_ORDER_STATUS_ID', DEFAULT_ORDERS_STATUS_ID, MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_ORDER, MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_ORDER_DESC, $s++, "mb_cfg_pull_down_order_statuses(", "tep_get_order_status_name");
		$this->_toConf('ORDER_STATUS_ID', 2, MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_PENDING, MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_PENDING_DESC, $s++, "tep_cfg_pull_down_order_statuses(", "tep_get_order_status_name");
		$this->_toConf('EMAIL', STORE_OWNER_EMAIL_ADDRESS, MODULE_PAYMENT_MONEYBOOKERS_TEXT_EMAIL, MODULE_PAYMENT_MONEYBOOKERS_TEXT_EMAIL_DESC, $s++, 'mb_cfg_email(');
		$this->_toConf('SECRET_WORD', '', MODULE_PAYMENT_MONEYBOOKERS_TEXT_SECRET_WORD, MODULE_PAYMENT_MONEYBOOKERS_TEXT_SECRET_WORD_DESC, $s++, 'mb_cfg_scr_word(');
		$this->_toConf('ID', '', MODULE_PAYMENT_MONEYBOOKERS_TEXT_MONEYBOOKERS_ID, MODULE_PAYMENT_MONEYBOOKERS_TEXT_MONEYBOOKERS_ID_DESC, $s++, 'mb_cfg_ident(');
		$this->_toConf('INFO_MAIL', STORE_OWNER_EMAIL_ADDRESS, MODULE_PAYMENT_MONEYBOOKERS_TEXT_EMAIL_TO_MSG, MODULE_PAYMENT_MONEYBOOKERS_TEXT_EMAIL_TO_MSG_DESC, $s++, 'mb_cfg_info_email(');
		

		$this->_addZone("Germany", 10000, 81);
		$this->_addZone("SoforZone", 10001, array(81, 14, 73, 222, 204));
		$this->_addZone('France', 10002, 73);
		$this->_addZone('Italy', 10003, 105);
		$this->_addZone('Poland', 10004, 170);
		$this->_addZone('Denmark', 10005, 57);
		$this->_addZone('Singapore', 10006, 188);
		$this->_addZone('Austria', 10007, 14);
		$this->_addZone('Bulgaria', 10008, 33);
		$this->_addZone('Netherlands', 10009, 150);
		$this->_addZone('Ireland', 10010, 103);
		$this->_addZone('Finland', 10011, 72);
		$this->_addZone('Australia', 10012, 13);
		$this->_addZone('Sweden', 10013, 203);
		$this->_addZone('MaestroZone', 10014, array(14, 195, 222));

	}

	function selection() {
		return false;
	}

	function javascript_validation() {
		return false;
	}

	function _addZone($country, $id, $countries) {
		tep_db_query("REPLACE INTO `" . TABLE_GEO_ZONES . "` (geo_zone_id, geo_zone_name, geo_zone_description, date_added) VALUES($id, '$country', 'Created By Moneybookers Plugin', NOW());");

		if (!is_array($countries)) {
			$countries = array($countries);
		}

		foreach ($countries as $c) {
			tep_db_query("INSERT INTO `" . TABLE_ZONES_TO_GEO_ZONES . "` (zone_country_id, zone_id, geo_zone_id, date_added) VALUES($c, 0, $id, NOW());");
		}

	}
}

?>
