<?php

// Displays the member of a group
// Use:
// {MEMBEROFGROUP(sep=>", ",max=>10,sort=>asc|desc,layout=>table,tabletitle="User",link=>userpage|userinfo|userpref)}groupname{MEMBEROFGROUP}
//
// If no groupname is given returns all users TODO

// this plugin is based on:
//	wikiplugin_userlist.php

function wikiplugin_memberofgroup_help() {
	return tra("Displays members of a group").":<br />~np~{MEMBEROFGROUP()}groupname{MEMBEROFGROUP}~/np~";
}

function wikiplugin_memberofgroup_info() {
	return array(
		'name' => tra('Member of Group'),
		'documentation' => 'PluginMemberOfGroup',		
		'description' => tra('Displays members of a group'),
		'prefs' => array( 'wikiplugin_memberofgroup' ),
		'body' => tra('Name of group of which you want a userlist. If empty it shows all members of group Registered'),
		'params' => array(
			'sep' => array(
				'required' => false,
				'name' => tra('Separator'),
				'description' => tra('String to use between elements of the list.'),
			),
			'max' => array(
				'required' => false,
				'name' => tra('Maximum'),
				'description' => tra('Result limit.'),
			),
			'sort' => array(
				'required' => false,
				'name' => tra('Sort Order'),
				'description' => 'asc|desc',
			),
			'layout' => array(
				'required' => false,
				'name' => tra('Layout'),
				'description' => 'table',
			),
			'tabletitle' => array(
				'required' => false,
				'name' => tra('Title of table'),
				'description' => 'Title of table',
			),
			'link' => array(
				'required' => false,
				'name' => tra('Link'),
				'description' => 'userpage|userinfo|userpref',
			),			
		),
	);
}

function wikiplugin_memberofgroup($data, $params) {
	global $tikilib, $userlib, $prefs, $tiki_p_admin, $tiki_p_admin_users;
	
	extract ($params,EXTR_SKIP);
	
	if (!isset($sep)) $sep=', ';
	if (!isset($max)) { $numRows = -1; } else { $numRows = (int) $max; }

	if (!isset($data) || !$userlib->group_exists($data)) {
		$data = 'Registered';
	} // else $data is set and a valid group

	$mid = '';
	if (isset($sort)) {
		$sort=strtolower($sort);
		if (($sort=='asc') || ($sort=='desc')) {
			$mid .= ' ORDER BY `login` '.$sort;
		}
	}
	
	$pre=''; $post='';
	if (isset($layout)) {
		if ($layout=='table') {
			if (isset($tabletitle)) {
				$pre='<table class=\'sortable\' id=\''.$tikilib->now.'\'><tr><th>'.$tabletitle.'</th></tr><tr><td>';
			} else {
				$pre='<table class=\'sortable\' id=\''.$tikilib->now.'\'><tr><th>'.tra('users').'</th></tr><tr><td>';
			}
			$sep = '</td></tr><tr><td>';
			$post='</td></tr></table>';
		}
	}
	
	//$result = $userlib->get_group_users($data);
	$bindvars=array();
	$query = "select `login`  from `users_users` uu, `users_usergroups` ug where uu.`userId`=ug.`userId` and `groupName`= '$data'".$mid;
	$result = $tikilib->query($query, $bindvars, $numRows);
	$ret = array();

	while ($row = $result->fetchRow()) {
		$res = '';
		if (isset($link)) {
			if ($link == 'userpage') {
				if ($prefs['feature_wiki_userpage'] == 'y') {
					global $wikilib; include_once('lib/wiki/wikilib.php');
					$page = $prefs['feature_wiki_userpage_prefix'].$row['login'];
					if ($tikilib->page_exists($page)) {
						$res = '<a href="'.$wikilib->sefurl($page).'" title="'.tra('Page').'">';
					}
				}
			} elseif ($link == 'userpref') {
				if ($prefs['feature_userPreferences'] == 'y' && ($tiki_p_admin_users == 'y' || $tiki_p_admin == 'y')) {
					$res = '<a href="tiki-user_preferences.php?userId='.$tikilib->get_user_id($row['login']).'" title="'.tra('Preferences').'">';
				}
			} elseif ($link == 'userinfo') {
				if ($tiki_p_admin_users == 'y' || $tiki_p_admin == 'y') {
					$res = '<a href="tiki-user_information.php?userId='.$tikilib->get_user_id($row['login']).'" title="'.tra('User Information').'">';
				} else {
					$user_information = $tikilib->get_user_preference($row['login'], 'user_information', 'public');
					if ($user_information != 'private' && $row['login'] != $user) {
						$res = '<a href="tiki-user_information.php?userId='.$tikilib->get_user_id($row['login']).'" title="'.tra('User Information').'">';
					}
				}
			}
		}
		$ret[] = $res.$row['login'].($res?'</a>':'');
	}
	return $pre.implode ( $sep, $ret ).$post;
}

?>