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

/**
 * Classified admin Controller
 *
 * This class handles user account related functionality
 *
 * @package		Show
 * @subpackage	ShowCore
 * @author		webhelios
 * @link		http://webhelios.com
 */



class Show_core extends CI_controller {

	var $PER_PAGE;
	var $active_theme = '';

	public function __construct()
	{
		parent::__construct();
		is_installed(); #defined in auth helper		
		
		//$this->PER_PAGE = get_per_page_value();#defined in auth helper
		if(get_settings('global_settings','enable_cache','No')=='Yes')
		{
			$this->output->cache(2);			
		}

		$this->PER_PAGE = get_settings('global_settings', 'posts_per_page', 6);


		$this->active_theme = get_active_theme();
		
		$this->load->model('admin/news_model');
		$this->load->model('show/show_model');

        $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');

        if(!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)))
	    {
	        $this->load->model('grab/grab_model');
	        $this->grab_model->init();	    	
	    }
	}

	public function index()
	{
		$this->home();	
	}

	public function home($banner_type='layer-slider')
	{			
		$value = array();
		
		if($banner_type=='google-map')
		$value['banner_type'] = 'Google Map';
		elseif($banner_type=='parallax-slider')
		$value['banner_type'] = 'Parallax Slider';
		else
		$value['banner_type'] = 'Layer Slider';

		$data['content'] 	= load_view('home_view',$value,TRUE);
		$data['alias']	    = 'home';
		load_template($data,$this->active_theme);
	}

	public function news($id='')
	{	
		$value = array();
		$this->load->model('admin/news_model');
		$value['post'] = $this->news_model->get_news_by_id($id);

		$title = '';
		$description = '';

		$data['alias']	    = 'detail';
		$id = 0;
		$status = 1;
		if($value['post']->num_rows()>0)
		{
			$row = $value['post']->row();
            $value['similar_posts'] = $this->news_model->get_similar_news_by_category($row->category, $row->sub_category, $row->id);
            $data['content'] 		= load_view('detail_view',$value,TRUE);
			$status = $row->status;			
			$id = $row->id;
			$seo['key_words'] 	= $row->tags;
			$title 				= $row->title;
			$description 		= $row->description;
			$this->news_model->inc_view_count_by_id($id);
		}

		if($status!=1)
		{
			$this->output->set_status_header('404');
			$data['content'] 	= load_view('404_view','',TRUE);
	        load_template($data,$this->active_theme,'template_view');			
		}
		else
		{
			$data['sub_title']			= $title;
			$description 	= strip_tags($description);
			$description 	= str_replace("'","",$description);
			$description 	= str_replace('"',"",$description);
			$seo['meta_description']	= $description;
			$data['seo']				= $seo;
			load_template($data,$this->active_theme);			
		}
	}

	public function categorynews($category_id='',$start='0')
    {
    	$value['posts'] 	= $this->news_model->get_category_news_by_range($category_id,$start,$this->PER_PAGE);
		$total 				= $this->news_model->get_category_news_by_range($category_id,'total');
		$value['pages']		= configPagination('show/categorynews/'.$category_id,$total,4,$this->PER_PAGE);
		$value['title']		= get_category_title_by_id($category_id);
        $data['content'] 	= load_view('allnews_view',$value,TRUE);
        $data['alias']	    = 'categorynews';
        $data['sub_title']	= get_category_title_by_id($category_id);
        load_template($data,$this->active_theme);
    }

    public function sourcenews($source_id='',$start='0')
    {
    	$value['posts'] 	= $this->news_model->get_source_news_by_range($source_id,$start,$this->PER_PAGE);
		$total 				= $this->news_model->get_source_news_by_range($source_id,'total');
		$value['pages']		= configPagination('show/sourcenews/'.$source_id,$total,4,$this->PER_PAGE);
		$value['title']		= get_source_title_by_id($source_id);
        $data['content'] 	= load_view('allnews_view',$value,TRUE);
        $data['alias']	    = 'sourcenews';
        $data['sub_title']	= get_source_title_by_id($source_id);
        load_template($data,$this->active_theme);
    }

    public function datenews($publish_time='',$start='0')
    {
    	$value['posts'] 	= $this->news_model->get_date_news_by_range($publish_time,$start,$this->PER_PAGE);
		$total 				= $this->news_model->get_date_news_by_range($publish_time,'total');
		$value['pages']		= configPagination('show/datenews/'.$publish_time,$total,4,$this->PER_PAGE);
		$value['title']		= translateable_date($publish_time);
        $data['content'] 	= load_view('allnews_view',$value,TRUE);
        $data['sub_title']	= translateable_date($publish_time);
        $data['alias']	    = 'datenews';
        load_template($data,$this->active_theme);
    }

    public function search()
    {
		$string = '';
    	foreach ($_POST as $key => $value) {

    		if(is_array($value))
    		{
    			$val = '';
    			foreach ($value as $row) 
    			{
    				$val .= $row.',';
    			}

    			$string .= $key.'='.$val.'|';	
    		}
    		else
			{
	    		$string .= $key.'='.$value.'|';			
			}    			
    	}
    	redirect(site_url('show/searchnews/'.$string));
    }

    public function searchnews($string='',$start='0')
    {
    	$data = array();
    	$string = rawurldecode($string);
    	$values = explode("|",$string);

    	foreach ($values as $value) {
    		$get 	= explode("=",$value);
    		$s 		= (isset($get[1]))?$get[1]:'';
    		$val 	= explode(",",$s);

    		if(count($val)>1)
    		{
	    		$data[$get[0]] = $val;
    		}
    		else
	    		$data[$get[0]] = (isset($get[1]))?$get[1]:'';
    	}

    	array_filter($data);

    	$value['posts'] 	= $this->news_model->get_news_search_result($data,$start,$this->PER_PAGE);
		$total 				= $this->news_model->get_news_search_result($data,'total');
		$value['pages']		= configPagination('show/searchnews/'.$string,$total,4,$this->PER_PAGE);
		$value['title']		= lang_key('search_result_for');
		$data 				= array();
        $data['content'] 	= load_view('allnews_view',$value,TRUE);
        $data['alias']	    = 'search';
        load_template($data,$this->active_theme);

    }


    public function terms()
    {
        $data['content'] 	= load_view('termscondition_view','',TRUE);
        $data['alias']	    = 'terms';
        load_template($data,$this->active_theme);
    }


	public function terms_check($str)
	{
		if (isset($_POST['terms_conditon'])==FALSE)
		{
			$this->form_validation->set_message('terms_check', lang_key('must_accept_terms'));
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}


	
    #********** page functions start**************#
	public function page($alias='')
	{	
		$value['alias']  = $alias;
		$value['query']  = $this->show_model->get_page_by_alias($alias);
		$data['content'] = load_view('page_view',$value,TRUE,$this->active_theme);
		$data['alias']   = $alias;
		load_template($data,$this->active_theme);
	}
    #********** page functions end**************#


    #********** Other page functions start**************#
    public function contact()
	{
		$a = rand (1,10);
		$b = rand (1,10);
		$c = rand (1,10)%3;
		if($c==0)
		{
			$operator = '+';
			$ans = $a+$b;
		}
		else if($c==1)
		{
			$operator = 'X';
			$ans = $a*$b;
		}
		else if($c==2)
		{
			$operator = '-';
			$ans = $a-$b;
		}

		$this->session->set_userdata('security_ans',$ans);
		$value['question']  = $a." ".$operator." ".$b." = ?";

		$data['content'] 	= load_view('contact_view',$value,TRUE);
		$data['alias']	    = 'contact';
		load_template($data,$this->active_theme);
	}

	function check_code($str)
	{
		if ($str != $this->session->userdata('security_ans'))
		{
			$this->form_validation->set_message('check_code', 'The %s is not correct');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	public function sendcontactemail()
	{
		$this->form_validation->set_rules('sender_name', 'Name', 'required');
		$this->form_validation->set_rules('sender_email', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('msg', 'Message', 'required');
		$this->form_validation->set_rules('ans', 'Code', 'required|callback_check_code');

		if ($this->form_validation->run() == FALSE)
		{
			$this->contact();	
		}
		else
		{

			$this->load->library('email');
			$this->email->from($this->input->post('sender_email'),$this->input->post('sender_name'));
			$this->email->to(get_settings('webadmin_email','contact_email','support@example.com'));

			$this->email->subject(lang_key('contact_subject'));
			$this->email->message($this->input->post('msg').'<br/>'.lang_key('phone').': '.$this->input->post('phone'));
			$this->email->send();

			$this->session->set_flashdata('msg', '<div class="alert alert-success">'.lang_key('mail_sent').'</div>');
			redirect(site_url('show/contact/'));			

		}

	}

	public function rss()
	{
		$this->load->helper('xml');
		$curr_lang 	= get_current_lang();
		if($curr_lang=='')
		$curr_lang = default_lang();

		$value = array();	
		$value['curr_lang'] = $curr_lang;	
		$value['feed_name'] = translate(get_settings('site_settings','site_title','newspilot'));
        $value['encoding'] = 'utf-8';
        $value['feed_url'] = site_url('show/rss');
        $value['page_description'] = lang_key('your web description');
        $value['page_language'] = $curr_lang.'-'.$curr_lang;
        $value['creator_email'] = get_settings('webadmin_email','contact_email','');
        $value['posts']	=  $this->show_model->get_properties_by_range(0,$this->PER_PAGE,'id','desc');
       # header("Content-Type: application/rss+xml");
		load_view('rss_view',$value,FALSE,$this->active_theme);
	}

    public function sitemap(){
        $this->load->helper('xml');
        $this->load->helper('file');
        $xml = read_file('./sitemap.xml');

        $value['title']='site map';

        $value['links'] = simplexml_load_string($xml);

        $data['content'] = load_view('sitemap_view',$value,TRUE,$this->active_theme);

        $data['alias']   = 'sitemap';

        load_template($data,$this->active_theme);
    }

    public function load_sub_categories($category_id)
    {
    	$this->load->model('admin/category_model');
    	$query = $this->category_model->get_all_subcategories_by_parent($category_id);
    	
    	$options = '<option value="">'.lang_key('select_one').'</option>';
    	foreach($query->result() as $row)
    	{
    		$options .= '<option value="'.$row->id.'">'.$row->title.'</option>';
    	}

    	echo $options;
    	die;
    }

    public function show404()
	{
		$this->output->set_status_header('404');
		$data['content'] 	= load_view('404_view','',TRUE);
        load_template($data,$this->active_theme,'template_view');
	}
	

}





/* End of file install.php */

/* Location: ./application/modules/show/controllers/show_core.php */