<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
/**
	 * 
	 */
	class Add_category_model extends CI_Model
	{
		
		function __construct()
		{
			parent::__construct();
		}

		function db_insert()
		{
			$name = $this->input->post('name');
			$title = $this->input->post('title');
			$des = $this->input->post('des');
			$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($name)));
				$sure = $this->db->where('slug', $slug);
				$sure = $this->db->get('category');
				if($sure->num_rows() > 0){
					return 3;
				}else{
					$data =  array(
						'category_name' =>$name,
						'slug' =>$slug,
						'cat_title' =>$title,
						'cat_dis' =>$des
					);

					$q = $this->db->insert('category', $data);
					if($q){
						return 1;
					}else{
						return 2;
					}

				}
		}

		
		function get_all(){
			$q = $this->db->get('category');
			if($q->num_rows() > 0){
				// return $q->num_rows();
				return $q->result();
			}else{
				return 0;
			}
		}

			function select($id)
			{
				$data = $this->db->where('id', $id);
				$data = $this->db->get('category');
				return $data->result();
			}

			function add_to_index($id)
			{
				$st = 1;
				$data = array(
						'status'=>$st
				);
				$qu = $this->db->where('id', $id);
				$qu = $this->db->update('category',$data);
				if($qu){
					return 1;
				}else{

				return 0;
				}
			}

			function remove_from_index($id)
			{
				$st = 0;
				$arr = array(
						'status'=>$st
				);
				$data = $this->db->where('id', $id);
				$data = $this->db->update('category',$arr);
				if($data){
					return 1;
				}else{

				return 0;
				}
			}

			function update()
			{
				$name = $_POST['name'];
				$title = $_POST['title'];
				$des = $_POST['des'];
				$id = $_POST['id'];
				$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($name)));
				$sure = $this->db->where('slug', $slug);
				$sure = $this->db->get('category');
				if($sure->num_rows() > 0){
					$data =  array(
						'category_name' =>$name,
						'cat_title' =>$title,
						'cat_dis' =>$des

					);
						$up = $this->db->where('id', $id);
						$up = $this->db->update('category', $data);
						if($up){
							return 1;
						}else{
							return 2;
						}

				}else{
					$data =  array(
						'category_name' =>$name,
						'slug'=> $slug,
						'cat_title' =>$title,
						'cat_dis' =>$des

					);
						$up = $this->db->where('id', $id);
						$up = $this->db->update('category', $data);
						if($up){
							return 3;
						}else{
							return 0;
						}
				}
			}


				function delete($id)
				{
					$data = $this->db->where('id', $id);
					$data = $this->db->delete('category');
					if($data){
						return 1;
					}else{
						return 0;
					}
				}


	}	

?>