<?php
$module_css_path = ($line_domain['theme'] != "" && $line_domain['theme'] != "default") ? ($path . ('assets/themes/' . $line_domain['theme'] . '/')) : $path;
//$must_cache = false;
$pfcache_ttl = 60;

if(isset($module_params)) {
    $def_location = isset($module_params['def_location']) ? $module_params['def_location'] : '';
    $def_category_list = isset($module_params['def_category_list']) ? $module_params['def_category_list'] : 'yes';
    $def_ads = isset($module_params['def_ads']) ? $module_params['def_ads'] : 'no';
    $def_ad_id = isset($module_params['ws_ad_unit_id']) ? $module_params['ws_ad_unit_id'] : '';
}
else {
    $q = "SELECT l.* FROM left_content l, module m WHERE l.module_id=m.module_id AND m.url='jobs_ext' AND l.catdom_id='$domain_id';";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        $row = mysql_fetch_assoc($r);
        $module_params = unserialize(base64_decode($row['module_params']));
    }

    $def_location = isset($module_params['def_location']) ? $module_params['def_location'] : '';
    $def_category_list = isset($module_params['def_category_list']) ? $module_params['def_category_list'] : 'yes';
    $def_ads = isset($module_params['def_ads']) ? $module_params['def_ads'] : 'no';
    $def_ad_id = isset($module_params['ws_ad_unit_id']) ? $module_params['ws_ad_unit_id'] : '';
}

//echo '<pre>', var_export($module_params, true), '</pre>';
//echo '<pre>', var_export($_REQUEST, true), '</pre>';
$show_page = true;
if(isset($_GET['shortName']) && $_GET['shortName'] == "jobs_redirect") {
    $show_page = false;
    $redirect_found = false;

    if(isset($_GET['job_jobs_id']) && intval($_GET['job_jobs_id']) > 0) {
        $job_jobs_id = intval($_GET['job_jobs_id']);
        $q = "SELECT * FROM job_jobs WHERE job_jobs_id='$job_jobs_id'";
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) == 1) {
            $redirect_found = true;
            $row = mysql_fetch_assoc($r);
            $url = $full_addon_uri . "jobs/job/$job_jobs_id/" . url_slug(trim(stripslashes($row['JobTitle'])));

            //echo $url;

            $send301 = array(
                'send301' => 1,
                'url'   => $url
            );
        }
    }

    if(isset($_GET['job_cv_id']) && intval($_GET['job_cv_id']) > 0) {
        $job_cv_id = intval($_GET['job_cv_id']);
        $q = "SELECT * FROM job_cvs WHERE job_cv_id='$job_cv_id'";
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) == 1) {
            $redirect_found = true;
            $row = mysql_fetch_assoc($r);
            $url = $full_addon_uri . "resumes/resume/$job_cv_id/" . url_slug(trim(stripslashes($row['FirstName'])) . ' ' . trim(stripslashes($row['LastName']))) ;

            //echo $url;

            $send301 = array(
                'send301' => 1,
                'url'   => $url
            );
        }
    }

    if(isset($_GET['jobs_url']) && $_GET['jobs_url'] == "jobs_cv_list" ) {
        $redirect_found = true;
        $url = $full_addon_uri . "resumes/" ;

        //echo $url;

        $send301 = array(
            'send301' => 1,
            'url'   => $url
        );
    }

    if(isset($_GET['jobs_url']) && $_GET['jobs_url'] == "jobs_job_list" ) {
        $redirect_found = true;
        $url = $full_addon_uri . "jobs/" ;

        //echo $url;

        $send301 = array(
            'send301' => 1,
            'url'   => $url
        );
    }

    if(!$redirect_found) {
        $send404 = true;
        require_once(dirname(__FILE__) . "/../404/404.php");

    }
}

if($show_page) {

    $include_file = dirname(__FILE__) . "/jobs_main.php";
    $jobs_url = isset($_REQUEST['jobs_url']) ? trim($_REQUEST['jobs_url']) : '';
    $module_type = isset($_REQUEST['module_type']) ? trim($_REQUEST['module_type']) : '';
    $locations = array("Nicosia", "Limassol", "Larnaca", "Paphos", "Famagusta");

    if ($jobs_url != "") {

        $jparts = explode('/', $jobs_url);

        if (is_array($jparts) && count($jparts) > 0) {

            $first_segment = $jparts[0];
            if(substr($first_segment, 0, 3) == "in-") {
                $first_segment = "location";
            }

            switch ($first_segment) {
                case "location":
                    $def_location = ucfirst(str_replace('in-', '', $jparts[0]));

                    if(in_array($def_location, $locations)) {

                        if (isset($jparts[1]) && $jparts[1] == "category") {
                            if (isset($jparts[2]) && intval($jparts[2]) > 0) {
                                $cid = intval($jparts[2]);
                                $include_file = dirname(__FILE__) . "/category_jobs.php";
                            } else {
                                $include_file = "";
                                $send404 = true;
                                require(dirname(__FILE__) . "/../404/404.php");
                            }
                        } else {
                            $include_file = "";
                            $send404 = true;
                            require(dirname(__FILE__) . "/../404/404.php");
                        }
                    }
                    else {
                        $include_file = "";
                        $send404 = true;
                        require(dirname(__FILE__) . "/../404/404.php");
                    }
                break;
                case "job":
                    if (isset($jparts[1]) && intval($jparts[1]) > 0) {
                        $jid = intval($jparts[1]);
                        $include_file = dirname(__FILE__) . "/job_details.php";
                    } else {
                        $include_file = "";
                        $send404 = true;
                        require(dirname(__FILE__) . "/../404/404.php");
                    }
                    break;

                case "resume":
                    if (isset($jparts[1]) && intval($jparts[1]) > 0) {
                        $rid = intval($jparts[1]);
                        $include_file = dirname(__FILE__) . "/resume_details.php";
                    } else {
                        $include_file = "";
                        $send404 = true;
                        require(dirname(__FILE__) . "/../404/404.php");
                    }
                    break;

                // Requests For Jobs in a category
                case "category":
                    if (isset($jparts[1]) && intval($jparts[1]) > 0) {
                        $cid = intval($jparts[1]);
                        $include_file = dirname(__FILE__) . "/category_jobs.php";
                    } else {
                        $include_file = "";
                        $send404 = true;
                        require(dirname(__FILE__) . "/../404/404.php");
                    }
                break;
            }
        } else {
            if (isset($_GET['shortName']) && $_GET['shortName'] == "jobs_ext") {
                if ($module_type == "jobs") {
                    $include_file = dirname(__FILE__) . "/jobs.php";
                }

                if ($module_type == "resumes") {
                    $include_file = dirname(__FILE__) . "/resumes.php";
                }
            }
        }

        /*$jobs_url = str_replace('-', '_', $jobs_url);
        $jobs_file = dirname(__FILE__) . "/$jobs_url" . ".php";
        if(file_exists($jobs_file)) {
            $include_file = $jobs_file;
        }*/
    } else {
        if (isset($_GET['shortName']) && $_GET['shortName'] == "jobs_ext") {
            if ($module_type == "jobs") {
                $include_file = dirname(__FILE__) . "/jobs.php";
            }

            if ($module_type == "resumes") {
                $include_file = dirname(__FILE__) . "/resumes.php";
            }
        }

    }

    if ($include_file != "") {

        $q = "SELECT * FROM job_config WHERE id='1';";
        $r = @mysql_query($q);
        if ($r && mysql_num_rows($r) == 1) {
            $row = mysql_fetch_assoc($r);
            // Jobs Days to live
            $jdl = $row['Expiration'] == "yes" ? intval($row['DaysToLive']) : 0;

            // Resumes days to live
            $rdl = $row['ExpirationCV'] == "yes" ? intval($row['DaysToLiveCV']) : 0;
        }

        require_once($include_file);
    }
}