<?php
$type = 'latest';
$count = isset($_REQUEST['count']) ? intval($_REQUEST['count']) : 10;
if($count < 1 || $count > 30) $count = 10;

if(isset($_REQUEST['company'])) {
    $type = 'company';
}
elseif(isset($_REQUEST['category'])) {
    $type = 'category';
}

$items = array();
$channel = array(
    'title' => 'CyprusJobs.com - Latest Jobs',
    'description' => 'Checkout the latest ' . $count . ' jobs on CyprusJobs.com',
    'link' => 'https://www.cyprusjobs.com'
);

if($type == "company") {
    $feed_url = 'https://'.$_SERVER['HTTP_HOST'].'/rss/feed?company=' . urlencode($_REQUEST['company']);
    $company = mysql_real_escape_string(trim($_REQUEST['company']));
    $channel['title'] = 'CyprusJobs.com - ' . trim($_REQUEST['company']) . ' Jobs';
    $channel['description'] = 'Checkout the latest ' . $count . ' job openings from ' . trim($_REQUEST['company']);
    $q = "SELECT * FROM job_jobs WHERE CompanyName='$company' AND active='yes' AND job_status='1' ORDER BY updated_at DESC LIMIT 0,$count";
}

if($type == "category") {
    $feed_url = 'https://'.$_SERVER['HTTP_HOST'].'/rss/feed?category=' . intval($_REQUEST['category']);
    $channel['title'] = 'CyprusJobs.com - ' . 'Category Jobs';
    $category = intval($_REQUEST['category']);

    $qcateg = "SELECT * FROM job_cat WHERE job_cat_id='$category'";
    $rcateg = @mysql_query($qcateg);
    if($rcateg && mysql_num_rows($rcateg) == 1) {
        $rowcateg = mysql_fetch_assoc($rcateg);
        $channel['title'] = 'CyprusJobs.com - ' . trim(stripslashes($rowcateg['title_en'])) . ' Jobs';
        $channel['description'] = 'Checkout the latest ' . $count . ' jobs on ' . trim(stripslashes($rowcateg['title_en'])) . ' category';
    }

    $q = "SELECT * FROM job_jobs WHERE job_jobs_id IN (SELECT job_id FROM job_categs WHERE categ_id='$category') AND active='yes' AND job_status='1' ORDER BY updated_at DESC LIMIT 0,$count";
}

if($type == "latest") {
    $feed_url = 'https://'.$_SERVER['HTTP_HOST'].'/rss/feed';
    $q = "SELECT * FROM job_jobs WHERE active='yes' AND job_status='1' ORDER BY updated_at DESC LIMIT 0,$count";
}




$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while($row = mysql_fetch_assoc($r)) {
        $job = array_map('stripslashes', $row);
        $items [] = array(
            'title' => xmlEscape($job['JobTitle']),
            'link' => 'https://www.cyprusjobs.com/jobs/job/'.$job['job_jobs_id'].'/' . url_slug($job['JobTitle']),
            'pubDate' => date('r', strtotime($row['updated_at']))
        );
    }
}

$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">' . "\n";
//$xml .= '<rss version="2.0">' . "\n";

$xml .= '<channel>' . "\n";
    $xml .= '<title>'.xmlEscape($channel['title']).'</title>' . "\n";
	$xml .= '<link>'.$feed_url.'</link>' . "\n";
	$xml .= '<description>'.xmlEscape($channel['description']).'</description>' . "\n";
	$xml .= '<atom:link href="'.$feed_url.'" rel="self" type="application/rss+xml" />' . "\n";
	$xml .= '<ttl>60</ttl>' . "\n";

    foreach($items as $item) {
        $xml .= '<item>' . "\n";
			$xml .= '<title>'.$item['title'].'</title>' . "\n";
			$xml .= '<pubDate>'.$item['pubDate'].'</pubDate>' . "\n";
			$xml .= '<link>'.$item['link'].'</link>' . "\n";
			$xml .= '<guid>'.$item['link'].'</guid>' . "\n";
        $xml .= '</item>' . "\n";
    }

$xml .= '</channel>' . "\n";

$xml .= '</rss>';

ob_end_clean();
header('Content-Type: application/atom+xml');

echo $xml;