Location: PHPKode > projects > astwebpanel > web/dao/GroupMembersDao.php
<?php
/**
 * Copyright (C) 2004 - 2006, John Tarlton.
 *
 * This file is part of AstWebPanel - A web management user interface for Asterisk.
 *
 * AstWebPanel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation. See the LICENSE file distributed
 * with AstWebPanel.
 *
 * Database access object.
 */

require_once('./php/dbase.php');

/* Database access object for group membership.
*/
class GroupMembersDao
{
	function getMemberNames($groupname, &$membernames)
	{
    	global $db;

		$membernames = $db->getCol("SELECT username FROM users 
									WHERE userid IN 
									(SELECT userid FROM groupmembers 
									 WHERE groupid = (SELECT groupid FROM groups WHERE groupname = ?) )", 
									'username', $groupname);
	}

	function getMembership($username, &$groupnames)
	{
    	global $db;

		$groupnames = $db->getCol("SELECT groupname FROM groups 
								   WHERE groupid IN 
									(SELECT groupid FROM groupmembers 
									 WHERE userid = (SELECT userid FROM users WHERE username = ?) )", 
									'groupname', $username);
	}

	function update($groupname, &$new)
	{
		$mnames = array();
		$this->getMemberNames($groupname, $mnames);

		$del = array_diff($mnames, $new);
		$this->delete($groupname, $del);
	
		$add = array_diff($new, $mnames);
		$this->insert($groupname, $add);
	}

	function insert($groupname, $usernames)
	{
    	global $db;

		$query = "INSERT INTO groupmembers (groupid, userid) 
                  VALUES (
					(SELECT groupid FROM groups WHERE groupname = ?),
					(SELECT userid FROM users WHERE username = ?)
                  )";

  		$stmt = $db->prepare($query);	

		foreach ($usernames as $username)
		{
			$querydata = array($groupname, $username);
	   		$result = $db->execute($stmt, $querydata); 
			if (DB::isError($result))
				return FALSE;
		}
		return TRUE;
	}

	function delete($groupname, $usernames)
	{
    	global $db;

		$query= "DELETE FROM groupmembers 
                 WHERE groupid = (SELECT groupid FROM groups WHERE groupname = ?)
                 AND userid = (SELECT userid FROM users WHERE username = ?)";
 
  		$stmt = $db->prepare($query);	
		
		foreach ($usernames as $username)
		{
			$querydata = array($groupname, $username);
	   		$result = $db->execute($stmt, $querydata); 
			if (DB::isError($result))
				return FALSE;
		}
		return TRUE;
	}
}


?>
Return current item: astwebpanel