<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

defined('_JEXEC') or die;

$adminPath = dirname(__FILE__) . '/';
require_once $adminPath . 'kernel/class.AriKernel.php';
require_once $adminPath . 'defines.php';

AriKernel::import('Application.ARISmartContent.Utils.Helper');
AriKernel::import('Install.Installer');
AriKernel::import('Joomla.Models.Model');
AriKernel::import('Joomla.Tables.Table');

jimport('joomla.application.component.model');
jimport('joomla.database.table');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');

function com_install() 
{
	$installer = new AriSmartContentInstall(
		array(
			'option' => 'com_arismartcontent'
		)
	);

	$res = $installer->install();
	$isSuccess = $installer->isSuccess();

	if (!$isSuccess)
		echo nl2br(trim($res));

	return $isSuccess;	
}

class AriSmartContentInstall extends AriInstallerBase
{
	var $_tblPrefix = 'AriSmartContentTable';
	var $_modelPrefix = 'AriSmartContentModel';
	
	function __construct($options)
	{	
		parent::__construct($options);
		
		AriModel::addIncludePath($this->adminPath . 'models');
		AriTable::addIncludePath($this->adminPath . 'tables');
		
		$lang = JFactory::getLanguage();
		$lang->load('com_arismartcontent');
	}
	
	function installSteps()
	{
		if (!$this->isDbSupportUtf8())
		{
			trigger_error(ARI_INSTALL_ERROR_UTF8, E_USER_ERROR);
			return false;
		}
		
		$this->_executeSqlScript();

		$path = dirname(__FILE__) . '/';

		$this->installPlugin($path . 'install/plugins/plg_system_ariextensions.zip');
		$this->installPlugin($path . 'install/plugins/plg_system_arismartcontent.zip');

		$currentVersion = $this->_getCurrentVersion();

		$this->_applyUpdates($currentVersion);		
		$this->_updateConfig(ARISMARTCONTENT_VERSION, $currentVersion);

		return true;
	}

	function _executeSqlScript()
	{
		$scriptPath = dirname(__FILE__) . '/install/sql/mysql/install.sql';
		$database = $this->_db;

		$sql = file_get_contents($scriptPath);
		
		if (J3_0)
		{
			$queries = $database->splitSql($sql);
			if (is_array($queries))
				foreach ($queries as $query)
				{
					$database->setQuery($query);
					$database->query();
					if ($database->getErrorNum())
					{
						trigger_error(sprintf(ARI_INSTALL_ERROR_EXECUTEQUERY, $database->getErrorMsg()), E_USER_ERROR);
						return false;
					}
				}
		}
		else
		{
			$database->setQuery($sql);
			$database->queryBatch();
			if ($database->getErrorNum())
			{
				trigger_error(sprintf(ARI_INSTALL_ERROR_EXECUTEQUERY, $database->getErrorMsg()), E_USER_ERROR);
				return false;
			}
		}
		
		return true;
	}

	function _updateConfig($version, $prevVersion)
	{
		$cfg = AriSmartContentHelper::getConfig();

		$cfg->set('Version', $version);
		
		if ($prevVersion != $version)
			$cfg->set('PrevVersion', $prevVersion);

		$cfg->save();
	}

	function _getCurrentVersion()
	{
		$db = $this->_db;
		$db->setQuery('SELECT ParamValue FROM #__arismartcontent_config WHERE ParamName = "Version" LIMIT 0,1');
		$version = $db->loadResult();
		if (empty($version) || $db->getErrorNum())
			$version = ARISMARTCONTENT_VERSION;

		if (version_compare($version, '2.0.0', '<'))
			$version = '1.0.0';

		return $version;
	}
}