<?php
include_once ('lib/userprefs/scrambleEmail.php');
include_once ('lib/userprefs/userprefslib.php');

// Displays an alphabetically sorted list of users of a particular group
// if no group is entered, all registered users are displayed
// Useage:
// {USERLIST()}groupname{USERLIST}
// disp_num=>1			--> writes a number in front of every name					default = 0
// disp_userpage=>1		--> shows links to all users that have a personal userpage	default = 1
// disp_alpha=>1		--> shows names in groups of beginning letters				default = 1
// disp_total=>1		--> shows total number of users in list at the end			default = 1
// disp_email=>1		--> shows email address of given user (if set to public)	default = 0
// disp_realname=>1		--> shows real name of given user (if set to public)		default = 0
//
// If no groupname is given, plugin returns all users

function wikiplugin_userlist_help() {
	return tra('Displays an alphabetically sorted list of registered users');
}

function wikiplugin_userprint($disp_num,$user,$disp_userpage,$disp_email,$disp_realname,$wp_user_count) {
	global $tikilib,$userlib,$userprefslib;
	$ret = ('<tr>');

	// alternate class of every row
	if ($wp_user_count % 2) {
		$class = 'even';
	}
	else {
		$class = 'odd';
	}
	
	// get realname
	$realname = '';
	$userprefs = $userprefslib->get_userprefs($user);
	for ($j = 0; $j < count($userprefs); $j++) {
		if ($userprefs[$j]["prefName"] == "realName") {
			$realname = $userprefs[$j]["value"];
		}
	}
	// get email address and check if it's public
	$user_info = $userlib->get_user_info($user);
	// $email = scrambleEmail($user_info['email']);
	$email = $user_info['email'];

	// $public = $tikilib->get_user_preference($user,'email is public');

	if ($disp_num != 0) {
		$ret .= ('<td class="'.$class.'">'.$wp_user_count.'</td>');
	}

	$ret .= ('<td class="'.$class.'"><a class="wiki" href="messu-compose.php?to='.$user.'" title="'.tra('Send a message to').' '.$user.'"><img src="img/icons/icon_ultima.gif" width="18" height="9" border="0" alt="'.tra('Send message').'" /></a> [tiki-user_information.php?view_user='.$user.'|'.$user.']</td>');

	// if you want to show all email addresses indstead of only public ones, uncomment following line
	$public = 'y';


	if ($disp_realname != 0) {
		if ($public == 'y' && $realname != '') {
			$ret .= '<td class="'.$class.'">'.$realname.'</td>';
		}
		else {
			$ret .= '<td class="'.$class.'">&nbsp;</td>';
		}
	}

	if ($disp_email != 0) {
		if ($public == 'y' && $email != '') {
			$ret .= '<td class="'.$class.'">[mailto:'.$email.'|'.$email.']</td>';
		}
		else {
			$ret .= '<td class="'.$class.'">&nbsp;</td>';
		}
	}


	if ($disp_userpage != 0) {
		if ($tikilib->page_exists('userPage'.$user)) {
			$ret .= '<td class="'.$class.'">((userPage'.$user.'|userPage'.$user.'))</td>';
		}
		else {
			$ret .= '<td class="'.$class.'">&nbsp;</td>';
		}
	}

	$ret .= ("</tr>\r");

	return $ret;
}

// function used to sort an array - NOT case-sensitive
function wikiplugin_compare_users($a, $b) {
   return strcmp(strtolower($a), strtolower($b));
}

function wikiplugin_userlist($data, $params) {
	global $userlib, $tikilib;

	extract ($params);
	if(!isset($disp_alpha))		{ $disp_alpha = 1; }
	if(!isset($disp_userpage))	{ $disp_userpage = 1; }
	if(!isset($disp_num))		{ $disp_num = 0; }
	if(!isset($disp_total))		{ $disp_total = 1; }
	if(!isset($disp_email))		{ $disp_email = 0; }
	if(!isset($disp_realname))	{ $disp_realname = 0; }
	

	$colcount = 1;

	$ret = '<table class="normal">';
	$ret .= '<tr>';

	if ($disp_num != 0) {
		$ret .= '<td class="odddark">&nbsp;</td>';
		$colcount++;
	}

	$ret .= '<td class="odddark">'.tra('username').'</td>';

	if ($disp_realname != 0) {
		$ret .= '<td class="odddark">'.tra('Real Name').'</td>';
		$colcount++;
	}

	if ($disp_email != 0) {
		$ret .= '<td class="odddark">'.tra('email').'</td>';
		$colcount++;
	}

	if ($disp_userpage != 0) {
		$ret .= '<td class="odddark">'.tra('userpage').'</td>';
		$colcount++;
	}

	$ret .= '</tr>';

	if (empty($data)) {
		$userdata = $userlib->get_users(0);

		foreach ($userdata['data'] as $usertemp) {
			$users[] = $usertemp['user'];
		}
	}
	else {
		$users = $userlib->get_group_users($data);
	}

	usort($users, 'wikiplugin_compare_users');	// sort the users

	$wp_user_count = 0;
	foreach ($users as $user) {
		if ($wp_user_count >= 1) {
			$prev_user = $users[$wp_user_count-1];
		}
		else {
			$prev_user = 0;
		}
		$wp_user_count++;

		if ($disp_alpha != 0) {
			if (strtolower($prev_user[0]) != strtolower($user[0])) {
				$ret .= ("<tr><td class='odddark' colspan='".$colcount."'>".strtoupper($user[0])."</td></tr>\r");
			}
		}

		$ret .= (wikiplugin_userprint($disp_num,$user,$disp_userpage,$disp_email,$disp_realname,$wp_user_count));
	}

	if ($disp_total != 0) {
		$ret .= ("<tr><td class='odddark' colspan='".$colcount."'>&nbsp;</td></tr>\r");
		$ret .= ("<tr><td class='odddark' colspan='".$colcount."'>".tra('Total').": ".$wp_user_count."</td></tr>\r");
	}

	$ret .= ('</table>');

	return $ret;
}
?>