<?php
/**
 * @module		com_ola
 * @script		ola.php
 * @author-name Christophe Demko
 * @adapted by  Ribamar FS
 * @copyright	Copyright (C) 2012 Christophe Demko
 * @license		GNU/GPL, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
define('DS', DIRECTORY_SEPARATOR);

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

/**
 * Script file of Ola component
 */
class com_jegroupbuyInstallerScript
{
	/**
	 * method to install the component
	 *
	 * @return void
	 */
	function install($parent) 
	{
		$this->installitems();
	}
 
	/**
	 * method to uninstall the component
	 *
	 * @return void
	 */
	function uninstall($parent) 
	{
	
		$db			= & JFactory::getDBO();

		if(JFolder::exists(JPATH_ROOT.DS.'plugins'.DS.'jegroupbuypayment')){
			JFolder::delete(JPATH_ROOT.DS.'plugins'.DS.'jegroupbuypayment');
		}
		
		if (JFile::exists(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuy2Checkout.ini')) {
			JFile::delete(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuy2Checkout.ini');
		}
		if (JFile::exists(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyAuthorizedotnet.ini')) {
			JFile::delete(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyAuthorizedotnet.ini');
		}
		
		if (JFile::exists(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyCashondelivery.ini')) {
			JFile::delete(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyCashondelivery.ini');
		}
		if (JFile::exists(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyccAvenue.ini')) {
			JFile::delete(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyccAvenue.ini');
		}
		if (JFile::exists(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyGooglecheckout.ini')) {
			JFile::delete(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyGooglecheckout.ini');
		}
		if (JFile::exists(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyPaypal.ini')) {
			JFile::delete(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.plg_jegroupbuyPaypal.ini');
		}
		
		//Code for uninstall the jeTestimonial plugin
			$query			= $db->getQuery(true);
			
			$query->select('extension_id');
			$query->from('#__extensions');
			$query->where("folder = 'jegroupbuypayment'");
			$db->setQuery( $query);

			$extension_id 				= $db->loadResult();
			
			$query = "DELETE  FROM `#__extensions` WHERE `folder`='jegroupbuypayment' AND `type` = 'plugin'";
			$db->setQuery( $query );
			$db->query();
		// Code ended for uninstall jeTestimonial plugin

		echo '<p> <b> <span style="color:#009933"> JE Groupbuy Component and Plugin has been Uninstalled successfully </span></b> </p>';

	}
 
	/**
	 * method to update the component
	 *
	 * @return void
	 */
	function update($parent) 
	{
		$this->installitems();
	}

	function installitems(){
		
		$db			= JFactory::getDBO();
		$query		= $db->getQuery(true);
		
		$status		= true;
		
		
		/*Adding additional field video
		$query = "SELECT * FROM #__jegroupbuy_product";
		$db->setQuery( $query );
		$total = $db->loadObjectlist();
				
		$s = array_key_exists("video", $total);
		
		if(!$s)
		{
			$query  = "ALTER TABLE  `#__jegroupbuy_product`  ADD `video` varchar(255) NOT NULL AFTER  `currencysymbol`" ;
			$db->setQuery( $query );
			$db->query();
		}*/
		
		
	// Insert default configuration details
		$query 		= "SELECT count(*) FROM #__jegroupbuy_settings";
		$db->setQuery( $query );
		$total_rows = $db->loadResult();

		if(!$total_rows) {
			$settings				=  new stdClass();

			$settings->id				= '1';
			$settings->test_mode		= '1';
			$settings->businessid		= 'example@paypalid.com';
			$settings->show_category	= '0';
			$settings->show_subscription= '0';
			$settings->show_share		= '0';
			$settings->show_googlemap	= '0';
			$settings->show_comments	= '0';
			$settings->layout			= 'default';

			$stored					= $db->insertObject('#__jegroupbuy_settings', $settings);

			// If the store failed return false.
			if (!$stored) {
				JError::raiseWarning('', JText::_('COM_JEGROUPBUY_COMPONENT_INSTALL_ERROR'));
				$status			=  false;
			}
		}

	// Move Payment Plugin
	
	if(!JFolder::exists(JPATH_ROOT.DS.'plugins'.DS.'jegroupbuypayment')) 
	{
		JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'jegroupbuypayment', JPATH_ROOT.DS.'plugins'.DS.'jegroupbuypayment');
		
$query = "INSERT INTO `#__extensions` ( `extension_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES ('','jeGroupBuy - Paypal', 'plugin', 'jegroupbuyPaypal', 'jegroupbuypayment', 0, 1, 1, 1, '{\"legacy\":true,\"name\":\"jeGroupBuy Paypal payment\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy Paypal payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 4, 0),('','jeGroupBuy - GoogleCheckOut', 'plugin', 'jegroupbuyGooglecheckout', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - GoogleCheckOut\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy GoogleCheckOut payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 3, 0),('','jeGroupBuy - Authorizedotnet', 'plugin', 'jegroupbuyAuthorizedotnet', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - Authorizedotnet\",\"type\":\"plugin\",\"creationDate\":\"May 30, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy Authorizedotnet payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),('','jeGroupBuy - ccAvenue', 'plugin', 'jegroupbuyccAvenue', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - ccAvenue\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy ccAvenue payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 2, 0),('','jeGroupBuy - 2Checkout', 'plugin', 'jegroupbuy2Checkout', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - 2Checkout\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy 2Checkout payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),('','jeGroupBuy - Cashondelivery', 'plugin', 'jegroupbuyCashondelivery', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - Cashondelivery\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy Cashondelivery payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),('','jeGroupBuy - Banktransfer', 'plugin', 'jegroupbuyBanktransfer', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - Banktransfer\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy Banktransfer payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 0, 0)";			

		$db->setQuery( $query );
		$db->query();
		
		//Create add_to_cart table
		
		$query 		= "CREATE TABLE IF NOT EXISTS `#__jegroupbuy_addtocart`(`cart_id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL,`prod_id` varchar(255) NOT NULL,  `pro_quantity` varchar(255) NOT NULL,`total_cost` float NOT NULL,PRIMARY KEY (`cart_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
		$db->setQuery( $query );
		$db->query();
		
		//Adding additional field video
		
		$db->setQuery('SHOW FULL COLUMNS FROM #__jegroupbuy_product where field = "video"');
		$array = $db->loadObject();
		if(empty($array)){
			$db = JFactory::getDBO();
			$query 	= "ALTER TABLE `#__jegroupbuy_product` ADD `video` varchar(255) NOT NULL AFTER `currencysymbol`";
			$db->setQuery( $query );
			$db->query();
		}
				
		//Adding additional field dealtype
		
		$db->setQuery('SHOW FULL COLUMNS FROM #__jegroupbuy_product where field = "dealtype"');
		$arrays = $db->loadObject();
		if(empty($arrays)){
			$db = JFactory::getDBO();
			$query 	= "ALTER TABLE `#__jegroupbuy_product` ADD `dealtype` tinyint(3) NOT NULL AFTER `video`";
			$db->setQuery( $query );
			$db->query();
		}
	
	} else{
	
		JFolder::delete(JPATH_ROOT.DS.'plugins'.DS.'jegroupbuypayment');
		JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'jegroupbuypayment', JPATH_ROOT.DS.'plugins'.DS.'jegroupbuypayment');
		
		$query1	= $db->getQuery(true);
		$query1->select('extension_id');
		$query1->from('#__extensions');
		$query1->where("element = 'jegroupbuyBanktransfer'");
		$db->setQuery( $query1);
		$db->query();
	
		$id = $db->loadResult(); 
		
		if(empty($id))
		{
			
			$query	= $db->getQuery(true);
			$query  = "INSERT INTO `#__extensions` ( `extension_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES ('','jeGroupBuy - Banktransfer', 'plugin', 'jegroupbuyBanktransfer', 'jegroupbuypayment', 0, 1, 1, 0, '{\"legacy\":false,\"name\":\"jeGroupBuy - Banktransfer\",\"type\":\"plugin\",\"creationDate\":\"May 26, 2011\",\"author\":\"JExtension\",\"copyright\":\"[2011] JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0 Stable\",\"description\":\"jeGroupBuy Banktransfer payment\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 0, 0)";

			$extnstored1 = $db->setQuery( $query);
			$db->query();
		}
		
		
		//Create add_to_cart table
		$query 		= "CREATE TABLE IF NOT EXISTS `#__jegroupbuy_addtocart`(`cart_id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL,`prod_id` varchar(255) NOT NULL,  `pro_quantity` varchar(255) NOT NULL,`total_cost` float NOT NULL,PRIMARY KEY (`cart_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
		$db->setQuery( $query );
		$db->query();	
		
		//Adding additional field video
		
		$db->setQuery('SHOW FULL COLUMNS FROM #__jegroupbuy_product where field = "video"');
		$array = $db->loadObject();
		if(empty($array)){
			$db = JFactory::getDBO();
			$query 	= "ALTER TABLE `#__jegroupbuy_product` ADD `video` varchar(255) NOT NULL AFTER `currencysymbol`";
			$db->setQuery( $query );
			$db->query();
		}
				
		//Adding additional field dealtype
		
		$db->setQuery('SHOW FULL COLUMNS FROM #__jegroupbuy_product where field = "dealtype"');
		$arrays = $db->loadObject();
		if(empty($arrays)){
			$db = JFactory::getDBO();
			$query 	= "ALTER TABLE `#__jegroupbuy_product` ADD `dealtype` tinyint(3) NOT NULL AFTER `video`";
			$db->setQuery( $query );
			$db->query();
		}
	
	}		
	
	// Insert sample template
		$query 		= "SELECT count(*) FROM #__jegroupbuy_template";
		$db->setQuery( $query );
		$total_temp = $db->loadResult();

		if(!$total_temp) {
			$template				=  new stdClass();

			$template->id			= '1';
			$template->template		= '<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td width="100%" align="left">
											<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td width="50%">
											<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td width="50%">
											<p>Shop E-mail:<br />Shop Phone: <br />Shop Address:<br />Shop URL:</p>
											</td>
											<td>
											<p>{shop_email}<br />{shop_phone}<br />{shop_address_1}<br />{shop_url}</p>
											</td>
											</tr>
											</tbody>
											</table>
											</td>
											<td width="50%" align="center" valign="middle">{logo}</td>
											</tr>
											<tr>
											<td width="100%">
											<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td style="background-color: #d6d6d6;" colspan="2" width="50%" align="left">
											<h4 style="margin: 0px;">User Details</h4>
											</td>
											</tr>
											<tr>
											<td width="50%" align="left">User Name:</td>
											<td align="left">{user_name}</td>
											</tr>
											<tr>
											<td align="left">User EmailId:</td>
											<td align="left">{user_mailid}</td>
											</tr>
											<tr>
											<td align="left">Payment Type:</td>
											<td align="left">{user_payment}</td>
											</tr>
											</tbody>
											</table>
											</td>
											</tr>
											<tr>
											<td colspan="2" width="100%" align="left">
											<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td width="100%" align="left" valign="top">
											<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td style="background-color: #d6d6d6;" colspan="2" width="50%" align="left">
											<h4 style="margin: 0px;">Coupon Information</h4>
											</td>
											</tr>
											<tr>
											<td width="21%" align="left">Coupon Code:</td>
											<td align="left">{coupon_code}</td>
											</tr>
											<tr>
											<td align="left">Coupon Date:</td>
											<td align="left">{coupon_date}</td>
											</tr>
											</tbody>
											</table>
											</td>
											</tr>
											<tr>
											<td width="100%" align="left" valign="top">
											<table style="width: 100%;" border="0" cellspacing="0" cellpadding="2">
											<tbody>
											<tr>
											<td style="background-color: #d6d6d6;" colspan="2" width="50%" align="left">
											<h4 style="margin: 0px;">Order Information</h4>
											</td>
											</tr>
											<tr>
											<td width="21%" align="left">Order Number:</td>
											<td align="left">{order_number}</td>
											</tr>
											<tr>
											<td align="left">Order Date:</td>
											<td align="left">{order_date}</td>
											</tr>
											<tr>
											<td align="left">Transaction Id:</td>
											<td align="left">{transaction_id}</td>
											</tr>
											<tr>
											<td align="left">Order Status:</td>
											<td align="left">{order_status}</td>
											</tr>
											</tbody>
											</table>
											</td>
											</tr>
											</tbody>
											</table>
											</td>
											</tr>
											<tr>
											<td style="background-color: #d6d6d6;" colspan="2" align="left">
											<h4 style="margin: 0px;">Order Items</h4>
											</td>
											</tr>
											<tr>
											<td colspan="2" width="100%" align="left">
											<table border="0" cellspacing="0" cellpadding="2" width="100%">
											<tbody>
											<tr>
											<td width="32%" align="left" valign="top">Name</td>
											<td width="10%" align="left" valign="top">Product Price<br /></td>
											<td width="12%" align="left" valign="top">Discount %<br /></td>
											<td width="12%" align="left" valign="top">You Save<br /></td>
											<td width="12%" align="left" valign="top">Deal Price<br /></td>
											</tr>
											<tr>
											<td width="32%" align="left" valign="top">{product_name}</td>
											<td width="10%" align="left" valign="top">{product_price}</td>
											<td width="12%" align="left" valign="top">{discount_price}</td>
											<td width="12%" align="left" valign="top">{you_save}</td>
											<td width="12%" align="left" valign="top">{deal_price}</td>
											</tr>
											</tbody>
											</table>
											</td>
											</tr>
											</tbody>
											</table>
											</td>
											</tr>
											</tbody>
											</table>';

			$store_template					= $db->insertObject('#__jegroupbuy_template', $template);

			// If the store failed return false.
			if (!$store_template) {
				JError::raiseWarning('', JText::_('COM_JEGROUPBUY_COMPONENT_INSTALL_ERROR'));
				$status			=  false;
			}
		}
	
		if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'jegroupbuypayment')) {
			JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'jegroupbuypayment');
		}
		
		$path1 = JPath::clean(JPATH_SITE.DS."images".DS."jegroupbuy".DS."category");
		$path2 = JPath::clean(JPATH_SITE.DS."images".DS."jegroupbuy".DS."storelogo");
		
		if (!is_dir($path1) && !is_file($path1))
		{
			JFolder::create($path1);
			$data = "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>";
			JFile::write($path1.DS."index.html", $data);		
		}
		if (!is_dir($path2) && !is_file($path2))
		{
			JFolder::create($path2);
			$data = "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>";
			JFile::write($path2.DS."index.html", $data);		
		}
		
		if($status == true)
			echo '<p> <b> <span style="color:#009933">JEXTN GroupBuy Component has been installed successfully.</span></b> </p>';
	}
}
