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

	class Post extends Admin_Controller
	{
		
		function __construct()
		{
			parent::__construct();
			$this->load->helper(array('string','text','admin_function'));
			$this->load->model("mod_post");
		}
	
		function index()
		{
			$tab['activeTab'] = "post";
			$data["post"] = $this->mod_post->getpost();
			$data["post"] = $data["post"]->result_array(); 
			
			$this->load->view("header",$tab);
			$this->load->view("post/view",$data);
			$this->load->view("footer");	
		}	
		
				
		function add()
		{
			$tab['activeTab'] = "post";
			$this->form_validation->set_rules('title', 'Title', 'trim|required');
			if ($this->form_validation->run() == TRUE)
			{
				//Altering Database 
				$data = array(
							'postCode' 	 => $this->checkCode(random_string('nozero', 8)),
							'postTitle'	 => $this->input->post('title'),
							'postSlug'	 => slug($this->input->post('title')),
							'postDesc'	 => $this->input->post('desc'),
							'postStatus' => $this->input->post('status'),
							'postDate'	 => date('Y-m-d H:i:s'),
							'postBy'	 => $this->session->userdata('userId'),
							'postView'	 => 0,

				);
				$msg = $this->mod_post->add($data);	
				$postID = $this->db->insert_id();
				
				//add categories
				$catID = $this->input->post('catID');
				if(is_array($catID))
				{
					foreach($_POST['catID'] as $catID)
					{
						$stmt = array(
							'postID' => $postID,
							'catID' => $catID,		
						);
						$this->mod_post->addcats($stmt);
					}
				}

				//End Altering Database
				if($msg == 1 || $msg == 0)
				{
					$this->session->set_flashdata('message',"New Post is Successfully Added");
					redirect('admin/post/add');		
				}
				else
				{
					$this->session->set_flashdata('message',"New Post is Failure Added!");
					redirect('admin/post/add');		
				}
			}
			
			$this->load->view("header",$tab);
			$this->load->view("post/add");
			$this->load->view("footer");	
		}
		
		
		function edit()
		{	
			$tab['activeTab'] = "post";
			
			$id = $this->uri->segment(4);
			$postID = $this->mod_post->getById($id);
			if(!$postID)
			{
				redirect("admin/post");
			}
			
			if(@$_POST['update_post'])
			{
				$this->form_validation->set_rules('title', 'Title', 'trim|required');
				if ($this->form_validation->run() == TRUE)
				{
					$postdata = array(
								'postCode'	 => $this->input->post('code'),
								'postTitle'	 => $this->input->post('title'),
								'postSlug'	 => slug($this->input->post('title')),
								'postDesc'	 => $this->input->post('desc'),
								'postStatus' => $this->input->post('status'),
								'postDate'	 => date('Y-m-d H:i:s'),
								'postBy'	 => $this->session->userdata('userId'),
					);
					
					$msg = $this->mod_post->update($postdata,$id);
					$this->mod_post->delcat($id);
					$this->mod_post->delsubcat($id);
					$this->mod_post->deltags($id);
					//$postID = $this->db->insert_id();
				
					//add categories
					$catID = $this->input->post('catID');
					if(is_array($catID))
					{
						foreach($_POST['catID'] as $catID)
						{
							$stmt = array(
								'postID' => $id,
								'catID' => $catID,		
							);
							$this->mod_post->addcats($stmt);
						}
					}
		
					
					if($msg == 1 || $msg == 0)
					{
						$this->session->set_flashdata('message',"Post is Successfully Update");
						redirect('admin/post');
				  	}
					else
					{
						$this->session->set_flashdata('message',"Post is Failure Update!");
						redirect('admin/post');	
					}
					
				}
				else
				{
					$this->session->set_flashdata('errors', validation_errors());
					redirect('/admin/category/edit/'.$id.'', 'refresh');
				}	
			}
			
			$data['post'] = $postID;

			$this->load->view("header",$tab);
			$this->load->view("post/edit",$data);
			$this->load->view("footer");		
		}
		
		
		function delete()
		{
			$id = $this->uri->segment(4);
			$this->mod_post->delete($id);
			$this->mod_post->delcat($id);
			//$this->mod_post->deltags($id);
			//$this->mod_post->delsubcat($id);
			$this->session->set_flashdata('message',"Post deleted successfully");
			redirect("admin/post");		
		}
		
		
		function checkCode($code)
		{
			$result = $this->mod_post->getByCode($code);
			if($result == false)
			{
				return $code.date('d');		
			}
			else
			{
				return $code;
			}
		}
			
	}


