<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

	/**
	 * MAIN CMS CONTROLLER
	 * ihmsMedia CMS
	 * Roland Ihms
	 */
	function Main()
	{
		parent::__construct();
		$this->load->model('main_model');
		$this->load->helper('file');
	}
	
	//+++++++++++++++++++++++++++
	//MAIN
	//++++++++++++++++++++++++++
	public function index()
	{
		$data['title'] = "Marley Tjitjo Architects Inc";
		$data['metaD'] = "Marley Tjitjo Architects Inc. (MTA) is an architectural practice based in Windhoek, providing professional services in the commercial, residential, industrial and institutional building environment.";
		$data['pageID'] = 1;
		$this->load->view('home', $data);
	}
	
	//+++++++++++++++++++++++++++
	//Projects
	//++++++++++++++++++++++++++
	public function projects()
	{
		$data['title'] = "Projects - Marley Tjitjo Architects Inc";
		$data['metaD'] = "Marley Tjitjo Architects Inc, Institutional, Industrial, Commercial and Residential Projects";		
		$data['pageID'] = 3;
		$this->load->view('projects', $data);
	}
	
	//+++++++++++++++++++++++++++
	//Projects
	//++++++++++++++++++++++++++
	public function contact()
	{
		$data['pageID'] = 5;		
		//$data['projects'] = $this->main_model->get_projects();
		$data['title'] = "Contact - Marley Tjitjo Architects Inc";
		$data['metaD'] = "Marley Tjitjo Architects Inc Contact, we would like to hear from you!";
		$this->load->view('contact', $data);
	}	
	
	//+++++++++++++++++++++++++++
	//PROJECT
	//++++++++++++++++++++++++++
	public function project($slug)
	{	
		$project = $this->main_model->get_project($slug);
		$project['pageID'] = 3;
		$project['scrollID'] = 1;
	
		$title = $project['title'] . " - Marley Tjitjo Architects Inc";
		$metaD = 'Marley Tjitjo Architects Inc - ' . $project['metaD']; 
	
		$project['title'] = $title;
		$project['metaD'] = $metaD;
		
		$this->load->view('project', $project);
	}
	
	//+++++++++++++++++++++++++++
	//MANAGEMENT
	//++++++++++++++++++++++++++
	public function profile()
	{	
		$page['title'] = "Profile and Services - Marley Tjitjo Architects Inc";
		$page['metaD'] = "Marley Tjitjo Architects Inc. (MTA) is an architectural practice based in Windhoek, providing professional services in the commercial, residential, industrial and institutional building environment.";
		
		//$page = $this->main_model->get_page('profile');
		$page['pageID'] = 2;
		$this->load->view('profile', $page);
	}
	
	//+++++++++++++++++++++++++++
	//MANAGEMENT
	//++++++++++++++++++++++++++
	public function clients()
	{	
		$page['title'] = "Clients - Marley Tjitjo Architects Inc";
		$page['metaD'] = "Marley Tjitjo Architects Inc Clients include ministry, organizations and individuals";
		
		//$page = $this->main_model->get_page('profile');
		$page['pageID'] = 4;
		$this->load->view('clients', $page);
	}	
	
	
	function load_image_pop($pid, $gid)
	{	
		$this->main_model->getPopImage($pid, $gid);		
	}			


	//+++++++++++++++++++++++++++
	//MANAGEMENT
	//++++++++++++++++++++++++++
	public function about()
	{	
		$page = $this->main_model->get_page('about');
		$page['pageID'] = 2;
		$this->load->view('about', $page);
	}


	//+++++++++++++++++++++++++++
	//PAGE
	//++++++++++++++++++++++++++
	public function page($slug)
	{	
		$page = $this->main_model->get_page($slug);
		if($slug == 'contact'){
			$page['contact'] = 'yes';
		}
		$this->load->view('page', $page);
	}
	
	
	
	//+++++++++++++++++++++++++++
	//POST
	//++++++++++++++++++++++++++
	public function post($slug)
	{	
		$page = $this->main_model->get_post($slug);
		$this->load->view('post', $page);
	}
	//+++++++++++++++++++++++++++
	//NEWS
	//++++++++++++++++++++++++++
	public function news($query = '')
	{	
	$posts['pageID'] = 5;
		$posts['query'] = '';//$this->main_model->get_posts($query);
		$posts['title'] = $this->config->item('site_title'). ' News';
		$posts['heading'] =  'keeping you informed...';
		
		$this->load->view('blogroll', $posts);
	}
	//+++++++++++++++++++++++++++
	//CATEGORIES
	//++++++++++++++++++++++++++
	public function category($cat_id, $cat_name)
	{	
		$posts['query'] = "SELECT * FROM posts JOIN post_cat_int ON posts.post_id = post_cat_int.post_id WHERE post_cat_int.cat_id = '".$cat_id."'";
		$posts['title'] = 'News by category';
		$posts['heading'] = $this->config->item('site_name'). $this->clean_url_str($cat_name, $replace=array(), $delimiter=' ');
		
		$this->load->view('blogroll', $posts);
	}

	//+++++++++++++++++++++++++++
	//CONTACT US SUBMISSION
	//++++++++++++++++++++++++++
	public function contact_do()
	{
		$email = $this->input->post('email', TRUE);
		$name = $this->input->post('name', TRUE);
		$msg = $this->input->post('msg', TRUE);
		$captcha = $this->input->post('captcha', TRUE);
		$x = $this->input->post('x', TRUE);
		$y = $this->input->post('y', TRUE);
		
		//VALIDATE INPUT
		if(!filter_var( $email, FILTER_VALIDATE_EMAIL )){
			$val = FALSE;
			$error = 'Email address is not valid.';	
			
		}elseif($name == ''){
			$val = FALSE;
			$error = 'Please provide us with your full name.';	
		//CORRECT CAPTCHA
		}elseif(($x + $y) != $captcha){
			$val = FALSE;
			$error = 'Your answer did not match.';
						
		}else{
			$val = TRUE;
		}
		
		$settings = $this->get_settings();
		//IF VALIDATED
		if($val == TRUE){
			
				 $data1 = array(
				  'name'=> $name ,
				  'email'=> $email ,
				  'body'=> $msg ,
				  'type'=> 'enquiry',
				  'email_to' => $settings['contact_email'],
				  'subject' => 'Enquiry from '.$name
				);
				//GET WHK LOCAL DATE
			date_default_timezone_set('Africa/Windhoek');
			$time = date("r"); // local time
				$insertdata1 = array(
				  'name'=> $name ,
				  'email'=> $email ,
				  'body'=> $msg ,
				  'type'=> 'enquiry',
				   'datetime' => date('y-m-d h:i:S',strtotime($time)),

				);
			//SEND EMAIL LINK
			$this->load->model('email_model');	
			$this->email_model->send_enquiry($data1);	
			
			$this->db->insert('enquiries',$insertdata1);
			
			
			$data['basicmsg'] = 'Thanks, '. $name. '! We have succesfully sent your enquiry.' ;

			echo '<div class="alert alert-success">
         			<button type="button" class="close" data-dismiss="alert">×</button>
            		'.$data['basicmsg'].'</div>
					<script type="text/javascript">
					$("#msg").val("").empty();
					</script>
					';	
		
		//IF NOT VALIDATED	
		}else{

			$data['error'] = $error;
			echo '<div class="alert alert-error">
         			<button type="button" class="close" data-dismiss="alert">×</button>
            		'.$data['error'].'</div>';	
			
		}
	
	
			
	}
	
	public function contact_ajax($bus_id)
	{
		$email = $this->input->post('email', TRUE);
		$name = $this->input->post('name', TRUE);
		$msg = $this->input->post('msg', TRUE);
		$captcha = $this->input->post('captcha', TRUE);
		$x = $this->input->post('x', TRUE);
		$y = $this->input->post('y', TRUE);
		
		//VALIDATE INPUT
		if(!filter_var( $email, FILTER_VALIDATE_EMAIL )){
			$val = FALSE;
			$error = 'Email address is not valid.';	
			
		}elseif($name == ''){
			$val = FALSE;
			$error = 'Please provide us with your full name.';	
		//CORRECT CAPTCHA
		}elseif(($x + $y) != $captcha){
			$val = FALSE;
			$error = 'Your security answer did not match. What is '.$x . ' + ' . $y;
						
		}else{
			$val = TRUE;
		}
		
		//IF VALIDATED
		if($val == TRUE){

     	$settings = $this->get_settings();

				$data1 = array(
				  'name'=> $name ,
				  'email'=> $email ,
				  'body'=> $msg ,
				  'type'=> 'enquiry',
				  'email_to' => $settings['contact_email'],
				  'subject' => 'Enquiry from '.$name
				);
			//GET WHK LOCAL DATE
			date_default_timezone_set('Africa/Windhoek');
			$time = date("r"); // local time
				$insertdata1 = array(
				  'name'=> $name ,
				  'email'=> $email ,
				  'body'=> $msg ,
				  'type'=> 'enquiry',
				  'datetime' => date('y-m-d h:i:S',strtotime($time)),

				);
			//SEND EMAIL LINK
			$this->load->model('email_model');	
			$this->email_model->send_enquiry($data1);	
			
			$this->db->insert('enquiries',$insertdata1);

			$data['basicmsg'] = 'Thanks, '. $name. '! We have succesfully sent your enquiry.' ;
			echo '<div class="alert alert-success">
         			<button type="button" class="close" data-dismiss="alert">×</button>
            		'.$data['basicmsg'].'</div>
					<script type="text/javascript">
					$("#msg").setCode("");
					</script>
					';	
		
		//IF NOT VALIDATED	
		}else{

			$data['error'] = $error;
			echo '<div class="alert alert-error">
         			<button type="button" class="close" data-dismiss="alert">×</button>
            		'.$data['error'].'</div>';	
			
		}
	
			
	}
	//+++++++++++++++++++++++++++
	//SUBMIT COMMENT
	//++++++++++++++++++++++++++
	public function submit_comment()
	{
		
		$this->load->library('user_agent');
		//TEST IF ROBOT
		if ($this->agent->is_robot())
		{
			echo '<div class="alert alert-error">
         			<button type="button" class="close" data-dismiss="alert">×</button>
            		Sorry, only humans can discuss this matter!</div>';	
		
		//IS HUMAN
		}else{
			
			$email = $this->input->post('email', TRUE);
			$name = $this->input->post('name', TRUE);
			$msg = strip_tags($this->input->post('body', TRUE));
			$post_id = $this->input->post('post_id', TRUE);

			//VALIDATE INPUT
			if(!filter_var( $email, FILTER_VALIDATE_EMAIL )){
				$val = FALSE;
				$error = 'Email address is not valid.';	
				
			}elseif($name == ''){
				$val = FALSE;
				$error = 'Please provide us with your name.';	
			
			}else{
				$val = TRUE;
			}
			
			
			//IF VALIDATED
			if($val == TRUE){
				//GET WHK LOCAL DATE
				date_default_timezone_set('Africa/Windhoek');
				$time = date("r"); // local time
					 $data1 = array(
					  'name'=> $name ,
					  'email'=> $email ,
					  'cont_id'=> $post_id ,
					  'body'=> $msg ,
					  'type'=> 'comment',
					  'datetime' => date('y-m-d h:i:S',strtotime($time)),
					  'status' => 'moderate'
					);

				
				$this->db->insert('comments',$data1);
				$msgpre = str_replace("'"," ", strip_tags($msg));
				//BUILD PREVIEW
				$pre = "<div class='comment'><div class='commentbox right'><div class='arrow'></div><h3 class='commentbox-title'>".$name ."<span class='pull-right'>Being moderated</span></h3><div class='popover-content'>".$msgpre."<small style='color:#CCC'>".date('y-m-d h:i:S',strtotime($time))."</small></div></div></div><br />";
				
				$data['basicmsg'] = 'Thanks, '. $name. '! You your comment will be moderated..' ;
	
				echo '<div class="alert alert-success">
						<button type="button" class="close" data-dismiss="alert">×</button>
						'.$data['basicmsg'].'</div>
						<script type="text/javascript">
						
							$(".redactor").html("").empty();
							$("#comment_id").prepend("'.$pre.'");
							console.log("'.$pre.'");
							//load_comments();
						
						</script>
						';	
			
			//IF NOT VALIDATED	
			}else{
	
				$data['error'] = $error;
				echo '<div class="alert alert-error">
						<button type="button" class="close" data-dismiss="alert">×</button>
						'.$data['error'].'</div>';	
				
			}
		
		}
			
	}
	
	//+++++++++++++++++++++++++++
	//LOAD COMMENTS
	//++++++++++++++++++++++++++
	public function load_comments($post_id)
	{
		
		$this->main_model->get_comments($post_id);
	}
	//+++++++++++++++++++++++++++
	//GET SETTINGS
	//++++++++++++++++++++++++++		 
		 
	function get_settings(){

		$test = $this->db->get('settings');
		return $test->row_array();	

	}
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	//CLEAN BUSINESS URL SLUG
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++		 	

	//setlocale(LC_ALL, 'en_US.UTF8');
	function clean_url_str($str, $replace=array(), $delimiter='-') {
		if( !empty($replace) ) {
			$str = str_replace((array)$replace, ' ', $str);
		}
	
		$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
		$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
		$clean = strtolower(trim($clean, '-'));
		$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
	
		return $clean;
	}
	
}

/* End of file admin.php */
/* Location: ./application/controllers/welcome.php */