<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * Groups Controller
 *
 */
class rseventsControllerImport extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	 
	function __construct()
	{
		parent::__construct();
	}

	function edit()
	{
		JRequest::setVar( 'view', 'groups' );
		JRequest::setVar( 'layout', 'edit'  );
		parent::display();
	}
	
	function importeventlist()
	{
		$db = JFactory::getDBO();
		
		// Load the rsevents models 
		$event = $this->getModel('events');
		$categories = $this->getModel('categories');
		$locations = $this->getModel('locations');
		
		// Load all catgories from eventlist 
		
		$db->setQuery("SELECT id, catname , alias , catdescription ,published FROM #__eventlist_categories");
		
		$eventlist_cats = $db->loadObjectList();
		if (count($eventlist_cats) > 0) 
		{
			$catsmap = array();
			foreach ($eventlist_cats as $cat) 
			{
				$categories_row = &JTable::getInstance('Rsevents_Categories','Table');
				$oldcat = $cat->id;
				$cat->id = null;
				
				$categories_row->IdCategory = $cat->id;
				$categories_row->CategoryName = $cat->catname;
				$categories_row->CategoryAlias = $cat->alias;
				$categories_row->CategoryDescription = $cat->catdescription;
				$categories_row->published = $cat->published;
				$categories_row->ordering = 100;
				$categories_row->store();
				
				$catsmap[$oldcat] = $categories_row->IdCategory;
				
			}
		}
		
		// Load all venues from eventlist 
		
		$db->setQuery("SELECT id ,alias, venue , locdescription, url, street, city ,state,country ,plz,published FROM #__eventlist_venues");
		
		$eventlist_locations = $db->loadObjectList();
		if (count($eventlist_locations) > 0) 
		{
			$locationmap = array();
			foreach ($eventlist_locations as $key => $location) 
			{
				$locations_row = &JTable::getInstance('Rsevents_Locations','Table');
				
				$oldlocation = $location->id;
				$location->id = null;
				
				$locations_row->IdLocation = $location->id;
				$locations_row->LocationName = $location->venue;
				$locations_row->LocationAlias = $location->alias;
				$locations_row->LocationURL = $location->url;
				$locations_row->LocationCity = $location->city;
				$locations_row->LocationAddress = $location->street;
				$locations_row->LocationZip = $location->plz;
				$locations_row->LocationState = $location->state;
				$locations_row->LocationCountry = $location->country;
				$locations_row->LocationDescription = $location->locdescription;
				$locations_row->published = $location->published;
				$locations_row->ordering = 100;
				$locations_row->store();
				
				$locationmap[$oldlocation] = $locations_row->IdLocation;
				
			}
		}
		
		// Load all the events from eventlist 
		
		$db->setQuery("SELECT id , locid , catsid , dates , enddates , title , alias , datdescription , created_by FROM #__eventlist_events");
		
		$eventlist_events = $db->loadObjectList();
		foreach ($eventlist_events as $key => $event) 
		{
			$event_row = &JTable::getInstance('Rsevents_Events','Table');
			
			if (isset($locationmap[$event->locid])) $event_row->IdLocation = $locationmap[$event->locid];
			
			
			
			$event_row->IdDesign = 1;
			$event_row->IdUser = $event->created_by;
			$event_row->EventName = $event->title;
			$event_row->EventAlias = $event->alias;
			$event_row->EventDescription = $event->datdescription;
			$event_row->EventType = 1;
			$event_row->EventOverbooking = 0;
			$event_row->EventStartDate = strtotime($event->dates);
			$event_row->EventEndDate = strtotime($event->enddates);
			
			$newevent = $event_row->store();
			
			if (isset($catsmap[$event->catsid])) 
			{	
				$db->setQuery("INSERT INTO #__rsevents_events_cat SET IdEvent = ".$event_row->IdEvent." , IdCategory = ".$catsmap[$event->catsid]);
				$db->query();
			}
		}
		
		if (count(JError::getErrors()) == 0) {
			$this->setRedirect('index.php?option=com_rsevents&task=import', JText::_('RSE_EVENTLIST_SUCCESS') );
		}
		else $this->setRedirect('index.php?option=com_rsevents&task=import');
	}
	
	function importjcal()
	{
		$db = JFactory::getDBO();
		
		// Load the rsevents models 
		$event = $this->getModel('events');
		$categories = $this->getModel('categories');
		$locations = $this->getModel('locations');
		
		// Load all catgories from JCal Pro 
		
		$db->setQuery("SELECT cat_id, cat_name , description ,published FROM #__jcalpro2_categories");
		
		$jcalpro_cats = $db->loadObjectList();
		if (count($jcalpro_cats) > 0) 
		{
			$catsmap = array();
			foreach ($jcalpro_cats as $cat) 
			{
				$categories_row = &JTable::getInstance('Rsevents_Categories','Table');
				$oldcat = $cat->cat_id;
				$cat->cat_id = null;
				
				$categories_row->IdCategory = $cat->cat_id;
				$categories_row->CategoryName = $cat->cat_name;
				$categories_row->CategoryDescription = $cat->description;
				$categories_row->published = $cat->published;
				$categories_row->ordering = 100;
				$categories_row->store();
				
				$catsmap[$oldcat] = $categories_row->IdCategory;
				
			}
		}
		
		// Create a defaut location since JCal Pro has no location manager
		
		$db->setQuery("INSERT INTO #__rsevents_locations SET LocationName = 'default_jcal' , LocationAddress='default_jcal' , published = '1' , ordering='100' ");
		$db->query();
		$locationid = $db->insertId();
		
		
		// Load all the events from JCal Pro 
		
		$db->setQuery("SELECT title, owner_id, description, url, email, cat, private, start_date , end_date FROM #__jcalpro2_events");
		
		$jcalpro_events = $db->loadObjectList();
		foreach ($jcalpro_events as $key => $event) 
		{
			$event_row = &JTable::getInstance('Rsevents_Events','Table');
			
			if ($event->private==2) $event->private = 1;
			$event_row->IdLocation = $locationid;
			$event_row->IdDesign = 1;
			$event_row->IdUser = $event->owner_id;
			$event_row->EventName = $event->title;
			$event_row->EventDescription = $event->description;
			$event_row->EventType = $event->private;
			$event_row->EventURL = $event->url;
			$event_row->EventEmail = $event->email;
			$event_row->EventOverbooking = 0;
			$event_row->EventStartDate = strtotime($event->start_date);
			$event_row->EventEndDate = strtotime($event->end_date);
			
			$newevent = $event_row->store();
			
			if (isset($catsmap[$event->cat])) 
			{	
				$db->setQuery("INSERT INTO #__rsevents_events_cat SET IdEvent = ".$event_row->IdEvent." , IdCategory = ".$catsmap[$event->cat]);
				$db->query();
			}
			
		}
		
		if (count(JError::getErrors()) == 0) {
			$this->setRedirect('index.php?option=com_rsevents&task=import', JText::_('RSE_JCAL_SUCCESS') );
		}
		else $this->setRedirect('index.php?option=com_rsevents&task=import');
	}
	
	function importjevents()
	{
		$db = JFactory::getDBO();
		
		// Load the rsevents models 
		$event = $this->getModel('events');
		$categories = $this->getModel('categories');
		$locations = $this->getModel('locations');
		
		// Load all catgories from JEvents 
		
		$db->setQuery("SELECT jc.id , c.name , c.alias ,c.description ,c.published FROM #__jevents_categories jc LEFT JOIN #__categories c ON c.id=jc.id WHERE c.section= 'com_jevents'");
				
		$jevents_cats = $db->loadObjectList();
		
		if (count($jevents_cats) > 0) 
		{
			$catsmap = array();
			foreach ($jevents_cats as $cat) 
			{
				$categories_row = &JTable::getInstance('Rsevents_Categories','Table');
				$oldcat = $cat->id;
				$cat->id = null;
				
				$categories_row->IdCategory = $cat->id;
				$categories_row->CategoryName = $cat->name;
				$categories_row->CategoryAlias = $cat->alias;
				$categories_row->CategoryDescription = $cat->description;
				$categories_row->published = $cat->published;
				$categories_row->ordering = 100;
				$categories_row->store();
				
				$catsmap[$oldcat] = $categories_row->IdCategory;
				
			}
		}
		
		// Create the location 
		
		
		$db->setQuery("INSERT INTO #__rsevents_locations SET LocationName = 'default_jevents' , LocationAddress = 'default_jevents' , published = '1' , ordering='100'");
		$db->query();
		$locationid = $db->insertId();
		
		
		// Load all the events from JEvents
		
		$db->setQuery("SELECT e.catid, e.created_by, ed.dtstart, ed.dtend, ed.summary ,ed.description FROM #__jevents_vevent e LEFT JOIN #__jevents_vevdetail ed ON ed.evdet_id=e.detail_id");
		
		$jevents_events = $db->loadObjectList();
		foreach ($jevents_events as $key => $event) 
		{
			$event_row = &JTable::getInstance('Rsevents_Events','Table');
			
			$event_row->IdLocation = $locationid;
			$event_row->IdDesign = 1;
			$event_row->IdUser = $event->created_by;
			$event_row->EventName = $event->summary;
			$event_row->EventDescription = $event->description;
			$event_row->EventType = 1;
			$event_row->EventOverbooking = 0;
			$event_row->EventStartDate = $event->dtstart;
			$event_row->EventEndDate = $event->dtend;
			
			$newevent = $event_row->store();			
			if (isset($catsmap[$event->catid])) 
			{	
				$db->setQuery("INSERT INTO #__rsevents_events_cat SET IdEvent = ".$event_row->IdEvent." , IdCategory = ".$catsmap[$event->catid]);
				$db->query();
			}
		}
		
		if (count(JError::getErrors()) == 0) {
			$this->setRedirect('index.php?option=com_rsevents&task=import', JText::_('RSE_JEVENTS_SUCCESS') );
		}
		else $this->setRedirect('index.php?option=com_rsevents&task=import');
	
	}
	
}