// Copyright (C) 2007 The Trustees of Princeton University
//
// $Id$ $
//
// Get session and API handles
require_once 'plc_session.php';
global $plc, $api, $adm;
// Print header
require_once 'plc_drupal.php';
include 'plc_header.php';
require_once 'plc_functions.php';
require_once 'nifty.php';
require_once 'details.php';
drupal_set_title('Account Registration');
// Drupalish, but does not use Drupal itself to generate the form
global $person_form;
$person_form = array();
$person_form['first_name'] = array('title' => 'First name', 'required' => TRUE,
'maxlength' => 60, 'size' => 15);
$person_form['last_name'] = array('title' => 'Last name', 'required' => TRUE,
'maxlength' => 60, 'size' => 15);
$person_form['title'] = array('title' => 'Title', 'required' => FALSE,
'maxlength' => 60, 'size' => 5);
$person_form['phone'] = array('title' => 'Telephone', 'required' => FALSE,
'maxlength' => 60, 'size' => 20);
$person_form['email'] = array('title' => 'E-mail', 'required' => TRUE,
'maxlength' => 60, 'size' => 30);
$person_form['password'] = array('title' => 'Password', 'required' => TRUE,
'maxlength' => 60, 'size' => 20);
$person_form['site_ids'] = array('title' => 'Site', 'required' => TRUE);
if (0)
$person_form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
//////////////////// additional messages
$person_form['email']['comment'] = <<< EOF
Your E-mail address must be able to receive e-mail and will be
used as your $PLC_NAME username
EOF;
$person_form['site_ids']['comment'] = <<< EOF
Select the site where you belong
EOF;
if (0)
$person_form['roles']['comment'] = <<< EOF
Do not select the Principal Investigator or Technical
Contact roles unless you have spoken with the current PI of your
site, and you intend to assume either or both of these roles.
Use Command-Clic to unselect or for multiple selection
EOF;
//////////////////// parse form values and store in $person
global $person;
$person = array();
foreach ($person_form as $name => $item) {
if (!empty($_REQUEST[$name])) {
$person[$name] = $_REQUEST[$name];
}
}
// Filter out "Select a site"
if (!empty($person['site_ids'])) {
$person['site_ids'] = array_filter($person['site_ids'],
create_function('$site_id', 'return intval($site_id) > 0;'));
}
//////////////////// minimal checking
function check_form ($person) {
global $person_form;
global $adm;
// Look for missing/blank entries
$missing = array();
foreach ($person_form as $name => $item) {
if ($item['required'] && empty($person[$name])) {
$missing[] = $item['title'];
}
}
// missing fields
if (!empty($missing)) {
$warnings=array();
foreach ($missing as $field) $warnings []= "$field field is required.";
print html_div(plc_itemize($warnings),"messages error");
return FALSE;
}
// check that the email address is not already used on this peer
$email=$person['email'];
$already = $adm->GetPersons ( array('email'=>$person['email'],'peer_id'=>NULL) );
if ( ! empty ($already) ) {
print html_div(plc_itemize(array("Email $email already used !")),"messages error");
return FALSE;
}
return TRUE;
}
//////////////////// perform api calls
function register_person ($person) {
global $adm;
$errors = errors_init ();
// N.B.: site_ids and roles are ignored by AddPerson()
$person_id = $adm->AddPerson($person);
$errors = errors_record ($adm,$errors);
if (empty($errors)) {
$adm->begin();
// Add person to requested sites
foreach ($person['site_ids'] as $site_id) {
$adm->AddPersonToSite($person_id, intval($site_id));
$adm->SetPersonPrimarySite($person_id, intval($site_id));
}
// Add requested roles. Always add the user role.
$adm->AddRoleToPerson('user', $person_id);
if (!empty($person['roles'])) {
foreach ($person['roles'] as $role) {
$adm->AddRoleToPerson($role, $person_id);
}
}
// Send an e-mail containing a link back to this page, which will
// verify the given e-mail address as valid. PIs can still create
// and enable accounts on behalf of their users, they just have to
// find and enable the accounts manually after registering them.
$adm->VerifyPerson($person_id);
// Disable submit button
$person['person_id'] = $person_id;
$adm->commit();
$errors = errors_record ($adm,$errors);
}
if (!empty($errors)) {
errors_display($errors);
} else {
$email=$person['email'];
$text="Your registration request has been received. An e-mail has been sent to $email with further instructions.";
print html_div($text,"messages status");
}
}
////////////////////
if (!empty($person) && check_form ($person))
register_person($person);
$PLC_NAME = htmlspecialchars(PLC_NAME);
// E-mail address verified, go ahead and notify the PI (and possibly
// support if a PI role was requested) that a registration request was
// received.
if (!empty($_REQUEST['id']) && !empty($_REQUEST['key'])) {
$person_id = intval($_REQUEST['id']);
if ($adm->VerifyPerson($person_id, $_REQUEST['key']) != 1) {
plc_error($adm->error());
} else {
$persons = $adm->GetPersons(array($person_id));
$person = $persons[0];
// Remove the password field from the form so that it is not
// highlighted as missing.
unset($person_form['password']);
$messages = array();
$messages []= 'Your e-mail address has been verified. ';
$messages []= 'The PI(s) at your site have been notified of your account registration ';
if (! in_array('pi', $person['roles'])) {
$messages []= 'They are responsible for enabling your account.';
} else {
$support = PLC_MAIL_SUPPORT_ADDRESS;
$messages []= "They should contact $PLC_NAME Support <$support>. ";
$messages []= "$PLC_NAME Support will enable your account once authorized by your PI(s).";
}
plc_itemize($messages,"messages status");
}
}
$adm->begin();
// All defined sites
// cannot register with foreign site
$adm->GetSites(array('is_public' => TRUE, 'peer_id' => NULL,'-SORT'=>'name'),
array('site_id', 'name','enabled','peer_id'));
// All defined roles
$adm->GetRoles();
list($sites, $roles) = $adm->commit();
// Prepend invalid site to beginning of list to force user to select a site
$sites = array_merge(array(array('site_id' => -1, 'name' => 'Select a site', 'enabled' => TRUE)), $sites);
// Drop down selection box for each site
function site_option($site) {
global $person;
$site_id = $site['site_id'];
if (!empty($person['site_ids']) && in_array($site_id, $person['site_ids'])) {
$selected = 'selected="selected"';
} else {
$selected = "";
}
$option = "";
return $option;
}
$role_options = implode("\n", array_map('role_option', $roles));
$nifty=new PlekitNifty ('register','person-register','medium');
$nifty->start();
$details = new PlekitDetails(TRUE);
$details -> start();
$details->form_start(l_person_register(),array());
// Do not allow resubmits
$register_button="";
if (empty($person['person_id'])) {
$details->tr($register_button,'center');
$details->space();
}
foreach ($person_form as $name => $item) {
if ( ! empty($item['comment'])) {
$details->space();
$details->tr($item['comment'] . ":");
}
$title = $item['title'];
$required = $item['required'] ? '*' : "";
$class = $item['required'] ? "required" : "";
if ($item['required'] && !empty($person) && empty($person[$name])) {
$class .= " error";
}
// Label part
$left_part = "";
// input part
switch ($name) {
case 'site_ids':
$right_part= "";
break;
case 'roles':
$right_part="";
if (0) { /* Not letting users select PI or Tech any more. Its only lead to confusion and abuse. */
$right_part = "";
}
break;
default:
$maxlength = $item['maxlength'];
$size = $item['size'];
$value = !empty($person[$name]) ? $person[$name] : "";
$type = $name == 'password' ? "password" : "text";
$right_part = "";
break;
}
$details->th_td($left_part,$right_part);
}
// Do not allow resubmits
if (empty($person['person_id'])) {
$details->space();
$details->tr($register_button,'center');
}
$details->form_end();
$details->end();
$nifty->end();
include 'plc_footer.php';
?>