<?php
/**
 * Adodis Blog package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 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()
{
	$status			=  true;
	// Joomla predefiend function to connect db
	$db				= & JFactory::getDBO();

	// Insert default configuration details
	$query 			= "SELECT count(*) FROM #__adodisblog_settings";
	$db->setQuery( $query );
	$total_rows 	= $db->loadResult();

	// Check the whether the data's already there.
	if(!$total_rows) {
		$query  	= "INSERT INTO `#__adodisblog_settings` " .
					" (`id`, `title_page`, `orderby`, `sortby`, `show_title`, `description_page`, `show_description`, `bg_color`, `mail_admin_temp`, `mail_user_temp`, `mail_admin_sub`, `mail_user_sub`) " .
					" VALUES " .
					" (1, 'Title Here', 'ordering', 'desc', 0, 'Title Description here.', 0, '#E0F0FF', '<p>Hai Admin,</p>\r\n<p>A New [blog/comment] is Added in Adodis Blog Component By [username] and the [blog/comment] have [published/unpublished] successfully.</p>\r\n<p>Admin link</p>\r\n<p>[admin-link]</p>\r\n<p>Site link</p>\r\n<p>[site-link]</p>\r\n<p>Thank you.</p>\r\n<p> </p>', '<p>Hai [username],</p>\r\n<p>You have Added [blog/comment] in Adodis Blog Component.</p>\r\n<p>Status : [published/unpublished]</p>\r\n<p>You can view your [blog/comment] once it Published by Admin in the below link</p>\r\n<p>[site-link]</p>\r\n<p>Thanks and Regards,</p>\r\n<p>Admin.</p>', 'New [b/c] Added', 'New [b/c] Added')";
		$db->setQuery( $query );
		$db->query();
	}
	
		
	// Code for Install Adodis Blog user plugin.
		if(!JFolder::exists(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers')) {
			JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers'.DS.'adodisblogusers', JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers');
			
			if (!JFile::exists(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.php')) {
			JFile::move(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers'.DS.'adodisblogusers.php',JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.php');
			}
			
			if (!JFile::exists(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.xml')) {
				JFile::move(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers'.DS.'adodisblogusers.xml',JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.xml');
			}
			
			if(JFile::exists(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.php') && JFile::exists(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.xml') && JFolder::exists(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers')) {
				
				//Insert plugin details.
				$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 ('', 'User - Adodis Blog Users', 'plugin', 'adodisblogusers', 'user', 0, 1, 1, 0, '{\"legacy\":true,\"name\":\"User - Adodis Blog Users\",\"type\":\"plugin\",\"creationDate\":\"Januaury, 2012\",\"author\":\"JExtension\",\"copyright\":\"[2012] - JExtn\",\"authorEmail\":\"contact@jextn.com\",\"authorUrl\":\"http:\\/\\/www.jextn.com\",\"version\":\"1.0.0\",\"description\":\"Adodis Blog user plugin. This plugin is responsible to Insert user''s .\",\"group\":\"\"}', '{}', '', '', 0, '0000-00-00 00:00:00', 0, 0)";
				$db->setQuery( $query );
				$db->query();
						
				JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers');	
				
				$query 			= "SELECT count(*) FROM #__adodisblog_users";
				$db->setQuery( $query );
				$total_rows 	= $db->loadResult();
				
				// Check the whether the data's already there.
				if(!$total_rows) {
					//Insert Already Exits Users.
					$query 					= "SELECT * from #__users";
					$db->setQuery( $query );
					$userdetails			= $db->loadObjectList();
					foreach( $userdetails as $key=>$value) {
						$data 				= new stdClass();
						$data->id 			= null;
						$data->name 		= $value->name;
						$data->username 	= $value->username;
						$data->email 		= $value->email;
						$data->user_id 		= $value->id;
						$data->block 		= 1;
						
						$db 				= JFactory::getDBO();
						$db->insertObject( '#__adodisblog_users', $data );
					}
					//Insert Already Exits Users Ends.
				}
			}
		}	
		else
		{ 
			JFolder::delete(JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers');
			
			JFolder::move(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers'.DS.'adodisblogusers', JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers');	
			
			JFile::move(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers'.DS.'adodisblogusers.php',JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.php');
			
			JFile::move(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers'.DS.'adodisblogusers.xml',JPATH_ROOT.DS.'plugins'.DS.'user'.DS.'adodisblogusers'.DS.'adodisblogusers.xml');
			
			JFolder::delete(JPATH_ROOT.DS.'components'.DS.'com_adodisblog'.DS.'plg_adodisblogusers');
		}

	if($status == true) {
		// Message area.
		echo '<p> <b> <span style="color:#009933"> Adodis Blog component and plugin has been Installed Successfully. </span> </b> </p>';
	}
}


?>

