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

class Categories extends CI_Controller {

	function __construct(){
		//session_start();
		parent::__construct();
		if(!isset($_SESSION['logged'])){
			redirect('admin');
		}
	}
	public function index()
	{
		$this->load->model('categories_model');
		
		if(isset($_GET['cid']) && $_GET['cid']> 0){
			$selected_cat=$_GET['cid'];
		}else{
			$selected_cat=0;
		}
		$parent_categories=$this->categories_model->getParentcategories();
		$categories=$this->categories_model->getcategories($selected_cat);
		$data['selected_cat']=$selected_cat;
		$data['categories']=$categories;
		$data['parent_categories']=$parent_categories;
		$data['main_content']='admin/categories/index';
		$data['title']='Manage Categories :: Website Control panel';
		$data['bodyid']='categories';
		$this->load->view('admintemplate',$data);
	}
	public function add()
	{
		if(isset($_POST) && !empty($_POST)){
			$name=$this->input->post('cname');
			$order=$this->input->post('corder');
			$parent_id=$this->input->post('cparent');
			$description=$this->input->post('cdesc');
			$id=$this->input->post('cid');
			$action=$this->input->post('action');
			$data = array(
			   'name' => $name ,
			   'order' => $order ,
			   'parent_id' => $parent_id,
			   'description' => $description
			   
			);
			if($action=='new'){
				$result=$this->db->insert('categories', $data); 
			}else{
				$result=$this->db->update('categories', $data, array('id' => $id));
			}
			

			if($result){

			redirect('admin/categories');
			}
		}
	}
  public function delete($id){
    
    $this->db->delete('categories',array('id'=>$id));
      redirect('admin/categories');
    
  }
}

?>
