<?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_status = initialize("filter_status", "get", "text");
	$filter_user = initialize("filter_user", "get", "integer");
	$sort = initialize("sort", "get", "text");
	$start = initialize("start", "get", "integer");
	
	// clean up the posted values if necessary
	if (is_array($filter_status)) { $filter_status = implode(",", $filter_status); }
	
	// compare filter values to those in the database
	prepare_filters("user", $user, "content_review", "status,user");

	// modify the query based on the selected options
	$filter_SQL = "";
	if ($filter_status != "") { $filter_SQL .= " AND FIND_IN_SET(content.status, '$filter_status') "; }
	if ($filter_user > 0) { $filter_SQL .= " AND content.user='$filter_user' "; }
	if ($sort) {
		$sort_SQL = " ORDER BY $sort ";
	} else {
		$sort_SQL = " ORDER BY date DESC ";
	}

	// get the list
	// we're only showing the latest version of each field whose latest version is not yet published
	$query = "
		SELECT
			SQL_CALC_FOUND_ROWS
			content.ID,
			path,
			field,
			status.ID AS status_ID,
			status.name AS status,
			user.name AS editor,
			user.email AS editor_email,
			DATE_ADD(date, INTERVAL $timezone HOUR) AS date,
			DATE_ADD(schedule, INTERVAL $timezone HOUR) AS schedule,
			comment
		FROM content
		LEFT JOIN status ON status.ID=content.status
		LEFT JOIN user ON user.ID=content.user
		INNER JOIN (SELECT MAX(ID) AS ID FROM content GROUP BY path, field) AS content_latest ON content_latest.ID=content.ID
		WHERE 1=1 $filter_SQL
		AND FIND_IN_SET(content.status, '1,2,3')
		$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 is no content that matches 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('path, field')\">Name</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('status.name')\">Status</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('schedule DESC')\">Scheduled</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('user.name')\">Editor</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('date DESC')\">Date</a></td>\n";
		$output .= "<td class=\"heading\"><a href=\"JavaScript:sort('comment')\">Comment</a></td>\n";
		$colspan = ($reviewer) ? 4 : 3 ;
		$output .= "<td class=\"heading\" colspan=\"$colspan\">&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);
			
			$name_display = "$path, $field";
			
			$comment_display = build_excerpt($ID, $comment, 64);
			
			$output .= "<tr class=\"$this_row_style\" valign=\"top\">\n";
			$output .= "<td class=\"body\">$name_display</td>\n";
			$output .= "<td class=\"body\">$status</td>\n";
			$output .= "<td class=\"body\">$schedule</td>\n";
			$output .= "<td class=\"body\"><a href=\"mailto:$editor_email\">$editor</a></td>\n";
			$output .= "<td class=\"body\">$date</td>\n";
			$output .= "<td class=\"body\">$comment_display</td>\n";
			$output .= "<td class=\"body\"><a href=\"JavaScript:view('$ID')\">View</a></td>\n";
			$output .= "<td class=\"body\"><a href=\"JavaScript:edit('$path', '$field')\">Edit</a></td>\n";
			if ($reviewer) {
				if (($status_ID < 3)&&($schedule)) {
					$output .= "<td class=\"body\"><a href=\"JavaScript:schedule('$ID')\">Schedule</a></td>\n";
				} else {
					$output .= "<td class=\"body\"><a href=\"JavaScript:publish('$ID', '$schedule')\">Publish</a></td>\n";
				}
			}
			$output .= "<td class=\"body\"><a href=\"JavaScript:revert('$path', '$field')\">Revert</a></td>\n";
			$output .= "</tr>\n";
				
		}
		
		$output .= "</table>\n";
	
		//$output .= "<p class=\"caption\">Displaying $i of ".get_row_count("content")." content paths</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;

?>
