<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Devotee extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('devotee_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();
        $data['results'] = $this->devotee_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 Devotee 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, 'devotee/list');
    }
    
   /*  public function add() {
        $data = array();
        $actions = $this->actions();
        $data['form_action'] = $actions['insert'];
        $data['form_action'] = $actions['insert'];
        $this->render($data, 'devotee/add');
    } 
    
    private function get_devotee_url_key($name, $id = '') {
        $url_key = get_url_key($name);
        if ($this->devotee_model->devotee_url_key_exists($url_key, $id)) {
            $this->uri_key_index ++;
            return $this->get_devotee_url_key($name . '_' . $this->uri_key_index, $id);
        }
        
        return $url_key;
    }
    
    public function insert() {
        $insert = array(
            'name' => $this->input->post('devotee_name'),
            'tamil_name' => $this->input->post('tamil_devotee_name'),
            'url_key' => $this->get_devotee_url_key($this->input->post('devotee_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->devotee_model->insert($insert);  
        if ($insert_id > 0)
            $this->session->set_flashdata('success', 'Devotee Name added successfully');    
        
        else
            $this->session->set_flashdata('error', 'Devotee Name not added!');  
        
        redirect('devotee', 'refresh');
    }
    
    public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['devotee'] = $this->devotee_model->get_by_id($id);
        $actions = $this->actions();
        $data['form_action'] = $actions['update'] . '/' . $id;
        $this->render($data, 'devotee/edit');
    }
    
    public function update($id = '') {
        if (empty($id)) {
            show_400_error();
        }
       
        $update = array(
            'name' => $this->input->post('devotee_name'),
            'tamil_name' => $this->input->post('tamil_devotee_name'),
            'url_key' => $this->get_devotee_url_key($this->input->post('devotee_name')),
            'status' => $this->input->post('status'),
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->devotee_model->update($update, $id) > 0) {
            $this->session->set_flashdata('success', 'Devotee updated successfully');
        }        
        else
            $this->session->set_flashdata('error', 'Devotee not updated!');    
        
        redirect('devotee','refresh');
    }*/
    
    public function update($id = '',$status) {
        if (empty($id)) {
            show_400_error();
        }
       
        $update = array(
            'status' => $status,
            'updated_by' => $this->user_data['user_id'],
            'updated_on' => date('Y-m-d H:i:s')
        );
        
        if ($this->devotee_model->update($update, $id) > 0) {
            $this->session->set_flashdata('success', 'Devotee updated successfully');
        }        
        else
            $this->session->set_flashdata('error', 'Devotee not updated!');    
        
        redirect('devotee','refresh');
    }
    
    public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }
        
        $data = array();
        $data['devotee'] = $this->devotee_model->get_by_id($id);
        $sms_types = explode(',',$data['devotee']->bd_sms_types);
        $data['sms'] = $this->devotee_model->get_sms_types($sms_types);
        $data['members'] = $this->devotee_model->get_members_by_devoteee_id($id);
        $this->render($data, 'devotee/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->devotee_model->update($update,$id) > 0){
            $this->session->set_flashdata('success', 'Devotee Name deleted successfully');
        }            
        else 
            $this->session->set_flashdata('error', 'Devotee Name not deleted!');
        
        redirect( 'devotee', 'refresh');
    }
    
    public function devotee_name_exists() {
        $data =  $this->devotee_model->devotee_name_exists($_POST['id'],$_POST['name']);
        echo json_encode($data);
        exit(0);
    }
}
?>