<?php

if(!@include("./misc/fileLoc.inc.php")){
	if(!@include("./admin/misc/fileLoc.inc.php")){
		if(!@include("../admin/misc/fileLoc.inc.php")){
			if(!@include("../../admin/misc/fileLoc.inc.php")){
				print("MISSING REQUIRED CONFIGURATION INFORMATION!<!--\n\n\nMissing fileLoc.inc.php\n\n\n-->");
				exit();
			}
		}
	}
}

include_once($mods."/config.inc.php");
include_once($mods."/serverAccess.inc.php");
include_once($classes."/JPContentBuilder.class.php");
include_once($classes."/JPJob.class.php");
include_once($mods."/globals.inc.php");

// New config item!
$configOptions['JobPostingMiniJobListTemplate'] = $configOptions['JobPostingTemplateLocation'] . '/miniJobsPlugin.html';



// Receive the language code from the compiled page.
if(!isset($LANGCODE) || $LANGCODE == '') { $LANGCODE = "EN"; }

$db = connectDB();
$MODCODE = "JOB_POSTINGS";
$gVars = globalVars($MODCODE);



//Instantiate the builder
$builder = new JPContentBuilder();

if(!isset($jobsSortBy)) { $jobsSortBy = "Title"; }
if(!isset($jobsOrder)) { $jobsOrder = "ASC"; }
if(!isset($jobsLimit)) { $jobsLimit = 10; }
if(!isset($jobRotationSpeed)) { $jobRotationSpeed = 20; }
$html = '';
$html2 = '';

if(($jcontent = file_get_contents($configOptions['JobPostingMiniJobListTemplate'])) !== false)
{
	//Start by generating the menu
	//$jcontent = $builder->generateJobList($tables,$content,$cID,$jpsID,$limit,$cap,$sortBy,$sortOrder);
	
	$post = $builder->getContent('{{JOB_DISPLAY}}', '{{/JOB_DISPLAY}}', $jcontent);
	
	// Build out query.
	$q = "
		SELECT 
			J.ID as JobID, J.Title, J.WebAddress, J.ShortDescription, J.City, J.State, J.DateLastEdited, J.StartDate,
			E.ID as EmployerID, E.Company, 
			C.ID as CategoryID, C.Name as Category
		FROM 
			".$tables['JobPostingJobs']." J, ".$tables['Users']." E, ".$tables['JobPostingCategories']." C 
		WHERE 
			E.ID=J.EmployerID AND 
			C.Active='Y' AND 
			C.ID=J.CategoryID AND 
			J.JobPostingSystemID=" . $jobSystemID . " AND 
			J.Active='Y' AND 
			J.StartDate<='" . date("Y-m-d") . " 23:59:59' AND 
			J.EndDate>='" . date("Y-m-d") . " 00:00:00'";
		$q .= " " . $builder->getJobSort($sortBy, $sortOrder) . " limit " . $jobsLimit;
	
	$r = mysql_query($q);
	$rows = mysql_num_rows($r);
	
	while($ra = mysql_fetch_array($r))
	{
		$tmp = $post;
		
		$tmp = str_replace("{{CATEGORY}}", $ra['Category'], $tmp);
		$tmp = str_replace("{{JOB_TITLE}}", $ra['Title'], $tmp);
		$tmp = str_replace("{{JOB_ID}}", $ra['JobID'], $tmp);
		$tmp = str_replace("{{CATEGORY_ID}}", $ra['CategoryID'], $tmp);
		$tmp = str_replace("{{JOB_START_DATE}}", $ra['StartDate'], $tmp);
		$tmp = str_replace("{{JOB_DATE_LAST_EDITED}}", $ra['DateLastEdited'], $tmp);
		$tmp = str_replace("{{EMPLOYER_COMPANY}}", $ra['Company'], $tmp);		
		$tmp = str_replace("{{JOB_CITY}}", $ra['City'], $tmp);
		$tmp = str_replace("{{JOB_STATE}}", $ra['State'], $tmp);
		$tmp = str_replace("{{ShortDescription}}", $ra['ShortDescription'], $tmp);
		$tmp = str_replace("{{POSTING_COMPANY}}", $gVars['COMPANY'],$tmp);
		$tmp = str_replace("{{POSTING_CATEGORY}}", $gVars['CATEGORY'],$tmp);
		$tmp = str_replace("{{POSTING_STARTDATE}}", $gVars['START_DATE'],$tmp);
		
		
		if(strlen($ra['WebAddress']) > 4)
		{
			if(strtolower(substr($ra['WebAddress'],0,4)) == 'http')
			{
				$tmp = str_replace("{{WEB_ADDRESS}}", $ra['WebAddress'], $tmp);
			}
			else
			{
				$tmp = str_replace("{{WEB_ADDRESS}}", 'http://' . $ra['WebAddress'], $tmp);
			}
		}
		else
		{
			// We don't have a web address, so lets kill the anchor.
			$tmp = $builder->replaceTag('{{JOB_WEB_ADDRESS}}', '{{/JOB_WEB_ADDRESS}}', '', $tmp);
		}
		
		$tmp = str_replace("{{JOB_WEB_ADDRESS}}", '', $tmp);
		$tmp = str_replace("{{/JOB_WEB_ADDRESS}}", '', $tmp);
		$tmp = replaceLanguageTags($tmp, $gVars);
		
		// Here is where the magic happens. We want all items to be added to the $html variable in order, then we want to add the first two
		// a second time, with the first one getting the {{JOB_REPEAT}} tag set correctly.
		if($count <2)
		{
			if($count == 0)
			{
				$html2 .= str_replace("{{JOB_REPEAT}}", ' id="rpt"',$tmp);			
			}
			else
			{
				$html2 .= str_replace("{{JOB_REPEAT}}", '',$tmp);			
			}
		}
		$count++;

		// $html is our building output.
		$html .= str_replace("{{JOB_REPEAT}}", '',$tmp);
		
	} // end of loop through items.
	

	// We add the duplicates to the bottom of the list, so that if there were ten items, the actual list is twelve long.	
	$html .= $html2;

	$jcontent = $builder->replaceTag('{{JOB_DISPLAY}}', '{{/JOB_DISPLAY}}', $html, $jcontent);
	
	$jcontent = parseJobLanguagePlaceholders($jcontent);
	
	// Replace the speed placeholder in the javascript. We can add more of these to allow 
	// control of the direction of scroll, the ID of the repeat item (needs to be dynamic 
	// if multiple job module plugins are used on a page, for example), etc.
	$jcontent = str_replace("{{JOBROTATIONSPEED}}", $jobRotationSpeed, $jcontent);
	$jcontent = str_replace("{{SELF}}", $_SERVER['PHP_SELF'], $jcontent);
	$jcontent = str_replace("{{JOB_MODULE_LINK}}", $jobModulePage, $jcontent);
	
	echo $jcontent;
}


/****************************************************************************
 *                                                                          *
 * Required functions need to be created only once, even if the include is  *
 * loaded multiple times. As such, they need to appear above any potential  *
 * calls. The first is actually most of the content of this file.           *
 ****************************************************************************/
 


function parseJobLanguagePlaceholders($html)
{
   global $gVars;
   $html = str_replace("{{POSTING_DATE}}", $gVars['POSTING_DATE'],$html);
	$html = str_replace("{{POSITION}}",      $gVars['POSITION'],$html);
	$html = str_replace("{{LAST_UPDATED}}",  $gVars['LAST_UPDATED'],$html);		
	$html = str_replace("{{SUBMITTED_BY}}", $gVars['EMPLOYER'],$html);
	$html = str_replace("{{APPLY_NOW}}", $gVars['APPLY_NOW'],$html);
	$html = str_replace("{{WEBSITE}}", $gVars['WEBSITE'],$html);
	$html = str_replace("{{JOB_VIEW_ALL}}", $gVars['VIEWALL'], $html);
	$html = str_replace("{{POSTING_MORE_INFO}}", $gVars['MORE_INFO'],$html);


	
	return $html;
}



function replaceLanguageTags($str,$gVars)
{
	while(list($k,$v) = each($gVars))
	{
		$str = str_replace("{{".$k."}}",$v,$str);
	}
	return($str);
}




		

	






