<?php
include_once(dirname(__FILE__) . '/membertoshow.inc.php');

add_shortcode('memberslist_show', 'ml_show_memberslist');
add_shortcode('memberslist_show_members', 'ml_show_members');

add_action('wp_print_styles', 'ml_add_css');
add_action('init', 'ml_add_js');

function ml_add_js() {
	$membersListJS = WP_PLUGIN_URL . '/memberslist/memberslist.js';
	$queryJS = WP_PLUGIN_URL . '/memberslist/jquery-1.3.2.min.js';

	wp_register_script('membersListJS', $membersListJS);
	wp_enqueue_script('memberListJS', $membersListJS);

	wp_register_script('membersListJQuery', $queryJS);
	wp_enqueue_script('membersListJQuery', $queryJS);
}

function ml_add_css() {
	$cssURL = WP_PLUGIN_URL . '/memberslist/memberslist.css';
	wp_register_style('membersListCSS', $cssURL);
	wp_enqueue_style('membersListCSS');
}

function ml_show_members($params) {
	global $_GET;

	extract(shortcode_atts(array(
		'vcards' => 'no',
		'show' => '',
		'sort' => 'no',
	), $params));

	$usersToShow = explode(',', $show);

	$result = '';

	if(count($usersToShow) > 0) {
		$usersToShow = ml_getMembersToShow($usersToShow, ($sort == 'yes' ? true : false));
		foreach($usersToShow as $userToShow) {
			$result .= $userToShow->getListEntry(true);
		}
	}

	return $result;
}

function ml_show_memberslist($params) {
	global $_GET;

	extract(shortcode_atts(array(
		'vcards' => 'no',
		'sort' => 'no',
		'groupbycountry' => 'no',
		'exclude' => '',
	), $params));

	if($exclude != '') $exclude = explode(',', $exclude);

	if($groupbycountry == 'yes') $sort = 'yes';

	$usersToShow = ml_getMembersToShow(false, ($sort == 'yes' ? true : false), (is_array($exclude) ? $exclude : false));
	$result = '';

	if($groupbycountry == 'no') {
		if(count($usersToShow) > 0) {
			$result = '<ul class="memberslist">';
			foreach($usersToShow as $userToShow) {
				$result .= $userToShow->getListEntry();
			}
			$result .= '</ul>';
		} else {
			$result = 'no members to show';
		}
	} else if($groupbycountry == 'yes') {
		if(count($usersToShow) > 0) {
			$lastCountry = '';// $usersToShow[0]->getCimyValue('COUNTRY');
			$lastInst = '';
			$result = '<div class="memberstable">';
			$even = true;
			
			foreach($usersToShow as $userToShow) {
				if($userToShow->getCimyValue('COUNTRY') != $lastCountry) {
					$lastCountry = $userToShow->getCimyValue('COUNTRY');
					$result .= '<p class="country">' . $lastCountry  . '</p>';
				}

				if($userToShow->getCimyValue('INSTITUTION') != $lastInst) {
					$lastInst = $userToShow->getCimyValue('INSTITUTION');
					$result .= '<p class="institution">' . $lastInst . '</p>';
				}

				$result .= '<span class="membersprofile ' . ($even ? 'even' : 'uneven')  . '">';
				$result .= $userToShow->getListEntry(false, false, false);
				$result .= '</span>';
				$even = !$even;
			}

			$result .= '</div>';
		} else {
			$result = 'no members to show';
		}
	} else {
		$result = 'unknown value for (yes|no) option <strong>groupbycountry</strong>: ' .
			$groupbycountry;
	}


	return $result;
}

/**
 * Returns an array containing the user objects of the users
 * to show
 * @param usernames an string array of user names which allows to filter the output
 * @return an array containing the user object as
 * @param sortMembers sorts the members according to their sort string
 * @param excludeUsers an string array containing the user names of the users to exclude
 * from the listing
 *  <code>MemberToShow</code> instances
 */
function ml_getMembersToShow($usernames = false, $sortMembers = false, $excludeUsers = false) {
	$userIDs = ml_getUserIDs();
	$rolesToDisplay = ml_getRolesToDisplay();
	$result = Array();


	foreach($userIDs as $userID) {
		$curUserData = get_userdata($userID);
		if($excludeUsers != false && is_array($excludeUsers) && in_array($curUserData->user_login, $excludeUsers))
			continue;

		$found = false;

		foreach($rolesToDisplay as $roleToDisplay) {
			if($found == true) continue;
			if(array_key_exists($roleToDisplay, $curUserData->wp_capabilities)) $found = true;
		}

		if($found == false) continue;
		if(is_array($usernames) && !in_array($curUserData->user_login, $usernames)) continue;

		$curMemberToShow = new MemberToShow($userID, $curUserData);
		$result[] = $curMemberToShow;
	}

	if($sortMembers)
		usort($result, 'ml_sortMembersToShow');

	return $result;
}

/**
 * Sorts the array of MemberToShow instances using their sort string
 */
function ml_sortMembersToShow($a, $b) {
	return strcmp($a->getSortString(), $b->getSortString());
}

/**
 * Returns the names of the roles to display
 * @return an array with the names of the roles to display
 */
function ml_getRolesToDisplay() {
	global $wpdb;
	/*global $wp_roles;

	$result = Array();

	foreach($wp_roles->role_names as $rolename => $roleDisplayName) { 
		if(get_option('ml_enable_role_' . $rolename, 'no') == 'yes') {
			$result[] = $rolename;
		}
	}*/

	$result = Array();

	$roles = $wpdb->get_results("SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'ml_enable_role_%' AND option_value = 'yes' ");

	foreach($roles as $curRole) {
		$result[] = substr($curRole->option_name, 15);
	}


	if(get_option('ml_administrator_override')) {
		$adminIdx = array_search('administrator', $result);
		if(is_numeric($adminIdx))
			unset($result[$adminIdx]);
	}

	return $result;
}

function ml_getUserIDs() {
	global $wpdb;
	$result = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users"));

	return $result;
}
?>
