<?php

	$administrator = ($role == 3);

	if ($administrator) {
		// get the record ID passed in from the list
		$ID = initialize("ID", "get", "integer");
	} else {
		// only allow non-adminstrators to edit their own records
		$ID = $user;
	}
	
?>

<html>

	<head>
		<title><?php print EDITOR_NAME; ?></title>
		<?php eval(include_namespace(HEAD_FILE)); ?>
		<script language="JavaScript">
		
			function validate() {
				if (!edit.name.value) {
					alert("Please enter a username.");
					edit.name.focus();
					return false;
				}
				if (!edit.timezone.selectedIndex) {
					alert("Please select a time zone.");
					edit.timezone.focus();
					return false;
				}
				return true;
			}
		
			function cancel() {
				location = "<?=$wrapper_page?>?page=users_manage";
			}
			
			function delete_user(ID, label) {
				if (confirm("Are you sure you wish to delete the user '" + label + "'?")) {
					location = "<?=$editor_index?>?page=users_submit&ID=" + ID + "&delete=1";
				}
			}
			
		</script>
	</head>
	
	<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onload="load_list();">
	
		<?php eval(include_namespace(HEADER_FILE)); ?>

		<p class="title">Edit user</p>
		
		<div id="feedback"><?php print get_feedback(); ?></div>

		<form name="edit" method="post" action="<?=$editor_index?>?page=users_submit&ID=<?php print $ID; ?>" onsubmit="return validate();">
		
			<div class="column_third">
		
				<?php
				
					$output = "";
				
					// get the record from the database, or set default values for a new record
					if ($ID) {
						$query = "
							SELECT
								name,
								email,
								timezone,
								role,
								active
							FROM user
							WHERE ID=$ID
						";
						$result = db_do_query($query);
						$row = db_get_row($result, 0);
						extract($row);
						$password = "xxxxxxxx"; // we can't put the actual password into the field because it's one-way encrypted, so we'll just use this string instead
					} else {
						$name = "";
						$password = "";
						$email = "";
						$timezone = "";
						$role = 0;
						$active = 1;
					}
					
					// clean up the data if necessary
					$active_checked = ($active) ? "checked" : "" ;
					
					// build menus for relational data
					$role_menu = make_menu("Role...", "role", "role", "ID", "name", 0, 0, 0, "", $role);
					$timezone_menu = make_menu("Time zone...", "timezone", "timezone", "ID", "name", 0, "offset DESC", 0, "", $timezone);
		
					if (INTEGRATION_MODE) {
						$output .= "<p class=\"body\">User ID<br />\n";
						$output .= "<input type=\"text\" name=\"ID\" size=\"8\" value=\"$ID\" class=\"form\" /></p>\n";
					}
				
					if ($administrator) {
						$output .= "<p class=\"body\">Username<br />\n";
						$output .= "<input type=\"text\" name=\"name\" size=\"24\" value=\"$name\" class=\"form\" /></p>\n";
					} else {
						$output .= "<input type=\"hidden\" name=\"name\" value=\"$name\" />\n";
					}
				
					$output .= "<p class=\"body\">Password<br />\n";
					$output .= "<input type=\"password\" name=\"password\" size=\"24\" value=\"$password\" class=\"form\" /></p>\n";
				
					$output .= "<p class=\"body\">Email<br />\n";
					$output .= "<input type=\"text\" name=\"email\" size=\"24\" value=\"$email\" class=\"form\" /></p>\n";
				
					$output .= "<p class=\"body\">Time zone<br />\n";
					$output .= $timezone_menu."</p>\n";
				
					if ($administrator) {
						$output .= "<p class=\"body\">Role<br />\n";
						$output .= $role_menu."</p>\n";
					} else {
						$output .= "<input type=\"hidden\" name=\"role\" value=\"$role\" />\n";
					}
				
					if ($administrator) {
						$output .= "<p class=\"body\"><input type=\"checkbox\" name=\"active\" value=\"1\" $active_checked /> Active</p>\n";
					} else {
						$output .= "<input type=\"hidden\" name=\"active\" value=\"$active\" />\n";
					}
					
					$output .= "<p class=\"body stack_left\">\n";
					$output .= "<input type=\"submit\" value=\"Save\" class=\"form\" />\n";
					$output .= "</p>\n";
					
					if ($administrator) {
						$output .= "<p class=\"body stack_right\">\n";
						$output .= "<input type=\"button\" value=\"Cancel\" onclick=\"cancel();\" class=\"form\" />\n";
						$output .= "<input type=\"button\" value=\"Delete\" onclick=\"delete_user('$ID', '$name');\" class=\"form\" />\n";
						$output .= "</p>\n";
					}
					
					print $output;
				
				?>
				
			</div>
			
			<div class="column_third">
			
				<p class="subheading">Limit access to...</p>
				
				<?php

					$groups = get_default_groups();
					$paths = get_paths($groups);
					$access = get_value("user", "ID", "access", $ID);
					$access = ($access) ? explode(",", $access) : array() ;
					$content_file_extensions = explode(",", CONTENT_FILE_EXTENSIONS);

					$output = "";
					
					$output .= "<select name=\"access[]\" size=\"12\" class=\"form\" style=\"width: 220px;\" multiple>\n";
					foreach ($paths as $path) {
						$file_extension = preg_replace("/^.*\.(\w+?)$/", "$1", $path);
						if ( (CONTENT_TYPE == "database") || ((strpos($path, ".LCK") === false)&&(in_array($file_extension, $content_file_extensions))) ) {
							$selected = (in_array($path, $access)) ? "selected" : "" ;
							$output .= "<option value=\"$path\" $selected>$path</option>\n";
						}
					}
					$output .= "</select>\n";

					print $output;

				?>
			
			</div>
			
			<div class="column_third">
			
				<p class="subheading">Recent edits</p>
				
				<?php

					$output = "";
					
					// this gets overwritten by the edit form above, so we have to restore it now
					$timezone = initialize("timezone", "session", "text");
					if ($timezone == "") { $timezone = 0; }
				
					// get a list of recent edits
					$query = "
						SELECT
							SQL_CALC_FOUND_ROWS
							content.ID,
							path,
							field,
							status.name AS status,
							DATE_ADD(date, INTERVAL $timezone HOUR) AS last_saved
						FROM content
						LEFT JOIN status ON status.ID=content.status
						LEFT JOIN user ON user.ID=content.user
						WHERE content.user=$ID
						ORDER BY date DESC
						LIMIT 0, ".MINI_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=\"body\">This user has not performed any edits</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>Content</a></td>\n";
						$output .= "<td class=\"heading\"><a>Status</a></td>\n";
						$output .= "<td class=\"heading\"><a>Last saved</a></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 = "<a href=\"JavaScript:select_content('$path,$field', '$path, $field');\">$path, $field</a>";
							
							$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\">$last_saved</td>\n";
							$output .= "</tr>\n";
								
						}
						
						$output .= "</table>\n";
					
						$output .= "<p class=\"body\"><a href=\"$wrapper_page?page=content_history&filter_user=$user\">View all edits</a></p>\n";
					
					}
					
					print $output;

				?>
			
			</div>
			
			<div class="reset"></div>

		</form>

		<?php eval(include_namespace(FOOTER_FILE)); ?>

	</body>
	
</html>