<?php
/**
* @version 1.0.0
* @package RSFiles! 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 RsfilesControllerThemes extends rsfilesController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

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

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$db = JFactory::getDBO();
		$model = $this->getModel('themes');
		$post=JRequest::get('post',JREQUEST_ALLOWRAW);
		$row= & JTable::getInstance('Rsfiles_Themes','Table');
		
		if(!$row->bind($post)) {
			return JError::raiseWarning(500, $row->getError());
		}

		if ($row->store()) 
		{
			if(empty($post['IdTheme']))
			{
				$db->setQuery("INSERT INTO #__rsfiles_templates SET IdTheme = ".$row->IdTheme." , TemplateName = 'Files template' , TemplateLayout = '' ,Type = 1 ");
				$db->query();
				$db->setQuery("INSERT INTO #__rsfiles_templates SET IdTheme = ".$row->IdTheme." , TemplateName = 'Download template' , TemplateLayout = '' ,Type = 2 ");
				$db->query();
				$db->setQuery("INSERT INTO #__rsfiles_templates SET IdTheme = ".$row->IdTheme." , TemplateName = 'Details template' , TemplateLayout = '' ,Type = 3 ");
				$db->query();
			}
			
			$msg = JText::_('RSFL_THEME_SAVED' );
		} else {
			JError::raiseWarning(500, $row->getError());

			$msg = JText::_('RSFL_THEME_ERROR_SAVING' );
		}
		
		switch(JRequest::getCmd('task'))
		{
			case 'apply' :
					$link = 'index.php?option=com_rsfiles&task=edittheme&cid='.$row->IdTheme;

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

			break;
		}
		// redirect after saving
		$this->setRedirect($link, $msg);
	}
	
	function savetemplate()
	{
		$model = $this->getModel('themes');
		$post=JRequest::get('post',JREQUEST_ALLOWRAW);
		$row= & JTable::getInstance('Rsfiles_Templates','Table');
		
		if(!$row->bind($post)) {
			return JError::raiseWarning(500, $row->getError());
		}
		
		if ($row->store()) 
			$msg = JText::_('RSFL_TEMPLATE_SAVED' );
		else 
		{
			JError::raiseWarning(500, $row->getError());
			$msg = JText::_('RSFL_TEMPLATE_ERROR_SAVING' );
		}
		
		switch(JRequest::getCmd('task'))
		{
			case 'applytemplate' :
					$link = 'index.php?option=com_rsfiles&task=edittemplate&cid='.$row->IdTemplate.'&IdTheme='.$row->IdTheme;

			break;
			
			case 'savetemplate' :
					$link = 'index.php?option=com_rsfiles&task=edittheme&cid='.$row->IdTheme;

			break;
		}
		// redirect after saving
		$this->setRedirect($link, $msg);
		
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$db = JFactory::getDBO();
		$model = $this->getModel('themes');
		$row =& JTable::getInstance('Rsfiles_Themes','Table');
		$cids=JRequest::getVar('cid',array(0),'post','array');
		
		$db->setQuery("SELECT IdTheme FROM #__rsfiles_themes WHERE `default` = 1 ");
		$defaultId = $db->loadResult();
		
		if(!empty($cids))
		{
			foreach($cids as $cid) 
			{
				if($cid == $defaultId) continue;
				if($cid == 1) continue;
				if(!$row->delete($cid)) {
					$msg = JText::_('RSFL_THEME_ERROR_DELETE' );
				} else {
					$db->setQuery("DELETE FROM #__rsfiles_templates WHERE IdTheme = ".$cid);
					$db->query();
					$msg = JText::_( 'RSFL_THEME_DELETED' );
				}
			}
		} else
		{
			$msg = JText::_('RSFL_SELECT_THEME' );
		}
		$this->setRedirect( 'index.php?option=com_rsfiles&task=themes', $msg ); 
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$this->setRedirect( 'index.php?option=com_rsfiles&task=themes', JText::_('RSFL_CANCEL' ) );
	}
	
	function canceltemplate()
	{
		$IdTheme = JRequest::getVar('IdTheme');
		$this->setRedirect( 'index.php?option=com_rsfiles&task=edittheme&cid='.$IdTheme, JText::_('RSFL_CANCEL' ) );
	}
	
	function publish()
	{
		global $mainframe;

		$db 	=& JFactory::getDBO();
		$msg ='';
		$cid		= JRequest::getVar( 'cid', array(), '', 'array' );
		$publish	= ( $this->getTask() == 'publish' ? 1 : 0 );

		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1)
		{
			$action = $publish ? 'publish' : 'unpublish';
			JError::raiseError(500, JText::_('RSFL_THEME_ERROR_PUBLISH'.' '.$action, true ) );
		}

		$db->setQuery("SELECT IdTheme FROM #__rsfiles_themes WHERE `default`=1");
		$defaultId = $db->loadResult();
		foreach($cid as $i => $idTheme)
		if($defaultId == $idTheme) 
		{
			unset($cid[$i]);
			$msg=JText::_('RSFL_THEME_UNPUBLISH_ERROR');
		}
		
		if(count($cid)> 0)
		{
			$cids = implode( ',', $cid );
			
			$query = 'UPDATE #__rsfiles_themes'
			. ' SET published = ' . (int) $publish
			. ' WHERE IdTheme IN ( '. $cids .' )'
			;
			$db->setQuery( $query );
			if (!$db->query())
			{
				JError::raiseError(500, $db->getErrorMsg() );
			}
		}
		
		$mainframe->redirect( 'index.php?option=com_rsfiles&task=themes', $msg );
	}

	function makedefault()
	{

		$db 	=& JFactory::getDBO();
		$cid		= JRequest::getVar( 'cid', array(), '', 'array' );
		if(is_array($cid)) $cid = $cid[0];
		
		$db->setQuery("UPDATE #__rsfiles_themes SET `default` = 0 ");
		$db->query();
		$db->setQuery("UPDATE #__rsfiles_themes SET published = 1 WHERE IdTheme =".$cid);
		$db->query();
		$db->setQuery("UPDATE #__rsfiles_themes SET `default` = 1 WHERE IdTheme =".$cid);
		$db->query();
		
		$this->setRedirect( 'index.php?option=com_rsfiles&task=themes' );
	}
	
}