<?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();

	// 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;
			}
		}

	// 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 align="left" valign="top" width="10%">Quantity</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 align="left" valign="top" width="10%">{quantity}</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;
			}
		}
		
	$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">JE GroupBuy Component has been installed successfully.</span></b> </p>';
}
?>

