<?php
/*
 admin.inc.php - contains admin area specific code
 */


/** registers the backend functionality */
add_action('admin_menu', 'ml_admin_menu');

/** admin menu hook */
function ml_admin_menu() {
	add_options_page('Memberslist options', 'Memberslist', 'edit_users', 'memberslist@fstoffel.de', 'ml_plugin_options');
	add_action('admin_init', 'ml_register_settings');
}

/** register settings */
function ml_register_settings() {
	global $wp_roles;

	register_setting('menulist_options_group', 'ml_content_page', 'ml_checkintval');
	register_setting('menulist_options_group', 'ml_entriesperpage', 'ml_checkintval');
	register_setting('menulist_options_group', 'ml_noindex');
	register_setting('menulist_options_group', 'ml_administrator_override');

	foreach($wp_roles->role_names as $role => $roleName) {
		register_setting('menulist_options_group', 'ml_enable_role_' . $role);
	}
}

/**
 * Sanitize function for integer values
 *
 * @param toCheck the item to check for integer value
 * @return true, if <code>toCheck</code> is an integer value, false otherwise
 */
function ml_checkintval($toCheck) {
	if(!is_numeric($toCheck)) return 1;
	if($toCheck < 0) return abs($toCheck);
	return $toCheck;
}

/** plugin options */
function ml_plugin_options() {
	global $wp_roles;
	?>
	<div class="wrap"><div id="icon-users" class="icon32"><br></div>
	<h2>Memberslist <?php _e('options') ?></h2><form method="post" action="options.php">

	<?php settings_fields('menulist_options_group'); ?>

	<h3><?php _e('Display settings') ?></h3>
	<table class="form-table">
	<tr valign="top">
	<th scope="row"><label for="ml_entriesperpage"><?php _e('Entries per page') ?></label></th>
	<td><input name="ml_entriesperpage" id="ml_entriesperpage" value="<?= get_option('ml_entriesperpage', 10) ?>" class="small-text" type="text">
	<span class="description"><?php _e('the number of members to show per page - <strong>not yet functional</strong>') ?></span></td></tr>
	<tr valign="top"><th scope="row"><label for="meta_noindex_select"><? _e('Noindex tag') ?></label></th>
	<td><input name="ml_noindex" id="ml_noindex" <?php checked('on', get_option('ml_noindex', 'on')) ?> type="checkbox">
	<?php _e('Add the <code>noindex</code> meta tag to prevent search engine spiders to index the members list - <strong>not yet functional</strong>.') ?>
	</td></tr>
	</table>

	<h3><?php _e('Roles to display') ?></h3>
	<p class="description">
	<?php _e('Select the roles to show in the members listing. Only users, which are associated to <strong>at least one</strong> selected role are shown in the member listing.') ?>
	</p>
	<table class="form-table">

	<?php
	foreach($wp_roles->role_names as $role => $roleName) {
		?>
		<tr valign="top"><th scope="row"></th>
		<td><input name="ml_enable_role_<?= $role ?>" value="yes" id="ml_enable_role_<?= $role ?>" <?php checked('yes', get_option('ml_enable_role_' . $role, 'yes')) ?> type="checkbox"> <?= $roleName ?>
		</td></tr>
		<?php
	}
	?>

	<tr valign="top"><th scope="row">Hide administrators</th>
	<td><input name="ml_administrator_override" value="yes" name="ml_administrator_override" <?php checked('yes', get_option('ml_administrator_override', 'yes')) ?> type="checkbox">
	<?php _e('<strong>Always</strong> hide users which have the <code>Administrator</code> role.') ?>
	</td></tr>
	</table>

	<p class="submit"><input name="Submit" class="button-primary" value="<?php _e('Save Changes') ?>" type="submit"></p>
	</form></div>
	<?php
}

/** returns an array with the page names */
function ml_getPages() {
	$result = '<select name="ml_content_page" style="min-width:10em">';

	$curPage = get_option('ml_content_page', 0);
	$pages = get_pages('sort_column=menu_order');
	
	foreach($pages as $page) {
		$result .= '<option value="' . $page->ID . '"';
		if($page->ID == $curPage) $result .= ' selected="selected"';
		$result .= '>' . $page->post_title . '</option>';
	}

	$result .= '</select>';

	return $result;
}
?>
