<?php
require_once (PATH_TABLES . "admin/UserGroupTable.php") ;
require_once (PATH_MODELS . "admin/UserRight.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class UserGroupClass extends MasterBase {
	
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= UserGroupTable::C_TABLE ;
		$this->fldid 	= UserGroupTable::C_ID ;
		$this->flddesc 	= UserGroupTable::C_NAME ;
        $this->fldcode 	= UserGroupTable::C_CODE ;
		$this->fldorg 	= UserGroupTable::C_RIGHTS ;
	}
	function __destruct() {
	}
    function getAllOption() {
        $sql = "SELECT * FROM " . $this->tbl;
        
        $rows = $this->db->getTable($sql);

        if (is_null($rows)||count($rows) == 0)
            return null;
        else
            return $rows;
    }
	function getUserGroupByCode($usergrpCode) {
		$sql = "SELECT * FROM " . $this->tbl. " ";
		$sql .= "WHERE " . $this->fldcode . " = '" . $usergrpCode . "' ";

		$rows = $this->db->getTable($sql);

		if (is_null($rows)||count($rows) == 0)
			return null;
		else
			return $rows[0];
	}
	function getUserGroupList( $namefilter = '', $statusfilter = 'all', $orderby = '') {
		$sql = "SELECT * FROM " . $this->tbl . " ";
		$sql .= "WHERE " . $this->flddesc . " LIKE '%" . $namefilter . "%' ";

		 // if($statusfilter == 'active'){
        //     $sql .= "AND " . CustomerTable::C_STATUS . " = 1 ";
        // } else if($statusfilter == 'non-active'){
        //     $sql .= "AND " . CustomerTable::C_STATUS . " = 0 ";
        // }

        if($orderby != "")
            $sql .= " ORDER BY " . $orderby;

        $rows = $this->db->getTable($sql);

        return $rows;
	}

	function getUserGroupListSuggest() {
		$sql = "select distinct ".$this->flddesc." from " . $this->tbl." ";

		$rows = $this->db->getTable($sql);
		if (is_null($rows)||count($rows) == 0)
			return null;
		else
			return $rows[0];
	}
	function getUserGroupByUserGroupId($usergrpId) {
        $sql = "SELECT * FROM " . $this->tbl . " ";
        $sql .= "WHERE " . UserGroupTable::C_ID . " = " . $usergrpId . " ";

        $rows = $this->db->getTable($sql);

        if (is_null($rows) || count($rows) == 0)
            return 0;
        else
            return $rows[0][UserGroupTable::C_ID];
    }
	function getUserGroupId($id) {
        $sql = "SELECT * FROM " . $this->tbl . " ";
        if($id != "")
            $sql .= "WHERE " . $this->fldid . " = " . $id . " ";

        $rows = $this->db->getTable($sql);

        if (is_null($rows) || count($rows) == 0)
            return 0;
        else
            return $rows[0][UserGroupTable::C_ID];
    }

    function getUserGroupSuggest() {
        $sql = "select distinct (" . $this->fldcode . "), " . $this->flddesc . " from " . $this->tbl . " ";

        $rows = $this->db->getTable($sql);

        if (is_null($rows) || count($rows) == 0)
            return null;
        else
            return $rows;
    }

    public function getExistingRecords($user_group_code, $user_group_id=0) {
        $sql  = "SELECT * FROM " . $this->tbl .
            " WHERE " . $this->fldcode . " = '" . $user_group_code . "' ";

        if($user_group_id != 0) {
            $sql  .= " AND " . $this->fldid . " <> " . $user_group_id . " ";
        }

        $rows = $this->db->getTable($sql);

        if (is_null($rows) || count($rows) == 0) {
            return null;
        } else {
            return $rows;
        }
    }
}
?>