<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class God_name extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('god_name_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->god_name_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 God Name';
        $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, 'god_name/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'god_name/add');
    }
    
    private function get_god_url_key($god_name, $id = '') {
        $url_key = get_url_key($god_name);
        if ($this->god_name_model->god_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_god_url_key($god_name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $insert = array(
            'name' => $this->input->post('god_name'),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_god_name') ),
            'price' => $this->input->post('price'),
            'url_key' => $this->get_god_url_key($this->input->post('god_name')),
            '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['icon']['name'])) {
            $config = array();
            $config['upload_path'] = DIR_GOD_ICON;
            $config['allowed_types'] = implode('|', image_extensions());
            $this->upload->initialize($config);
            if (!file_exists(DIR_GOD_ICON)) {
                mkdir(DIR_GOD_ICON, 0777, true);
            }
            if ($this->upload->do_upload('icon')) {
                $upload_data = $this->upload->data();
                $insert['icon'] = $upload_data['file_name'];
            }
            else { 
                $this->session->set_flashdata('error', $this->upload->display_errors());
            }
        }
        
        $insert_id = $this->god_name_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'God Name added successfully');    
        
        else
            $this->session->set_flashdata('error', 'God Name not added!');  
        
        redirect('god_name', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['god_names'] = $this->god_name_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'god_name/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
       
        $update = array(
            'name' => $this->input->post('god_name'),
            'tamil_name' => htmlspecialchars( $this->input->post('tamil_god_name') ),
            'price' => $this->input->post('price'),
            'url_key' => $this->get_god_url_key($this->input->post('god_name')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        $god_names = $this->god_name_model->get_by_id($id);
        
        if($this->input->post('img_delete') != 1) {
            if (!empty($_FILES['icon']['name'])) {
                $config = array();
                $config['upload_path'] = DIR_GOD_ICON;
                $config['allowed_types'] = implode('|', image_extensions());
                $this->upload->initialize($config);
                if (!file_exists(DIR_GOD_ICON)) {
                    mkdir(DIR_GOD_ICON, 0777, true);
                }
                if ($this->upload->do_upload('icon')) {
                    if (!empty($god_names->icon)) {
                        if (file_exists(DIR_GOD_ICON . $god_names->icon)) {
                            unlink(DIR_GOD_ICON . $god_names->icon);
                        }
                    }
                    $upload_data = $this->upload->data();
                    $update['icon'] = $upload_data['file_name'];
                }
                else { 
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                }
            }
        }
        else {
            $update['icon'] = '';
                if (!empty($god_names->icon)) {
                    if (file_exists(DIR_GOD_ICON . $god_names->icon)) {
                        unlink(DIR_GOD_ICON . $god_names->icon);
                    }
                }
        }
        
        if ($this->god_name_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'God Name updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'God Name not updated!');    
        
        redirect('god_name','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['god_names'] = $this->god_name_model->get_by_id($id);
        $this->render($data, 'god_name/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->god_name_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'God Name deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'God Name not deleted!');
        
        redirect( 'god_name', 'refresh');
    }
    
    public function god_name_exists() {
        $data =  $this->god_name_model->god_name_exists($_POST['id'],$_POST['name']);
        echo json_encode($data);
        exit(0);
    }
}
?>