<?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' );


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

		// Register Extra tasks
		$this->registerTask( 'apply' , 'save');
	}


	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$db = &JFactory::getDBO();
		$model = $this->getModel('events');
		$post=JRequest::get('post',JREQUEST_ALLOWRAW);
		$cid = intval(JRequest::getVar('cid','0','request'));
		
		$row= & JTable::getInstance('Rsevents_Events','Table');
		$row->load($cid);
		$parentEvent = $row;
		
		
		if(!$row->bind($post)) {
			return JError::raiseWarning(500, $row->getError());
		}
		
		$hour = date('H',$row->EventStartDate);
		$min = date('i',$row->EventStartDate);
		
		$alsoRepeat = array();
		if(isset($post['EventRepeatAlso']) && is_array($post['EventRepeatAlso']))
			foreach($post['EventRepeatAlso'] as $date)
			{
				$datetounix = strtotime($date);
				$date = $datetounix	+ ((($hour * 60) + $min) * 60);
				$alsoRepeat[] = $date;
			}
		
		
		$row->EventRepeatUntil = strtotime($row->EventRepeatUntil);
		$row->EventRepeatAlso = implode(',',$alsoRepeat);
		
		
		$row->store();
				
		//repeating the event
		
		//set the parent of the repeating events
		$parent = $parentEvent->IdEvent; 
		
		
		$db->setQuery("SELECT TicketName,TicketPrice,TicketMaxAudience,TicketDescription,ordering FROM #__rsevents_tickets WHERE IdEvent='".$parent."'");
		$tickets = $db->loadObjectList();
		
		
		if($row->EventEnableRepeat == 1)
		{
		
			$db->setQuery("SELECT IdEvent FROM #__rsevents_events WHERE IdParent='".$parent."'");
			$eventIds = $db->loadResultArray();
			$eventIds = implode(',',$eventIds);
			
			$db->setQuery("DELETE FROM #__rsevents_tickets WHERE IdEvent IN (".$eventIds.")");
			$db->query();
		
			$db->setQuery("DELETE FROM #__rsevents_events WHERE IdParent='".$parent."'");
			$db->query();
			
			$db->setQuery("DELETE FROM #__rsevents_events_cat WHERE IdEvent IN (".$eventIds.")");
			$db->query();
			
			$repeatNumber = $row->EventRepeatNumber;
			$startdate = $row->EventStartDate;
			$startdates = array();
			
			//switch on the type of the repetition
			switch($row->EventRepeatType)
			{
				//Days
				case 1:
				
				while ($startdate<=$row->EventRepeatUntil)
				{
					$startdate = strtotime('+'.$repeatNumber.' days',  $startdate);
					if($startdate > $row->EventRepeatUntil) break;
					$startdates[] = $startdate;
					
				}
				//get dates with $startdates
				
				break;
				
				//Weeks
				case 2:
					
					while ($startdate<=$row->EventRepeatUntil)
					{
						$startdate = strtotime('+'.$repeatNumber.' weeks',  $startdate);
						if($startdate > $row->EventRepeatUntil) break;
						$startdates[] = $startdate;
						
					}
					//get dates with $startdates
					
				break;
				
				//Months
				case 3:
				
					while ($startdate<=$row->EventRepeatUntil)
					{
						$startdate = strtotime('+'.$repeatNumber.' months',  $startdate);
						if($startdate > $row->EventRepeatUntil) break;
						$startdates[] = $startdate;
						
					}
					//get dates with $startdates 
					
				break;
				
				//Years
				case 4:
				
					while ($startdate<=$row->EventRepeatUntil)
					{
						$startdate = strtotime('+'.$repeatNumber.' year',  $startdate);
						if($startdate > $row->EventRepeatUntil) break;
						$startdates[] = $startdate;
						
					}
					//get dates with $startdates 
					
				break;
			}	
			
			
			$startdates = array_merge($startdates,$alsoRepeat);
			
			$eventDuration = $parentEvent->EventEndDate - $parentEvent->EventStartDate; 
			
			foreach($startdates as $date) 
			{ 
				$newEvent = $parentEvent;
				$newEvent->EventStartDate = $date; 
				$newEvent->EventEndDate = $date + $eventDuration; 
				$newEvent->IdParent = $parent;
				$newEvent->IdEvent = null;
				if($newEvent->store()) 
					$msg=JText::_('RSE_REPEAT_SAVE'); 
				else 
					$msg =JText::_('RSE_REPEAT_ERROR'); 
				
				
				$db->setQuery("SELECT IdCategory FROM #__rsevents_events_cat WHERE IdEvent = ".$cid);
				$catids = $db->loadResultArray();
				
				foreach($catids as $catid)
				{
					$db->setQuery("INSERT INTO #__rsevents_events_cat SET IdEvent = ".$newEvent->IdEvent." , IdCategory = ".$catid);
					$db->query();
				}
					
				foreach($tickets as $ticket)
				{
					$db->setQuery("INSERT INTO #__rsevents_tickets SET IdEvent='".$newEvent->IdEvent."' , TicketName='".$db->getEscaped($ticket->TicketName)."' , TicketPrice = '".$ticket->TicketPrice."' , TicketMaxAudience='".$ticket->TicketMaxAudience."' , TicketDescription='".$db->getEscaped($ticket->TicketDescription)."' , ordering='".$ticket->ordering."'");
					$db->query();
				} 
			}
		}
		
		switch(JRequest::getCmd('task'))
		{
			case 'apply' :
					$link = 'index.php?option=com_rsevents&task=eventrepeat&cid='.$parent;

			break;
			
			case 'save' :
					$link = 'index.php?option=com_rsevents&task=listevents';

			break;
		}
		
		$this->setRedirect($link, $msg);
	}
	
	function cancel()
	{
		$msg = JText::_('RSE_CANCEL_OPERATION' );
		$this->setRedirect( 'index.php?option=com_rsevents&task=listevents', $msg );
	}
}