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

	class Category extends Admin_Controller
	{
		
		function __construct()
		{
			parent::__construct();
			$this->load->helper('admin_function');
			$this->load->model("mod_category");
		}
	
		function index()
		{
			$tab['activeTab'] = "category";
			$data["category"] = $this->mod_category->getcategory();
			$data["category"] = $data["category"]->result_array(); 
			
			$this->load->view("header",$tab);
			$this->load->view("category/view",$data);
			$this->load->view("footer");	
		}	
		
		
		function add()
		{
			$tab['activeTab'] = "category";
			$this->form_validation->set_rules('title', 'Title', 'trim|required');
			if ($this->form_validation->run() == TRUE)
			{
				//Altering Database
				$data = array(
							'catTitle'=>$this->input->post('title'),
							'catSlug'=>slug($this->input->post('title')),
							'catStatus'=>$this->input->post('status'),
							'createdby'=>$this->session->userdata('userId'),
							'createddate'=>date('Y-m-d H:i:s'),
				);
				$msg = $this->mod_category->add($data);	
				//End Altering Database
				if($msg == 1 || $msg == 0){
					$this->session->set_flashdata('message',"New Category is Successfully Added");
					redirect('admin/category/add');
						
				  }else{
					$this->session->set_flashdata('message',"New Category is Failure Added!");
					redirect('admin/category/add');
						
				}
			}
			
			$this->load->view("header",$tab);
			$this->load->view("category/add");
			$this->load->view("footer");	
		}
		
		
		function edit()
		{	
			$tab['activeTab'] = "category";
			
			$id = $this->uri->segment(4);
			$category = $this->mod_category->getById($id);
			if(!$category)
			{
				redirect("admin/category");
			}
			
			if(@$_POST['update_category'])
			{
				$this->form_validation->set_rules('title', 'Title', 'trim|required');
				if ($this->form_validation->run() == TRUE)
				{
					$postdata = array(
							'catTitle'=>$this->input->post('title'),
							'catSlug'=>slug($this->input->post('title')),
							'catStatus'=>$this->input->post('status'),
							'createdby'=>$this->session->userdata('userId'),
							'createddate'=>date('Y-m-d H:i:s'),
					);
					
					$msg=$this->mod_category->update($postdata,$id);
					$this->session->set_flashdata('message',"Category is Successfully Update");
					redirect("admin/category");
					
				}else{
				
				$this->session->set_flashdata('errors', validation_errors());
				redirect('/admin/category/edit/'.$id.'', 'refresh');
					
			}
				
				
			}
			
			$data['category'] = $category;
			$this->load->view("header",$tab);
			$this->load->view("category/edit",$data);
			$this->load->view("footer");		
		}
		
		
		function delete()
		{
			$id = $this->uri->segment(4);
			$this->mod_category->delete($id);
			$this->session->set_flashdata('message',"Category deleted successfully");
			redirect("admin/category");	
		}
			
	}


