<?php

	/* 
	** File: edit_user
	** Description: Edit the information about a particular user. Admin's can
	**   use this script to edit anyone, users can only use this script to edit
	**   information about themselves.
	** Version: $Revision: 1.22 $
	** Created: 6/25/99
	** Author: Shannon -jj Behrens
	** Email: shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/02/02 19:48:18 $
	** Log : $Log: edit_user,v $
	** Log : Revision 1.22  2000/02/02 19:48:18  leon
	** Log : Adding title and options to startBox.  This is based on code donated
	** Log : by Isaac Reuben.
	** Log :
	** Log : Revision 1.21  2000/02/02 18:59:49  leon
	** Log : Fixing bugs reported by Ted Henigson.
	** Log :
	** Log : Revision 1.20  1999/12/15 22:39:05  jj
	** Log : Plugged up whole that could potentially allow users to edit someone else's
	** Log : information.
	** Log :
	** Log : Revision 1.19  1999/12/08 00:55:09  leon
	** Log : Pulled functions for creating table rows out of addressFunctions and
	** Log : generalized them.  I'm leaving wrappers for the old functions, but I
	** Log : will removed them in the coming weeks.
	** Log :
	** Log : Revision 1.18  1999/12/06 21:17:26  leon
	** Log : ECML compliance
	** Log : Overhauled address functions to use ECML form field names.  Ditto with
	** Log : the billing fields on the checkout forms.  Note the change to build.sql.
	** Log : Yes, the address table changed to accomodate new fields.  The billing
	** Log : table changed too.
	** Log :
	** Log : Revision 1.17  1999/11/08 22:32:25  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.16  1999/11/04 21:35:42  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.15  1999/10/13 22:19:02  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.14  1999/10/13 00:42:29  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.13  1999/10/11 23:04:10  leon
	** Log : Removed all require()'s, added block to stop double include()'s
	** Log :
	** Log : Require wasn't giving any performance advantage because it was always
	** Log : being called with a variable.  Futhermore, using return inside a
	** Log : require()'d file does not work in PHP4.
	** Log :
	** Log : Revision 1.12  1999/09/09 17:02:57  jj
	** Log : On edit_user, used "password" as the field type for the password field.
	** Log :
	** Log : Revision 1.11  1999/09/08 17:50:43  jj
	** Log : Fixed a small error in a comment.
	** Log :
	** Log : Revision 1.10  1999/08/18 22:32:16  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.9  1999/08/17 18:41:02  jj
	** Log : Got rid of dataRow everywhere in favor of functions from addressFunctions
	** Log :
	** Log : Revision 1.8  1999/08/17 17:56:42  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.7  1999/08/10 23:16:42  jj
	** Log : Used $UserInfo for getting the user's address information rather than hitting the database.
	** Log :
	** Log : Revision 1.6  1999/08/10 20:55:52  jj
	** Log : Code was added to allow a user to edit his address and login information. Strict security was used so that he can't edit anyone else's.
	** Log :
	** Log : Revision 1.5  1999/08/10 18:01:47  jj
	** Log : Created and used an addressEditor function rather than cutting and pasting everytime.
	** Log :
	** Log : Revision 1.4  1999/08/09 22:30:31  jj
	** Log : Any selects for Country now use makeCountrySelect (an include)
	** Log :
	** Log : Revision 1.3  1999/08/09 18:41:33  jj
	** Log : Changed a few field labels to make them "nicer".
	** Log :
	** Log : Revision 1.2  1999/08/09 18:27:29  jj
	** Log : Got rid of invoice status "Description" field any place where user could see it. God rid of any mention of batches (since Batch is not in build.sql).
	** Log :
	** Log : Revision 1.1  1999/07/28 19:41:10  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	** 
	** Copyright (c) 1999 Working Dogs. All rights reserved. 
	**
	** Expected input: Possibly $ID. If this is the admin, let him edit whatever
	** he wants (default to editing himself). Otherwise, force the user to 
	** edit his own profile.
	*/ 

	// Use the function to create the address editor forms
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");

	//functions for drawing pretty boxes
	include(APPLICATION_ROOT . "/modules/include/box");

	/* 
	** Deal with access issues. If this is the admin, let him edit whatever
	** he wants (default to editing himself). Otherwise, force the user to 
	** edit his own profile.
	*/
	if ( !$UserInfo["Permission"]["Administrate"] )
	{
		//copy user's own info
		$EditUser = $UserInfo;
	}
	else
	{
		//get info on the user
		$EditUser = GetUserInfo($ID);
	}	


	/*
	** If this is the admin, give him a link back to "edit_users" and tell him
	** which user he is editing.
	*/
	if ( $UserInfo["Permission"]["Administrate"] )
	{
		startBox(L_EDITUSER_VIEWINGUSER . " " .
			prepareText($EditUser["Address"]["Name_First"]) . " " . 
			prepareText($EditUser["Address"]["Name_Last"]));

		print("<A HREF=\"" . ScreenURL("edit_users"));
		print("\">" . L_EDITUSER_RETURN . "</A> " . L_EDITUSER_LISTOFUSERS . "\n");

		endBox();
	}



	/*
	** This is the section where the user can edit login information.
	*/
	
	// Start the section table and the form
	print(StartForm("edit_user", $method='post', $action='UPDATE_LOGIN_PASSWORD', 
		$secure=TRUE, array("ID"=>$EditUser["ID"])));

	startBox(L_EDITUSER_LOGININFORMATION);

	// Do a small table to contain the data in text boxes
	print("<TABLE>\n");
	
	printEditRow(L_EDITUSER_LOGIN, "Login", $EditUser["Login"]);
	
	print("<TR>\n");
	print("<TD>" . L_EDITUSER_PASSWORD . "</TD>\n");
	print("<TD><INPUT TYPE=\"password\" NAME=\"Password\" VALUE=\"" . 
		prepareText($Password) . "\"></TD>\n");
	print("</TR>\n");

	print("</TABLE>\n");

	// Finish up the login section	
	print("<input type=\"submit\" value=\"" . L_EDITUSER_SUBMIT . "\">\n");
	print("<input type=\"reset\" value=\"" . L_EDITUSER_RESET . "\">\n");

	print("</form>\n");

	endBox();



	/* 
	** This is the section where the user can edit address information.
	*/ 
	//Start the section table and the form
	print(StartForm("edit_user", 'post', 'UPDATE_ADDRESS', TRUE, 
		array("addressID"=>$EditUser["Address"]["ID"], "ID"=>$EditUser["ID"])));


	startBox(L_EDITUSER_NAMEANDADDRESS);

	printAddressFields(
		$EditUser["Address"], 
		"input", 
		"ALL", 
		L_EDITUSER_UPDATE_ADDRESS, 
		L_EDITUSER_RESET_ADDRESS, 
		TRUE);

	endBox();

	print("<br>\n");

	/*
	** This is the section where the admin is given the list of invoices
	** that are associated with this user. Each invoice will have a link
	** that will take the admin to a page where he can edit the invoice.
	** If this is not the admin, there's no point in listing this user's 
	** past orders (the user should just go look at his order history).
	*/
	if($UserInfo["Permission"]["Administrate"])
	{
		/* Start the section table. */ 
		startBox(L_EDITUSER_INVOICES);

		/* Get the list of invoices. */
		$listInvoicesQuery = "SELECT i.Invoice, s.Name, i.Created, " . 
			"i.Description ";
		$listInvoicesQuery .= "FROM status s, invoice_status i, invoice n ";
		$listInvoicesQuery .= "WHERE i.Status = s.ID AND i.Invoice=n.ID " . 
			"AND n.Active = 'Y' AND n.User = " . $EditUser["ID"] . " ";
		$listInvoicesQuery .= "ORDER BY i.Invoice DESC, i.Created DESC";
		/* Execute the query. */
		if ( !($listInvoicesRecords = mysql_query
			($listInvoicesQuery, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . " -->\n");
		}
		/* Sucessful query, it's possible that there are no invoices. */ 
		else	
		{	
			/* No rows. */ 
			if (!mysql_num_rows($listInvoicesRecords))
			{
				print("<p><i>" . L_EDITUSER_NOINVOICES . "</i>\n");
			}

			/* 
			** There are invoices. List them. Each should have a link to a page
			** where they can edit that invoice. 
			*/ 
			else
			{
				/* Start the table and do the headings. */
				print("<TABLE>\n");
				print("<TR>\n");
				print("<TD><B>" . L_EDITUSER_INVOICE . "</B></TD>\n");
				print("<TD><B>" . L_EDITUSER_STATUS . "</B></TD>\n");
				print("<TD><B>" . L_EDITUSER_DATE . "</B></TD>\n");
				print("<TD><B>" . L_EDITUSER_NOTES . "</B></TD>\n");
				print("</TR>\n");

				$currentInvoice = -1;
				while ($row = mysql_fetch_row($listInvoicesRecords))
				{
					/* 
					** If this is just another status record for the same invoice, 
					** skip it. 
					*/
					if ($currentInvoice == $row[0])
					{
						continue;
					}
					/* Nope, it's a new invoice. */
					else
					{
						$currentInvoice = $row[0];

						/* Parse the row. */
						$i = 0;
						$Invoice = $row[$i++];
						$Status = $row[$i++];
						$Created = $row[$i++];
						$Description = $row[$i++];

						/* Output it. */
						print("<TR>\n");
						print("<TD ALIGN=\"CENTER\">" . 
							"<A HREF=\"" . ScreenURL("edit_invoice") .
							"&invoice_ID=$Invoice\">");
						print("$Invoice</A></TD>\n");

						print("<TD>" . prepareText($Status) . "</TD>\n");
						print("<TD>$Created</TD>\n");
						print("<TD>" . prepareText($Description) . "</TD>\n");
						print("</TR>\n");
					}
				}
				print("</TABLE>\n");
			}
		}	

		/* Finish up the invoices section. */ 
		endBox();
	}

?>
