[Sahana_proj] POST variable array

Alex Lanstein aclan at conncoll.edu
Wed Mar 14 22:39:39 EDT 2007


Hi Turner,

I'd bet dollars to doughnuts that that's a bug with the sahana library.  
The problem is that the SELECT box is an array itself, as im sure you've 
noticed.  I bet the sahana function that allows you to throw an 
associative array and magically have them all be "hidden" values in the 
form doesnt support two dimensional arrays.  I suppose I could look and 
see.... yeah, see, post the form and then look at the source:

<select multiple:"multiple" style="visibility:hidden" 
name="hospital_names[]" >
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in 
<b>/var/www/sahana/mod/hr/lib_hr.inc</b> on line <b>584</b><br />
</select>

Sahana is nice enough to hide all these messages from you :-P I dont 
have a copy in front of me, but im sure that "hidden" function just 
doesnt handle MULTIPLE SELECT elements.

It's the wrong way to do it anyways - it's totally beat.  That sahana 
function should be using $_SESSION to store the variables between POSTs, 
but i digress.  I wrote up a little thing on how to do it that way at 
the bottom of my post. 

Had a couple other comments:


foreach($_POST as $key => $value) {
     if($value == "Array") {

Good use of the foreach, but doing a "if ($value" to check whether it's 
an array or not isn't the strictest of checks... after all, what if 
$value actually was a string that was "Array" :-)   use is_array()

$hospital_names = array();
$hospital_names = $_POST["hospital_names"];

Not entirely sure on this one. First off, you don't need to declare 
arrays in php.
This is just as valid:
$arr = array();
$arr[1] = "aaa";
$arr[2] = "123";

as this:
$arr[1] = "aaa";
$arr[2] = "123";

anyways, thats it.  sometime this weekend ill prob just fix that sahana 
function, if the st patty's day celebrations don't get the better of me...

have fun at ft myers professor...i unfortunately can't make it down 
(work), but i /will/ be at opening day on 4/10 :-)  so there :-P



as promised, $_SESSION rant follows:


------------
this was what i though the problem was before i actually looked at the 
live version

Although $_POST may be a superglobal, it is not a variable which keeps 
it's "state".  When you click a link (presuming its not a submit link 
off a form), it clears out $_POST.  $_POST is a client side variable - 
there is a very nifty firefox extension called urlparams which shows you 
all of $_REQUEST

The right/wrong answer for what you're trying to do, is do the "go back" 
link like this (I hope my thunderbird doesn't screw it up):

<a href="javascript:history.go(-1)">go and try again, you did it wrong</a>

That is analogous to clicking "back" on the browser - it literally POSTs 
back to the page again. 

The right answer for this is to use $_SESSION

Logic goes like this

if (!empty($_POST))
    use $_POST
else if (!empty($_SESSION) && empty($_POST))
    use $_SESSION
else
    they're at the form for the first time

if $_POST is not empty and $_SESSION is, you'll want to duplicate $_POST 
into session.  That can be done by something like this:

foreach ($_POST as $key => $value)
    $_SESSION[$key] = $value;

Depending on your php config, you may need to explicitly start the 
session with a simple session_start(). 

$_SESSION stays "stateful" while your browser session is active.  
Exactly what you would imagine a "browser session" to be, is as long as 
$_SESSION sticks around - for all intents and purposes - as long as the 
same window is open. 

-----------end not the issue------------

acl


Turner Hayes wrote:
> Hello all,
>
>    I've been having a little trouble with one of our functionalities. 
> In editing information about a hospital staff member (or adding a 
> staff member), a user is asked to select from a multi-select box which 
> hospital(s) the particular staff member is employed at. This is passed 
> in the $_POST variable to a validation function before any information 
> is actually added to the database. If there is something wrong with 
> the input, the user is informed of the problems and given a button to 
> go back and try again. I want the information that the user submitted 
> to be preserved, so that when he goes back to fix the errors, he 
> doesn't have to input all the information again. Unfortunately, the 
> array of hospitals doesn't seem to be appearing once the user is 
> redirected from the validation page. I'm probably missing something, 
> so I thought hey, the more eyes, the better.
>
> If you so desire, you can see the problem for yourself on our server: 
> http://sahana.cs.wesleyan.edu/index.php?mod=hr&act=view_all_staff 
> <http://sahana.cs.wesleyan.edu/index.php?mod=hr&act=view_all_staff>
> Click the pencil icon (courtesy of PHPmyadmin) to edit a staff member, 
> then make sure to make a fatal error, such as inputing no staff name 
> or unchecking all specialty boxes. Click "Edit staff", then click the 
> button to go back when the error page comes up. You will see that none 
> of the previously selected hospitals are selected (in fact none are 
> selected). You will also see some debugging printouts that inform you 
> that no array of hospitals was passed in $_POST. I'm attaching the 
> relevant code for your perusal: the relevant functions are 
> shn_hr_edit_staff() and shn_hr_validate_edit_staff() (lines 386-555).
>
> I sincerely apologize for the long, rambling email, but it's been 
> frustrating me. As you may guess, I've had an extraordinarily boring 
> vacation.
>
> Thank you,
>    Turner
> ------------------------------------------------------------------------
>
> <?php
>
> /**
> * This file includes all the methods for staff management in Hospital Registry
> *
> * LICENSE: This source file is subject to LGPL license
> * that is available through the world-wide-web at the following URI:
> *  {@link http://www.gnu.org/copyleft/lesser.html}
> *
> * @author Turner Hayes <thayes at wesleyan.edu>
> * @author Bach Vu Dao <bdao at wesleyan.edu>
> * @version 1.0
> * @package Sahana
> * 
> */
>
>
> /**
> * Global variable containing much-used variables throughout Sahana.
> */
> global $global;
>
> /**
> * Library functions for validating forms
> */
> require_once $global['approot']."/inc/lib_validate.inc";
> /**
> * Database handilng functions
> */
> require_once $global['approot']."/inc/handler_db.inc";
> /**
> * Library functions for the hospital registry
> */
> include_once "lib_hr.inc";
>
> /**
>  * This function will display the add staff form in the hospital registry
>  * 
>  * @author Bach Dao
>  * @author Turner Hayes <thayes at wesleyan.edu>
>  * @access public
>  * @todo fix the hospital selection box so that it will select the correct hospitals if there is a POST variable  
>  * set for the hospital selection (right now PHP doesn't seem to be passing it as an array)
>  */
> function shn_hr_add_staff(){
> 	echo "<br /><br />\$_POST:<br />";
> 	foreach($_POST as $key => $value) {
> 		if($value == "Array") {
> 			echo "(";
> 			foreach($_POST[$key] as $k => $v) {
> 				echo "$k => $v,";
> 			}
> 			echo ")";
> 		}
> 		else {
> 			echo "$key => $value<br />";
> 		}
> 	}
> 	echo "<br /><br />";
> 	
> 	echo "<h1>Add a new staff member</h1>";
> 	echo "<div id='formcontainer'>";
> 	
> 	shn_form_fopen("validate_add_staff");
> 	
> 	//general information
> 	shn_form_fsopen("General Information");
> 	echo "<div class='info'>";
> 	echo "This is general information about a staff member";
> 	echo "</div>";
> 	$extra_opts['req'] = true;
> 	shn_form_text(_("Name: "), 'name', 'size="50"', $extra_opts);  //Get name
> 	shn_form_text(_("Email: "), 'email', 'size="50"');
>
> 	shn_form_fsclose();
> 	
> 	//specialty information
> 	shn_form_fsopen("Specialties Information");
> 	echo "<div class='info'>";
> 	echo "This is information about the staff specialties";
> 	echo "</div>";
> 	
> 	$spec_names = get_field_data_from_db('shm_spec', 'name');
> 	foreach ($spec_names as $spec){
> 		$checked = null;
> 		if(isset($_POST[$spec])) {
> 			$checked = "checked";
> 		}
> 		shn_form_checkbox($spec, $spec, $checked);
> 	}
> 	shn_form_fsclose();
> 	
> 	//hospital assignment information
> 	shn_form_fsopen("Hospital Assignment Information");
> 	echo "<div class='info'>";
> 	echo "This is information about the staff hospital assignment";
> 	echo "</div>";
> 	$hosp_names = array();
> 	$hosp_names = get_field_data_from_db('shm_hospital', 'name');
>
> 	$selected_hosps = array();
> 	$selected_hosps = $_POST["hospital_names"];
>
> 	if($selected_hosps) {
> 		echo "<br /><br />\$selected_hosps:<br />";
> 		foreach($selected_hosps as $key => $value) {
> 			echo "$key => $value<br />";
> 		}
> 	}
> 	$extra_opts['value'] = $selected_hosps;
> 	shn_form_multi_select("hospital_names", $hosp_names, _("Hospitals: "), 'multiple="multiple" size=5', $extra_opts);
>
> 	shn_form_fsclose();
> 	shn_form_submit("Add staff");
> 	shn_form_fclose();
> 	print ("</div>");
> 	
> }
>
> /**
>  * This function will validate if the the input to the add hospital page is correct
>  * 
>  * @author Bach Dao
>  * @author Turner Hayes <thayes at wesleyan.edu>
>  * @access public
>  */
> function shn_hr_validate_add_staff(){
> 	// echo "<br /><br />\$_POST:<br />";
> 	// foreach($_POST as $key => $value) {
> 		// if($value == "Array") {
> 			// echo "(";
> 			// foreach($_POST[$key] as $k => $v) {
> 				// echo "$k => $v,";
> 			// }
> 			// echo ")";
> 		// }
> 		// else {
> 			// echo "$key => $value<br />";
> 		// }
> 	// }
> 	// echo "<br /><br />";
>
>
> 	$name = $_POST["name"];
> 	$email = $_POST["email"];
> 	
> 	$specs = array();
> 	$spec_names = get_field_data_from_db('shm_spec', 'name');
> 	foreach ($spec_names as $spec){
> 		if(isset($_POST[$spec])){
> 			$specs[] = $spec;
> 		}
> 	}
> 	
> 	if(!$specs) {
> 		$errors = true;
> 		add_error("You must select at least one specialty.");
> hid	}
> 	
> 	$hospital_names = array();
> 	$hospital_names = $_POST["hospital_names"];
> 	
> 	echo "<br /><br />\$hospital_names:<br />";
> 	foreach($hospital_names as $key => $value) {
> 		echo "$key => $value<br />";
> 	}
> 	
> 	if(!$hospital_names) {
> 		$errors = true;
> 		add_error("You must select at least one hospital of employment");
> 	}
> 	
> 	if(!shn_validate_field($name, 'Staff Name', 75, true)) {
> 		$errors = true;
> 	}
> 	
> 	if(!shn_valid_email($email)){
> 		$errors = true;
> 		add_error("You did not enter a valid email address.");
> 	}
> 	
> 	if($errors) {
> 		display_errors();
> 		echo "<br /><br />";
> 		echo "<form method='POST' action='index.php?mod=hr&act=add_staff'>";
> 		shn_form_hidden($_POST);
> 		echo "<input type='submit' value='Click to go back and resubmit'>";
> 		echo "</form>";
> 	}
> 	else {
> 		shn_form_hidden($_POST);
> 		shn_hr_add_staff_process();
> 	}
> 	
> }
>
> /**
>  * This function will process the data sent from the validate_add_staff function
>  * 
>  * This will perform all the sql queries
>  * @author Bach Dao
>  * @author Turner Hayes <thayes at wesleyan.edu>
>  * @access public
>  */
> function shn_hr_add_staff_process(){
> 	global $global;
> 	
> 	
> 	$name = $_POST["name"];
> 	
> 	$email = $_POST["email"];
> 	
> 	$specs = array();
> 	$spec_names = get_field_data_from_db('shm_spec', 'name');
> 	foreach ($spec_names as $spec){
> 		if(isset($_POST[$spec])){
> 			$specs[] = $spec;
> 		}
> 	}
> 	$hospital_names = $_POST["hospital_names"];
> 	
> 	$h_ids = array();
> 	//get the id of the hospital
> 	foreach($hospital_names as $h_name) {
> 		$h_id = get_field_from_id("shm_hospital", "name", $h_name, "hospital_id");
> 		$h_ids[] = $h_id;
> 	}
> 	
> 	//add the staff into the database
> 	$sql = "INSERT INTO shm_staff SET name='$name', email='$email'";
> 	$result = $global["db"]->Execute($sql);
>
> 	if($result) {
> 		//get the id of the new staff
> 		$staff_id = get_field_from_id("shm_staff", "name", $name, "staff_id");
>
> 		//for each spec add a new entry in the shm_specialdis
> 		foreach($specs as $specialty){
> 			$spec_id = get_field_from_id("shm_spec", "name", $specialty, "spec_id");
> 			$sql = "INSERT INTO shm_specialdis SET staff_id='$staff_id', spec_id='$spec_id'";
> 			$result = $global["db"]->Execute($sql);
> 			if(!$result) {
> 				echo "<b class='red'>Unable to assign to specialty $specialty.</b>\n<br />\n";
> 			}
> 		}
> 		
> 		//for each hospital add a new entry in the shm_staffdis
> 		foreach($h_ids as $h_id){
> 			$hospital = get_field_from_id("shm_hospital", "hospital_id", $h_id, "name");
> 			$sql = "INSERT INTO shm_staffdis SET staff_id='$staff_id', hospital_id='$h_id'";
> 			$result = $global["db"]->Execute($sql);
> 			if(!$result) {
> 				echo "<b class='red'>Unable to assign to hospital $hospital.</b>\n<br />\n";
> 			}
> 		}
> 		echo "<b class='red'>Added staff member $name.</b>";
> 	}
> 	else {
> 		echo "<b class='red'>Unable to add staff member $name.</b>";
> 	}
> 	shn_hr_view_all_staff();
> }
>
>
> /**
> * Displays information about each staff member in the database
> *
> * This function displays a list of all staff members in the database, with information such as name, email,
> * specialty (or specialties), and place(s) of employment. 
> *
> * @author Turner Hayes <thayes at wesleyan.edu>
> * @access public
> * @todo add paging capabilities, similar to {@link shn_hr_view_all_problems}
> */
>
> function shn_hr_view_all_staff() {
> 	global $global;
> 	
> 	echo "<h2><center>All hospital staff</center></h2>\n<br />\n<br />\n";
> 	
> 	$staff_ids = get_field_data_from_db("shm_staffdis", "staff_id");
> 	
> 	foreach($staff_ids as $staff_id) {
> 		$staff_name = get_field_from_id("shm_staff", "staff_id", $staff_id, "name");
> 		$staff_email = get_field_from_id("shm_staff", "staff_id", $staff_id, "email");
> 		$hosp_ids = get_field_data_from_db("shm_staffdis", "hospital_id", null, "staff_id", $staff_id);
> 		$spec_ids = get_field_data_from_db("shm_specialdis", "spec_id", null, "staff_id", $staff_id);
> 		
> 		echo "<h3>$staff_name<div align='right'><a href='index.php?mod=hr&act=edit_staff&id=$staff_id'><img src='../theme/default/img/edit.png' border=0 /></a>&nbsp;&nbsp;<a href='index.php?mod=hr&act=confirm_delete_staff&id=$staff_id'><img src='../theme/default/img/del.png' border=0 /></a></div></h3>\n<br />\n";
> 		echo "<b>Email: </b><a href='mailto:$staff_email'>$staff_email</a>\n<br />\n<br />\n";
> 		echo "<b>Specialties:</b>\n<br />\n<br />\n";
> 		foreach($spec_ids as $spec_id) {
> 			$spec_name = get_field_from_id("shm_spec", "spec_id", $spec_id, "name");
> 			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$spec_name\n<br />\n";
> 		}
> 		echo "<br />\n<br />\n";
> 		echo "<b>Hospitals that employ $staff_name:</b>\n<br />\n<br />\n";
> 		foreach($hosp_ids as $h_id) {
> 			$hospital = get_field_from_id("shm_hospital", "hospital_id", $h_id, "name");
> 			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?mod=hr&act=view_specific_hosp&id=$h_id'>$hospital</a>\n<br />\n";
> 		}
> 		
> 		echo "\n<br />\n<br />\n<br />\n";
> 	}
> }
>
>
>
> /**
> * Creates the confirmation message for deleting a staff member
> *
> * This function asks the user if they really do want to delete the selected staff member, and provides
> * them with a submit button to confirm their decision.
> *
> * @author Turner Hayes <thayes at wesleyan.edu>
> * @access public
> */
>
> function shn_hr_confirm_delete_staff(){
> 	echo "<h1>Delete a staff member</h1>";
> 	$staff_id = $_GET["id"];
> 	$name = get_field_from_id("shm_staff", "staff_id", $staff_id, "name");
> 	if($name) {
> 		$extra_opts['req_message']=false;
> 		shn_form_fopen("delete_staff", null, $extra_opts);
> 		echo "You are about to delete staff member <b>$name</b>. Are you sure?<br /><br />";
> 		echo  "By clicking Delete, all information about the staff member will be erased. This action is irreversible.<br /><br />";
> 		
> 		shn_form_hidden(array("id" => $staff_id));
> 		shn_form_submit("Delete staff member");
> 		shn_form_fclose();
> 	}
> 	else {
> 		echo "<b>There is no staff memeber with this id.</b>";
> 	}
> }
>
>
> /**
> * Executes the query on the database and deletes distribution information for the staff member
> *
> * @author Turner Hayes <thayes at wesleyan.edu>
> * @access public
> */
>
> function shn_hr_delete_staff(){
> 	global $global;
> 	$staff_id = $_POST["id"];
> 	$name = get_field_from_id("shm_staff", "staff_id", $staff_id, "name");
> 	
> 	$sql = "DELETE FROM shm_staff WHERE staff_id='$staff_id'";
> 	$result = $global['db']->Execute($sql);
> 	
> 	if($result){
> 		$sql = "DELETE FROM shm_staffdis WHERE staff_id='$staff_id'";
> 		$staffdis_del_result = $global['db']->Execute($sql);
> 		//we only want to delete all information about the staff member if the staff member information itself has been
> 		//successfully deleted
> 		if(!$staffdis_del_result) {
> 			echo "<b> class='red'>This staff member's hospital distribution information could not be deleted.</b><br /><br />";
> 		}
> 		$sql = "DELETE FROM shm_specialdis WHERE staff_id='$staff_id'";
> 		$specdis_del_result = $global['db']->Execute($sql);
> 		if(!$specdis_del_result) {
> 			echo "<b class='red'>This staff member's specialty distribution information could not be deleted.</b><br /><br />";
> 		}
> 		echo "<b class='red'>The staff member $name has been successfully deleted.</b>";
> 		shn_hr_view_all_staff();
> 	}
> 	else {
> 		echo "<b class='red'>The staff member $name could not be deleted.</b>";
> 		shn_hr_view_all_staff();
> 	}
> }
>
>
> /**
>  * This function will display the edit staff form in the hospital registry (staff id obtained from GET variable)
>  * 
>  * @author Turner Hayes <thayes at wesleyan.edu>
>  * @access public
>  * @todo fix the hospital selection box so that it will select the correct hospitals if there is a POST variable  
>  * set for the hospital selection (right now PHP doesn't seem to be passing it as an array)
>  */
>
> function shn_hr_edit_staff() {
> 	global $global;
> 	
> 	$staff_id = $_GET['id'];
> 	
> 	$staff_name = get_field_from_id("shm_staff", "staff_id", $staff_id, "name");
> 	$staff_email = get_field_from_id("shm_staff", "staff_id", $staff_id, "email");
> 	$staff_hosp_ids = array();
> 	$staff_hosp_ids = get_field_data_from_db("shm_staffdis", "hospital_id", null, "staff_id", $staff_id);
> 	foreach($staff_hosp_ids as $s_h_id) {
> 		$h_name = get_field_from_id("shm_hospital", "hospital_id", $s_h_id, "name");
> 		$staff_hosps[$h_name] = $h_name;
> 	}
> 	$staff_spec_ids = array();
> 	$staff_spec_ids = get_field_data_from_db("shm_specialdis", "spec_id", null, "staff_id", $staff_id);
> 	
> 	$staff_specs = array();
> 	foreach($staff_spec_ids as $s_id) {
> 		$s_name = get_field_from_id("shm_spec", "spec_id", $s_id, "name");
> 		$staff_specs[$s_name] = $s_name;
> 	}
> 	
> 	echo "<h1>Edit information for staff member $staff_name</h1>";
> 	echo "<div id='formcontainer'>";
> 	
> 	shn_form_fopen("validate_edit_staff");
> 	
> 	//general information
> 	shn_form_fsopen("General Information");
> 	echo "<div class='info'>";
> 	echo "This is general information about a staff member";
> 	echo "</div>";
> 	$extra_opts['req'] = true;
> 	$extra_opts['value'] = $staff_name;
> 	shn_form_text(_("Name: "), 'name', 'size="50"', $extra_opts);  //Get name
> 	$extra_opts['req'] = false;
> 	$extra_opts['value'] = $staff_email;
> 	shn_form_text(_("Email: "), 'email', 'size="50"', $extra_opts);
>
> 	shn_form_fsclose();
> 	
> 	//specialty information
> 	shn_form_fsopen("Specialties Information");
> 	echo "<div class='info'>";
> 	echo "This is information about the staff specialties";
> 	echo "</div>";
> 	
> 	$spec_names = array();
> 	$spec_names = get_field_data_from_db('shm_spec', 'name');
> 	foreach ($spec_names as $spec){
> 		$checked = null;
> 		
> 		if(isset($staff_specs[$spec])) {
> 			$checked = "checked";
> 		}
> 		shn_form_checkbox($spec, $spec, $checked);
> 	}
> 	shn_form_fsclose();
> 	
> 	//hospital assignment information
> 	shn_form_fsopen("Hospital Assignment Information");
> 	echo "<div class='info'>";
> 	echo "This is information about the staff hospital assignment";
> 	echo "</div>";
> 	$hosp_names = array();
> 	$hosp_names = get_field_data_from_db('shm_hospital', 'name');
>
> 	$selected_hosps = array();
> 	$selected_hosps = ($_POST["hospital_names"]) ? $_POST["hospital_names"] : $staff_hosps;
> 	
> 	echo "<br />\$_POST['hospital_names'] = ".$_POST['hospital_names']."<br />";
> 	echo "<br /><br />\$staff_hosps:<br />";
> 	foreach($staff_hosps as $key => $value) {
> 		echo "$key => $value<br />";
> 	}
>
> 	if($selected_hosps) {
> 		echo "<br /><br />\$selected_hosps:<br />";
> 		foreach($selected_hosps as $key => $value) {
> 			echo "$key => $value<br />";
> 		}
> 	}
> 	$extra_opts['value'] = $selected_hosps;
> 	shn_form_multi_select("hospital_names", $hosp_names, _("Hospitals: "), 'multiple="multiple" size=5', $extra_opts);
>
> 	shn_form_fsclose();
> 	shn_form_hidden(array("staff_id" => $staff_id));
> 	shn_form_submit("Edit staff");
> 	shn_form_fclose();
> 	print ("</div>");
> }
>
>
> /**
>  * This function will validate if the the input to the edit hospital page is correct
>  * 
>  * @author Turner Hayes <thayes at wesleyan.edu>
>  * @access public
>  */
>  
> function shn_hr_validate_edit_staff(){
> 	// echo "<br /><br />\$_POST:<br />";
> 	// foreach($_POST as $key => $value) {
> 		// if($value == "Array") {
> 			// echo "(";
> 			// foreach($_POST[$key] as $k => $v) {
> 				// echo "$k => $v,";
> 			// }
> 			// echo ")";
> 		// }
> 		// else {
> 			// echo "$key => $value<br />";
> 		// }
> 	// }
> 	// echo "<br /><br />";
>
>
> 	$name = $_POST["name"];
> 	$email = $_POST["email"];
> 	$staff_id = $_POST['staff_id'];
> 	
> 	$spec_names = get_field_data_from_db('shm_spec', 'name');
> 	foreach ($spec_names as $spec){
> 		if(isset($_POST[$spec])){
> 			$specs = true;
> 			break;
> 		}
> 	}
> 	
> 	if(!$specs) {
> 		$errors = true;
> 		add_error("You must select at least one specialty.");
> 	}
> 	
> 	$hospital_names = array();
> 	$hospital_names = $_POST["hospital_names"];
> 	
> 	// echo "<br /><br />\$hospital_names:<br />";
> 	// foreach($hospital_names as $key => $value) {
> 		// echo "$key => $value<br />";
> 	// }
> 	
> 	if(!$hospital_names) {
> 		$errors = true;
> 		add_error("You must select at least one hospital of employment");
> 	}
> 	
> 	if(!shn_validate_field($name, 'Staff Name', 75, true)) {
> 		$errors = true;
> 	}
> 	
> 	if(!shn_valid_email($email)){
> 		$errors = true;
> 		add_error("You did not enter a valid email address.");
> 	}
> 	
> 	if($errors) {
> 		display_errors();
> 		echo "<br /><br />";
> 		echo "<form method='POST' action='index.php?mod=hr&act=edit_staff&id=$staff_id'>";
> 		shn_form_hidden($_POST);
> 		echo "<input type='submit' value='Click to go back and resubmit'>";
> 		echo "</form>";
> 	}
> 	else {
> 		shn_form_hidden($_POST);
> 		shn_hr_edit_staff_process();
> 	}
> 	
> }
>
>
> /**
>  * This function will process the data sent from the validate_edit_staff function
>  * 
>  * This will perform all the sql queries
>  * @author Turner Hayes <thayes at wesleyan.edu>
>  * @access public
>  * @todo improve the specialty and hospital distribution update, as described in the code
>  */
> function shn_hr_edit_staff_process(){
> 	global $global;
> 	
> 	
> 	$name = $_POST["name"];
> 	
> 	$email = $_POST["email"];
> 	$staff_id = $_POST['staff_id'];
> 	
> 	$specs = array();
> 	$spec_names = get_field_data_from_db('shm_spec', 'name');
> 	foreach ($spec_names as $spec){
> 		if(isset($_POST[$spec])){
> 			$specs[] = $spec;
> 		}
> 	}
> 	$hospital_names = $_POST["hospital_names"];
> 	
> 	$h_ids = array();
> 	//get the ids of the hospitals
> 	foreach($hospital_names as $h_name) {
> 		$h_id = get_field_from_id("shm_hospital", "name", $h_name, "hospital_id");
> 		$h_ids[] = $h_id;
> 	}
> 	
> 	//add the staff into the database
> 	$sql = "UPDATE shm_staff SET name='$name', email='$email' WHERE staff_id='$staff_id'";
> 	echo "<br />\$sql:<br />$sql<br /><br />";
> 	
> 	$result = $global["db"]->Execute($sql);
>
> 	if($result) {
> 	
> 		/**This commented out section was an attempt at making the editing more efficient; since we don't know
> 		*  how many specialties the edited staff member will have, we need to remove all existing specialty
> 		*  distribution information before adding the new info (because there may be more or fewer specialties
> 		*  after editing, meaning we can't simply update the entries in shm_specialdis). It would be much better
> 		*  if we could update as many rows as possible, and delete any remaining rows if they end up unneeded,
> 		*  or insert new rows if there weren't enough existing entries in shm_specialdis. That's what this was meant
> 		*  to be, but it sadly didn't work. It simply deleted all entries from shm_specialdis for the staff member.
> 		*/
> 		
> 		/*$current_dis_ids = get_field_data_from_db("shm_specialdis", "dis_id", null, "staff_id", $staff_id);
> 		$num_current_specs = count($current_dis_ids);
> 		
> 		$i = 0;		//index to keep track of how many spec distributions have been updated
> 		
> 		foreach($current_dis_ids as $dis_id) {
> 			$spec_id = get_field_from_id("shm_specialdis", "dis_id", $dis_id, "specs[$i]");
> 			//if there are still distribution rows available to be updated and all distribution fields can be updated
> 			//(that is, there are fewer or equal existing dis_ids than/to the number new specialties)
> 			if(($i<$num_current_specs) && ($i < count($specs))) {
> 				$sql = "UPDATE shm_specialdis SET spec_id='$spec_id' WHERE dis_id='$dis_id'";
> 				$result = $global['db']->Execute($sql);
> 				if(!$result) {
> 					echo "<b class='red'>Unable to update specialty $specs[$i].</b>";
> 				}
> 				$i++;
> 			}
> 			//all existing dis_ids have been updated, need to add new rows until number of selected specialties
> 			else if($i < count($specs)) {
> 				$sql = "INSERT INTO shm_specialdis SET staff_id='$staff_id', spec_id='$spec_id";
> 				$result = $global['db']->Execute($sql);
> 				if(!$result) {
> 					echo "<b class='red'>Unable to add specialty $specs[$i].</b>";
> 				}
> 			}
> 			//eliminate remaining specialties for this staff member (if the new set of specialties is less than
> 			//the previous one)
> 			else if($i<$num_current_specs) {
> 				$sql = "DELETE FROM shm_specialdis WHERE dis_id=$dis_id";
> 				$result = $global['db']->Execute($sql);
> 				if(!$result) {
> 					echo "<b class='red'>Unable to remove specialty $specs[$i].</b>";
> 				}
> 			}
> 		} */
> 	
> 	
> 		//first delete all existing specialty distribution information for this staff member
> 		$sql = "DELETE FROM shm_specialdis WHERE staff_id='$staff_id'";
> 		$result = $global['db']->Execute($sql);
> 		
> 		//then, for each spec add a new entry in the shm_specialdis table
> 		foreach($specs as $specialty){
> 			$spec_id = get_field_from_id("shm_spec", "name", $specialty, "spec_id");
> 			$sql = "INSERT INTO shm_specialdis SET staff_id='$staff_id', spec_id='$spec_id'";
> 			$result = $global["db"]->Execute($sql);
> 			if(!$result) {
> 				echo "<b class='red'>Unable to assign to specialty $specialty.</b>\n<br />\n";
> 			}
> 		}
> 		
> 		//first delete all existing hospital distribution information for this staff member
> 		$sql = "DELETE FROM shm_staffdis WHERE staff_id='$staff_id'";
> 		$result = $global['db']->Execute($sql);
> 		
> 		//then, for each hospital add a new entry in the shm_staffdis table
> 		foreach($h_ids as $h_id){
> 			$hospital = get_field_from_id("shm_hospital", "hospital_id", $h_id, "name");
> 			$sql = "INSERT INTO shm_staffdis SET staff_id='$staff_id', hospital_id='$h_id'";
> 			$result = $global["db"]->Execute($sql);
> 			if(!$result) {
> 				echo "<b class='red'>Unable to assign to hospital $hospital.</b>\n<br />\n";
> 			}
> 		}
> 		echo "<b class='red'>Edited staff member $name.</b>";
> 	}
> 	else {
> 		echo "<b class='red'>Unable to edit staff member $name.</b>";
> 	}
> 	shn_hr_view_all_staff();
> }
> ?>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Sahana_proj mailing list
> Sahana_proj at lists.trincoll.edu
> http://lists.trincoll.edu/cgi-bin/mailman/listinfo/sahana_proj
>   




More information about the Sahana_proj mailing list