<?php

	/*
	** File: edit_users
	** Description: speadsheet like page with which the admin can see the 
	**   list of users (people who sign up to buy stuff). Will have links
	**   to edit each user. 
	** Version: $Revision: 1.7 $
	** Created: 6/24/1999
	** Author: Shannon -jj Behrens
	** Email: shannon.behrens@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/08 01:56:01 $
	** Log : $Log: edit_users,v $
	** Log : Revision 1.7  1999/12/08 01:56:01  leon
	** Log : Fixed bug with using the old schema for the address table
	** Log :
	** Log : Revision 1.6  1999/11/08 22:32:26  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.5  1999/10/13 22:19:02  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.4  1999/10/13 00:42:29  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.3  1999/08/18 22:32:16  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.2  1999/07/30 17:51:44  jj
	** Log : Streamlined edit_users (cut out one query); other minor fixes.
	** 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.
	*/

	/*
	** We'll have the following fields in our "spreadsheet": 
	** Login, FirstName, LastName, Number, ActiveInvoices, MostRecentPurchase.
	** Everything will be a straight link, except "Login", whic will be a link
	** to a page where you can edit the user's information. 
	*/

	$successfulSoFar = TRUE;

	/* 
	** Make sure the user has admin rights. 
	*/
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		print(L_EDITUSERS_DENIED . "<BR>\n");
		$successfulSoFar = FALSE;
	}
	


	/* Get the main user information. */ 
	if ($successfulSoFar) 
	{
		$userQuery = "SELECT u.ID, u.Login, a.Name_First, a.Name_Last ";
		$userQuery .= "FROM user u, address a ";
		$userQuery .= "WHERE u.Address = a.ID";
		/* Execute the query. */ 
		if (! ($userRecords = mysql_query($userQuery, $DatabaseLink)) )
		{
			print("<!-- unable to query the database: " . mysql_errno()); 
			print(": " . mysql_error() . " -->\n");
			$successfulSoFar = FALSE;
		}
		/* If we're successful, we should make sure that we have at least 1 user.*/
		else
		{
			if (!mysql_num_rows($userRecords))
			{
				print("<!-- There were no users in the database!<BR>\n");
				print("Here's the query that I used: $userQuery -->\n");
				$successfulSoFar = FALSE;
			}
		}		
	}


	
	/* If everything's okay, we can start listing the users. */
	if ($successfulSoFar)
	{
	
	
		
		/* Start the table and headings. */
		print("<TABLE BORDER=\"1\" WIDTH=\"100%\"><TR><TD>\n");
		print("<TABLE WIDTH=\"100%\">\n");
		print("<TR>\n");
		print("<TD><B>" . L_EDITUSERS_LOGIN . "</B></TD>\n");
		print("<TD><B>" . L_EDITUSERS_FIRSTNAME . "</B></TD>\n");
		print("<TD><B>" . L_EDITUSERS_LASTNAME . "</B></TD>\n");
		print("<TD><B>" . L_EDITUSERS_PURCHASES . "</B></TD>\n");
		print("<TD><B>" . L_EDITUSERS_MOSTRECENTPURCHASE . "</B></TD>\n");
		print("</TR>\n");
	

	
		/* Output a line for each user. */
		while ($userRow = mysql_fetch_row($userRecords))
		{
			/* Parse the record and start the row. */ 
			$i = 0;
			$ID = $userRow[$i++];
			$Login = $userRow[$i++];
			$FirstName = $userRow[$i++];
			$LastName = $userRow[$i++];
			print("<TR>\n");

			/* Output the Login, which will link to edit_user. */ 
			print("<TD>");
			print("<A HREF=\"" . ScreenURL("edit_user"));
			print("&amp;ID=$ID\">");
			print(prepareText($Login));
			print("</A></TD>\n");

			/* Output the FirstName and LastName. */
			print("<TD>" . prepareText($FirstName) . "</TD>\n");
			print("<TD>" . prepareText($LastName) . "</TD>\n");

			/* 
			** Now figure out how may (active) invoices there are for the user,
			** and when his most recent purchase was.
			*/ 
			$invoicesQuery = "SELECT Created FROM invoice ";
			$invoicesQuery .= "WHERE Active = 'Y' ";
			$invoicesQuery .= "AND User = " . $ID . " ";
			$invoicesQuery .= "ORDER BY Created";
			/* Execute the query. */
			if ( !($invoicesRecords = mysql_query
				($invoicesQuery, $DatabaseLink)) )
			{
				print("<!-- Unable to query database: ");
				print(mysql_errno() . ": " . mysql_error());
				print("  Query was: " . $invoicesQuery . " -->\n");
				$successfulSoFar = FALSE;
			}
			else 
			{
				/* 
				** Get first row, in order to set $mostCurrent. Then, go 
				** ahead and set $countInvoices.
				*/
				if ( list($created) = mysql_fetch_row($invoicesRecords) )
				{
					$mostCurrent = $created;
					$countInvoices = mysql_num_rows($invoicesRecords);
				}
				/* Hmm, the user must not have purchased anything yet. */
				else
				{
					$mostCurrent = "None";
					$countInvoices = 0;
				}
				
				/* Output all that we've struggled so hard to learn :) */
				print("<TD>$countInvoices</TD>\n");
				print("<TD>$mostCurrent</TD>\n");
			}

			/* Finish up. */ 
			print("</TR>\n");			
			
		} /* End of loop through each user. */ 
	
	
	
	} /* end of "if successful so far" */
		

		
	/* Finish up the table. */ 
	print("</TABLE>\n");
	print("</TABLE></TD></TR>\n");
	
?>

