<?php

class IndexController extends baseController {
    function index() {
        global $website_options, $guest_options; 
        
        $is_mobile_uri = (bool) preg_match("/^\/m(\?.*)?/", $_SERVER['REQUEST_URI']);
		$is_simple_uri = (bool) preg_match("/^\/simple(\?.*)?/", $_SERVER['REQUEST_URI']);
		$is_comingsoon_uri = (bool) preg_match("/^\/coming-soon(\?.*)?/", $_SERVER['REQUEST_URI']);
        /**
         if ($_SESSION['user']['is_admin']) {
            header("Location: /admin/");
            exit;
        } else {
			header("Location: /");
		}
         * */
        
        $cond = [];

        if (isset($_GET['search'])) {
            $cond['search'] = addslashes(htmlentities(strip_tags($_GET['search'])));
        } else {
            $cond['is_featured'] = 1;
        }
		$userip = $_SERVER['REMOTE_ADDR'];
		
		// ANY DYNAMIC (ie. DB-based) FEATURE YOU WANT TO APPEAR ON THE HOME PAGE
		// 1. FIRST ADD THE MODEL
        $authModel      = new authModel();
        $userModel      = new userModel();
        //$projectModel   = new projectModel();
		//$categoryModel  = new categoryModel();
		//$testimonialModel = new testimonialModel();
		//$logosModel 	= new logosModel();	
        //$usertypeModel = new usertypeModel();	
		//$questionsModel = new questionsModel(); // addViews are in this
		//$squareusersmodel = new squareusersModel();
		
		// 2. THEN USE THE MODEL TO CALL THE FUNCTION THAT DISPLAYS THE CONTENT
		//$logos		= $logosModel->getList();
		//$testimonials = $testimonialModel->getList();
		
		// 3. THEN GO TO YOUR VIEWS/VIEWFILE.TPL.PHP AND CALL THE 
		// OBJECT WITH A VARIABLE AND/OR FOREACH, ETC.
		
		///////////////////////////////////////////////////////////////////////////////////
		
		//$questionsModel->addaViewNew('home', $userip, 'home'); // change to authModel or userModel
		
		//$categories     = $categoryModel->getList();	
		//$players = $squareusersmodel->getLeaderboard();
		//$leaderboards = $squareusersmodel->getLeaderboards();
		//$challenges = $projectModel->getList();
		//$challenges = $squareusersmodel->getChallenges();
		//$playersonlinenow = $questionsModel->getTotalOnline();	
		
		//$randomPlayers = mt_rand(0, count(5) - 1);
		//$playersonline = 20 + $playersonlinenow + $randomPlayers;
		
        //$projects        = $projectModel->getList($cond);
        //$recent_projects = $projectModel->getList(['order' => 'timestamp_posted DESC', 'limit' => 4]);
		//$recent_projects = $projectModel->getList(['project_type_id' => 3, 'order' => 'timestamp_posted DESC', 'limit' => 4]);
        //$recent_groups   = $groupModel->getList(['order' => 'date_created DESC', 'limit' => 8]);

//        $categories = $categoryModel->getList();
		//$recent = $projectModel->getRecent();
		//$recentMembers = $userModel->getRecentMembers();
		
//		$recentMembers1 = $userModel->getRecentMembers1();
//		$recentMembers2 = $userModel->getRecentMembers2();
//		$recentMembers3 = $userModel->getRecentMembers3();
//		$recentMembers4 = $userModel->getRecentMembers4();
//		echo "<pre>";
//		print_r($recentMembers);		
		//echo "<pre>";
		//print_r($recent);
		//print_r($testimonials);
		
		//////////////////////////////////////////////////////////
		//                 SUBSCRIBERS MODULE                   //
		//////////////////////////////////////////////////////////
		
		$subscribersModel  = new subscribersModel();
		$total_subscribers_raw = 3150 + $subscribersModel->getTotalCount();
		$total_spotsleft_raw = 20000 - $total_subscribers_raw;
		$total_subscribers = number_format($total_subscribers_raw,"0","",",");
		$total_spotsleft = number_format($total_spotsleft_raw,"0","",","); 
		  
//		$events 	= $eventsModel->getList(['order' => 'date ASC', 'limit' => 3]);
//		$eventsother = $eventsotherModel->getList(['limit' => 3]);
//		$specials   = $specialsModel->getList(['order' => 'date ASC', 'limit' => 3]);	
       // $user_types = $usertypeModel->getList();    	

        //$featured_projects = $projectModel->getFeaturedListings();
//		echo "<pre>";
//		print_r($featured_projects);
		
        //$total_projects = $projectModel->getTotalCount();

        //$featured_brands = $userModel->getFeaturedAccounts(['slider_group' => "App Intro"]);
        //$featured_models = $userModel->getFeaturedAccounts(['slider_group' => "Featured"]);
		
        //$featured_accounts = $userModel->getFeaturedAccounts(['limit' => 6]);
        //$featured_brands   = array_slice($featured_accounts, 0, 3);
        //$featured_models   = array_slice($featured_accounts, 3, 3);		

        //$avatars = $userModel->getRandomThumbs();

//        $nearby_errors = [];
//        if ($website_options['enable_homepage_nearby']) :
//            $nearby_cond = [
//                'nearby_search' => 1,
//                'latitude'      => 38.149576, // Chicago, IL
//                'longitude'     => -79.0716958, // Chicago, IL
//            ];
//
//            if ($_SESSION['user_id'] && empty($_GET['search_location'])) {
//                $current_user = $userModel->getUser($_SESSION['user_id']);
//
//                $nearby_cond['latitude']  = $current_user['location_last_lat'];
//                $nearby_cond['longitude'] = $current_user['location_last_lng'];
//            }
//            elseif (!empty($_GET['search_location'])) {
//                // center around given location
//                $search_location = $_GET['search_location'];
//
//                $search_location_lat = $_GET['search_location_lat'];
//                $search_location_lng = $_GET['search_location_lng'];
//
//                if (!empty($search_location_lat) && !empty($search_location_lng)) {
//                    $nearby_cond['latitude']  = $search_location_lat;
//                    $nearby_cond['longitude'] = $search_location_lng;
//                }
//                else
//                    $nearby_errors[] = "The address you entered could not be Geolocalized. Please check the Address again!";
//            }
//            $search_category = $_GET['search_category'];
//            $search_distance = $_GET['search_distance'];
//            if (!empty($search_category))
//                $nearby_cond['category_id'] = $search_category;
//
//            if (!empty($search_distance))
//                $nearby_cond['distance'] = $search_distance;
//
//            $nearby_cond['is_active'] = 1;
//            $nearby_projects = $projectModel->getList($nearby_cond);
//            $fallback_coordinates = "38.149576,-79.0716958"; // Chicago, IL
//        endif;

        // MOBILE REDIRECTION
        if (!$is_mobile_uri) {
            $url = "/m";
//            if ($_GET['show_settings'])
//                $url = "/m?show_settings=0";
            mobile_device_detect(true,false,true,true,true,true,true,$url,false);
        }

        if (isset($_GET['search'])) {
            $meta_title = 'BASE - ' . ucwords($cond['search']) . ' Search Results';
            require('views/search/search.tpl.php');
        }

        if ($is_mobile_uri && !isset($_GET['not_mobile'])) {
            require('views/index/m.tpl.php');
		} elseif ($is_simple_uri && !isset($_GET['not_mobile'])) {
			require('views/index/simple.tpl.php');
		} elseif ($is_comingsoon_uri && !isset($_GET['not_mobile'])) {
			require('views/index/coming-soon.tpl.php');	       
        } else {
            if($website_options['custom_homepage']){
			    $urlh = $website_options['custom_homepage'];
			    header("Location:$urlh");
            }else{
                require('views/index/index.tpl.php');
            }  
		}   

    }
}
?>