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

defined( '_JEXEC' ) or die( 'Restricted access' );

function rsfl_get_license($id)
{
	$db =& JFactory::getDBO();
	$db->setQuery("SELECT LicenseName FROM #__rsfiles_licenses WHERE IdLicense =".$id);
	return $db->loadResult();
}


function RSFilesBuildRoute(&$query)
{
	$segments = array();
	
	if (!empty($query['view']))
		switch ($query['view'])
		{
			case 'files':
			{
				if(!isset($query['layout'])) $query['layout'] = 'default';
				
				switch($query['layout'])
				{
					default:
						$segments[] = 'files';						
					break;
					
					case 'timeperiod':
						$segments[] = 'time-period';
					break;
					
					case 'details':
						$segments[] = 'details';
					break;
					
					case 'download':
						$segments[] = 'download';
					break;
					
					case 'edit':
						$segments[] = 'edit';
					break;
					
					case 'view':
						$segments[] = 'view';
					break;
					
					case 'report':
						$segments[] = 'report';
					break;
					
					case 'bookmarks':
						$segments[] = 'bookmarks';
					break;
					
					case 'agreement':
						$segments[] = 'agreement';
						$segments[] = $query['cid'].'-'.rsfl_get_license($query['cid']);
					break;
					
					case 'search':
						$segments[] = 'search';
					break;
					
					case 'license':
						$segments[] = 'license';
						$segments[] =  $query['cid'].'-'.rsfl_get_license($query['cid']);
					break;
					
					case 'upload':
						$segments[] = 'upload';
					break;
					
					case 'newfolder':
						$segments[] = 'new-folder';
					break;
					
					case 'emaildownload':
						$segments[] = 'email-download';
					break;
					
				}
			}
			break;
		}
		
		
		if (!empty($query['task']))
		switch ($query['task'])
		{
			case 'removefile':
				$segments[] = 'delete-file';
			break;
			
			case 'bookmark':
				$segments[] = 'bookmark';
			break;
			
			case 'processbookmarks':
				$segments[] = 'process-bookmarks';
			break;
			
			case 'deletebookmark':
				$segments[] = 'delete-bookmark';
			break;
			
			case 'remove':
				$segments[] = 'delete-folder';
			break;
			
			case 'download':
				$segments[] = 'download-file';
			break;
			
			case 'save':
				$segments[] = 'save-file';
			break;
			
		}
	
	unset($query['task'] ,$query['cid'] , $query['controller'] ,$query['view'], $query['layout'],$query['tmpl']);
	
	return $segments;
}

function RSFilesParseRoute($segments)
{
	$query = array();
	
	$segments[0] = str_replace(':', '-', $segments[0]);
	
	switch ($segments[0])
	{		
		case 'files':
			$query['view'] = 'files';
		break;
		
		case 'time-period':
			$query['view'] = 'files';
			$query['layout'] = 'timeperiod';
		break;
		
		case 'details':
			$query['view'] = 'files';
			$query['layout'] = 'details';
		break;
		
		case 'download':
			$query['view'] = 'files';
			$query['layout'] = 'download';
		break;
		
		case 'edit':
			$query['view'] = 'files';
			$query['layout'] = 'edit';
		break;
		
		case 'view':
			$query['view'] = 'files';
			$query['layout'] = 'view';
		break;
		
		case 'report':
			$query['view'] = 'files';
			$query['layout'] = 'report';
		break;
		
		case 'agreement':
			$query['view'] = 'files';
			$query['layout'] = 'agreement';
			$query['cid'] = @intval($segments[1]);
			$query['tmpl'] = 'component';
		break;
		
		case 'search':
			$query['view'] = 'files';
			$query['layout'] = 'search';
		break;
		
		case 'license':
			$query['view'] = 'files';
			$query['layout'] = 'license';
			$query['cid'] = @intval($segments[1]);
		break;
		
		case 'upload':
			$query['view'] = 'files';
			$query['layout'] = 'upload';
		break;
		
		case 'email-download':
			$query['view'] = 'files';
			$query['layout'] = 'emaildownload';
			$query['tmpl'] = 'component';
		break;
		
		case 'new-folder':
			$query['view'] = 'files';
			$query['layout'] = 'newfolder';
			$query['tmpl'] = 'component';
		break;
		
		case 'delete-file':
			$query['task'] = 'removefile';
			$query['controller'] = 'files';
		break;
		
		case 'delete-folder':
			$query['task'] = 'remove';
			$query['controller'] = 'files';
		break;
		
		case 'download-file':
			$query['task'] = 'download';
			$query['controller'] = 'files';
		break;
		
		case 'save-file':
			$query['task'] = 'save';
			$query['controller'] = 'files';
		break;
		
		case 'bookmark':
			$query['task'] = 'bookmark';
			$query['controller'] = 'files';
		break;
		
		case 'process-bookmarks':
			$query['task'] = 'processbookmarks';
			$query['controller'] = 'files';
		break;
		
		case 'delete-bookmark':
			$query['task'] = 'deletebookmark';
			$query['controller'] = 'files';
		break;
		
		case 'bookmarks':
			$query['view'] = 'files';
			$query['layout'] = 'bookmarks';
		break;
		
	}
	
	return $query;
}
?>