<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

$status = new JObject();
$status->modules = array();
$status->plugins = array();



//PLUGIN INSTALLATION SECTION

$plugins = &$this->manifest->getElementByPath('plugins');
if (is_a($plugins, 'JSimpleXMLElement') && count($plugins->children())) {

	foreach ($plugins->children() as $plugin)
	{
		$pname		= $plugin->attributes('plugin');
		$pgroup		= $plugin->attributes('group');
		$porder		= $plugin->attributes('order');

		// Set the installation path
		if (!empty($pname) && !empty($pgroup)) {
			$this->parent->setPath('extension_root', JPATH_ROOT.DS.'plugins'.DS.$pgroup);
		} else {
			$this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('No plugin file specified'));
			return false;
		}


		 //Filesystem Processing Section


		// If the plugin directory does not exist, lets create it
		$created = false;
		if (!file_exists($this->parent->getPath('extension_root'))) {
			if (!$created = JFolder::create($this->parent->getPath('extension_root'))) {
				$this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('Failed to create directory').': "'.$this->parent->getPath('extension_root').'"');
				return false;
			}
		}

		/*
		* If we created the plugin directory and will want to remove it if we
		* have to roll back the installation, lets add it to the installation
		* step stack
		*/
		if ($created) {
			$this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
		}

		// Copy all necessary files
		$element = &$plugin->getElementByPath('files');
		if ($this->parent->parseFiles($element, -1) === false) {
			// Install failed, roll back changes
			$this->parent->abort();
			return false;
		}

		// Copy all necessary files
		$element = &$plugin->getElementByPath('languages');
		if ($this->parent->parseLanguages($element, 1) === false) {
			// Install failed, roll back changes
			$this->parent->abort();
			return false;
		}

		// Copy media files
		$element = &$plugin->getElementByPath('media');
		if ($this->parent->parseMedia($element, 1) === false) {
			// Install failed, roll back changes
			$this->parent->abort();
			return false;
		}


		//Database Processing Section

		$db = &JFactory::getDBO();

		// Check to see if a plugin by the same name is already installed
		$query = 'SELECT `id`' .
		' FROM `#__plugins`' .
		' WHERE folder = '.$db->Quote($pgroup) .
		' AND element = '.$db->Quote($pname);
		$db->setQuery($query);
		if (!$db->Query()) {
			// Install failed, roll back changes
			$this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.$db->stderr(true));
			return false;
		}
		$id = $db->loadResult();

		// Was there a plugin already installed with the same name?
		if ($id) {

			if (!$this->parent->getOverwrite())
			{
				// Install failed, roll back changes
				$this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('Plugin').' "'.$pname.'" '.JText::_('already exists!'));
				return false;
			}

		} else {
			$row =& JTable::getInstance('plugin');
			$row->name = JText::_(ucfirst($pgroup)).' - '.JText::_(ucfirst($pname));
			$row->ordering = $porder;
			$row->folder = $pgroup;
			$row->iscore = 0;
			$row->access = 0;
			$row->client_id = 0;
			$row->element = $pname;
			$row->published = 1;
			$row->params = '';

			if (!$row->store()) {
				// Install failed, roll back changes
				$this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.$db->stderr(true));
				return false;
			}
		}

		$status->plugins[] = array('name'=>$pname,'group'=>$pgroup);
	}
}

// BEGIN UPDATE PROCEDURE
$db = &JFactory::getDBO();

$db->setQuery("DESCRIBE `#__rsevents_events`"); 
$events_table = $db->loadObjectList();

foreach($events_table as $row)
{
	if(!($row->Field == 'IdParent'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `IdParent` TINYINT( 2 ) NOT NULL");
		$db->query();
	}
	
	if(!($row->Field == 'EventEnableRepeat'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventEnableRepeat` TINYINT( 2 ) NOT NULL");
		$db->query();
	}
	
	if(!($row->Field == 'EventRepeatType'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventRepeatType` TINYINT( 2 ) NOT NULL");
		$db->query();
	}
	
	if(!($row->Field == 'EventRepeatNumber'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventRepeatNumber` INT( 10 ) NOT NULL");
		$db->query();
	}
	
	if(!($row->Field == 'EventRepeatUntil'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventRepeatUntil` INT( 11 ) NOT NULL");
		$db->query();
	}
	
	if(!($row->Field == 'EventRepeatAlso'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventRepeatAlso` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventRegistrationForm'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventRegistrationForm` TINYINT( 2 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventArchive'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventArchive` TINYINT( 2 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventAutoApprove'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventAutoApprove` TINYINT( 2 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventPaypalEmail'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventPaypalEmail` VARCHAR( 255 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventIcon'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventIcon` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventReturnUrl'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventReturnUrl` VARCHAR( 255 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventKeywords'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventKeywords` VARCHAR( 255 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventMetaDescription'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventMetaDescription` VARCHAR( 255 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventAmPm1'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventAmPm1` INT( 1 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventAmPm2'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventAmPm2` INT( 1 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'EventCreation'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_events` ADD `EventCreation` INT( 15 ) NOT NULL");
		$db->query();
	}
}

$db->setQuery("DESCRIBE `#__rsevents_subscriptions`");
$subscriptions_table = $db->loadObjectList();

foreach($subscriptions_table as $row)
{
	if(!($row->Field == 'SubscriptionFormId'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_subscriptions` ADD `SubscriptionFormId` INT( 2 ) NOT NULL");
		$db->query();
	}
	if(!($row->Field == 'UserIp'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_subscriptions` ADD `UserIp` VARCHAR( 15 ) NOT NULL");
		$db->query();
	}
}

$db->setQuery("DESCRIBE `#__rsevents_tickets`");
$tickets_table = $db->loadObjectList();

foreach($tickets_table as $row)
{
	if(!($row->Field == 'TicketsPerUser'))
	{
		$db->setQuery("ALTER TABLE `#__rsevents_tickets` ADD `TicketsPerUser` INT( 5 ) NOT NULL");
		$db->query();
	}
}

//categories updates
$db->setQuery("SELECT IdEvent,IdCategory FROM #__rsevents_events");
$categories = $db->loadObjectList();

if(!empty($categories))
foreach($categories as $cat)
{
	$db->setQuery("INSERT INTO #__rsevents_events_cat SET IdEvent = ".$cat->IdEvent." , IdCategory =".$cat->IdCategory);
	$db->query();
}

// END UPDATE PROCEDURE
?>

	<div align="left" width="100%">
 		<img src="../administrator/components/com_rsevents/images/rs_events_logo.jpg" alt="RSEvents! Logo"/>
 	</div>
 	<br/>
	<table class="adminform">
		<tr>
			<td align="left">
			<strong>RSEvents 1.0.0 Component for Joomla!</strong><br/>
			&copy; 2007-2009 by <a href="http://www.rsjoomla.com" target="_blank">http://www.rsjoomla.com</a><br/>
			All rights reserved.
			<br/><br/>
			This Joomla! Component has been released under <a href="http://www.rsjoomla.com/terms-and-conditions.html" target="_blank">GPL</a>.<br/>
			<br/><br/>

			<b>Visit <a href="http://www.rsjoomla.com/" target="_blank">http://www.rsjoomla.com/</a> - for common issues, tech support, user manual, additional downloads and up to date knowledge base articles related to RSEvents!.</b><br/><br/>

			Thank you for using RSEvents!.
			<br/><br/>
			The rsjoomla.com team.
			<br/><br/>
			</td>
		</tr>
	</table><br/>
<br/><br/>
	<div align="left" width="100%"><b>RSEvents! 1.0.0 Installed</b></div>