<?php
/*
 * @package Sj Js Job Filter
 * @version 1.0.0
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @copyright (c) 2016 YouTech Company. All Rights Reserved.
 * @author YouTech Company http://www.smartaddons.com
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
jimport('joomla.application.component.controller');
jimport( 'joomla.environment.request' );

class SJJsJobFilterControllerJob extends JControllerLegacy{

    function display($cachable = false, $urlparams = false) {
    	error_reporting(E_ALL ^ E_NOTICE);
	
        JFactory::getApplication()->input->set('task', 'search');
        JFactory::getApplication()->input->set('view', 'job');
		
        $model = $this->getModel('Job','SJJsJobFilterModel');        
        $modelitems = new SJJsJobFilterModelJob();       
        $modelitems->getData();
		
        $document = JFactory::getDocument();        
        $viewType = $document->getType();      
        $view = $this->getView('job', $viewType,'SJJsJobFilterView');      
        $view->setModel($model);        
        $view->setModel($modelitems);        
        $user = JFactory::getUser();        
        $cache = false;        
        parent::display($cache);
    }

	function search(){		
		$url = $this->buildUrlOfResultsPage();
		$this->setRedirect(JRoute::_($url, false));
	}
	
	function shareurl(){
		$url = $this->buildUrlOfResultsPage();
		echo JURI::base().substr(JRoute::_($url, false), strlen(JURI::base(true)) + 1);
		exit();
	}
	
	function buildUrlOfResultsPage(){
		$post   = JFactory::getApplication()->input->getArray($_POST);	
	
		$app	= JFactory::getApplication();
		$menu	= $app->getMenu();
		$items	= $menu->getItems('link', 'index.php?option=com_sjjsjobfilter&view=search');

		if(isset($items[0])) {
			$post['Itemid'] = $items[0]->id;
		} elseif (JFactory::getApplication()->input->getInt('Itemid') > 0) {
			$post['Itemid'] = JFactory::getApplication()->input->getInt('Itemid');
		}
		

		
		unset($post['task']);
		unset($post['btnSubmit']);
		$uri = JURI::getInstance();
		//$uri->setQuery($post);
		$uri->setVar('option', 'com_sjjsjobfilter');
		$uri->setVar('view', 'job');

		foreach($post AS $key=>$value) {
			if(!empty($value)) {
				$uri->setVar($key, $value);
			}
		}
		$url = 'index.php'.$uri->toString(array('query', 'fragment'));
		$url = str_replace(array('<', '>'), array('',''), $url);

		return $url;
	}
}
