<?php
/**
* @package ccNewsletter
* @version 1.0.7
* @author  Chill Creations <info@chillcreations.com>
* @link    http://www.chillcreations.com
* @copyright Copyright (C) 2008 - 2010 Chill Creations-All rights reserved
* @license GNU/GPL, see LICENSE.php for full license.
* See COPYRIGHT.php for more copyright notices and details.

This file is part of ccNewsletter.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
**/

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

function com_install() {

		$db			=& JFactory::getDBO();
		global $mainframe;
		jimport('joomla.filesystem.folder');
		jimport('joomla.filesystem.file');
		
	$jversion = new JVersion();
	$current_version =  $jversion->getShortVersion();
	$current_version=substr($current_version,0,3);		

//Module installation
		//Install ccNewsletter Module
	if(!JFolder::exists(JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter')) 
	{		
		if($current_version=="1.5")		
		{
			JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15'.DS.'mod_ccnewsletter', JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter');
			if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter')) 		
				JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter');			
			$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 ('', 'ccNewsletter', '', '1', 'left', 0, '0000-00-00 00:00:00', 0, 'mod_ccnewsletter', '0', '0', '1', 'style=mootools
			introduction=
			lname=Name
			lemail=Email
			lsubscribe=Subscribe
			lunsubscribe=UnSubscribe
			lmove=Move
			lclose=Close
			namewarning=Enter your name!!
			emailwarning=Enter the valid email!!
			terms_cond_warn=Check the Terms and condition!!
			unsubscribe_button=0
			showterm=0
			popuptype=1
			showterm_text=Terms and condition
			id=0
			cache=1
			cache_time=900
			moduleclass_sfx=', '0', '0', '')";
			$db->setQuery( $query );
			$db->query();
		}
		else
		{
			JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter', JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter');
			if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15')) 		
				JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15');			
			$query = "INSERT INTO `#__modules` (`id`, `title`, `content`, `ordering`, `position`, `checked_out`, `checked_out_time`, `published`, `module`,  `access`, `showtitle`, `params`,  `client_id`,`language`) VALUES ('', 'ccNewsletter', '', '1', 'position-4', 0, '0000-00-00 00:00:00', 0, 'mod_ccnewsletter',  '1', '1', 'style=mootools
			introduction=
			lname=Name
			lemail=Email
			lsubscribe=Subscribe
			lunsubscribe=UnSubscribe
			lmove=Move
			lclose=Close
			namewarning=Enter your name!!
			emailwarning=Enter the valid email!!
			terms_cond_warn=Check the Terms and condition!!
			unsubscribe_button=0
			showterm=0
			popuptype=1
			showterm_text=Terms and condition
			id=0
			cache=1
			cache_time=900
			moduleclass_sfx=',  '0','*')";
			$db->setQuery( $query );
			$db->query();		

			$db->setQuery("SELECT count(*) FROM  #__extensions WHERE element='mod_newsletter'");
			if(!$db->loadResult())
			{			
				
				$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 ('ccNewsletter', 'module', 'mod_ccnewsletter', 'mod_ccnewsletter', 0, 1, 1, 1, '{\"legacy\":true,\"name\":\"ccNewsletter module\",\"type\":\"module\",\"creationDate\":\"May 26, 2011\",\"author\":\"Chill Creations\",\"copyright\":\"[2011] Chill Creations\",\"authorEmail\":\"info@chillcreations.com\",\"authorUrl\":\"http:\\/\\/www.chillcreations.com\",\"version\":\"1.2.2\",\"description\":\"ccNewsletter module\",\"group\":\"\"}', '', '', '', 0, '0000-00-00 00:00:00', 1, 0)";
				$db->setQuery( $query );
				$db->query();		
			}
		}
		$id 		= $db->insertid();
		
		$query = "INSERT INTO `#__modules_menu` (`moduleid`, `menuid`) VALUES ('$id', '0')";
		$db->setQuery( $query );
		$db->query();		
	} 
	else
	{		
		if($current_version=="1.5")
		{
			if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15'.DS.'mod_ccnewsletter')) 
			{
				if(JFolder::exists(JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter'))
				{				
					JFolder::delete(JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter');
					JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15'.DS.'mod_ccnewsletter', JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter');
				}
			}
			if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter')) 		
				JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter');
			
		}
		else
		{
			if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter')) 
			{
				if(JFolder::exists(JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter'))
				{				
					JFolder::delete(JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter');
					JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'mod_ccnewsletter', JPATH_ROOT.DS.'modules'.DS.'mod_ccnewsletter');
				}
			}
			if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15')) 		
				JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'j15');
			
		}
	}
		
	
	//Install ccNewsletter Module
	if($current_version!="1.5")
	{		
		if(!is_writable(JPATH_SITE . DS . "images" . DS . "stories" ))
		{
			@mkdir(JPATH_SITE . DS . "images" . DS . "stories" );
		}
		if(!is_writable(JPATH_SITE . DS . "images" . DS . "stories".DS."ccnewsletter" ))
		{
			@mkdir(JPATH_SITE . DS . "images" . DS . "stories".DS."ccnewsletter" );
		}
	}
	if(is_writable(JPATH_SITE . DS . "images" . DS . "stories" ))
	{
		if(!file_exists(JPATH_SITE.DS."images".DS."stories".DS."ccnewsletter"))
		{			
			//$message ="ccNewsletter component installed successfully, also install the module!";
			rename(JPATH_SITE.DS."administrator".DS."components".DS."com_ccnewsletter".DS."ccnewsletter", JPATH_SITE.DS."images".DS."stories".DS."ccnewsletter");			
		}
		elseif((file_exists(JPATH_SITE.DS."images".DS."stories".DS."ccnewsletter"))&&(!file_exists(JPATH_SITE.DS."administrator".DS."components".DS."com_ccnewsletter".DS."install.xml")))
		{			
			//$message ="ccNewsletter component installed successfully, also install the module!";
		}
		else
		{
		
			$adminDir = JPATH_ADMINISTRATOR .DS. 'components';
			$siteDir = JPATH_SITE .DS. 'components';
			$file = $adminDir.DS.'com_ccnewsletter'.DS.'install.xml';
			$xml = JFactory::getXMLParser('Simple');
			$xml->loadFile($file);
			$xml = $xml->document;
			$version_no = $xml->version[0]->data();		
			if($version_no == "1.0.6")
			{
				$message = "You already have ccNewsletter 1.0.6";
				JError::raiseWarning(100, $message);
				$mainframe->redirect('index.php?option=com_installer');
			}
			else
			{
				//$message ="ccNewsletter component upgraded from  $version_no to 1.0.6 successfully!";
			}

		}
	}
	
	//font moving code		
	if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'models'.DS.'fonts')) 		
	{
		if(!JFolder::exists(JPATH_ROOT.DS.'media'.DS.'fonts'))
			JFolder::copy(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'models'.DS.'fonts', JPATH_ROOT.DS.'media'.DS.'fonts');
	}

	if(JFolder::exists(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'models'.DS.'fonts')) 		
			JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_ccnewsletter'.DS.'models'.DS.'fonts');
//-----------------------------

	if( file_exists(JPATH_SITE.DS."administrator".DS."components".DS."com_spsnewsletter".DS."admin.spsnewsletter.php"))
	{
		$query = "INSERT INTO #__ccnewsletter_subscribers (name, email, plainText, enabled)"
				. "\n (SELECT name, email, plainText, '1' FROM #__spsnewsletter_subscribers)";
		$db->setQuery( $query );
		$db->query();

		$query = "INSERT INTO #__ccnewsletter_newsletters (name, body)"
				. "\n (SELECT name, body FROM #__spsnewsletter_newsletters)";
		$db->setQuery( $query );
		$db->query();

		$query = "SELECT params FROM #__components WHERE name='spsNewsletter' LIMIT 0, 1";
		$db->setQuery( $query );
		$parameters = $db->loadResult();

		$query = "UPDATE #__components SET params='". $parameters . "' WHERE name='ccNewsletter'";
		$db->setQuery( $query );
		$db->query();
	}
	else
	{

		$query = "SELECT count(*) FROM #__ccnewsletter_newsletters";
		$db->setQuery( $query );
		$total_rows = $db->loadResult();
		if(!$total_rows)
		{
			$query = "INSERT INTO #__ccnewsletter_newsletters (`name`, `body`) VALUES"
				. "\n ('My First Newsletter', '<center><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"100%\" width=\"100%\"><tbody><tr>"
				. "\n <td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr>"
				. "\n <td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr>"
				. "\n <td><img src=\"images/stories/ccnewsletter/default_template/logo_banner.gif\" style=\"max-width: 600px;\" width=\"600\" alt=\"logo_banner\" /></td></tr></tbody></table></td></tr>"
				. "\n <tr><td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr><td valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
				. "\n <tbody><tr><td valign=\"top\"><table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" width=\"100%\"><tbody><tr><td valign=\"top\">"
				. "\n <h1><img src=\"images/stories/ccnewsletter/default_template/left_header.gif\" width=\"340\" alt=\"left_header\" /></h1>"
				. "\n <h1><span style=\"font-size: 17px; color: #000000;\">Our first newsletter</span></h1>"
				. "\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque non erat eu elit imperdiet elementum ac eu purus. Duis lobortis risus sed erat consectetur sed rutrum mauris hendrerit. Sed pulvinar leo sed dui congue condimentum bibendum mi euismod.                                                                                                                                                                          Sed at dolor massa. Morbi viverra feugiat massa, eu aliquet diam consequat nec. Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim."
				. "\n <h1 style=\"font-size: 1.3em; font-family: Helvetica, Arial, sans-serif; font-weight: bold; vertical-align: bottom; color: #666666; text-align: left; width: 340px; padding: 0px;\"><img style=\"border: 0px none initial;\" alt=\"left_header\" width=\"340\" src=\"images/stories/ccnewsletter/default_template/left_header.gif\" /></h1>"
				. "\n <h1 style=\"font-size: 1.3em; font-family: Helvetica, Arial, sans-serif; font-weight: bold; vertical-align: bottom; color: #666666; text-align: left; width: 340px; padding: 0px;\"><span style=\"font-size: 17px; color: #000000;\">A new website</span></h1>"								
                . "\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque non erat eu elit imperdiet elementum ac eu purus. Duis lobortis risus sed erat consectetur sed rutrum mauris hendrerit. Sed pulvinar leo sed dui congue condimentum bibendum mi euismod.Sed at dolor massa. Morbi viverra feugiat massa, eu aliquet diam consequat nec. Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim.</td>"                    
                . "\n </tr></tbody></table></td></tr></tbody></table></td>"                        
                . "\n <td valign=\"top\" width=\"200\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\"><tbody><tr><td valign=\"top\">"                                                                    
				. "\n <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"200px\" style=\"padding-top: 20px; padding-left: 20px; padding-bottom: 20px;\"><tbody><tr><td valign=\"top\">"																		
				. "\n <table border=\"0\" cellpadding=\"0\" cellspacing=\"4\"><tbody><tr>"                                                                                    
				. "\n <td align=\"left\" valign=\"middle\" width=\"16\"><img src=\"images/stories/ccnewsletter/default_template/facebook_icon.png\" style=\"margin: 0px !important;\" width=\"16\" height=\"16\" alt=\"facebook_icon\" /></td>"
                . "\n <td align=\"left\" valign=\"top\">Connect on facebook</td></tr><tr>"
                . "\n <td align=\"left\" valign=\"middle\" width=\"16\"><img src=\"images/stories/ccnewsletter/default_template/twitter_icon.png\" style=\"margin: 0px !important;\" width=\"16\" height=\"16\" alt=\"twitter_icon\" /></td>"				
				. "\n <td align=\"left\" valign=\"top\">Follow on Twitter</td></tr></tbody></table></td></tr></tbody></table>"
                . "\n <table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" width=\"100%\"><tbody><tr>"
				. "\n <td valign=\"top\"><img src=\"images/stories/ccnewsletter/default_template/right_header.gif\" style=\"max-width: 160px;\" width=\"160\" alt=\"right_header\" /><h3>Summer discount</h3>"
                . "\n Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim lectus tempor ut.                                                                                                                                                                                           <br /><br /> <img style=\"max-width: 160px; border: 0px none initial;\" alt=\"right_header\" width=\"160\" src=\"images/stories/ccnewsletter/default_template/right_header.gif\" />"                                                                    
                . "\n <h3>New partners</h3>Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim lectus tempor ut.</td></tr></tbody></table>"                                                    
                . "\n </td></tr></tbody></table></td></tr></tbody></table></td></tr>"                                                        
                . "\n <tr><td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" width=\"600\"><tbody><tr><td valign=\"top\"><table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" width=\"100%\">"                                                    
                . "\n <tbody><tr><td colspan=\"7\" valign=\"middle\">To unsubscribe from this newsletter, click this [unsubscribe link]!</td></tr></tbody></table></td></tr></tbody></table></td></tr>"                                                        
                . "\n </tbody></table><br /></td></tr></tbody></table></center>')";                                            
			$db->setQuery( $query );
			$db->query();			
			JFolder::move(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_ccnewsletter'.DS.'assets'.DS.'default_template', JPATH_ROOT.DS.'images'.DS.'stories'.DS.'ccnewsletter'.DS.'default_template');
			
		}
		else
		{
			
			$tempname="New newsletter template";			
			$query = "SELECT count(*) FROM #__ccnewsletter_newsletters where name='".$tempname."'";
			$db->setQuery( $query );
			$total_rows = $db->loadResult();			
			if(!$total_rows)
			{
				$query = "INSERT INTO #__ccnewsletter_newsletters (`name`, `body`) VALUES"
					. "\n ('New newsletter template', '<center><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"100%\" width=\"100%\"><tbody><tr>"
					. "\n <td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr>"
					. "\n <td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr>"
					. "\n <td><img src=\"images/stories/ccnewsletter/default_template/logo_banner.gif\" style=\"max-width: 600px;\" width=\"600\" alt=\"logo_banner\" /></td></tr></tbody></table></td></tr>"
					. "\n <tr><td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tbody><tr><td valign=\"top\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
					. "\n <tbody><tr><td valign=\"top\"><table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" width=\"100%\"><tbody><tr><td valign=\"top\">"
					. "\n <h1><img src=\"images/stories/ccnewsletter/default_template/left_header.gif\" width=\"340\" alt=\"left_header\" /></h1>"
					. "\n <h1><span style=\"font-size: 17px; color: #000000;\">Our first newsletter</span></h1>"
					. "\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque non erat eu elit imperdiet elementum ac eu purus. Duis lobortis risus sed erat consectetur sed rutrum mauris hendrerit. Sed pulvinar leo sed dui congue condimentum bibendum mi euismod.                                                                                                                                                                          Sed at dolor massa. Morbi viverra feugiat massa, eu aliquet diam consequat nec. Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim."
					. "\n <h1 style=\"font-size: 1.3em; font-family: Helvetica, Arial, sans-serif; font-weight: bold; vertical-align: bottom; color: #666666; text-align: left; width: 340px; padding: 0px;\"><img style=\"border: 0px none initial;\" alt=\"left_header\" width=\"340\" src=\"images/stories/ccnewsletter/default_template/left_header.gif\" /></h1>"
					. "\n <h1 style=\"font-size: 1.3em; font-family: Helvetica, Arial, sans-serif; font-weight: bold; vertical-align: bottom; color: #666666; text-align: left; width: 340px; padding: 0px;\"><span style=\"font-size: 17px; color: #000000;\">A new website</span></h1>"								
					. "\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque non erat eu elit imperdiet elementum ac eu purus. Duis lobortis risus sed erat consectetur sed rutrum mauris hendrerit. Sed pulvinar leo sed dui congue condimentum bibendum mi euismod.Sed at dolor massa. Morbi viverra feugiat massa, eu aliquet diam consequat nec. Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim.</td>"                    
					. "\n </tr></tbody></table></td></tr></tbody></table></td>"                        
					. "\n <td valign=\"top\" width=\"200\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"200\"><tbody><tr><td valign=\"top\">"                                                                    
					. "\n <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"200px\" style=\"padding-top: 20px; padding-left: 20px; padding-bottom: 20px;\"><tbody><tr><td valign=\"top\">"																		
					. "\n <table border=\"0\" cellpadding=\"0\" cellspacing=\"4\"><tbody><tr>"                                                                                    
					. "\n <td align=\"left\" valign=\"middle\" width=\"16\"><img src=\"images/stories/ccnewsletter/default_template/facebook_icon.png\" style=\"margin: 0px !important;\" width=\"16\" height=\"16\" alt=\"facebook_icon\" /></td>"
					. "\n <td align=\"left\" valign=\"top\">Connect on facebook</td></tr><tr>"
					. "\n <td align=\"left\" valign=\"middle\" width=\"16\"><img src=\"images/stories/ccnewsletter/default_template/twitter_icon.png\" style=\"margin: 0px !important;\" width=\"16\" height=\"16\" alt=\"twitter_icon\" /></td>"				
					. "\n <td align=\"left\" valign=\"top\">Follow on Twitter</td></tr></tbody></table></td></tr></tbody></table>"
					. "\n <table border=\"0\" cellpadding=\"20\" cellspacing=\"0\" width=\"100%\"><tbody><tr>"
					. "\n <td valign=\"top\"><img src=\"images/stories/ccnewsletter/default_template/right_header.gif\" style=\"max-width: 160px;\" width=\"160\" alt=\"right_header\" /><h3>Summer discount</h3>"
					. "\n Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim lectus tempor ut.                                                                                                                                                                                           <br /><br /> <img style=\"max-width: 160px; border: 0px none initial;\" alt=\"right_header\" width=\"160\" src=\"images/stories/ccnewsletter/default_template/right_header.gif\" />"                                                                    
					. "\n <h3>New partners</h3>Vivamus ullamcorper malesuada libero. Nulla facilisi. Phasellus tempus feugiat eros, vel dignissim lectus tempor ut.</td></tr></tbody></table>"                                                    
					. "\n </td></tr></tbody></table></td></tr></tbody></table></td></tr>"                                                        
					. "\n <tr><td align=\"center\" valign=\"top\"><table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" width=\"600\"><tbody><tr><td valign=\"top\"><table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" width=\"100%\">"                                                    
					. "\n <tbody><tr><td colspan=\"7\" valign=\"middle\">To unsubscribe from this newsletter, click this [unsubscribe link]!</td></tr></tbody></table></td></tr></tbody></table></td></tr>"                                                        
					. "\n </tbody></table><br /></td></tr></tbody></table></center>')";                                            
				$db->setQuery( $query );
				$db->query();		
			}
			JFolder::move(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_ccnewsletter'.DS.'assets'.DS.'default_template', JPATH_ROOT.DS.'images'.DS.'stories'.DS.'ccnewsletter'.DS.'default_template');		
			 
		}
	}

	/* Adding synstatus and activation fields for older version */
	/*
	$query = "SELECT activation FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_acknowledgement  ADD  `activation` TINYINT( 1 ) NOT NULL DEFAULT  '0'";
		$db->setQuery( $query );
		$db->query();
	}

	$query = "SELECT synstatus FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_acknowledgement  ADD  `synstatus` TINYINT( 1 ) NOT NULL DEFAULT  '0'";
		$db->setQuery( $query );
		$db->query();
	}

	$query = "SELECT deactivation FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_acknowledgement  ADD  `deactivation` TINYINT( 1 ) NOT NULL DEFAULT  '0'";
		$db->setQuery( $query );
		$db->query();
	}*/
	$query = "SELECT deactivation_title FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_acknowledgement  ADD  `deactivation_title` varchar( 255 ) NOT NULL ";
		$db->setQuery( $query );
		$db->query();
	}
	$query = "SELECT deactivation_content FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_acknowledgement  ADD  `deactivation_content` mediumtext NOT NULL ";
		$db->setQuery( $query );
		$db->query();
	}

	$query = "SELECT deactivation_content FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_acknowledgement  ADD  `deactivation_content` mediumtext NOT NULL ";
		$db->setQuery( $query );
		$db->query();
	}

	$query = "SELECT activation FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if($db->query())
	{
		$query = "ALTER TABLE `#__ccnewsletter_acknowledgement`  DROP `activation`";
		$db->setQuery( $query );
		$db->query();
	}

	$query = "SELECT synstatus FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	if($db->query())
	{
		$query = "ALTER TABLE `#__ccnewsletter_acknowledgement`  DROP `synstatus`";
		$db->setQuery( $query );
		$db->query();
	}	
	
	

	

	//configuration from parameters
	$query = "SELECT count(*) FROM #__ccnewsletter_configuration";
	$db->setQuery( $query );
	$total_rows = $db->loadResult();
	if($total_rows<=0)
	{
		$fromname="";
		$fromEmail="";
		$testEmailAddress="";
		$emailperbatch="";
		$batchinterval="";
		$scripttimeout="";
		$emailvalidation="";
		$status="";
		$synstatus="";
		$activation="";
		$Syncallusers="";
		$Captcha="";
		$unsubscribe="";
		if($current_version=="1.5")
		{
			$params = &JComponentHelper::getParams( 'com_ccnewsletter' );		
			$fromname=$params->get('fromName');
			$fromEmail=$params->get('fromEmail');
			$testEmailAddress=$params->get('testEmailAddress');
			$emailperbatch=$params->get('emailperbatch');
			$batchinterval=$params->get('batchinterval');
			$scripttimeout=$params->get('scripttimeout');
			$emailvalidation=$params->get('emailvalidation');		
		}

		if(trim($emailperbatch)=="")
			$emailperbatch="75";
		if(trim($batchinterval)=="")
			$batchinterval="10";
		if(trim($scripttimeout)=="")
			$scripttimeout="120";

		if(trim($emailvalidation)=="")
			$emailvalidation="0";
		if($current_version=="1.5")
			$status=$params->get('status');
		if(trim($status)=="")
			$status="0";
		if($current_version=="1.5")
			$synstatus=$params->get('synstatus');
		if(trim($synstatus)=="")
			$synstatus="0";
		if($current_version=="1.5")
			$activation=$params->get('activation');
		if(trim($activation)=="")
			$activation="0";
		$emaildeactivation="0";
		if($current_version=="1.5")
			$Syncallusers=$params->get('Syncallusers');
		if(trim($Syncallusers)=="")
			$Syncallusers="0";
		if($current_version=="1.5")
			$Captcha=$params->get('Captcha');
		if(trim($Captcha)=="")
			$Captcha="0";
		if($current_version=="1.5")
			$unsubscribe=$params->get('unsubscribe');
		if(trim($unsubscribe)=="")
			$unsubscribe="0";
		if($current_version=="1.5")
			$date_format="%d-%m-%Y";		
		else
			$date_format="d-m-Y";		
		$query = "INSERT INTO #__ccnewsletter_configuration (`id`, `fromname`, `fromemail`, `testemail`, `emailbatch`, `batchinter`, `scripttimeout`, `extendemail`, `syncalluser`, `captcha`, `unsubscribebt`,`date_format`,`acknowledgeoff`,`acknowledgeon`,`emailactivation`,`deactivation`) VALUES ( 1,".$db->Quote($fromname).", ".$db->Quote($fromEmail).", ".$db->Quote($testEmailAddress).", ".$db->Quote($emailperbatch).", ".$db->Quote($batchinterval).", ".$db->Quote($scripttimeout).", $emailvalidation,$Syncallusers,$Captcha,$unsubscribe,".$db->Quote($date_format).",$status,$synstatus,$activation,$emaildeactivation)";		
		$db->setQuery( $query );
		$db->query();
	}	

	$query = "SELECT embeddedimages FROM #__ccnewsletter_configuration";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE `#__ccnewsletter_configuration`  ADD  `embeddedimages` TINYINT(1) NOT NULL DEFAULT '1'";
		$db->setQuery( $query );
		$db->query();
	}		
	
	$query = "SELECT bignewsletterissue FROM #__ccnewsletter_configuration";
	$db->setQuery( $query );
	if($db->query())
	{
		$query = "ALTER TABLE `#__ccnewsletter_configuration` DROP `bignewsletterissue`";
		$db->setQuery( $query );
		$db->query();
	}	
	
	
	
	if(JFile::exists(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_ccnewsletter'.DS.'config.xml'))
	{
		JFile::delete(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_ccnewsletter'.DS.'config.xml');
	}
	
	/* Insert acknowledgement configuration */
	$query = "SELECT count(*) FROM #__ccnewsletter_acknowledgement";
	$db->setQuery( $query );
	$total_rows = $db->loadResult();
	if(!$total_rows)
	{
		$query = "INSERT INTO #__ccnewsletter_acknowledgement (`id`, `subs_title`, `subs_content`, `unsubs_title`, `unsubs_content`, `deactivation_title`, `deactivation_content`) VALUES ( 1,'Welcome to Sitename', 'Welcome mail content here', 'successfully unsubscribed from sitename', 'Your email has been unsubscribed successfully', 'Deactivation link from sitename', 'Deactivation content here')";
		$db->setQuery( $query );
		$db->query();
	}

	
		$query = "SELECT enabled FROM #__ccnewsletter_newsletters";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE  #__ccnewsletter_newsletters  ADD  `enabled` TINYINT( 1 ) NOT NULL DEFAULT  '0'";
		$db->setQuery( $query );
		$db->query();

		$query = "ALTER TABLE  #__ccnewsletter_newsletters  ADD  `lastsentdate` DATE NOT NULL default '0000-00-00'";
		$db->setQuery( $query );
		$db->query();

		$query = "ALTER TABLE  #__ccnewsletter_newsletters  ADD  `totalsubscriber` BIGINT NOT NULL DEFAULT  '0'";
		$db->setQuery( $query );
		$db->query();		
	}
	$query = "SELECT ordering FROM #__ccnewsletter_newsletters";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE `#__ccnewsletter_newsletters` ADD   `ordering` int(10) NOT NULL AFTER `totalsubscriber`";
		$db->setQuery( $query );
		$db->query();
	}

	jimport('joomla.filesystem.folder');

	
		$db =& JFactory::getDBO();

		$query = 'SELECT *' .
				' FROM #__components' .
				' WHERE parent = 0' .
				' ORDER BY iscore, name';
		$db->setQuery($query);
		$rows = $db->loadObjectList();

	if(file_exists(JPATH_SITE.DS."images".DS."stories".DS."ccnewsletter".DS."params"))
	{
		$myFile = JPATH_SITE .DS. 'images'.DS.'stories'.DS.'ccnewsletter'.DS.'params'.DS.'config.txt';
		$fh = fopen($myFile, 'r');
		$theData = fread($fh, 9999999);
		fclose($fh);

		$query="UPDATE  #__components SET params='$theData' WHERE name='ccNewsletter'";
		$db->setQuery( $query );
		$db->query();

		//$myFile = "myfiles/test.txt";
		unlink($myFile);

		$mydir = JPATH_SITE .DS. 'images'.DS.'stories'.DS.'ccnewsletter'.DS.'params';
		if (is_dir($mydir)) {
		rmdir($mydir);
		} else {
		echo $mydir.' does not exists';
		}

	}
	
	$query = "SELECT lastSentNewsletter FROM #__ccnewsletter_subscribers";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query ="ALTER TABLE #__ccnewsletter_subscribers ADD `lastSentNewsletter` INT( 11 ) default NULL";
		$db->setQuery( $query );
		$db->query();
		$query = "SELECT lastSentNewsletter FROM #__ccnewsletter_subscribers";
		$db->setQuery( $query );
		if(!$db->query())
		{
			echo "<div style=\"color:red\">The ccnewsletter_subscribers table not altered</div>";
		}
	}
	//ordering field supporing for joomla 1.5+1.6+1.7
	$query = "SELECT ordering FROM #__ccnewsletter_subscribers";
	$db->setQuery( $query );
	if(!$db->query())
	{
		$query = "ALTER TABLE `#__ccnewsletter_subscribers` ADD   `ordering` int(10) NOT NULL AFTER `lastSentNewsletter`";
		$db->setQuery( $query );
		$db->query();
	}	
	
	
//configuration date issue solved.
	
	$sql = "SELECT date_format FROM #__ccnewsletter_configuration WHERE id = 1 LIMIT 1";
	$db->setQuery($sql);
	$dtformat = $db->loadResult();

	$flag="0";
	$newstr="";
	if($dtformat!="")
	{
		for($i=0;$i<=strlen($dtformat)-1;$i++)
		{
			if(trim(substr($dtformat,$i,1))=="%")
			{
				$flag="1";

			}
		}
	}
	if($flag=="0")
	{
		for($i=0;$i<=strlen($dtformat)-1;$i++)
		{
			if((ord(substr($dtformat,$i,1))>=65 AND ord(substr($dtformat,$i,1))<=90) OR (ord(substr($dtformat,$i,1))>=97 AND ord(substr($dtformat,$i,1))<=122))
			{
				$newstr.="%".substr($dtformat,$i,1);
			}
			else
			{
				$newstr.=substr($dtformat,$i,1);
			}

		}
		$sql = "UPDATE #__ccnewsletter_configuration SET date_format='".$newstr."' WHERE id = 1";
		$db->setQuery($sql);
		$db->query();
	}		

	//echo $message;	
}
?>
