<?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
*/

defined( '_JEXEC' ) or die( 'Restricted access' ); 
jimport( 'joomla.application.component.view');

require_once(JPATH_SITE.DS.'components'.DS.'com_rsevents'.DS.'models'.DS.'events.php');

class rseventsViewCalendar extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$document = & JFactory::getDocument();
		$document->addStyleSheet(JURI::root(true).'/components/com_rsevents/style.css');
		
		$calendar = new RSEventsCalendar;
		$month = JRequest::getVar('month', '0');
		$year = JRequest::getVar('year', '0');
		$calendar->setDate($month, $year);

		$params = &$mainframe->getParams();
		$order = $params->get('order','DESC');
		
		$this->assignRef('calendar', $calendar);
		
		$layout = JRequest::getVar('layout','','request');
		$rseventsConfig = $mainframe->getUserState('rseventsConfig');
		$this->assignRef('rseventsConfig',$rseventsConfig);
	
		switch($layout)
		{
			case 'day':
				$date = strtotime(JRequest::getVar('date','','request'));
				$eventsList = rseventsModelEvents::getEventDays($date,0,$order);
				$Itemid = intval(JRequest::getVar('Itemid','','request'));
				$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
				$layouts = array();
				
				foreach($eventsList as $i => $objList)
				{
					$categoryname = array();
					$layout = $rseventsConfig['layouts.eventintro'];
					
					// event link
					$eventLink = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$objList->IdEvent.':'.JFilterOutput::stringURLSafe($objList->EventName).$itemId));
					//categories
					$db->setQuery("SELECT c.CategoryName , c.IdCategory FROM #__rsevents_categories c LEFT JOIN #__rsevents_events_cat ec ON ec.IdCategory = c.IdCategory WHERE ec.IdEvent =".$objList->IdEvent);
					$categs = $db->loadObjectList();
					foreach($categs as $categ)
						$categoryname[] = '<a href="'.html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=listcategoryevents&cid='.$categ->IdCategory.':'.JFilterOutput::stringURLSafe($categ->CategoryName).$itemId)).'">'.$categ->CategoryName.'</a>';
					
					$categoryname = implode(',',$categoryname);
					
					
					//location link
					$locationLink = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=locations&layout=show&cid='.$objList->IdLocation.'&IdEvent='.$objList->IdEvent.$itemId));
					

					//get the icon of the event
					$small_icon = str_replace('.jpg','_'.$rseventsConfig['event.icon.small'].'.jpg',$objList->EventIcon);
					if($rseventsConfig['enable.event.icon'] && $objList->EventIcon != '')
						$eventIcon = '<div class="rsevents_icon"><a href="'.$eventLink.'"><img src="'.JURI::root().'components/com_rsevents/images/thumbs/'.$small_icon.'" /></a></div>';
					else 
						$eventIcon = '';
					
					
					$bad  = array('{EventLink}','{CategoryName}','{LocationLink}','{EventIcon}');
					$good = array($eventLink,$categoryname,$locationLink,$eventIcon);
					
					$layout = str_replace($bad,$good,$layout);
					$properties = array();
					$values = array();
					$objList->EventStartDate = rseventsHelper::translateDate(date($rseventsConfig['global.dateformat'],$objList->EventStartDate));
					$objList->EventEndDate = rseventsHelper::translateDate(date($rseventsConfig['global.dateformat'],$objList->EventEndDate));
					$objList->EventDescription = rseventsHelper::strip_html_tags(rseventsModelEvents::shorten($objList->EventDescription));
					
					foreach($objList as $property => $value)
					{
						$properties[] = '{'.$property.'}';
						$values[] = $value;
					}

					$layouts[] = str_replace($properties, $values, $layout);
					
					$this->assignRef('layouts',$layouts);
					
				}
			break;
			
			case 'week':
				$date = JRequest::getVar('date','','request');
				$startweek = strtotime($date);
				$endweek = $startweek+(6*86400);
				$eventsList = rseventsModelEvents::getEventDays($startweek,$endweek,$order);
				$Itemid = intval(JRequest::getVar('Itemid','','request'));
				$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
				$layouts = array();
				foreach($eventsList as $i => $objList)
				{
					$categoryname = array();
					
					$layout = $rseventsConfig['layouts.eventintro'];
					// event link
					$eventLink = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$objList->IdEvent.':'.JFilterOutput::stringURLSafe($objList->EventName).$itemId));
					//categories
					$db->setQuery("SELECT c.CategoryName , c.IdCategory FROM #__rsevents_categories c LEFT JOIN #__rsevents_events_cat ec ON ec.IdCategory = c.IdCategory WHERE ec.IdEvent =".$objList->IdEvent);
					$categs = $db->loadObjectList();
					foreach($categs as $categ)
						$categoryname[] = '<a href="'.html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=listcategoryevents&cid='.$categ->IdCategory.':'.JFilterOutput::stringURLSafe($categ->CategoryName).$itemId)).'">'.$categ->CategoryName.'</a>';
					
					$categoryname = implode(',',$categoryname);
					
					//location link
					$locationLink = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=locations&layout=show&cid='.$objList->IdLocation.'&IdEvent='.$objList->IdEvent.$itemId));
					

					//get the icon of the event
					$small_icon = str_replace('.jpg','_'.$rseventsConfig['event.icon.small'].'.jpg',$objList->EventIcon);
					if($rseventsConfig['enable.event.icon'] && $objList->EventIcon != '')
						$eventIcon = '<div class="rsevents_icon"><a href="'.$eventLink.'"><img src="'.JURI::root().'components/com_rsevents/images/thumbs/'.$small_icon.'" /></a></div>';
					else 
						$eventIcon = '';
					
					
					$bad  = array('{EventLink}','{CategoryName}','{LocationLink}','{EventIcon}');
					$good = array($eventLink,$categoryname,$locationLink,$eventIcon);
					
					$layout = str_replace($bad,$good,$layout);
					$properties = array();
					$values = array();
					$objList->EventStartDate = rseventsHelper::translateDate(date($rseventsConfig['global.dateformat'],$objList->EventStartDate));
					$objList->EventEndDate = rseventsHelper::translateDate(date($rseventsConfig['global.dateformat'],$objList->EventEndDate));
					$objList->EventDescription = rseventsHelper::strip_html_tags(rseventsModelEvents::shorten($objList->EventDescription));
					
					foreach($objList as $property => $value)
					{
						$properties[] = '{'.$property.'}';
						$values[] = $value;
					}

					$layouts[] = str_replace($properties, $values, $layout);
					
					$this->assignRef('layouts',$layouts);
					
				}
				
			break;
		}
		
		
		parent::display($tpl);
	}
	
	
}