<?php

/* 
 * Author       : Jai K
 * Purpose      : The controller for Form Download Master
 * Created On   : 2016-12-28 18:34
 */

class Form_Download extends REF_Controller {
    public $controller;
    
    function __construct() {
        parent::__construct();
        
        $segments = $this->uri->segments;
        $this->controller = $segments[1];
        
        
        $this->load->model('form_download_model');
    }
    
    function index( $id='' ) {
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Form(s)',
            'controller'     => $this->controller,
            'actions'        => $actions,
            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
            
            'results'       => $this->form_download_model->get( TBL_FORM_DOWNLOAD,'', array('order_by' => 'updated_on DESC') ),
        );
        
        
        $data['add_btn']    = 'Upload Form';
        $data['view_link']  = $actions['view'] . '/';
        $data['add_link']   = $actions['add'] . '/'.$id;
        $data['edit_link']  = $actions['edit'] . '/';
        $data['delete_link']= $actions['delete'] . '/';
        
        // Load Views
        $this->render( $data, 'form-download/index' );
    }
    
    function add( $id='' ) {
        $this->manipulate( $id );
    }
    
    function edit( $id ) {
        $this->manipulate( $id, array( 'page' => 'edit' ) );
    }
    
    function view( $id ) {
        $this->manipulate( $id, array( 'page' => 'view' ) );
    }
    
    function export( $page = 'pdf', $id ) {
        $this->manipulate( $id, array( 'page' => $page ) );
    }
    
    function manipulate( $id, $args = array() ) {
        $actions = $this->actions();
        $post = $this->input->post();
        #echo '<pre>'; print_r($post);die;
        $session_data = $this->session->userdata(TBL_PREFIX . 'user_logged_in');
        $token = $this->session->userdata( 'rtoken-'.$session_data['user_id'] );
        
        $page = $page_file_name = !empty( $args['page'] ) ? $args['page'] : 'add';
        $page_file_name         = $page == 'pdf' ? 'view' : $page_file_name;
        
        $data = array(
            'id'                => $id,
            'title'             => 'Form',
            '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'),
        );
        
        $data['token'] = $this->auto_generation_code( rand( 999, 9999 ) );
        $this->session->set_userdata( 'rtoken-'.$session_data['user_id'], $data['token'] );
        
        
        if( !empty( $post['submit'] ) && $post['token'] == $token ) :
            // insert
            $insert_id = $this->form_download_model->manipulate($id);
            $msg = 'Form is uploaded successfully.';
            
            $this->session->set_flashdata('success', $msg);

            if($page == 'add') :
                redirect( $this->controller . '/add/' );
            else :
                // Redirect
                redirect( $this->controller . '/index/' );
            endif;

        endif;
        
        if( !empty( $id ) ) :
            $data['results'] = $this->form_download_model->get( TBL_FORM_DOWNLOAD, array('id' => $id) );
            $data['results'] = $data['results'][0];
        endif;

         //echo "<pre>Data: "; print_r( $data ); die;
        // Load Views
        $this->render( $data, 'form-download/'.$page_file_name );
    }
    
    function delete( $id ) {
        
        $this->form_download_model->delete( $id );
        
        // Redirect
        $this->session->set_flashdata('success', 'The record is deleted successfully');
        redirect( $this->controller . '/index/' );
        
    }
    
    function ajax() {
        $results =  $this->form_download_model->ajax();
        
        echo json_encode( $results );
    }
    
    
    
    
    
} // class    

