<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Madappalli extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('madappalli_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->madappalli_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 Prasadam';
        $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, 'madappalli/list');
    }
    
    public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $data['units'] = $this->madappalli_model->unit_list();
        $this->render($data, 'madappalli/add');
    }
    
    private function get_madappalli_url_key($prasadam_name, $id = '') {
        $url_key = get_url_key($prasadam_name);
        if ($this->madappalli_model->prasadam_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_madappalli_url_key($prasadam_name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $insert = array(
            'prasadam_name' => $this->input->post('prasadam_name'),
            'prasadam_tamil_name' => htmlspecialchars( $this->input->post('tamil_prasadam_name') ),
            'unit' => $this->input->post('unit'),
            'price' => $this->input->post('price'),
            'url_key' => $this->get_madappalli_url_key($this->input->post('prasadam_name')),
            'status' => $this->input->post('status'),
            'created_by' => $this->user_data['user_id'],
            'created_on' => date('Y-m-d H:i:s')
        );
        
        $insert_id = $this->madappalli_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Prasadam added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Prasadam not added!');  
        
        redirect('madappalli', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['units'] = $this->madappalli_model->unit_list();
        $data['madappalli'] = $this->madappalli_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'madappalli/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
       
        $update = array(
            'prasadam_name' => $this->input->post('prasadam_name'),
            'prasadam_tamil_name' => htmlspecialchars( $this->input->post('tamil_prasadam_name') ),
            'unit' => $this->input->post('unit'),
            'price' => $this->input->post('price'),
            'url_key' => $this->get_madappalli_url_key($this->input->post('prasadam_name')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->madappalli_model->update($update, $id) > 0)
            $this->session->set_flashdata('success', 'Prasadam updated successfully');  
        
        else
            $this->session->set_flashdata('error', 'Prasadam not updated!');    
        
        redirect('madappalli','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['madappalli'] = $this->madappalli_model->get_by_id($id);
        $this->render($data, 'madappalli/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->madappalli_model->update($update,$id) > 0)
            $this->session->set_flashdata('success', 'Prasadam deleted successfully');
            
        else 
            $this->session->set_flashdata('error', 'Prasadam not deleted!');
        
        redirect( 'madappalli', 'refresh');
    }
    
    public function prasadam_name_exists() {
        $data =  $this->madappalli_model->prasadam_name_exists($_POST['id'],$_POST['name']);
        echo json_encode($data);
        exit(0);
    }
}
?>