<?php

/* 
 * Author       : Jai K
 * Purpose      : Model for Form Download
 * Created On   : 2016-12-28 18:40
 */

class Form_Download_Model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        
        
        
    }
    
    function get( $tbl_name, $where = array(), $args = array() ) {
        if ( ! is_array($where)) $where = array();
        $where['status'] = !empty( $where['status'] ) ? $where['status'] : 1;
        if( !empty( $args['order_by'] ) ) :
            $this->db->order_by( $args['order_by'] );
        endif;
        
        $q = $this->db->get_where( $tbl_name, $where );
        //echo "Query: ".$this->db->last_query(); die;
        return $q->result_array();
            
    }
    
    function manipulate(){
        $post   = $this->input->post();
        $date_now = date("Y-m-d H:i:s");
        $session_data = $this->session->userdata( TBL_PREFIX . 'user_logged_in' );
        $segments = $this->uri->segments;
        $page   = $segments[2];
        $is_add = $page == 'add' || $page == 'insert' ? TRUE : FALSE;
        $db_id  = $is_add ? '' : $segments[3];
        
        $args = array(
            'frm_name'  => $post['name'],
            'frm_slug'  => get_var_name( $post['name'] ),
            'status'    => $post['status'],
            'updated_on'=> $date_now,
            'updated_by'=> $session_data['user_id'],
        );
        
        $hid_name = 'file_remove';
        if( !empty( $_FILES['file']['name'] ) ) :
            // Upload
            $hid_name = 'hidden_file';
            
            $this->load->library('upload');
            
            if( !file_exists( DIR_FORM_DOWNLOAD ) ) :
                mkdir( DIR_FORM_DOWNLOAD, 0777, TRUE );
                chmod( DIR_FORM_DOWNLOAD, 0777);
            endif;
            
            $file_name = $args['frm_slug'].'-'.time();
            $config = array(
                'upload_path'   => DIR_FORM_DOWNLOAD,
                'file_name'     => $file_name,
                'allowed_types' => '*',
            );
            
            $this->upload->initialize($config);
            $this->upload->do_upload('file');
            $uploaded_data = $this->upload->data();
            $file_name .= $uploaded_data['file_ext'];
            
            $args['frm_file'] = $file_name;
        elseif( !empty( $post[$hid_name] ) ) :
            $args['frm_file'] = null;    
        endif;
        
        if( !empty( $post[$hid_name] ) ) :
            // Unlink Prev. file
            foreach( $post[$hid_name] as $k => $file_name ) :
                unlink( $file_name );
            endforeach;
        endif;
        
        
        if( !empty( $db_id ) ) :
            // Update
            
            $this->db->update( TBL_FORM_DOWNLOAD, $args, array('id' => $db_id) );
        else :
            // Insert
            
            $args['created_on'] = $date_now;
            $args['created_by'] = $session_data['user_id'];
            
            $this->db->insert( TBL_FORM_DOWNLOAD, $args );
            $db_id = $this->db->insert_id();
        endif;
        
        return $db_id;
    }
    
    function delete( $id ) {
        $date_now = date("Y-m-d H:i:s");
        $session_data = $this->session->userdata( TBL_PREFIX . 'user_logged_in' );
        
        $args = array(
            'status'    => '2',
            'updated_on'=> $date_now,
            'updated_by'=> $session_data['user_id'],
            
        );
        
        $this->db->update( TBL_FORM_DOWNLOAD, $args, array('id' => $id) );
    }
    
    function ajax() {
        
        $post = $this->input->post();
        $return = array();
        
        switch( $post['act'] ) :
            case 'list' :
                $per_page = 5;//PAGINATION_LIMIT;
                $total_cnt = $post['totalCnt'];
                $page_no = $post['pageNo'];
                $where = array(
                    'status' => 1,
                );
                
                if( empty( $page_no ) || empty( $total_cnt ) ) :
                    $page_no = 1;
                
                    // Get Total
                    $this->db->select('COUNT(id) AS total_cnt');
                    $q = $this->db->get_where( TBL_FORM_DOWNLOAD, $where );
                    $total_cnt = $q->row_array()['total_cnt'];
                endif;
                
                $return['extra'] = array(
                    'totalCnt' => $total_cnt,
                    'perPage'  => $per_page, 
                );
                
                $this->db->limit( $per_page, --$page_no * $per_page );
                $this->db->order_by( 'updated_on', 'DESC' );
                $q = $this->db->get_where( TBL_FORM_DOWNLOAD, $where );
                //echo "Query: ".$this->db->last_query(); die;
                $results = $q->result_array();
                foreach( $results as $k => $result ) :
                    $result['file_url'] = DIR_FORM_DOWNLOAD . $result['frm_file'];
                    $result['prev_html'] = !empty( $result['frm_file'] ) ? get_file_preview( $result['file_url'], 'width: 45px; height: 45px; background: #fff; border-radius: 5px; padding: 5px; box-shadow: 3px 3px 3px #eee;', FALSE ) : '';
                    $result['updated_on'] = date("d M, Y h:i A", strtotime( $result['updated_on'] ));
                    
                    $return['results'][$k] =  $result;
                endforeach;
            break;    
        endswitch;
        
        return $return;
    }
    
}
