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

	class Blog extends Site_Controller {
		
		function __construct()
		{
			parent::__construct();
			$this->load->model(array('Mod_blog','Mod_category'));
			$this->load->helper(array('text','string','my_function','my_pagination'));	
		}		
	
		
		public function index($page=1)
		{
			$data["article"] = $this->Mod_blog->getarticle($page);
			$data["article"] = $data["article"]->result_array(); 
			$rows = $this->Mod_blog->article_countAll();
			$data["pagination"] = article_pagination($rows,10);
				
			$this->load->view('header');
			$this->load->view('blog/blog-posts',$data);
			$this->load->view('blog/blog_sidebar');
			$this->load->view('footer');
		}	
		
		public function detail()
		{
			$id = $this->uri->segment(3);
			$detail = $this->Mod_blog->getById(clean($id));
			if(!$detail)
			{
				log_message('error', 'variable was not correctly(detail), '.$id.'');
				redirect("pages/error_404");
				exit();
			}
				
			//$this->user_view($detail->postID);
			$data['detail'] = $detail;
			//$data['post'] = $this->Mod_blog->related_posts($detail->postID);
	
			$title = $detail->postTitle;
			$desc = word_limiter(strip_tags($detail->postDesc),20);
			$imgurl = find_images($detail->postDesc);
			$url = public_url($detail->postSlug);
			//$data["meta_tags"] = meta_tags($meta,$title,$desc,$imgurl,$url);
	
			$this->load->view('header');
			$this->load->view('blog/blog-single-post',$data);
			$this->load->view('blog/blog_sidebar');
			$this->load->view('footer');
		}
		
		
		public function category()
		{
			$id = $this->uri->segment(3);
			if ($this->security->xss_clean($id, TRUE) === FALSE||$id=="")
			{
				log_message('error', 'invalid character(category), '.$id.'');
				redirect("pages/error_404");
				exit();
			}
				
			if($cats = $this->Mod_category->category(clean($id)) AND count($cats) > 0)
			{
				$result = array();
				$query = $this->Mod_category->getcategory($cats->catID);
				foreach($query->result() as $v)
				{
					$article = $this->Mod_category->show_article($v->postID);
					foreach ($article->result() as $v)
					{
						$result[] = $v;
					}
				}
					
				$data['title'] = $cats->catTitle;
				$data['slug'] = $cats->catSlug;
				$data['article'] = $result;
				$total = $this->Mod_category->category_countAll($cats->catID);
				$data["pagination"] = category_pagination($total,10,$cats->catSlug);
					
				$this->load->view("header");
				$this->load->view("blog/blog-category",$data);
				$this->load->view("blog/blog_sidebar");
				$this->load->view("footer");			
			}
			else
			{
				log_message('error', 'invalid character(category), '.$id.'');
				redirect("Error_404");	
				exit();
			}		
		}
		
			
		private function user_view($postID)
		{
			$ip_address = $this->input->ip_address();
			$date = date("Y-m-d");
			$result = $this->Mod_blog->view_article($postID,$ip_address,$date);
			
			if($result->num_rows() == 0)
			{			
				$this->Mod_blog->insert_view($postID,$ip_address,$date);
				$this->Mod_blog->update_view($postID);	
			}
			else
			{
				$this->Mod_blog->delete_view($date);				
			}		
		}
		
	}