<?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 rseventsControllerSettings extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
		$this->registerTask( 'apply' , 'save');

	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		global $mainframe;
		$model = $this->getModel('settings');
		$db = & JFactory::getDBO();
		$post = JRequest::get('post',JREQUEST_ALLOWRAW);
		$rseventsConfigPost = $post['rseventsConfig'];
		if (strpos($rseventsConfigPost['layouts.eventintro'],'{EventName}') !== false && strpos($rseventsConfigPost['layouts.eventintro'],'{EventLink}') !== false )
		{
			if(($rseventsConfigPost['locations.gmaps'] == 1 && $rseventsConfigPost['locations.gMaps.key'] != '') || ($rseventsConfigPost['locations.gmaps'] == 0))
			{
				$db->setQuery("SELECT * FROM `#__rsevents_config`");
				$rseventsConfigDb = $db->loadObjectList();
				foreach ($rseventsConfigDb as $objConfig)
				{  
					if(isset($rseventsConfigPost[$objConfig->ConfigName]))
					{
						
						$db->setQuery("UPDATE #__rsevents_config SET ConfigValue='".$db->getEscaped($rseventsConfigPost[$objConfig->ConfigName])."' WHERE ConfigName='".$objConfig->ConfigName."'");
						$db->query();
						$rseventsConfig[$objConfig->ConfigName] = $rseventsConfigPost[$objConfig->ConfigName];
					}
				} 
				$mainframe->setUserState('rseventsConfig',$rseventsConfig);
				$msg = JText::_('RSE_SETTINGS_SAVE');
			} else $msg = JText::_('RSE_GOOGLE_KEY_ERROR');
		} else $msg = JText::_('RSE_PLACEHOLDER_MISSING');
		
		switch(JRequest::getCmd('task'))
		{
			case 'apply' :
					$link = 'index.php?option=com_rsevents&task=editsettings';

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

			break;
		}
		$this->setRedirect($link, $msg);
	}
	
	//saving the Google center 
	
	function saveCenter()
	{
	
		global $mainframe;
		$model = $this->getModel('settings');
		$db = & JFactory::getDBO();
		$post = JRequest::get('post');
		$rseventsConfigPost = $post['rseventsConfig'];
			if(($rseventsConfigPost['locations.gmaps'] == 1 && $rseventsConfigPost['locations.gMaps.key'] != '') || ($rseventsConfigPost['locations.gmaps'] == 0))
			{
				$db->setQuery("SELECT * FROM `#__rsevents_config`");
				$rseventsConfigDb = $db->loadObjectList();
				foreach ($rseventsConfigDb as $objConfig)
				{  
					if(isset($rseventsConfigPost[$objConfig->ConfigName]))
					{
						
						$db->setQuery("UPDATE #__rsevents_config SET ConfigValue='".$rseventsConfigPost[$objConfig->ConfigName]."' WHERE ConfigName='".$objConfig->ConfigName."'");
						$db->query();
						$rseventsConfig[$objConfig->ConfigName] = $rseventsConfigPost[$objConfig->ConfigName];
					}
				} 
				$mainframe->setUserState('rseventsConfig',$rseventsConfig);
				$msg = JText::_('RSE_SETTINGS_SAVE');
			} else $msg = JText::_('RSE_GOOGLE_KEY_ERROR');
		$link = 'index.php?option=com_rsevents&task=editsettings';
		$mainframe->redirect($link, $msg);
		
	}
	
	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_('RSE_CANCEL_OPERATION' );
		$this->setRedirect( 'index.php?option=com_rsevents', $msg );
	}
	
}