<?php

/**
 * @Copyright Copyright (C) 2009-2010 Ahmad Bilal
 * @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
 * Company:     Buruj Solutions
 + Contact:     https://www.burujsolutions.com , info@burujsolutions.com
 * Created on:  Nov 22, 2010
 ^
 + Project:     JS Jobs
 ^ 
 */
 
defined('_JEXEC') or die('Restricted access');

class jobslist {

    private $fields;

    function __construct() {
        $this->fields = JSModel::getJSModel('fieldsordering')->getFieldsOrderingByFieldFor(2);
    }
    function printjobs(&$jobs, &$config, &$fieldsordering ,&$Itemid){
		if(empty($jobs)){
			return false;
		}
        $user = JFactory::getUser(); 
        $role = JSModel::getJSModel('userrole')->getUserRole($user->id);       
        $showgoogleadds = $config['googleadsenseshowinlistjobs'];
        $afterjobs = $config['googleadsenseshowafter'];
        $googleclient = $config['googleadsenseclient'];
        $googleslot = $config['googleadsenseslot'];
        $googleaddhieght = $config['googleadsenseheight'];
        $googleaddwidth = $config['googleadsensewidth'];
        $googleaddcss = $config['googleadsensecustomcss'];

        $html = "";
        $date_now = date('Y-m-d H:i:s');
        $noofjobs = 1;
        $common = JSModel::getJSModel('common');
		$index = 0;
		
        foreach ($jobs as $job) { 
			if($index%2 == 0){
                $cls_sj = ' sj_plus';
            }else{
                $cls_sj = ' sj_minus';
            } 
            $index++;  
		
            $companyaliasid = JSModel::getJSModel('common')->removeSpecialCharacter($job->companyid);
            $link_viewcomp = "index.php?option=com_jsjobs&c=company&view=company&layout=view_company&nav=31&cd=" . $companyaliasid . "&Itemid=" . $Itemid;
            $jobaliasid = JSModel::getJSModel('common')->removeSpecialCharacter($job->jobaliasid);
            $link_viewjob = "index.php?option=com_jsjobs&c=job&view=job&layout=view_job&bd=".$jobaliasid."&Itemid=".$Itemid;
            $path = $common->getCompanyLogo($job->companyid, $job->logofilename , $config);
            $expiredate = date('Y-m-d H:i:s', strtotime($job->created.'+ '.$config['newdays'].' days'));
            if($config['labelinlisting'] == 1){
                $hourago = "";
            }else{
                $hourago = "";
            }
            $startTimeStamp = strtotime($job->created);
            $endTimeStamp = strtotime("now");
            $timeDiff = abs($endTimeStamp - $startTimeStamp);
            $numberDays = $timeDiff / 86400;  // 86400 seconds in one day
            // and you might want to convert to integer
            $numberDays = intval($numberDays);
            if ($numberDays != 0 && $numberDays == 1) {
                $day_text = JText::_('Day');
            } elseif ($numberDays > 1) {
                $day_text = JText::_('Days');
            } elseif ($numberDays == 0) {
                $day_text = JText::_('Today');
            }
            if ($numberDays == 0) {
                $hourago .= $day_text;
            } else {
                $hourago .= $numberDays.' '.$day_text.' '.JText::_('Ago');
            }

            $html .='<div class="js-jobs-wrapper'.$cls_sj.'">';
            $html .=    '<div class="js-toprow">';
				$html .=    '<div class="row">';
					$html .=    '<div class="col-sm-2 text-center">';
						$html .=    '<div class="jsimage">';
						$_field = $this->getFieldStatus('company');
						if($_field[0] == 1 ){
							$html .=   '<a href="'.$link_viewcomp.'"><img src="'.$path.'" title="'.$job->companyname.'"></a>';
						}
						$html .=    '</div>';            
					$html .=    '</div>';
					
					$html .=    '<div class="col-sm-10">';
						$html .=    '<h2 class="heading p-t-1">';
						$_field = $this->getFieldStatus('jobcategory');
						if($_field[0] == 1 ){
							if($config['labelinlisting'] == 1){
							//$html .='<span class="js-bold">' .JText::_($_field[1]).': </span>';
							}
							$html .=JText::_($job->cat_title);
						}
						$html .=    '</h2>';
						$html .=    '<h3 class="heading p-t-0"><a class="jobtitle" href="'.$link_viewjob.'">'.$job->title.'</a>';
						if($date_now < $expiredate){
							$html .=    '<span class="js-status bg-new">'.JText::_("New").'</span>';
						}
							$html .=    '</span>';
						$html .=    '</h3>';
						$html .=    '<ul>';
						$html .=    '<li><i class="fa fa-map-marker"></i>';
						$_field = $this->getFieldStatus('city');
						if($_field[0] == 1 ){
							$html .= $job->location;
						}
						$html .=    '</li>';
						$html .=    '<li><i class="fa fa-clock-o"></i>';
						$_field = $this->getFieldStatus('jobtype');
						if($_field[0] == 1 ){
							$html .=  '<span class="js-type">'.JText::_($job->jobtypetitle).'</span>';
						}
						$html .=    '</li>';
						$html .=    '<li><i class="fa fa-calendar"></i> '.date('Y-m-d', strtotime($job->created)).'</li>';
						$html .=    '<li><i class="fa fa-money"></i>';
						if($_field[0] == 1 ){
							if($config['labelinlisting'] == 1){
							//$html .='<span class="js-bold">' .JText::_($_field[1]).': </span>';
							}
							$html .=$job->salary;
						}
						$html .=    '</li>';
						$html .=    '</ul>';
						if($config['showapplybutton']==1){
							if($job->jobapplylink == 1 && !empty($job->joblink)){
								if(!strstr($job->joblink ,'http')){
									$job->joblink = 'http://'.$job->joblink;
								}
								$target = 'target="_blank"';
								if (filter_var($job->joblink, FILTER_VALIDATE_URL) === false) {
									$job->joblink = '#';
									$target = '';
								}
								$html .= '<a class="js-btn-apply" href="'.$job->joblink.'" '.$target.' >'.JText::_('Apply Now').'</a>';
							}elseif(!empty($config['applybuttonredirecturl'])){
								$html .= '<a class="js-btn-apply" href="'.$config['applybuttonredirecturl'].'" target="_blank">'.JText::_('Apply Now').'</a>';
							}else{
								$html .='<a class="js-btn-apply" href="#" onclick="getApplyNowByJobid('.$job->jobid.');">'.JText::_('Apply Now','js-jobs').'</a>';
							}
						}
					$html .=   '</div>
					</div>
                </div>
            </div>'; // END Main ROW WRAPPER

                if ($showgoogleadds == 1) {
                    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 

                    if ($noofjobs % $afterjobs == 0) {
                        $html .= '<table cellpadding="0" cellspacing="0" border="0" width="100%" style="'.$googleaddcss.'">
                                        <tr>
                                            <td>
                                                <script type="text/javascript">
                                                    google_ad_client = "' . $googleclient . '";
                                                    google_ad_slot = "' . $googleslot . '";
                                                    google_ad_width = "' . $googleaddwidth . '";
                                                    google_ad_height = "' . $googleaddhieght . '";
                                                </script>
                                                <script type="text/javascript"src="'.$protocol.'pagead2.googlesyndication.com/pagead/show_ads.js"></script>
                                            </td>
                                        </tr>
                                    </table>';
                    } $noofjobs++;
                }

        }

        $nextpage = JFactory::getApplication()->input->get('pagenum', 0);
        $nextpage += 1;
        $html .= '<a class="scrolltask" data-scrolltask="getNextJobs" data-offset="'.$nextpage.'"></a>';
        return $html;
    }

    function getFieldStatus($field){
        $return[0] = false;
        $return[1] = '';
        foreach($this->fields AS $f){
            if($f->field == $field){
                $return[0] = $f->showonlisting;
                $return[1] = $f->fieldtitle;
                break;
            }
        }
        return $return;
    }
}
?>
