All pastes #185601 Raw Edit

Someone

public text v1 · immutable
#185601 ·published 2006-09-29 03:20 UTC
rendered paste body
/*******inactive_form.php***********/
<?php

		include ('/var/www/dbconfig/dbconfig.php');
		include ('/var/www/php-lib/form_elements.php');

		include ('inactive_functions.php');







?>

<html>

<link href="/stylesheet/stylesheet_forms.css" rel="stylesheet" type="text/css">

<head>

<title>Inactive Notification Form</title>


</head>

<div id="containerhalfwidth">

<h1>Send Activation Request</h1>

<div id="bluecontainer">

<form action = "send_inactive_form.php" method=post name="inactive_form">

<div class=row><span class=left>Your name: </span></div><div class=row><span class=right><?php echo return_employee_dropdown($_COOKIE['user']); ?>&nbsp;<a STYLE="text-decoration:none" target="_blank" href="/dbadmin/employee_directory/add_department.php?message_2=Select the right department and enter other details...&back_page=/forms/inactive/inactive_form.php">*</a></div>
<div class=row><span class=left>Query: </span></div><div class=row><span class=right><?php echo return_all_query_types(); ?>&nbsp;<a  STYLE="text-decoration:none" target="_blank" href='/forms/inactive/inactive_query_edit.php'>*</a></div>
<div class=row><span class=left>State: </span></div><div class=row><span class=right><?php echo return_all_states(); ?></div>
<div class=row><span class=left>Serial#: </span></div><div class=row><span class=right><input type='text' name='serial'></div>
<div class=row><span class=left>Meter-Count: </span></div><div class=row><span class=right><input type='text' name='meter_count'></div>
<div class=row><span class=left>Machine: </span></div><div class=row><span class=right><?php echo return_all_models(); ?>&nbsp;<a STYLE="text-decoration:none" target="_blank" href='/dbadmin/copiers/add_new_category.php?page=/forms/inactive/inactive_form.php'>*</a></div>
<div class=row><span class=left>Company: </span></div><div class=row><span class=right><input type='text' name='company'></div>
<div class=row><span class=left>Street: </span></div><div class=row><span class=right><input type='text' name='street'></div>
<div class=row><span class=left>Suburb: </span></div><div class=row><span class=right><input type='text' name='suburb'></div>
<div class=row><span class=left>Postcode: </span></div><div class=row><span class=right><input type='text' name='postcode' maxlength='4' size='4'></div>
<div class=row><span class=left>Contact: </span></div><div class=row><span class=right><input type='text' name='contact'></div>
<div class=row><span class=left>Phone: </span></div><div class=row><span class=right><input type='text' name='number'></div>
<div class=row><span class=left>Notes: </span></div><div class=row><span class=right><textarea name='notes' rows='7' cols='20'></textarea></div>
<br>
<div class=row><span class=center><input type='submit' class='blue' value='   Email   '></div>


<br><br><div class=row><span class=center><h1><a href='/forms/inactive/view_requests.php'>View Activation Deposit</a></h1></div>

</form>


</div>

</div>

<body>





</body>


</html>


/***********send_inactive_form.php***************/
<?php

	include ('/var/www/dbconfig/dbconfig.php');
	include ('inactive_functions.php');



	$query_id = $_POST['query'];
	$state_id = $_POST['state'];
	$serial = $_POST['serial'];
	$meter_count = $_POST['meter_count'];
	$model_id = $_POST['model'];
	$company = $_POST['company'];
	$street = $_POST['street'];
	$suburb = $_POST['suburb'];
	$postcode = $_POST['postcode'];
	$contact = $_POST['contact'];
	$number = $_POST['number'];
	$notes = $_POST['notes'];



	$user_id = $_POST['user'];

	$cookie_life = time() + 31536000;
	setcookie('user', $user_id, $cookie_life);


	echo save_inactive_request($query_id, $state_id, $serial,
								$meter_count, $model_id, $company,
								$street, $suburb, $postcode,
									$contact, $number, $notes, $user_id);



	$member_list = return_member_id_list($query_id);

	$email_addresses = return_email_addresses($member_list);

	//print_r (return_email_addresses($member_list));


?>

<html>


<script language="JavaScript" type="text/javascript">

function check_status()
{

	var current_id = <?php  echo check_status($serial, $postcode); ?>;
	var last_id = <?php echo return_last_id_in_db(); ?>;

	if (current_id == last_id)
	{
		return -1;
	}
}


function send_inactive_form()
{


	if (check_status() == -1)
	{

		document.form.Enquiry.value = '\r\r';
		document.form.Enquiry.value += 'Serial#:  <?php echo $serial; ?>\r';
		document.form.Enquiry.value += 'Model:  <?php echo return_model_name($model_id); ?>\r';
		document.form.Enquiry.value += 'Meter-Count:  <?php echo $meter_count; ?>\r';
		document.form.Enquiry.value += 'Company:  <?php echo $company; ?>\r';
		document.form.Enquiry.value += 'Address:  <?php echo $street." ".$suburb." ".$postcode; ?>\r';
		document.form.Enquiry.value += 'State:  <?php echo return_state_name($state_id); ?>\r';
		document.form.Enquiry.value += 'Contact:  <?php echo $contact; ?>\r';
		document.form.Enquiry.value += 'Phone#:  <?php echo $number; ?>\r';
		document.form.Enquiry.value += 'Notes:  <?php echo $notes; ?>\r\r\r';

		document.form.Enquiry.value += 'Kind Regards\r\r';
		document.form.Enquiry.value += '	NOC';

		var mailaddress = '<?php

								$counter = 1;
								foreach ($email_addresses as $email)
								{
									if ($counter == sizeof($email_addresses))
									{
										echo $email;
									}
									else
									{
										echo $email.";";
									}

									$counter++;
								}

							?>';

		document.form.action = 'mailto: ' + mailaddress + '?subject=' + '<?php echo return_query_name($query_id); ?>';
		document.form.submit();
		window.location="inactive_form.php";

	}
	else
	{
		alert("The serial is pending for activation already...");
		window.location="view_requests.php";
	}



}

</script>

<head>
<title></title>
</head>


<body onLoad="send_inactive_form()">

<form name="form" method=post action="mailto: " enctype="text/plain">

<textarea name="Enquiry" rows="0" cols="0" style="visibility:hidden">
</textarea>

</form>

</body>

</html>



/******inactive_functions.php**************/

<?php


	//this function returns a state dropdown, requires form_elements.php
	function return_all_states($default_state = '')
	{
		$dbname = 'geographic';

		mysql_select_db($dbname);

		$sql = "SELECT id, state FROM state ORDER BY id";


		$result = mysql_query($sql);

		$state_names = array();
		$state_id = array();

		while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			array_push($state_id, $row['id']);
			array_push($state_names, $row['state']);
		}

		return return_dropdown(&$state_id, &$state_names, 'state', $default_state);

	}


	//this function returns a model dropdown, requires form_elements.php
	function return_all_models()
	{
		$dbname = 'copiers';

		mysql_select_db($dbname);

		$sql = "SELECT id, model_name FROM model ORDER BY model_name";


		$result = mysql_query($sql);

		$model_names = array();
		$model_id = array();

		while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			array_push($model_id, $row['id']);
			array_push($model_names, $row['model_name']);
		}

		return return_dropdown(&$model_id, &$model_names, 'model', '');

	}


	//this function returns a Query type dropdown, requires form_elements.php
	function return_all_query_types()
	{
		$dbname = 'inactives';

		mysql_select_db($dbname);

		$sql = "SELECT id, query_type_name FROM query ORDER BY id";


		$result = mysql_query($sql);

		$query_types = array();
		$query_id = array();

		while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			array_push($query_id, $row['id']);
			array_push($query_types, $row['query_type_name']);
		}

		return return_dropdown(&$query_id, &$query_types, 'query', '');

	}



	function return_last_id_in_db()
	{
		$dbname = "inactives";
		mysql_select_db($dbname);

		$sql = "SELECT id FROM request_data ORDER BY id DESC";

		$result = mysql_query($sql)
					or die ("Error in check_status()");

		if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			return $row['id'];
		}
	}


	//this function returns the member ids for a query type
	function return_member_id_list($query_id)
	{
		$dbname = "inactives";
		mysql_select_db($dbname);

		$sql = "SELECT member_id FROM inactive_distribution_lookup WHERE query_id = '".$query_id."'";

		$result = mysql_query($sql)
						or die ("Error in return_distribution_list()");

		$member_id_list = Array();

		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			array_push($member_id_list, $row['member_id']);
		}

		return $member_id_list;

	}



	function return_email_addresses(&$member_id_list)
	{
		$dbname = "emails";
		mysql_select_db ($dbname);

		$email_addresses = Array();

		foreach ($member_id_list as $id)
		{
			$sql = "SELECT email_addresses.email_address
				FROM email_addresses INNER JOIN email_lookup ON email_addresses.id = email_lookup.email_id
				WHERE email_lookup.member_id = '".$id."'";

			$result = mysql_query($sql)
						or die ("Error in return_email_addresses()");

			while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				array_push($email_addresses, $row['email_address']);
			}
		}

		return array_unique($email_addresses);

	}



	//this function will check if theres a pending request for supplied serial number,
	//the postcode is used as secondary key
	function check_status($serial, $postcode)
	{
		$dbname = "inactives";
		mysql_select_db($dbname);

		$sql = "SELECT id FROM request_data WHERE
				status = '0' AND serial = '".$serial."' AND postcode = '".$postcode."'";

		$result = mysql_query($sql)
					or die ("Error in check_status()");

		if (mysql_num_rows($result) > 0)
		{
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			return $row['id'];//return the open id instead
		}
		else
		{
			return -1;//return -1 to indicate its closed
		}
	}




	function save_inactive_request($query_id='', $state_id='', $serial='',
									$meter_count='', $model_id='', $company='',
										$street='', $suburb='', $postcode='',
											$contact='', $number='', $notes='', $user_id='')
	{



		$check_status = check_status($serial, $postcode);

		if ($check_status != -1)
		{
			$return_message = "Request for this serial is pending, request id= ".$check_status."<br>\r";
		}
		else if (is_numeric($query_id) &&
			is_numeric($state_id) &&
			is_numeric($model_id) &&
			is_numeric($user_id))
		{

			$dbname = "inactives";
			mysql_select_db($dbname);
			$timestamp = time();

			echo $timestamp.": ";


			$sql = "INSERT INTO request_data
						(`query_type_id`, `state_id`, `model_id`,
							`user_id`, `serial`, `meter_count`,
								`company_name`, `street_address`, `suburb`,
									`postcode`, `contact_name`, `contact_number`,
									`additional_info`, `status`, `date`)
					VALUES
						('".$query_id."', '".$state_id."', '".$model_id."',
							'".$user_id."', '".$serial."', '".$meter_count."',
								'".$company."', '".$street."', '".$suburb."',
									'".$postcode."', '".$contact."', '".$number."',
										'".$notes."', '0', '".$timestamp."')";


			$result = mysql_query($sql)
				or die ("Error in save_inactive_request()");

			$return_message = "System response: Request saved, please wait while emailing...<br>\r";

		}
		else
		{
			$return_message = "Lookup-ID error ocurred in function save_inactive_request<br>\r";
		}

		return $return_message;

	}


	function return_state_name($state_id)
	{
		$dbname = "geographic";
		mysql_select_db($dbname);

		$sql = "SELECT state FROM state WHERE id = '".$state_id."'";

		$result = mysql_query($sql)
						or die ("Error in return_state_name()");

		if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			return $row['state'];
		}

	}


	function return_model_name($model_id)
	{
		$dbname = "copiers";
		mysql_select_db($dbname);

		$sql = "SELECT model_name FROM model WHERE id = '".$model_id."'";

		$result = mysql_query($sql)
						or die ("Error in return_model_name()");

		if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			return $row['model_name'];
		}

	}


	//this function accepts a parameter (open, closed, all)
	//returning the appropriate results, also accepts date and state filter keys
	//need to send the date_before param as a timestamp
	function return_all_requests($type='all', $date_before='', $state, $user='')
	{



		$dbname = "inactives";
		mysql_select_db($dbname);

		$sql = "SELECT * FROM request_data";


		if ($type == '' || $type == 'all')
			;//do nothing
		else if ($type == 'closed')
			$sql .= " WHERE status = '-1'";//closed
		else if ($type == 'open')
			$sql .= " WHERE status = '0'";//open


		if ($state != '' && ($type == 'all' || $type == ''))
		{
			$sql .= " WHERE state_id = '".$state."'";
		}
		else if ($state != '')
		{
			$sql .= " AND state_id = '".$state."'";
		}


		if ($user != '' && ($state != '' || $type == 'closed' || $type == 'open'))

		//if ($user != '' && (state != '' || $type == 'all' || $type == ''))
		{
			$sql .= " AND user_id = '".$user."'";
		}
		else if ($user != '')
		{
			$sql .= " WHERE user_id = '".$user."'";
		}



		$sql .= " ORDER BY id";

		//echo $sql."<br>";


		$result = mysql_query($sql)
					or die ("Error in return_all_requests()");

		$return_value = "<table cellspacing = '3' cellpadding = '1' align='center'>";

		$colour_control = 0;
		$colour = "B9C48E";



		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{

			if ($colour_control == 0)
			{
				$colour = "B9C48E";
				$colour_control = 1;
			}
			else
			{
				$colour = "FFFFFF";
				$colour_control = 0;
			}


			if (is_numeric($date_before))
			{
				if ($row['date'] < $date_before)
				{
					$return_value .= "<tr bgcolor = '".$colour."'>
										<td>".date("d/m/y",$row['date'])."</td>
										<td>".$row['serial']."</td>
										<td>".return_state_name($row['state_id'])."</td>
										<td>".$row['company_name']."</td>
										<td>".$row['contact_name']."</td>
										<td>".$row['contact_number']."</td>
										<td><a STYLE='text-decoration:none' href='explode.php?inactive_id=".$row['id']."' target='_blank'>&nbsp;Explode&nbsp;</a></td>
										<td><a STYLE='text-decoration:none' href='close.php?inactive_id=".$row['id']."'>&nbsp;Close&nbsp;</a></td>
										<td><a STYLE='text-decoration:none' href='send_reminder.php?inactive_id=".$row['id']."'>&nbsp;Send Reminder&nbsp;</a></td>
									  </tr>";
				}
				else
				{
					;
				}
			}
			else
			{
				$return_value .= "<tr bgcolor = '".$colour."'>
									<td>".date("d/m/y",$row['date'])."</td>
									<td>".$row['serial']."</td>
									<td>".return_state_name($row['state_id'])."</td>
									<td>".$row['company_name']."</td>
									<td>".$row['contact_name']."</td>
									<td>".$row['contact_number']."</td>
									<td><a STYLE='text-decoration:none' href='explode.php?inactive_id=".$row['id']."' target='_blank'>&nbsp;Explode&nbsp;</a></td>
									<td><a STYLE='text-decoration:none' href='close.php?inactive_id=".$row['id']."'>&nbsp;Close&nbsp;</a></td>
									<td><a STYLE='text-decoration:none' href='send_reminder.php?inactive_id=".$row['id']."'>&nbsp;Send Reminder&nbsp;</a></td>
				  </tr>";
			}
		}


		$return_value .= "</table>";

		return $return_value;
	}



	function return_query_name($query_id = '')
	{
		if ($query_id != '')
		{
			$dbname = "inactives";
			mysql_select_db($dbname);

			$sql = "SELECT * FROM query WHERE id = '".$query_id."'";

			$result = mysql_query($sql)
					or die ("Error in return_query_name()");

			if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				return $row['query_type_name'];
			}
		}
		else
		{
			return "Error in return_query_name(), no id provided";
		}
	}

	function search_array(&$array, $key)
	{
		$result = 0;

		foreach ($array as $val)
		{
			if ($val == $key)
			{
				$result = 1;
				break;
			}
		}

		return $result;
	}

	function return_employee_dropdown($default_user='')
		{
			//this section finds out which department of employees to display in
			//the form
			$dbname = "inactives";
			mysql_select_db($dbname);

			$sql = "SELECT dept_id FROM control_data";

			$result = mysql_query($sql);
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$dept_id = $row['dept_id'];




			//this section grabs all the member_ids for given $dept_id group
			$dbname = "employee_directory";
			mysql_select_db($dbname);

			$sql = "SELECT member_id FROM employee WHERE dept_id = '".$dept_id."' ORDER BY member_id";
			$result = mysql_query($sql);

			$member_id_list = array();

			while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				array_push($member_id_list, $row['member_id']);
			}

			//this section grabs all the names associated with the member_id list.
			$dbname = "emails";
			mysql_select_db($dbname);

			$sql = "SELECT id, name FROM members";
			$result = mysql_query($sql);
			$member_names = array();

			while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				if (search_array($member_id_list, $row['id']) == 1)
					array_push($member_names, $row['name']);
			}


			return return_dropdown(&$member_id_list, &$member_names, 'user', $member_names[array_search($default_user, $member_id_list)]);


		}


		function return_status_type($val)
		{
			if ($val == -1)
			{
				return "Closed";
			}
			else if ($val == 0)
			{
				return "Open";
			}
			else
			{
				return "Undefined, ERROR";
			}
		}

		function close_request($id)
		{
			$dbname = "inactives";
			mysql_select_db($dbname);

			$sql = "UPDATE request_data SET status = '-1' WHERE id = '".$id."'";

			$result = mysql_query($sql);

			//header('Location: view_requests.php');

		}

?>

//close request function: i added header info, it caused trouble, commented it out, now a diff part of my app isfcked.