<?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
{
	/**
	* class object
	* 
	* @var $rlActions
	*/
	protected $rlActions;

	/**
	* class contructor
	* 
	*/
	public function __construct()
	{
		$this->loadClass('Actions')	;
		
		$this->rlActions = &$GLOBALS['rlActions'];
	}
	
	/**
	* install plugin
	* 
	*/
	public function install()
	{
		$this->query("CREATE TABLE `". RL_DBPREFIX ."ccbill_settings` (
			`ID` int(11) NOT NULL auto_increment,
			`Service` varchar(50) NOT NULL,
			`Item_ID`  int(11) NOT NULL default '0', 
			`Form` varchar(100) NOT NULL,
			`Allowed_types` varchar(255) NOT NULL,
			PRIMARY KEY (`ID`)
		) DEFAULT CHARSET=utf8");

		// add field to transactions	
		$sql = "SHOW COLUMNS FROM `" . RL_DBPREFIX . "transactions`";
		$list_fields = $this->getAll($sql);
		
		foreach ($list_fields as $lfKey => $lfValue) {
			$fields[] = $lfValue['Field'];
		}

		if (!in_array('Item_data', $fields)) {
			$sql = "ALTER TABLE `" . RL_DBPREFIX . "transactions` ADD `Item_data` Text NOT NULL default ''";
			$this->query($sql);
		}
	
		$gateway_info = array(
				'Key' => 'ccbill',
				'Recurring_editable' => 0,
				'Plugin' => 'ccbill'
			);

		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+ccbill' AND `Code` = '{$lValue['Code']}'", 'lang_keys')) {
						$update_names = array(
							'fields' => array(
								'Value' => 'CCBill'
							),
							'where' => array(
								'Code' => $lValue['Code'],
								'Key' => 'listing_groups+name+ccbill'
							)
						);
						$this->rlActions->updateOne($update_names, 'lang_keys');
					} else {
						$insert_names = array(
							'Code' => $lValue['Code'],
							'Module' => 'common',
							'Key' => 'payment_gateways+name+ccbill',
							'Value' => 'CCBill',
							'Plugin' => 'ccbill'
						);
						$this->rlActions->insertOne($insert_names, 'lang_keys');
					}
				}
			}
		}

		$sql = "ALTER TABLE `" . RL_DBPREFIX . "subscription_plans` ADD `sop_ccbill_type_id` varchar(100) NOT NULL default ''";
		$this->query($sql);

		if (version_compare($GLOBALS['config']['rl_version'], '4.4.1') < 0) {
			$this->fixPaymentController();
		}
	}
	
	/**
	* uninstall plugin
	* 
	*/
	public function uninstall()
	{
		// delete row from payment gateways table
		$sql = "DELETE FROM `".RL_DBPREFIX."payment_gateways` WHERE `Key` = 'ccbill' LIMIT 1";
		$this->query($sql);

		// delete transactions
		$sql = "DELETE FROM `".RL_DBPREFIX."transactions` WHERE `Gateway` = 'ccbill'";
		$this->query($sql);

		// remove ccbill settings table 
		$this->query("DROP TABLE `" . RL_DBPREFIX . "ccbill_settings`");

		// delete field from subscription plans table
		$this->query("ALTER TABLE `". RL_DBPREFIX ."subscription_plans` DROP `sop_ccbill_type_id`");
	}
	
	public function fixPaymentController()
	{
		if (is_writable(RL_CONTROL . 'payment.inc.php')) {
			$handle = fopen(RL_CONTROL . 'payment.inc.php', 'r');
			
			if ($handle) {
				while (!feof($handle))  {
					$content .= fgets($handle);
				}
				fclose ($handle);
				
				if (!empty($content)) {
					$content = str_replace('plugin', 'Plugin', $content);

					$handle_target = fopen(RL_CONTROL . 'payment.inc.php', 'w');
					
					if ($handle_target) {
						fwrite($handle_target, $content);
					}
					fclose($handle_target);
				}
				unset($content);
			}
		}
	}
}