set_submit_name(($action=="addform") ? __("Add") : __("Modify"));
if ( ($action=="modform") AND (!$book->been_here()) ) { // catch the empty ID
// grab record number "id"
$r = freemed::get_link_rec ($_REQUEST['id'], $table_name);
// Pull into the global scope
foreach ($r AS $k => $v) {
global ${$k};
${$k} = stripslashes($v);
}
// expand the arrays
$userphy = sql_expand ( $userphy );
$userfac = sql_expand ( $userfac );
$userphygrp = sql_expand ( $userphygrp );
// make sure default & verify are the same, so no errors
$userpassword1 = $userpassword2 = $userpassword;
// Use userlevel to determine flags
if ($userlevel > 0) {
$power = 0; unset ($_userlevel);
while (pow(2,$power) <= $userlevel) {
// Check and add if so
if (pow(2, $power) & $userlevel) {
// Add it...
$_userlevel[(pow(2,$power))] = pow(2,$power);
}
// Increment the current power...
$power++;
} // end looping...
// Pass _userlevel to userlevel
$userlevel = $_userlevel;
} else {
// Kludge for html_form::checkbox_widget to detect array
$userlevel = array ( 0 );
}
} // second modform if
if ($action=="addform") {
$page_title = __("Add User");
if (!$book->been_here()) {
global $userlevel; $userlevel = array( -1 );
}
} // addform if
// now the body
$phy_q = "SELECT * FROM physician WHERE phyref='no' ".
"ORDER BY phylname,phyfname";
$phy_r = $sql->query($phy_q);
// fetch all in-house docs
$book->add_page(
__("User"),
array (
"username", "userpassword", "userpassword1", "userpassword2",
"userdescrip", "userlevel", "usertype", "userrealphy"
),
html_form::form_table(array(
__("Username") =>
html_form::text_widget("username", 16),
__("Password") =>
"",
__("Password (Verify)") =>
"",
__("Description") =>
html_form::text_widget("userdescrip", 20, 50),
__("User level") =>
html_form::checkbox_widget(
"userlevel",
USER_ADMIN,
"Administrator"
).
" \n".
html_form::checkbox_widget(
"userlevel",
USER_DATABASE,
"Database Access"
).
" \n".
html_form::checkbox_widget(
"userlevel",
USER_DELETE,
"Delete Permission"
).
" \n".
html_form::checkbox_widget(
"userlevel",
USER_DISABLED,
"Disabled/Locked Out"
),
__("User type") =>
html_form::select_widget(
"usertype",
array(
__("Physician") => "phy",
__("Miscellaneous") => "misc"
)
),
__("Actual Physician") =>
freemed_display_selectbox($phy_r, "#phylname#, #phyfname#", "userrealphy")
))
);
$book->add_page(
__("Authorize"),
array (
"userfac", "userphy", "userphygrp"
),
"
".__("Authorized facilities")." :
".freemed::multiple_choice (
"SELECT CONCAT(psrname, ' (', psrcity, ', ', psrstate, ')') AS myfac FROM facility ORDER BY myfac", "myfac", "userfac", fm_join_from_array($userfac))."
".__("Authorized physicians")."
".freemed::multiple_choice ("SELECT * FROM physician ORDER BY phylname,
phyfname, phymname", "##phylname##, ##phyfname## ##phymname##", "userphy",
fm_join_from_array($userphy))."
".__("Authorized physician groups")."
".freemed::multiple_choice ("SELECT * FROM phygroup ORDER BY
phygroupname", "phygroupname", "userphygrp",
fm_join_from_array($userphygrp))."
"
);
// Handle "Cancel"
if ($book->is_cancelled()) {
Header("Location: ".$page_name);
die("");
}
if (!( $book->is_done() )) {
$display_buffer .= "
\n".$book->display()."
\n";
} else { // now the add/mod code itself
// Assemble flags
$flags = 0;
if (is_array($userlevel)) {
foreach($userlevel AS $k => $v) {
$flags |= $v;
}
}
//Fred Trotter
// in either case below we need the md5hash
// of the password
$md5_pass=md5($userpassword1);
if ($action=="mod" || $action=="modform") {
// Figure out whether we changed the password, or whether it is just
// being re-passed
if (strlen($userpassword1)==32) {
// Length 32 = passed MD5 password, pass as is
$_pass = $userpassword1;
} else {
// Otherwise use the hash
$_pass = $md5_pass;
}
$display_buffer .= "
".__("Modifying")." . . .
";
// build update query:
// only set the values that need to be
// changed... for example, don't set the
// creation date in a modify. also,
// remember the commas...
$query = $sql->update_query($table_name,
array (
"username" => $username,
"userpassword" => $_pass,
"userdescrip" => $userdescrip,
"userlevel" => ($flags+0),
"usertype" => $usertype,
"userfac" => sql_squash(array_unique($userfac)),
"userphy" => sql_squash(array_unique($userphy)),
"userphygrp" => sql_squash(array_unique($userphygrp)),
"userrealphy" => $userrealphy
),
array ( "id" => $id )
);
} else { // now the "add" guts
$display_buffer .= "
";
// Set automatic refresh
$refresh = $page_name;
} // if 'done'
break;
case "del":
$page_title = __("Deleting User");
// select only "id" record, and delete
if ($id != 1)
$result = $sql->query("DELETE FROM $table_name ".
"WHERE id='".addslashes($id)."'");
else { // if we tried to delete admin!!!
$display_buffer .= "
".__("You cannot delete admin!")."
";
template_display();
}
$display_buffer .= "
$record_name ".__("Deleted")."
".__("Go back to user menu")."
";
// Set automatic refresh
$refresh = $page_name."?action=view";
break;
default:
// with no anythings, ?action=search returns everything
// in the database for modification... useful to note in
// future...
// TODO: MIGRATE THIS TO freemed_display_itemlist FUNCTION
// OR MAKE IT A MODULE, INHEIRITING FROM THE MAINTENANCE
// MODULE
$query = "SELECT * FROM ".addslashes($table_name)." ".
"ORDER BY ".addslashes($order_field);
$result = $sql->query($query);
if ($result) {
$page_title = __("Users");
$display_buffer .= "
";
if (strlen($_ref)<5) {
$_ref="main.php";
} // if no ref, then return to home page...
} else {
$display_buffer .= "\n".__("No record found with that criteria.")."\n";
}
} // end master action switch
template_display();
?>