<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 J-Extension
 * @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()
{
	$db			=& JFactory::getDBO();
	$query = "SELECT count(*) FROM #__jegroupbuy_settings";
	$db->setQuery( $query );
	$confCount = $db->loadResult();
	if(!$confCount)
	{
		$query = "INSERT INTO `#__jegroupbuy_settings` (`id`, `show_subscription`, `show_share`, `show_googlemap`, `show_comments`,`layout`,`show_footer`, `apikey`) VALUES (1, '0', '0', '0', '0','default','1','');";
		$db->setQuery( $query);
	    $db->query();
	}
	
	//Install Jegroupbuy Module
	if(!JFolder::exists(JPATH_ROOT.DS.'modules'.DS.'mod_jegroupbuy')) {
		JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'mod_jegroupbuy', JPATH_ROOT.DS.'modules'.DS.'mod_jegroupbuy');
	
		$query = "INSERT INTO `#__modules` (`id`, `title`, `content`, `ordering`, `position`, `checked_out`, `checked_out_time`, `published`, `module`, `numnews`, `access`, `showtitle`, `params`, `iscore`, `client_id`, `control`) VALUES ('', 'JE Groupbuy', '', '1', 'left', '0', '0000-00-00 00:00:00', '0', 'mod_jegroupbuy', '0', '0', '1', '
cache_time=900
moduleclass_sfx=', '0', '0', '')";
		$db->setQuery( $query );
		$db->query();
		
		$query 		= 'SELECT id FROM `#__modules` WHERE  `module` = '."'mod_jegroupbuy'";
		$db->setQuery( $query );
		$id 		= $db->loadResult();
		
		$query = "INSERT INTO `#__modules_menu` (`moduleid`, `menuid`) VALUES ('$id', '0')";
		$db->setQuery( $query );
		$db->query();
	} 
	//Install Jegroupbuy Module		
	
	//plugin installation code	
	
	jimport( 'joomla.filesystem.folder' );
	jimport('joomla.filesystem.file');
	//folder moving code
	if (!JFolder::exists(JPATH_SITE.DS.'plugins'.DS.'jegroupbuypayment'))
	{
	//--jegroupbuyPaypal
		$db->setQuery("SELECT count(*) FROM  #__plugins WHERE element='jegroupbuyPaypal'");
	    if(!$db->loadResult())
		{
			$sql="INSERT INTO `#__plugins` ( `name`, `element`, `folder`, `access`, `ordering`, `published`, `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`) VALUES ('jeGroupBuy - Paypal', 'jegroupbuyPaypal', 'jegroupbuypayment', 0, 1, 0, 0, 0, 0, '0000-00-00 00:00:00', '');";
			$db->setQuery( $sql);
			$db->query();
		}

		//--jegroupbuyAuthorizedotnet
		$db->setQuery("SELECT count(*) FROM  #__plugins WHERE element='jegroupbuyAuthorizedotnet'");
	    if(!$db->loadResult())
		{
			$sql="INSERT INTO `#__plugins` ( `name`, `element`, `folder`, `access`, `ordering`, `published`, `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`) VALUES ('jeGroupBuy - Authorizedotnet', 'jegroupbuyAuthorizedotnet', 'jegroupbuypayment', 0, 3, 0, 0, 0, 0, '0000-00-00 00:00:00', '');";
			$db->setQuery( $sql);
			$db->query();		
		}

		//--jegroupbuy2Checkout
		$db->setQuery("SELECT count(*) FROM  #__plugins WHERE element='jegroupbuy2Checkout'");
	    if(!$db->loadResult())
		{
			$sql="INSERT INTO `#__plugins` ( `name`, `element`, `folder`, `access`, `ordering`, `published`, `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`) VALUES ('jeGroupBuy - 2Checkout', 'jegroupbuy2Checkout', 'jegroupbuypayment', 0, 3, 0, 0, 0, 0, '0000-00-00 00:00:00', '');";
			$db->setQuery( $sql);
			$db->query();		
		}		
		

		$sourcefile = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_jegroupbuy'.DS.'jegroupbuypayment';
		$destinationfile = JPATH_SITE.DS.'plugins'.DS.'jegroupbuypayment';
		JFolder::move($sourcefile, $destinationfile);
	}
	else
	{
		$destinationfile = JPATH_SITE.DS.'plugins'.DS.'jegroupbuypayment';
		JFolder::delete($destinationfile);
		$sourcefile = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_jegroupbuy'.DS.'jegroupbuypayment';
		$destinationfile = JPATH_SITE.DS.'plugins'.DS.'jegroupbuypayment';
		JFolder::move($sourcefile, $destinationfile);		

	}
//==============

	//Add 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();
	}
	
	$query 		= "SELECT `show_footer` FROM #__jegroupbuy_settings";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query  = "ALTER TABLE  `#__jegroupbuy_settings`  ADD `show_footer` tinyint( 3 ) NOT NULL AFTER  `layout`" ;
		$db->setQuery( $query );
		$db->query();
	}
	
	$query 		= "SELECT `apikey` FROM #__jegroupbuy_settings";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query  = "ALTER TABLE  `#__jegroupbuy_settings`  ADD `apikey` varchar( 255 ) NOT NULL AFTER  `show_footer`" ;
		$db->setQuery( $query );
		$db->query();
	}
		
	$query = "SELECT count(*) FROM #__jegroupbuy_template";
	$db->setQuery( $query );
	$temCount = $db->loadResult();
	if(!$temCount)
	{
		$query = "INSERT INTO `#__jegroupbuy_template` (`id`,`template`) VALUES (1,'<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>
<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 colspan=\"2\" width=\"100%\" align=\"left\">
<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=\"100%\">
<tbody>
<tr>
<td width=\"50%\" align=\"left\" valign=\"top\">
<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=\"100%\">
<tbody>
<tr>
<td style=\"background-color: #d6d6d6;\" colspan=\"2\" align=\"left\">
<h4 style=\"margin: 0px;\">User Details</h4>
</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">User Name:</td>
<td align=\"left\">{user_name}</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">User EmailId:</td>
<td align=\"left\">{user_mailid}</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">Payment Type:</td>
<td align=\"left\">{user_payment}</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<th colspan=\"2\" align=\"left\"> </th>
</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\" align=\"left\">
<h4 style=\"margin: 0px;\">Coupon Information</h4>
</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">Coupon Code:</td>
<td align=\"left\">{coupon_code}</td>
</tr>
<tr>
<td width=\"19%\" 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\" align=\"left\">
<h4 style=\"margin: 0px;\">Order Information</h4>
</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">Order Number:</td>
<td align=\"left\">{order_number}</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">Order Date:</td>
<td align=\"left\">{order_date}</td>
</tr>
<tr>
<td width=\"19%\" align=\"left\">Transaction Id:</td>
<td align=\"left\">{transaction_id}</td>
</tr>
<tr>
<td width=\"19%\" 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>');";
		$db->setQuery( $query );
		$db->query();
	}
	
	
	if(JFile::exists(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'views'.DS.'couponpage'.DS.'tmpl'.DS.'coupon.php'))
		JFile::delete(JPATH_ROOT.DS.'components'.DS.'com_jegroupbuy'.DS.'views'.DS.'couponpage'.DS.'tmpl'.DS.'coupon.php');
	
	$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);		
	}	
	
}
?>
