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

defined('_JEXEC') or die;

if (!file_exists(JPATH_BASE.'/components/com_jsjobs/JSApplication.php')) {
	die('Please install component com_jsjobs');
}
require_once (JPATH_BASE.'/components/com_jsjobs/JSApplication.php');

include_once dirname(__FILE__) . '/helper_base.php';

abstract class JobsTabsHelper extends JobsTabsBaseHelper
{
	public static function getList($params)
	{
		$lists = array();
		return $lists;
	}

	public static function getJobsLocations($params) {
		if (empty($params->get('country_id'))) return false;

		$db = JFactory::getDBO();
		$query = "SELECT * FROM `#__js_job_cities` WHERE `enabled` = 1 AND `countryid` = ".(int)$params->get('country_id');
		$query.=" ORDER BY cityName";
		if ($params->get('limit_location', -1)==-1)
			$query.=" LIMIT 999999";
		else
			$query.=" LIMIT ".(int)$params->get('limit_location', 20);
			try {
				$db->setQuery($query);
				$rows = $db->loadObjectList();
			}
			catch (Exception $e){
				echo $e->getMessage();
			}

		return $rows;
	}

	public function getJobsCompanies($params) {
		$db = JFactory::getDBO();
		$query = "SELECT id,name,alias,CONCAT(alias,'-',id) AS aliasid FROM `#__js_job_companies` WHERE `status` = 1";
		if ($params->get('limit_company', -1)==-1)
			$query.=" LIMIT 999999";
		else
			$query.=" LIMIT ".(int)$params->get('limit_company', 40);
			try {
				$db->setQuery($query);
				$rows = $db->loadObjectList();
			}
			catch (Exception $e){
				echo $e->getMessage();
			}

		return $rows;
	}

	public function getJobsItems($params) {
		$db = JFactory::getDBO();
		$client_auth_key = JSModel::getJSModel('common')->getClientAuthenticationKey();
        if ($client_auth_key != '') {
        	$id = "job.serverid AS id";
            $alias = ",CONCAT(job.alias,'-',job.serverid) AS jobaliasid ";
        }
        else {
        	$id = "job.id AS id";
            $alias = ",CONCAT(job.alias,'-',job.id) AS jobaliasid ";
        }
 		$curdate = date('Y-m-d');
 		$query = "SELECT $id, job.title, job.jobcategory, job.created, cat.cat_title $alias, job.city
            FROM `#__js_job_jobs` AS job 
            JOIN `#__js_job_categories` AS cat ON job.jobcategory = cat.id 
            WHERE job.status = 1 AND DATE(job.startpublishing) <= " . $db->Quote($curdate) . " AND DATE(job.stoppublishing) >= " . $db->Quote($curdate) . "
            GROUP BY job.id
            ORDER BY job.hits DESC LIMIT ".(int)$params->get('limit_item', 40);
			try {
				$db->setQuery($query);
				$rows = $db->loadObjectList();
			}
			catch (Exception $e){
				echo $e->getMessage();
			}
		
		return $rows;
	}
}