<?php

/* 
 * Author       : Jai K
 * Purpose      : The controller for SMS Types
 * Created On   : 2016-08-22 12:24    
 */

class SMS_Types extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        $this->load->model('sms_types_model');
    }
    
    function index() {
        $actions = $this->actions();
        
        $data = array(
            'add_btn'       => 'New SMS Type',
            'add_link'      => $actions['add'].'/',
            'edit_link'     => $actions['edit'].'/',
            'delete_link'   => $actions['delete'].'/',
            
            'results'       => $this->sms_types_model->get_all(),
            'success'       =>  $this->session->flashdata('success'),
            'error'         =>  $this->session->flashdata('error'),
        );
        
        $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, 'sms_types/index' );
        
    }
    
    function add() {
        $this->manipulate();
    }
    
    
    function edit($id) {
        $this->manipulate($id, array('page' => 'edit'));
    }
    
    function manipulate( $id, $args = array() ) {
        $actions = $this->actions();
        $post = $this->input->post();
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $page = !empty( $args['page'] ) ? $args['page'] : 'add';
        
        
        $data = array(
            'id'                => $id,
            'title'             => 'SMS Type',
            'is_'.$page         => TRUE,
            'controller'        => $this->controller,
            'actions'           => $actions,
            'form_action'       => base_url() . $this->controller .'/'.$page.'/'.$id,
            'success'           => $this->session->flashdata('success'),
            'error'             => $this->session->flashdata('error'),
        );
        
        if( $page != 'add' ) :
            
            $data['results'] = $this->sms_types_model->get_all( $id );
        endif;
        
        if( $page == 'add' || $page == 'edit' ) :
            $data['token'] = $this->auto_generation_code( rand( 999, 9999 ) );
            $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
        endif;
        
        if( !empty( $post['submit'] ) && $post['token'] == $token ) :
            // insert
            $insert_id = $this->sms_types_model->manipulate();

            if( $page == 'add' ) :
                $msg = 'SMS Type is added successfully.';
            elseif( $page == 'edit' ) :
                $msg = 'SMS Type is updated successfully.';
            endif;
            $this->session->set_flashdata('success', $msg);


            // Redirect
            redirect( $this->controller );
        endif;

        // Load Views
        $this->render( $data, 'sms_types/'.$page );
        
    }
    
    function delete($id) {
        $this->sms_types_model->delete($id);
        
        $this->session->set_flashdata('success', 'Record is deleted successfully');
                
        redirect( $this->controller );
    }
}

?>
