<?php
require_once(dirname(__FILE__) . "/../classes/Paginator.php");

$webpage_title_en = "Find Jobs In Cyprus";
$meta['description'] = "Check out the latest jobs in Cyprus. Filter through Limassol jobs, Nicosia jobs, Larnaca jobs, Paphos jobs or Famagusta jobs.";
$meta['keywords'] = "Cyprus jobs, Limassol jobs, Nicosia jobs, Larnaca jobs, Paphos jobs, Famagusta jobs";

$canonical_link = $protocol . $ultra_domainname . "/jobs/";

// Set breadcrumbs
$breadcrumbs [] = array(
    'link' => '',
    'title' => "Find Cyprus Jobs"
);

$optres['css'][] = $module_css_path . "amodules/jobsresumes/css/paginator.css";
$optres['css'][] = $module_css_path . "amodules/jobs_ext/css/jobs.css";
$optres['js'][] = $path . "amodules/jobs_ext/js/jobs.js";
function namesToCapital($str) {
	$ret = array();
	$str = explode(" ", $str);
	if(is_array($str) && count($str)>0) {
		foreach ($str as $i=>$s) {
			if(trim($s) != "") {
				$ret [] = ucfirst(trim(strtolower($s)));
			}
		}
	}
	
	if(count($ret) > 0) return implode(" ", $ret);
	return $str;
}

$have_filters = true;

$search = array(
    'job_cat_id' => "",
    'Location' => "",
    'Conditions' => "",
    'posted' => "all",
    'display' => "yes",
    'i_from' => 0,
    'i_limit' => 20
);



//echo '<pre>$_POST = ', var_export($_POST, true), '</pre>';


if(isset($_POST['jobs']) && isset($_POST['jobs']['display']) && $_POST['jobs']['display'] == "yes") {
	$have_filters = true;
	$jobs = $_POST['jobs'];
    $search = array_merge($search, $jobs);
    //echo '<pre>$search = ', var_export($search, true), '</pre>';
}

if(isset($_REQUEST['filter'])) {
    $filter = (trim($_REQUEST['filter']) != "") ? base64_decode($_REQUEST['filter']) : "";
    //var_dump($filter);
    if($filter != "") {
        $qs = array();
        parse_str($filter, $qs);
        //var_dump($qs);
        $search = array_merge($search, $qs);
    }
}


?>
<section class="article cyprus">
    <header>
        <h1>Find Jobs In Cyprus</h1>
        <!--[/SocialShareButtons/]-->
    </header>
    <div class="article-body ">
            <?php

            if($gads_show && $def_ads == "yes" && $def_ad_id != "") {
                $adsense_container_class = "no-margin";
                echo '<div style="margin-bottom: 6px;">';
                include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
                echo '</div>';
                unset($adsense_container_class);
            }
            ?>

            <form method="post" action="jobs/" id="search_form" class="form-horizontal" role="form">
                <div id="jsearch" class="box no-border">
                    <div class="box-body bg-gray">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-5">
                                    <div class="form-group">
                                        <label class="control-label">Job Category</label>
                                        <select name="jobs[job_cat_id]" class="form-control input-sm">
                                            <option value=""><?php echo 'Select Category';?></option>
                                            <?php
                                            $categs = array();
                                            $qexl = "select * from job_cat where active='yes' order by order_seq asc;";
                                            $rexl = @mysql_query($qexl);
                                            if($rexl && mysql_num_rows($rexl) > 0) {
                                                while ($rowexl = mysql_fetch_assoc($rexl)){
                                                    $categs[$rowexl['job_cat_id']] = stripslashes($rowexl['title_en']);
                                                    $selected = '';
                                                    if($search['job_cat_id'] == $rowexl['job_cat_id']) $selected = ' selected="selected"';
                                                    echo '<option value="',trim($rowexl['job_cat_id']),'" ',$selected,'>', stripslashes($rowexl['title_en']), '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-sm-5">
                                    <div class="form-group npl">
                                        <label class="control-label">Posted</label>
                                        <select name="jobs[posted]" class="form-control input-sm">
                                            <option value="all" selected="selected">Any Time</option>
                                            <?php
                                            $posted_vals = array(
                                                '0' => 'Today',
                                                '1' => 'Yesterday',
                                                '7'	=> 'Last 7 Days',
                                                '30'=> 'Last 30 Days'

                                            );
                                            foreach ($posted_vals as $pv=>$pv_title) {
                                                $selected = '';
                                                if($search['posted'] != "all" && $search['posted'] == $pv) $selected = 'selected="selected"';
                                                echo '<option value="',strval($pv),'" ',$selected,'>', $pv_title, '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-5">
                                    <div class="form-group">
                                        <label class="control-label">Location</label>
                                        <select name="jobs[Location]" class="form-control input-sm">
                                            <option value=""><?php echo 'Any'?></option>
                                            <?php
                                            $locations = array("Nicosia", "Limassol", "Larnaca", "Paphos", "Famagusta");
                                            foreach ($locations as $loc) {
                                                $selected = '';
                                                if($search['Location'] == $loc) $selected = 'selected="selected"';
                                                echo '<option value="',$loc,'" ',$selected,'>', $loc, '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-sm-5">
                                    <div class="form-group npl">
                                        <label class="control-label">Job Type</label>
                                        <?php
                                        $all_commitment = array(
                                            "full-time" 	=> "Full Time",
                                            "part-time"		=> "Part Time",
                                            "contract"		=> "Contract",
                                            "temp"			=> "Temporary",
                                            "internship"	=> "Internship",
                                            "other"			=> "Other"
                                        );
                                        ?>
                                        <select name="jobs[Conditions]" class="form-control input-sm">
                                            <option value=""><?php echo 'Any';?></option>
                                            <?php
                                            foreach ($all_commitment as $ctype=>$ctype_title) {
                                                $selected = '';
                                                if($search['Conditions'] == $ctype) $selected = 'selected="selected"';
                                                echo '<option value="',$ctype,'" ',$selected,'>', $ctype_title, '</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-5">
                                    <div class="form-group form-inline">
                                        <?php
                                        echo '<span>';
                                        echo '<select name="jobs[i_limit]" class="input-custom input-sm" style="width: 70px; margin-top: 10px;">';
                                        $limit_ar = array('10', '15', '20', '25', '30', '50');
                                        foreach ($limit_ar as $lt) {
                                            $selected = '';
                                            if(intval($search['i_limit']) == intval($lt) ) $selected = 'selected="selected"';
                                            echo '<option value="',$lt,'" ',$selected,'>',$lt,'</option>';
                                        }
                                        echo '</select> ', 'results per page' . '</span>';
                                        ?>
                                    </div>

                                </div>
                                <div class="col-sm-5">
                                    <div class="form-group jfoot">
                                        <button type="submit" name="search_jobs" class="btn btn-sm bg-black"><i class="fa fa-search"></i> Search</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <input type="hidden" name="jobs[i_from]" id="from" value="0">
                <input type="hidden" name="module_page" value="jobs_ext">
                <input type="hidden" name="jobs_url" value="jobs"/>
                <input type="hidden" name="jobs[display]" value="yes"/>
            </form>



            <?php



            if($have_filters) {

                foreach($search as $k=>$v) {
                    ${$k} = mysql_real_escape_string($v);
                }


                $q = "SELECT j.* FROM job_jobs j LEFT JOIN job_categs c ON j.job_jobs_id=c.job_id WHERE j.active='yes' AND j.job_status='1'";

                if($job_cat_id >0) {
                    //$q .= " AND job_cat_id='$job_cat_id' ";
                    $q .= " AND c.categ_id='$job_cat_id' ";
                }

                if($Location != "") {
                    $q .= " AND j.Location like '%$Location%' ";
                }

                if($Conditions != "") {
                    $q .= " AND j.Conditions='$Conditions' ";
                }

                switch (strval($posted)) {
                    case "all":
                        if($jdl > 0) {
                            $daystolive = date("Y-m-d 00:00:00", strtotime("-$jdl days", time()));
                            $q .= " AND j.updated_at>='$daystolive' ";
                        }
                        break;
                    case "0":
                        $today = date("Y-m-d 00:00:00");
                        $q .= " AND j.updated_at='$today' ";
                        break;
                    case "1":
                        $q .= " AND DATE(j.updated_at)=DATE_SUB(CURDATE(),INTERVAL 1 DAY) ";
                        break;
                    case "7":
                        $q .= " AND DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= DATE(j.updated_at) ";
                        break;
                    case "30":
                        $q .= " AND DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(j.updated_at) ";
                        break;
                }

                $q .= " ORDER BY j.updated_at DESC ";

                //echo $q;
                $total_records = 0;
                $r = @mysql_query($q);
                if($r && mysql_num_rows($r)>0){
                    $total_records = mysql_num_rows($r);
                }

                $q .= "	LIMIT $i_from, $i_limit;";

                //echo "<br/>".$q;

                if($total_records > 0) {

                    $paginator = new Paginator(array(
                        "base_url"  => "jobs/?",
                        "request_parameter_name" => "filter",
                        "records_per_page" => 20,
                        "show_all" => 0
                    ));

                    $search = array_map('stripslashes', $search);

                    //echo '<pre>$search = ', var_export($search, true), '</pre>';
                    $pagination = $paginator->paginate($total_records, $search);
                    echo $pagination;

                    $r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
                    if($r && mysql_num_rows($r)>0) {
                        ?>
                        <div class="container-fluid">

                                <?php
                                $cnt = 1 + $i_from;
                                while ($row = mysql_fetch_assoc($r)) {
                                    //$name = (($row['Genre'] == "m") ? "Mr." : "Ms.") . " " . stripslashes(trim($row['FirstName'])) . " " . stripslashes(trim($row['LastName']));
                                    $job_title = trim(stripslashes($row['JobTitle']));


                                    $locations = str_replace(";", ", ", trim($row['Location']));
                                    //$coo = trim(stripslashes($row['CountryOfOrigin']));
                                    $company_name = trim(stripslashes($row['CompanyName']));

                                    if(mb_strlen($company_name) > 50) {
                                        $company_name = wordwrap($company_name, 50, "<br />\n", true);
                                    }

                                    $job_jobs_id = intval($row['job_jobs_id']);
                                    //$job_cat_id = intval($row['job_cat_id']);

                                    $job_categs = [];
                                    $qc = "SELECT * FROM job_categs WHERE job_id='$job_jobs_id' ORDER BY id ASC;";
                                    $rc = @mysql_query($qc);
                                    if($rc && mysql_num_rows($rc) > 0) {
                                        while($rowrc = mysql_fetch_assoc($rc)) {
                                            if ($job_cat_id > 0 && $job_cat_id == intval($rowrc['categ_id'])) {
                                                array_unshift($job_categs, $categs[intval($rowrc['categ_id'])]);
                                            }
                                            else {
                                                $job_categs [] = $categs[intval($rowrc['categ_id'])];
                                            }
                                        }
                                    }


                                    $added_on = date("m/d/Y", strtotime($row['added_on']));
                                    $updated_at = date("d/m/Y", strtotime($row['updated_at']));

                                    // Salary
                                    $range = false;
                                    $base_salary = 0;
                                    $upto = false;
                                    $salary_min = intval($row['salary_min']);
                                    $salary_max = intval($row['salary_max']);

                                    if($salary_min > 0 && $salary_max > 0) $range = true;

                                    if(!$range) {
                                        $base_salary = $salary_max > $salary_min ? $salary_max : $salary_min;
                                        if($salary_min == 0) $upto = true;
                                    }


                                    // ********* End Salary


                                    ?>
                                    <div class="row job-line">
                                        <div class="jdata col-xs-10 job-title">
                                            <span class="job-num badge"><?=$cnt;?></span>
                                            <!--<a href="jobs/job-details.html?jid=<?/*=$job_jobs_id;*/?>" target="_self" class=""><?/*=$job_title;*/?></a>-->
                                            <a href="jobs/job/<?=$job_jobs_id;?>/<?=url_slug($job_title);?>" target="_self" class=""><?=$job_title;?></a>
                                        </div>
                                        <div class="jdata col-xs-10">
                                            <div class="row">
                                                <div class="col-sm-5 jline">
                                                    <div class="row">
                                                        <div class="col-sm-3 col-xs-3">Company:</div>
                                                        <div class="col-sm-7 col-xs-7">
                                                            <strong><?= $company_name; ?></strong>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-sm-5 jline">
                                                    <div class="row">
                                                        <div class="col-sm-3 col-xs-3">Location:</div>
                                                        <div class="col-sm-7 col-xs-7">
                                                            <strong><?= $locations; ?></strong>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>

                                        <div class="jdata col-xs-10">
                                            <div class="row">
                                                <div class="col-sm-5">
                                                    <div class="row">
                                                        <div class="col-sm-3 col-xs-3">Category:</div>
                                                        <div class="col-sm-7 col-xs-7">
                                                            <ul class="job-categories">
                                                                <?php
                                                                $hide_others = '';
                                                                for($i=0; $i<count($job_categs); $i++) {
                                                                    if($i == 0 && count($job_categs) > 1) {
                                                                        $hide_others = 'class="extra hidden"';
                                                                        echo '<li class="more-categs">' . $job_categs[$i] . ' <i class="fa fa-chevron-down toggle-categs" data-show="show"></i></li>';
                                                                    }
                                                                    else {
                                                                        echo '<li '.$hide_others.'>' . $job_categs[$i] . '</li>';
                                                                    }
                                                                }
                                                                ?>
                                                            </ul>
                                                        </div>
                                                    </div>

                                                </div>
                                                <div class="col-sm-5">
                                                    <?php
                                                    if (!$range && $base_salary > 0) {
                                                        ?>
                                                        <div class="row">
                                                            <div class="col-sm-3 col-xs-3">Salary:</div>
                                                            <div class="col-sm-7 col-xs-7">
                                                            <span class="text-bold text-primary">
                                                                <?php
                                                                if ($upto) echo '<small class="text-muted">Up to</small> ';
                                                                ?>
                                                                <meta itemprop="salaryCurrency" content="EUR"/>
                                                                &euro; <span itemprop="baseSalary"><?= $base_salary; ?></span>
                                                            </span>
                                                            </div>
                                                        </div>
                                                        <?php

                                                    }

                                                    if ($range) {
                                                        ?>
                                                        <div class="row">
                                                            <div class="col-sm-3 col-xs-3">Salary:</div>
                                                            <div class="col-sm-7 col-xs-7">
                                                            <span class="text-bold text-primary">
                                                                <span itemprop="baseSalary" itemscope="" itemtype="http://schema.org/PriceSpecification">
                                                                    <meta itemprop="priceCurrency" content="EUR"/>
                                                                    &euro; <span itemprop="minPrice"><?= ($salary_min); ?></span> - &euro; <span itemprop="maxPrice"><?= ($salary_max); ?></span>
                                                                </span>
                                                            </span>
                                                            </div>
                                                        </div>
                                                        <?php
                                                    }
                                                    ?>
                                                </div>
                                            </div>
                                        </div>


                                        <div class="jdata col-xs-10 date-line">
                                            <div class="row">
                                                <div class="col-xs-10">
                                                    <span class="job-updated">Last Updated: <?=$updated_at;?></span>

                                                    <span class="job-updated pull-right">ID: <strong><?=$job_jobs_id;?></strong></span>
                                                </div>
                                            </div>

                                        </div>
                                    </div>

                                    <?php

                                    if($cnt - $i_from == 1) {
                                        // Show Adsense
                                        if($gads_show && $def_ads == "yes" && $def_ad_id != "") {
                                            $adsense_container_class = "no-margin";
                                            echo '<div style="margin-top: 9px;margin-bottom: 6px; margin-left: -15px!important;margin-right: -15px!important;">';
                                            include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
                                            echo '</div>';
                                            unset($adsense_container_class);
                                        }
                                    }

                                    $cnt++;
                                }
                                ?>

                        </div>

                        <?php
                    }

                    echo $pagination;
                }
                else {
                    echo '<br><div class="alert alert-danger"><p class="text-white"><b><i class="fa fa-warning"></i> Sorry!</b></span><br/>There are no records matching your criteria.</p></div>';
                }
            }
            ?>


    </div>

</section>
<?php
?>