<?php

	// this file is used to display the list on the content_review page via AJAX

	$reviewer = ($role > 1);
	
	$output = "";

	// get filter values if the filter form was submitted
	$filters_submitted = initialize("filters_submitted", "get", "text");
	$filter_role = initialize("filter_role", "get", "text");
	$filter_active = initialize("filter_active", "get", "integer");
	$sort = initialize("sort", "get", "text");
	$start = initialize("start", "get", "integer");
	
	// clean up the posted values if necessary
	if (is_array($filter_role)) { $filter_role = implode(",", $filter_role); }
	
	// compare filter values to those in the database
	prepare_filters("user", $user, "users_manage", "role,active");

	// modify the query based on the selected options
	$filter_SQL = "";
	if ($filter_role != "") { $filter_SQL .= " AND FIND_IN_SET(user.role, '$filter_role') "; }
	if ($filter_active > 0) { $filter_SQL .= " AND user.active='$filter_active' "; }
	if ($sort) {
		$sort_SQL = " ORDER BY $sort ";
	} else {
		$sort_SQL = " ORDER BY name ";
	}

	// get the list
	$query = "
		SELECT
			SQL_CALC_FOUND_ROWS
			user.ID,
			user.name,
			role.name AS role,
			user.active,
			DATE_ADD(edit_dates.date, INTERVAL $timezone HOUR) AS last_edit
		FROM user
		LEFT JOIN role ON role.ID=user.role
		LEFT JOIN (SELECT user, MAX(date) AS date FROM content GROUP BY user) AS edit_dates ON edit_dates.user=user.ID
		WHERE 1=1 $filter_SQL
		$sort_SQL
		LIMIT $start, ".LIST_ITEMS_PER_PAGE."
	";
	$result = db_do_query($query);
	$count = db_num_rows($result);
	
	// do a follow-up query to get the total number of matches, not counting the LIMIT
	$query_total = "SELECT FOUND_ROWS()";
	$result_total = db_do_query($query_total);
	$row_total = db_get_row($result_total, 0);
	$total = implode("", $row_total);
	
	if ($count == 0) {
		$output .= "<p class=\"feedback_negative\">There are no users that match your current view</p>";
	} else {
		
		$row_styles = array("row_clear", "row_colored");		

		$output .= "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n";

		$output .= "<tr class=\"row_header\" valign=\"top\">\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('name')\">Username</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('role.name')\">Role</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('last_edit DESC')\">Last edit</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('active DESC')\">Active</a></td>\n";
		$output .= "<td class=\"heading\" colspan=\"2\">&nbsp;</td>\n";
		$output .= "</tr>\n";
		
		for ($i=0; $i < $count; $i++) {
			
			$this_row_style = $row_styles[$i % count($row_styles)];			

			$row = db_get_row($result, $i);
			extract($row);
			
			$active_display = ($active) ? "X" : "" ;
			
			$output .= "<tr class=\"$this_row_style\" valign=\"top\">\n";
			$output .= "<td class=\"body\">$name</td>\n";
			$output .= "<td class=\"body\">$role</td>\n";
			$output .= "<td class=\"body\">$last_edit</td>\n";
			$output .= "<td class=\"body\">$active_display</td>\n";
			$output .= "<td class=\"body\"><a href=\"JavaScript:edit('$ID')\">Edit</a></td>\n";
			$output .= "<td class=\"body\"><a href=\"JavaScript:delete_user('$ID', '$name')\">Delete</a></td>\n";
			$output .= "</tr>\n";
				
		}
		
		$output .= "</table>\n";
	
		//$output .= "<p class=\"caption\">Displaying $i of ".get_row_count("content")." content items</p>\n";
	
		$output .= "<p class=\"body\"><a href=\"JavaScript:add_user();\">Add a user</a></p>\n";
	
	}
	
	// build the pagination links
	$pagination_links = make_pagination_links($start, LIST_ITEMS_PER_PAGE, $total);
	if ($pagination_links) {
		$output .= "<p class=\"pagination\">$pagination_links</p>\n";
	}
	
	print $output;

?>
