<?php
/**
 * JE GroupBuy package
 * @author JExtension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 JExtension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
	defined('_JEXEC') or die('Restricted access');

function com_install()
{
		$status			=  true;
	// Joomla predefiend function to connect db
		$db			= JFactory::getDBO();
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');
	// 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';

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

			// If the store failed return false.
			if (!$stored) {
				JError::raiseWarning('', JText::_('COM_JEGROUPBUY_COMPONENT_INSTALL_ERROR'));
				$status			=  false;
			}
		}
		
	//New Table added
	$query = "CREATE TABLE IF NOT EXISTS `#__jegroupbuy_addtocart` (`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,  `uid` int(11) NOT NULL,  `prod_id` VARCHAR( 255 ) NOT NULL, `pro_quantity` VARCHAR( 255 ) NOT NULL,  `total_cost` float unsigned NOT NULL,  PRIMARY KEY (`cart_id`)) ENGINE = MyISAM  DEFAULT CHARSET=utf8";
	$db->setQuery( $query );
	$db->query();

	
	// 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` ( `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();	
		
	}  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 = 'jegroupbuyCashondelivery'");
		$db->setQuery( $query1);
		$db->query();
	
		$id = $db->loadResult(); 
		
		if(empty($id))
		{

			$query	= $db->getQuery(true);
			$query  = "INSERT INTO `#__extensions` ( `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 - 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)";
						
			$extnstored1 = $db->setQuery( $query);
			$db->query();
		}
		
		
		$querys	= $db->getQuery(true);
		$querys->select('extension_id');
		$querys->from('#__extensions');
		$querys->where("element = 'jegroupbuyBanktransfer'");
		
		$db->setQuery( $querys);
		$db->query();
	
		$id = $db->loadResult(); 
		
		if(empty($id))
		{

			$query	= $db->getQuery(true);
			$query  = "INSERT INTO `#__extensions` ( `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();
		}


		
	}
	
	
	// Instert New fields in db
	
	$query = "SELECT gmap_width FROM #__jegroupbuy_product";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__jegroupbuy_product  ADD  `gmap_width` int(5) NOT NULL  AFTER `expired`";
		$db->setQuery( $query );
		$db->query();
		$query = "ALTER TABLE  #__jegroupbuy_product  ADD  `gmap_height` int(5) NOT NULL  AFTER `gmap_width`";
		$db->setQuery( $query );
		$db->query();		
		$query = "ALTER TABLE  #__jegroupbuy_product  ADD  `gmap_latitude` varchar(250) NOT NULL  AFTER `gmap_height`";
		$db->setQuery( $query );
		$db->query();
		$query = "ALTER TABLE  #__jegroupbuy_product  ADD  `gmap_longitude` varchar(250) NOT NULL  AFTER `gmap_latitude`";
		$db->setQuery( $query );
		$db->query();
		$query = "ALTER TABLE  #__jegroupbuy_product  ADD  `gmap_zoom` int(4) NOT NULL  AFTER `gmap_longitude`";
		$db->setQuery( $query );
		$db->query();		
	}
	
	// Insert setting new fields in db
	
	$query = "SELECT show_subscription FROM #__jegroupbuy_settings";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__jegroupbuy_settings  ADD  `show_subscription` char(2) NOT NULL  AFTER `businessid`";
		$db->setQuery( $query );
		$db->query();
		
		$query = "ALTER TABLE  #__jegroupbuy_settings  ADD  `show_share` char(2) NOT NULL  AFTER `show_subscription`";
		$db->setQuery( $query );
		$db->query();
		
		$query = "ALTER TABLE  #__jegroupbuy_settings  ADD  `show_googlemap` char(2) NOT NULL  AFTER `show_share`";
		$db->setQuery( $query );
		$db->query();	

		$query = "ALTER TABLE  #__jegroupbuy_settings  ADD  `show_comments` char(2) NOT NULL  AFTER `show_googlemap`";
		$db->setQuery( $query );
		$db->query();

		$query = "ALTER TABLE  #__jegroupbuy_settings  ADD  `layout` varchar(25) NOT NULL  AFTER `show_comments`";
		$db->setQuery( $query );
		$db->query();		
	}	

	//Adding additional field currencysymbol
	$query 		= "SELECT `currencysymbol` FROM #__jegroupbuy_product";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query  = "ALTER TABLE  `#__jegroupbuy_product`  ADD `currencysymbol` varchar(255) NOT NULL AFTER  `params`" ;
		$db->setQuery( $query );
		$db->query();
	}
	
	//Add additional field quantity in payment
	
	$query 		= "SELECT `quantity` FROM #__jegroupbuy_payment";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query  = "ALTER TABLE  `#__jegroupbuy_payment`  ADD `quantity` varchar( 255 ) NOT NULL AFTER  `status`" ;
		$db->setQuery( $query );
		$db->query();
	}
	
	
	//Adding additional field video
	$query 		= "SELECT `video` FROM #__jegroupbuy_product";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query  = "ALTER TABLE  `#__jegroupbuy_product`  ADD `video` varchar(255) NOT NULL AFTER  `currencysymbol`" ;
		$db->setQuery( $query );
		$db->query();
	}
	
	
	//Adding additional field dealtype
	$query 		= "SELECT `dealtype` FROM #__jegroupbuy_product";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$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>';
}
?>

