<?php
/*
 * @package Sj JSJOB 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 ;

jimport('joomla.application.component.view');

class SJJSJOBFilterViewItemlist extends JViewLegacy
{

	function display($tpl = null)
	{
		$mainframe = JFactory::getApplication();
		if ($mainframe->isSite()) {
			$params = $mainframe->getParams('com_jsjobs');
		} else {
			$params = JComponentHelper::getParams('com_jsjobs');
		}
		
		$model = $this->getModel('itemlist');
		$limitstart = JRequest::getInt('limitstart');
		$view = JRequest::getWord('view');
		$task = JRequest::getWord('task');
		$db = JFactory::getDBO();

		// Set limit for model
		if (!$limit)
		$limit = 10;
		JRequest::setVar('limit', $limit);

		// Get items
		if (!isset($ordering))
		{
			$items = $model->getData();
		}
		else
		{
			$items = $model->getData($ordering);
		}

		if(count($items)==0){
			return JError::raiseNotice(500, JText::_('No results'));
		}
		// Pagination
		jimport('joomla.html.pagination');
		$total = count($items) ? $model->getTotal() : 0;
		$pagination = new JPagination($total, $limitstart, $limit);
		
		//Prepare items
		$user = JFactory::getUser();
		// $model = JModelLegacy::getInstance('item', 'K2Model');

		// for ($i = 0; $i < sizeof($items); $i++)
		// {
		// 	$items[$i] = $model->prepareItem($items[$i], $view, '');

		// 	if (!empty($items[$i]->created_by_alias))
		// 	{
		// 		$items[$i]->author = new stdClass;
		// 		$items[$i]->author->name = $items[$i]->created_by_alias;
		// 		$items[$i]->author->avatar = K2HelperUtilities::getAvatar('alias');
		// 		$items[$i]->author->link = JURI::root();
		// 	}
		// 	else
		// 	{
		// 		$author = JFactory::getUser($items[$i]->created_by);
		// 		$items[$i]->author = $author;
		// 		$items[$i]->author->link = JRoute::_(K2HelperRoute::getUserRoute($items[$i]->created_by));
		// 		$items[$i]->author->profile = $model->getUserProfile($items[$i]->created_by);
		// 		$items[$i]->author->avatar = K2HelperUtilities::getAvatar($author->id, $author->email, $params->get('userImageWidth'));
		// 	}

		// 	if (!isset($items[$i]->author->profile) || is_null($items[$i]->author->profile))
		// 	{

		// 		$items[$i]->author->profile = new JObject;
		// 		$items[$i]->author->profile->gender = NULL;

		// 	}
			
		// 	$items[$i]->votingPercentage = $model->getVotesPercentage($items[$i]->id);
		// 	$items[$i]->numOfvotes = $model->getVotesNum($items[$i]->id);
		// }

		// Set title
		$document = JFactory::getDocument();
		$application = JFactory::getApplication();
		$params->set('page_title', $title);
		
		$params->set('page_title_clean', $title);

		if ($mainframe->getCfg('sitename_pagetitles', 0) == 1)
		{
			$tmpTitle = JText::sprintf('JPAGETITLE', $mainframe->getCfg('sitename'), $params->get('page_title'));
			$params->set('page_title', $tmpTitle);
		}
		elseif ($mainframe->getCfg('sitename_pagetitles', 0) == 2)
		{
			$tmpTitle = JText::sprintf('JPAGETITLE', $params->get('page_title'), $mainframe->getCfg('sitename'));
			$params->set('page_title', $tmpTitle);
		}
		
		$document->setTitle($params->get('page_title'));

		$params->set('page_title', JText::sprintf('Search Results', count($items)));

		$this->assignRef('params', $params);
		$this->assignRef('pagination', $pagination);
		$this->assignRef('jobs', $items);

		// // Look for template files in component folders
		// $this->_addPath('template', JPATH_BASE.DS.'components'.DS.'com_jsjobs'.DS.'templates');
		// $this->_addPath('template', JPATH_BASE.DS.'components'.DS.'com_jsjobs'.DS.'templates'.DS.'default');

		// // Look for overrides in template folder (K2 template structure)
		// $this->_addPath('template', JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'com_jsjobs'.DS.'templates');
		// $this->_addPath('template', JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'com_jsjobs'.DS.'templates'.DS.'default');

		// // Look for overrides in template folder (Joomla! template structure)
		// $this->_addPath('template', JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'com_jsjobs'.DS.'default');
		// $this->_addPath('template', JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'com_jsjobs');

		// // Look for specific K2 theme files
		// if ($params->get('theme'))
		// {
		// 	$this->_addPath('template', JPATH_BASE.DS.'components'.DS.'com_jsjobs'.DS.'templates'.DS.$params->get('theme'));
		// 	$this->_addPath('template', JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'com_jsjobs'.DS.'templates'.DS.$params->get('theme'));
		// 	$this->_addPath('template', JPATH_SITE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'com_jsjobs'.DS.$params->get('theme'));
		// }

		$badchars = array('#', '>', '<', '\\');
		$searchword = JString::trim(JString::str_ireplace($badchars, '', JRequest::getString('searchword', null)));

		parent::display($tpl);
	}

}
