<?php
class ModelUserUserGroupPermission extends Model {
	public function editUserGroupPermission($user_group_id, $data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "user_group_permission 
		  SET `user_group_id` = " . $this->db->escape($user_group_id) . ", 
		  `permission` = '" . $this->db->escape($data['permission']) . "', 
		  `access` = " . $this->db->escape($data['access']) . ", 
		  `modify` = " . $this->db->escape($data['modify']) . " 
		  ON DUPLICATE KEY UPDATE `access` = " . $this->db->escape($data['access']) . ", `modify` = " . $this->db->escape($data['modify']));
	}

	public function editUserGroupPermissionMulti($user_group_id, $data) {
	  $valuesSQL = '';
	  foreach ($data as $value){
	    $valuesSQL .= "(" . $this->db->escape($user_group_id) . ", '". $this->db->escape($value['permission']) . "'," . $this->db->escape($value['access']) . "," . $this->db->escape($value['modify']) . "),";
    }
    $valuesSQL = substr($valuesSQL, 0, -1);

    $sql = "INSERT INTO " . DB_PREFIX . "user_group_permission (`user_group_id`, `permission`, `access`, `modify`)
      VALUES " . $valuesSQL
      . " ON DUPLICATE KEY UPDATE `access` = VALUES(`access`), `modify` = VALUES(`modify`)";

    $this->db->query($sql);
	}
	
	public function getUserGroupPermission($user_group_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user_group_permission ugp
      WHERE ugp.user_group_id = '" . (int)$user_group_id . "'");

		if ($query->rows > 0) {
      return $query->rows;
    } else {
		  return false;
    }
	}

}
