<?php

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

class SMS_Types_Model extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }
    
    function get_all($id = '') {
        $where = array(
            'sms.status !='   => 2,
        );
        
        if( !empty($id) ) :
            $where['sms.id'] = $id;
        endif;
        
        $q = $this->db->get_where( TBL_SMS_TYPES . ' AS sms', $where );
        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];
        
        if( !empty($post['name']) ) :
        
            $args = array(
                'sms_type_name'     => $post['name'],
                'sms_type_slug'     => get_var_name( $post['name'] ),
                'status'            => $post['status'],
            );

            if( $is_add ) :
                $args['created_on'] = $date_now;
                $args['created_by'] = $session_data['user_id'];

                $this->db->insert( TBL_SMS_TYPES, $args );
            else :
                // Update

                $args['updated_on'] = $date_now;
                $args['updated_by'] = $session_data['user_id'];

                $this->db->update( TBL_SMS_TYPES, $args, array('id' => $db_id) );
            endif;
        
        endif;
    }
    
    function delete($id) {
        $args = array(
            'status'    => 2,
        );
        
        $this->db->update( TBL_SMS_TYPES, $args, array('id' => $id) );
    }
    
}    
