<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL4HLD2A8696 - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Real Estate Agency
 *	DOMAIN: gevimmo.com
 *	FILE: RLINSTALL.CLASS.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive,
 *	non-transferable license for one domain or IP address. Therefore distribution,
 *	sale or transfer of the file in whole or in part without permission of Flynax
 *	respective owners is considered to be illegal and breach of Flynax License End
 *	User Agreement.
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2017 |  All copyrights reserved.
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

class rlInstall extends reefless
{
	/**
	* @var $rlActions
	*/
	protected $rlActions;

	/**
	* contsructor
	* 
	*/
	public function __construct()
	{
		if (!$GLOBALS['rlActions']) {
			$this->loadClass('rlActions');
		}
		$this->rlActions = &$GLOBALS['rlActions'];
	}

	/**
	* intsall plugin
	* 
	*/
	public function install()
	{
		// insert item to payment gateways table
		if (version_compare($GLOBALS['config']['rl_version'], '4.4.0') >= 0) {
			$gateway_info = array(
					'Key' => 'stripe',
					'Recurring_editable' => 1,
					'Plugin' => 'stripe'
				);

			if ($this->rlActions->insertOne($gateway_info, 'payment_gateways')) {
				if ($GLOBALS['languages']) {
					foreach ((array)$GLOBALS['languages'] as $lKey => $lValue) {
						if ($this->getOne('ID', "`Key` = 'payment_gateways+name+stripe' AND `Code` = '{$lValue['Code']}'", 'lang_keys')) {
							$update_names = array(
								'fields' => array(
									'Value' => 'Stripe'
								),
								'where' => array(
									'Code' => $lValue['Code'],
									'Key' => 'listing_groups+name+stripe'
								)
							);
							$this->rlActions->updateOne($update_names, 'lang_keys');
						} else {
							$insert_names = array(
								'Code' => $lValue['Code'],
								'Module' => 'common',
								'Key' => 'payment_gateways+name+stripe',
								'Value' => 'Stripe',
								'Plugin' => 'stripe'
							);
							$this->rlActions->insertOne($insert_names, 'lang_keys');
						}
					}
				}
			}

			$sql = "ALTER TABLE `" . RL_DBPREFIX . "subscriptions` ADD `Stirpe_item_data` varchar(255) NOT NULL default ''";
			$this->query($sql);
			
			// delete old configs
			$sql = "DELETE FROM `" . RL_DBPREFIX . "config` WHERE `Key` = 'stripe_divider' OR `Key` = 'stripe_module' ";
			$this->query($sql);
			
			// delete page
			$sql = "DELETE FROM `" . RL_DBPREFIX . "pages` WHERE `Key` = 'stripe' ";
			$this->query($sql);
		} else {                                                       
			$sql = "CREATE TABLE `" . RL_DBPREFIX . "stripe_subscriptions` (
				`ID` int(11) NOT NULL auto_increment,
				`Txn_ID` varchar(255) NOT NULL default '',
				`Date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
				`Status` enum('active','completed', 'canceled') NOT NULL default 'active',
				`Account_ID` int(11) NOT NULL default '0',
				`Plan_ID` int(11) NOT NULL default '0',
				`Item_ID` int(11) NOT NULL default '0',
				`Customer_ID` varchar(100) NOT NULL default '',
				`Subscription_ID` varchar(100) NOT NULL default '',
				`Count` int(4) NOT NULL default '0',
				`Item_data` text NOT NULL default '',
			  PRIMARY KEY (`ID`)
			) DEFAULT CHARSET=utf8";

			$this->query($sql);

			// listings
			$sql = "ALTER TABLE `" . RL_DBPREFIX . "listings` ADD `stripe_subscription_id` varchar(50) NOT NULL default ''";
			$this->query($sql);

			// listing plans
			$sql = "ALTER TABLE `" . RL_DBPREFIX . "listing_plans` ADD `stripe_subscription` ENUM('0','1') NOT NULL DEFAULT '0';";
			$this->query($sql);

			$sql = "ALTER TABLE `" . RL_DBPREFIX . "listing_plans` ADD `stripe_subscription_interval` ENUM('week', 'month','year') NOT NULL DEFAULT 'month'";
			$this->query($sql);

			$sql = "ALTER TABLE `" . RL_DBPREFIX . "listing_plans` ADD `stripe_subscription_count` INT(4) NOT NULL default '0'";
			$this->query($sql);
			
			// get max position
			/*$config_pos = $this->getRow("SELECT MAX(`Position`) AS `max` FROM `" . RL_DBPREFIX . "config` WHERE `Group_ID` = '6' AND `Plugin` <> 'stripe'");
			$config_pos = $config_pos['max'];
			
			$sql = "SELECT * FROM `" . RL_DBPREFIX . "config` WHERE `Group_ID` = '6' AND `Plugin` = 'stripe'";
			$stripe_configs = $this->getAll($sql);
			
			if ($stripe_configs)
			
			// add configs
			$configs[] = array(
					'Group_ID' => 6,
					'Position' => $config_pos++,
					'Key' => 'stripe_divider',
					'Type' => "divider",
					'Default' => '',
					'Plugin' => 'stripe',
					'name' => 'Stripe payment gateway'
				);
			$configs[] = array(
					'Group_ID' => 6,
					'Position' => $config_pos++,
					'Key' => 'stripe_module',
					'Type' => 'bool',
					'Default' => 1,
					'Plugin' => 'stripe',
					'name' => 'Stripe module'
				);

			foreach((array)$GLOBALS['languages'] as $lKey => $lValue) {
				foreach($configs as $cKey => $cValue) {
					if (!$this->getOne('ID', "`Key` = 'config+name+{$cValue['Key']}' AND `Code` = '{$lValue['Code']}'", 'lang_keys')) {
						$insert_config[] = array(
								'Code' => $lValue['Code'],
								'Module' => 'admin',
								'Key' => 'config+name+' . $cValue['Key'],
								'Value' => $cValue['name'],
								'Plugin' => 'stripe'								
							);
						$this->rlActions->insert($insert_config, 'lang_keys');
					}
					unset($configs[$cKey]['name']);
				}
			}

			$this->rlActions->insert($configs, 'config');*/
		}

		// shoppingCart plugin (only for shopping cart & bidding plugin)
		$sql = "ALTER TABLE `" . RL_DBPREFIX . "accounts` ADD `shc_stripe_enable` ENUM('0','1') NOT NULL DEFAULT '0'";
		$this->query($sql);

		$sql = "ALTER TABLE `" . RL_DBPREFIX . "accounts` ADD `shc_stripe_publishable_key` varchar(50) NOT NULL default ''";
		$this->query($sql);

		$sql = "ALTER TABLE `" . RL_DBPREFIX . "accounts` ADD `shc_stripe_secret_key` varchar(50) NOT NULL default ''";
		$this->query($sql);
	}

	/**
	* uninstall plugin
	* 
	*/
	public function uninstall()
	{
		// remove item from payment gateways table
		if (version_compare($GLOBALS['config']['rl_version'], '4.4.0') >= 0) {
			// get gateway info
			$sql = "SELECT * FROM `".RL_DBPREFIX."payment_gateways` WHERE `Key` = 'stripe' LIMIT 1";
			$gateway_info = $this->getRow($sql);
			
			// delete row from payment gateways table
			$sql = "DELETE FROM `".RL_DBPREFIX."payment_gateways` WHERE `Key` = 'stripe' LIMIT 1";
			$this->query($sql);

			// delete field from subscription plans table
			$this->query("ALTER TABLE `". RL_DBPREFIX ."subscriptions` DROP `Stirpe_item_data`");  
			
			// delete transactions
			$sql = "DELETE FROM `".RL_DBPREFIX."transactions` WHERE `Gateway` = 'stripe'";
			$this->query($sql);
			
			// delete transactions
			$sql = "DELETE FROM `".RL_DBPREFIX."subscriptions` WHERE `Gateway_ID` = '{$gateway_info['ID']}'";
			$this->query($sql);
		} else {
			// delete stripe subscriptions table
			$sql = "DROP TABLE `" . RL_DBPREFIX . "stripe_subscriptions`";
			$this->query($sql);
			
			// listings
			$this->query("ALTER TABLE `". RL_DBPREFIX ."listings` DROP `stripe_subscription_id`");

			// listing plans
			$this->query("ALTER TABLE `". RL_DBPREFIX ."listing_plans` DROP `stripe_subscription`");
			$this->query("ALTER TABLE `". RL_DBPREFIX ."listing_plans` DROP `stripe_subscription_interval`");
			$this->query("ALTER TABLE `". RL_DBPREFIX ."listing_plans` DROP `stripe_subscription_count`");	
		}

		// only for shoppingCart plugin
		$this->query("ALTER TABLE `". RL_DBPREFIX ."accounts` DROP `shc_stripe_enable`");
		$this->query("ALTER TABLE `". RL_DBPREFIX ."accounts` DROP `shc_stripe_publishable_key`");
		$this->query("ALTER TABLE `". RL_DBPREFIX ."accounts` DROP `shc_stripe_secret_key`");
	}
}