<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_glassware extends CI_Model {

    protected $idGroup = null;
    protected $iduser = null;

    public function __construct() {
        parent::__construct();
        $this->idGroup = $this->session->userdata("r_idGroup");
        $this->idUser = $userId = $this->session->userdata('r_userId');
    }

    function get_data(){
      $param = array();
      $query = "SELECT g.* FROM glassware g WHERE g.is_active = '1'";

      $result = $this->db->query($query, $param)->result_array();

      return $result;
    }

    function get_detail($id){
      $param = array();
      $param[] = $id;
      $query = "SELECT g.* FROM glassware g WHERE g.is_active = '1' AND g.id = ?";

      $result = $this->db->query($query, $param)->row_array();

      return $result;
    }

    function set_data($data, $data_keys, $id = null){
      $this->db->trans_begin();
      $param = array();
      for($i=0;$i<count($data_keys);$i++){
          $param[] = $data[$data_keys[$i]];
      }

      if(is_null($id)){
        $param_keys = "(".$data_keys[0];
        $keys = "(?";
        for($i=1;$i<count($data_keys);$i++){
          $param_keys .= ", ".$data_keys[$i];
          $keys .= ",?";
        }
        $keys .=")";
        $param_keys .= ")";

        $query = "INSERT INTO glassware ".$param_keys." VALUES ". $keys;
      }else{
        $param_keys = "SET ".$data_keys[0].'=?';
        for($i=1;$i<count($data_keys);$i++){
          $param_keys .= ", ".$data_keys[$i].'=?';
        }

        $query = "UPDATE glassware ".$param_keys. " WHERE id = ? AND is_active = '1'";
        $param[] = $id;

      }
      $this->db->query($query, $param);

      if($this->db->trans_status() == false){
        $this->db->trans_rollback();
        return false;
      }else{
        $this->db->trans_commit();
        return true;
      }
    }

}
