<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Statue_category extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('statue_category_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->statue_category_model->list_all();
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        $actions = $this->actions();
        $data['add_btn']    = 'Create New Category';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'];
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        $service_menu_id = get_statue_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '5');
        $this->render($data, 'statue_books/category/list');
    }
    
    private function get_category_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->statue_category_model->category_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_category_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $last_id = $this->statue_category_model->get_last_insert_id();
        if(count($last_id) > 0) {
            $last_code = $last_id->code;
            $last_no = (int)$last_code;
            $data['code'] = (($last_no + 1)  > 9999) ? ($last_no + 1)  : sprintf('0%03d', ($last_no + 1) );
        } else
            $data['code'] = '0001';
        
        $this->render($data, 'statue_books/category/add');
    }
    
    public function insert() {
        $insert = array(
            'code' => $this->input->post('code'),
            'name' => $this->input->post('category_name'),
            'url_key' => $this->get_category_url_key($this->input->post('category_name')),
            #'status' => $this->input->post('status'),
            'is_statue_cat' => 1,
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->statue_category_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Category added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Category not added!');
        redirect('statue_category', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['category'] = $this->statue_category_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'statue_books/category/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array(
            'code' => $this->input->post('code'),
            'name' => $this->input->post('category_name'),
            'url_key' => $this->get_category_url_key($this->input->post('category_name')),
            #'status' => $this->input->post('status'),
            'is_statue_cat' => 1,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        
        if ( $this->statue_category_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Category updated successfully');  
       
        else
            $this->session->set_flashdata('error', 'Category not updated!');    
        
        redirect('statue_category','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['category'] = $this->statue_category_model->get_by_id($id);
        $this->render($data, 'statue_books/category/view');
    }
    
    public function delete($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $update = array ( 
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->statue_category_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Category deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Category not deleted!');
        
        redirect( 'statue_category', 'refresh');
    }
    
    public function category_name_exists() {
        $data =  $this->statue_category_model->category_name_exists($_POST['category_name'], $_POST['category_id']);
        echo json_encode($data);
        exit(0);
    }
}
?>