<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Category extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('category_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->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_master_controller_id($actions['controller']);
        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '1');
        $this->render($data, 'category/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $data['seq_no'] = $this->category_model->active_category_count() + 1;
        $this->render($data, 'category/add');
    }
    
    private function get_category_url_key($category_name, $id = '') {
        $url_key = get_url_key($category_name);
        if ($this->category_model->category_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_category_url_key($category_name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $seq_no = $this->category_model->active_category_count() + 1;
        $seq_no = (!empty($this->input->post('category_order'))) ? $this->input->post('category_order') : $seq_no;
        $insert = array(
            'category_name' => $this->input->post('category_name'),
            'cat_slug'  => get_var_name($this->input->post('category_name')),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_category_name') ),
            'category_order'=> $seq_no,
            'grouping' => $this->input->post('grouping'),
            'is_separate' => $this->input->post('is_sepr'),
            'color_top' => $this->input->post('color_top'),
            'color_bottom' => $this->input->post('color_bottom'),
            'url_key' => $this->get_category_url_key($this->input->post('category_name')),
            'cat_prefix' => $this->input->post('cat_prefix'),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $image_name = '';
        if (!empty($_FILES['image']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_CATEGORY_IMAGE;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_CATEGORY_IMAGE)) {
                mkdir(DIR_CATEGORY_IMAGE, 0777, true);
            }
            if ($this->upload->do_upload('image')) {
                $upload_data = $this->upload->data();
                $insert['image'] = $upload_data['file_name'];
            }
            else { 
                $this->session->set_flashdata('error', $this->upload->display_errors());
            }
        }
        
        $insert_id = $this->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('category', 'refresh');
    }
    
    public function edit($category_id = '') {
        if (empty($category_id)) {
            show_400_error();
        }
        
        $data = array();
        $data['category'] = $this->category_model->get_category_by_id($category_id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $category_id;
        $this->render($data, 'category/edit');
    }
    
    public function update($category_id = '') {
        if (empty($category_id)) {
            show_400_error();
        }
        
        $seq_no = $this->category_model->active_category_count() + 1;
        $seq_no = (!empty($this->input->post('category_order'))) ? $this->input->post('category_order') : $seq_no;
        $update = array(
            'category_name' => $this->input->post('category_name'),
            'cat_slug'  => get_var_name($this->input->post('category_name')),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_category_name') ),
            'category_order'=> $seq_no,
            'grouping' => $this->input->post('grouping'),
            'is_separate' => $this->input->post('is_sepr'),
            'color_top' => $this->input->post('color_top'),
            'color_bottom' => $this->input->post('color_bottom'),
            'cat_prefix' => $this->input->post('cat_prefix'),
            'url_key' => $this->get_category_url_key($this->input->post('category_name')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        $category = $this->category_model->get_category_by_id($category_id);
        
        if($this->input->post('img_delete') != 1) {
            if (!empty($_FILES['image']['name'])) {
                $config = array();
                $config['upload_path'] = DIR_CATEGORY_IMAGE;
                $config['allowed_types'] = implode('|', image_extensions());
                $this->upload->initialize($config);
                if (!file_exists(DIR_CATEGORY_IMAGE)) {
                    mkdir(DIR_CATEGORY_IMAGE, 0777, true);
                }
                if ($this->upload->do_upload('image')) {
                    if (!empty($category->image)) {
                        if (file_exists(DIR_CATEGORY_IMAGE . $category->image)) {
                            unlink(DIR_CATEGORY_IMAGE . $category->image);
                        }
                    }
                    $upload_data = $this->upload->data();
                    $update['image'] = $upload_data['file_name'];
                }
                else { 
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                }
            }
        }
        else {
            $update['image'] = '';
                if (!empty($category->image)) {
                    if (file_exists(DIR_CATEGORY_IMAGE . $category->image)) {
                        unlink(DIR_CATEGORY_IMAGE . $category->image);
                    }
                }
        }
        if ($this->category_model->update($update, $category_id) > 0)
            $this->session->set_flashdata('success', 'Category updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'Category not updated!');    
        
        redirect('category','refresh');
    }
    
    public function view($category_id = '') {
        if (empty($category_id)) {
            show_400_error();
        }
        
        $data = array();
        $data['category'] = $this->category_model->get_category_by_id($category_id);
        $this->render($data, 'category/view');
    }
    
    public function delete($category_id = '') {
        if (empty($category_id)) {
            show_400_error();
        }
        
        $update = array ( 
            'status' => 2,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        $category = $this->category_model->get_category_by_id($category_id);
        if ($this->category_model->update($update,$category_id) > 0) {
            $this->session->set_flashdata('success', 'Category deleted successfully');
            if (!empty($category->image)) {
                if (file_exists(DIR_CATEGORY_IMAGE . $category->image)) {
                    unlink(DIR_CATEGORY_IMAGE . $category->image);
                }
            }
        }
        else 
            $this->session->set_flashdata('error', 'Category not deleted!');
        
        redirect( 'category', 'refresh');
    }
    
    public function category_name_exists() {
        $data =  $this->category_model->category_name_exists($_POST['category_name'], $_POST['category_id']);
        echo json_encode($data);
        exit(0);
    }
}
?>