<?php
class CategoryController extends baseController {
    function index() { 
    	$categoryModel = new categoryModel();
    	//$projectModel  = new projectModel();
        //$listingModel  = new listingModel();
		$blogModel 	   = new blogModel();
        $userModel     = new userModel();

        //$cat_id = $this->registry->args[1];
		$cat_name = $this->registry->args[1];
//		print_r($cat_name);
//		exit;

        //if (!is_numeric($cat_id) && $cat_id != 'all')
            //$cat_id = "all";
		if (!isset($cat_name) && $cat_name != 'all')
			$cat_name = "all";

        $cond = [];
        
		if (isset($_GET['search'])) {
			$search_term = $_GET['search'];
			//print_r($search_term);
			$cond = ['search' => $search_term];
		} elseif ($cat_name != 'all') {
			$category_map = $categoryModel->getbyname($cat_name);
//			print_r($category_map);
//			exit;
//			print_r($category_map['id']);
//			exit;
			$selected_category = $categoryModel->get($category_map['id']);
//			print_r($selected_category);
//			exit;
			$cat_id = $category_map['id']; // this sets the global variable from Line 9
//			print_r($cat_id);
//			exit;			
			$cat_title = $selected_category['title'];
			//print_r($category);
        	//$cond = ['category_id' => $cat_id];
			//print_r($cond);
			//exit;
			$cond = ['category_id' => $category_map['id']];
			//$cond = ['cat_id' => $category_map['id'], 'sched' => 1, 'bydate' => 1, 'feat' => 1];
        } else {
			$cond = ['sched' => 1, 'bydate' => 1, 'feat' => 1];
        	$category = [
        		'id' => 'all',
        		'title' => 'All'
        	];
        }
        //$projects = $projectModel->getList($cond);
		$blogs = $blogModel->getList($cond);
		//print_r($blogs);
        //print_r($projects);
        $categories = $categoryModel->getList();
		//print_r($categories);
		
		//$questionsModel 	= new questionsModel();
		//$challenges		= $questionsModel->getChallenges($cond);
//		echo "<pre>";
//		print_r($challenges); 
        //$featured_project = $listingModel->getFeatured(['category_id' => $cat_id]);
        //$total_projects = $listingModel->getTotalCount();

        //$fallback_coordinates = "{$projects[0]['location_lat']},{$projects[0]['location_lng']}";

        $current_user = null;
        $logged_in    = isset($_SESSION['user_id']) && isset($_SESSION['loggedin']) && $_SESSION['user_id'] && $_SESSION['loggedin'];

        if ($logged_in)
            $current_user = $userModel->getUser(['id' => $_SESSION['user_id']]);

    	require('views/category/index.tpl.php');
    }
}
?>