registration pages reviewed and nicer - check for existing email
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 18 Sep 2009 11:15:38 +0000 (11:15 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 18 Sep 2009 11:15:38 +0000 (11:15 +0000)
planetlab/css/plc_style.css
planetlab/includes/plc_functions.php
planetlab/persons/register.php
planetlab/sites/join_request.php
planetlab/sites/register.php
planetlab/sites/site_form.php
planetlab/sites/switch_site.php [deleted file]
planetlab/sites/test.php [deleted file]

index 1edabeb..7897556 100644 (file)
@@ -26,11 +26,11 @@ body {
 }
 
 .plc-warning {
-    padding: 4px;
+    padding:    4px;
     background:  #ffa223; 
 }
 .plc-error {
-    background: red;
+    background:  red;
     font-weight: bold;
 }
 
@@ -104,3 +104,21 @@ div#add_slice_in_site {
 #add_slice_choose_site {
     margin:20px ;
 }
+
+/* registrations */
+div.site-register {
+    background-color: #c8c7de;
+    padding: 5px;
+    margin: 10px;
+}
+/* site registration */
+div.site-pending {
+    background-color: #eadbc8;
+    padding: 5px;
+    margin: 10px;
+}
+div.person-register {
+    background-color: #e3d1cb;
+    padding: 5px;
+    margin: 10px;
+}
index facf215..c72c598 100644 (file)
@@ -345,11 +345,34 @@ function plc_itemize ($messages, $class="") {
 }
 
 //////////
+function truncate ($text,$numb,$etc = "...") {
+  if (strlen($text) > $numb) {
+    $text = substr($text, 0, $numb);
+    $text = $text.$etc;
+  }
+  return $text;
+}
+function html_div ($text,$class="") {
+  $html="<div";
+  if ($class) $html .= " class='$class'";
+  $html .= ">$text</div>";
+  return $html;
+}
+
 // should use the same channel as the php errors..
-function plc_error_html ($text)                { return  "<div class='plc-error'> " . $text . "</div>"; }
+function plc_error_html ($text)                { return  html_div ($text,'plc-error'); }
 function plc_error ($text)             { print plc_error_html ("Error " . $text); }
 
-function plc_errors ($errors) {
+function errors_init() { return array();}
+function errors_record ($adm, $errors) {
+  if ($adm->error()) {
+    $tmp=$adm->error();
+    $errors []= $tmp;
+  }
+  return $errors;
+}
+
+function errors_display ($errors) {
   if ($errors) {
     print( "<div class='plc-error'>" );
     print( "<p>The following errors occured:</p>" );
@@ -372,20 +395,6 @@ function plc_debug ($message,$object) {
   print "</pre>";
 }
 
-function truncate ($text,$numb,$etc = "...") {
-  if (strlen($text) > $numb) {
-    $text = substr($text, 0, $numb);
-    $text = $text.$etc;
-  }
-  return $text;
-}
-function html_div ($text,$class="") {
-  $html="<div";
-  if ($class) $html .= " class='$class'";
-  $html .= ">$text</div>";
-  return $html;
-}
-
 if (! function_exists ("drupal_set_error")) {
   function drupal_set_error ($text) {
     drupal_set_message ("<span class=error>$text</span>");
index c1840be..6232f77 100644 (file)
@@ -1,4 +1,3 @@
-
 <?php
 // $Id$
 //
@@ -34,49 +33,56 @@ global $plc, $api, $adm;
 
 // Print header
 require_once 'plc_drupal.php';
-drupal_set_title('Account Registration');
 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
-$form = array();
-$form['first_name'] = array('title' => 'First name', 'required' => TRUE,
+global $person_form;
+
+$person_form = array();
+$person_form['first_name'] = array('title' => 'First name', 'required' => TRUE,
                            'maxlength' => 60, 'size' => 15);
-$form['last_name'] = array('title' => 'Last name', 'required' => TRUE,
+$person_form['last_name'] = array('title' => 'Last name', 'required' => TRUE,
                            'maxlength' => 60, 'size' => 15);
-$form['title'] = array('title' => 'Title', 'required' => FALSE,
+$person_form['title'] = array('title' => 'Title', 'required' => FALSE,
                       'maxlength' => 60, 'size' => 5);
-$form['phone'] = array('title' => 'Telephone', 'required' => FALSE,
+$person_form['phone'] = array('title' => 'Telephone', 'required' => FALSE,
                       'maxlength' => 60, 'size' => 20);
-$form['email'] = array('title' => 'E-mail', 'required' => TRUE,
+$person_form['email'] = array('title' => 'E-mail', 'required' => TRUE,
                       'maxlength' => 60, 'size' => 30);
-$form['password'] = array('title' => 'Password', 'required' => TRUE,
+$person_form['password'] = array('title' => 'Password', 'required' => TRUE,
                       'maxlength' => 60, 'size' => 20);
-$form['site_ids'] = array('title' => 'Site', 'required' => TRUE);
+$person_form['site_ids'] = array('title' => 'Site', 'required' => TRUE);
 if (0)
-$form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
+  $person_form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
 
 //////////////////// additional messages
-$form['email']['comment'] = <<< EOF
+$person_form['email']['comment'] = <<< EOF
 Your <b>E-mail</b> address must be able to receive e-mail and will be
 used as your $PLC_NAME username
 EOF;
 
-$form['site_ids']['comment'] = <<< EOF
+$person_form['site_ids']['comment'] = <<< EOF
 Select the site where you belong 
 EOF;
 
 if (0)
-$form['roles']['comment'] = <<< EOF
+  $person_form['roles']['comment'] = <<< EOF
 Do not select the <b>Principal Investigator</b> or <b>Technical
 Contact</b> roles unless you have spoken with the current PI of your
 site, and you intend to assume either or both of these roles.
 <br> Use Command-Clic to unselect or for multiple selection
 EOF;
 
-////////////////////
+//////////////////// parse form values and store in $person
 global $person;
 $person = array();
-foreach ($form as $name => $item) {
+foreach ($person_form as $name => $item) {
   if (!empty($_REQUEST[$name])) {
     $person[$name] = $_REQUEST[$name];
   }
@@ -88,29 +94,50 @@ if (!empty($person['site_ids'])) {
                                     create_function('$site_id', 'return intval($site_id) > 0;'));
 }
 
-if (!empty($person)) {
+//////////////////// minimal checking
+function check_form ($person) {
+  global $person_form;
+  global $adm;
+  
   // Look for missing/blank entries
   $missing = array();
-  foreach ($form as $name => $item) {
+  foreach ($person_form as $name => $item) {
     if ($item['required'] && empty($person[$name])) {
       $missing[] = $item['title'];
     }
   }
+
+  // missing fields
   if (!empty($missing)) {
-    $error = "<ul>";
-    foreach ($missing as $field) {
-      $error .= "<li>$field field is required.</li>";
-    }
-    $error .= "</ul>";
+    $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;
   }
 
-  if (empty($error)) {
-    // N.B.: site_ids and roles are ignored by AddPerson()
-    $person_id = $adm->AddPerson($person);
-    $error = $adm->error();
-  }
+  return TRUE;
+}
+
+//////////////////// perform api calls
+function register_person ($person) {
+
+  global $adm;
+
+  $errors = errors_init ();
 
-  if (empty($error)) {
+  // 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
@@ -137,18 +164,22 @@ if (!empty($person)) {
     $person['person_id'] = $person_id;
 
     $adm->commit();
-    $error = $adm->error();
+    $errors = errors_record ($adm,$errors);
   }
 
-  if (!empty($error)) {
-    print '<div class="messages error">' . $error . '</div>';
+  if (!empty($errors)) {
+    errors_display($errors);
   } else {
-    print '<div class="messages status">Your registration request has been received. An e-mail has been sent to ';
-    print $person['email'];
-    print ' with further instructions.</div>';
+    $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
@@ -157,36 +188,31 @@ $PLC_NAME = htmlspecialchars(PLC_NAME);
 if (!empty($_REQUEST['id']) && !empty($_REQUEST['key'])) {
   $person_id = intval($_REQUEST['id']);
   if ($adm->VerifyPerson($person_id, $_REQUEST['key']) != 1) {
-    print '<div class="messages error">' . $adm->error() . '.</div>';
+    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($form['password']);
+    unset($person_form['password']);
 
-    print '<div class="messages status">';
-    print 'Your e-mail address has been verified. ';
-    print 'The PI(s) at your site have been notified of your account registration ';
+    $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'])) {
-      $support = PLC_MAIL_SUPPORT_ADDRESS;
-      print " and should contact <a href=\"mailto:$support\">$PLC_NAME Support <$support></a>. ";
-      print " $PLC_NAME Support will enable your account if authorized by your PI(s).";
+    if (! in_array('pi', $person['roles'])) {
+      $messages []= 'They are responsible for enabling your account.';
     } else {
-      print ' and are responsible for enabling your account.';
+      $support = PLC_MAIL_SUPPORT_ADDRESS;
+      $messages []= "They should contact <a href=\"mailto:$support\">$PLC_NAME Support <$support></a>. ";
+      $messages []= "$PLC_NAME Support will enable your account once authorized by your PI(s).";
     }
 
-    print '</div>';
+    plc_itemize($messages,"messages status");
   }
 }
 
-$self = $_SERVER['PHP_SELF'];
-if (!empty($_SERVER['QUERY_STRING'])) {
-  $self .= "?" . $_SERVER['QUERY_STRING'];
-}
-
 $adm->begin();
 
 // All defined sites
@@ -213,7 +239,7 @@ function site_option($site) {
     $selected = "";
   }
 
-  $option = "<option value=\"$site_id\" $selected";
+  $option = "<option value='$site_id' $selected";
   if ( ! $site['enabled'] )
     $option .= " disabled='disabled'";
   $option .= ">";
@@ -263,25 +289,25 @@ function role_option($role) {
 }
 $role_options = implode("\n", array_map('role_option', $roles));
 
-$self = $_SERVER['PHP_SELF'];
-if (!empty($_SERVER['QUERY_STRING'])) {
-  $self .= "?" . $_SERVER['QUERY_STRING'];
-}
-
-print <<<EOF
-<div class="content">
 
-<form action="$self" method="post">
+$nifty=new PlekitNifty ('register','person-register','medium');
+$nifty->start();
+$details = new PlekitDetails(TRUE);
+$details -> start();
+$details->form_start(l_person_register(),array());
 
-<table border="0" cellpadding="5" cellspacing="0">
-EOF;
+// Do not allow resubmits
+$register_button="<input type='submit' name='op' value='Register'  class='form-submit' />";
+if (empty($person['person_id'])) {
+  $details->tr($register_button,'center');
+  $details->space();
+}
 
-foreach ($form as $name => $item) {
+foreach ($person_form as $name => $item) {
 
   if ( ! empty($item['comment'])) {
-    $comment=$item['comment'];
-    print "<tr><td colspan='2'> &nbsp; </td></tr>";
-    print "<tr><td colspan='2'> $comment: </td></tr>";
+    $details->space();
+    $details->tr($item['comment'] . ":");
   }
 
   $title = $item['title'];
@@ -292,29 +318,19 @@ foreach ($form as $name => $item) {
   }
 
   // Label part
-  print "<tr>";
-  print <<<EOF
-    <td><label class="$class" for="edit-$name">$title: $required</label></td>\n
-EOF;
+  $left_part = "<label class='$class' for='edit-$name'>$title: $required</label>";
 
   // input part
   switch ($name) {
 
   case 'site_ids':
-    print <<<EOF
-        <td><select name="site_ids[]" id="edit-site_ids" class="form-select $class">
-         $site_options
-        </select></td>\n
-EOF;
+    $right_part= "<select name='site_ids[]' id='edit-site_ids' class='form-select $class'> $site_options </select>";
     break;
 
   case 'roles':
+    $right_part="";
     if (0) { /* Not letting users select PI or Tech any more.  Its only lead to confusion and abuse. */
-    print <<<EOF
-        <td><select name="roles[]" multiple="multiple" id="edit-roles" class="form-select $class">
-          $role_options
-        </select></td>\n
-EOF;
+      $right_part = "<select name='roles[]' multiple='multiple' id='edit-roles' class='form-select $class'> $role_options </select>";
     }
     break;
 
@@ -323,26 +339,22 @@ EOF;
     $size = $item['size'];
     $value = !empty($person[$name]) ? $person[$name] : "";
     $type = $name == 'password' ? "password" : "text";
-    print <<<EOF
-       <td><input type="$type" maxlength="$maxlength" name="$name" id="edit-$name" size="$size" value="$value" class="form-text $class"></td>\n
-EOF;
-
+    $right_part = "<input type='$type' maxlength='$maxlength' name='$name' id='edit-$name' size='$size' value='$value' class='form-text $class'>";
+    break;
   }
 
-  print "</tr>\n";
+  $details->th_td($left_part,$right_part);
 }
 
 // Do not allow resubmits
 if (empty($person['person_id'])) {
-  print '<tr><td colspan="2"><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
+  $details->space();
+  $details->tr($register_button,'center');
 }
 
-print <<<EOF
-</table>
-
-</form>
-</div>
-EOF;
+$details->form_end();
+$details->end();
+$nifty->end();
 
 include 'plc_footer.php';
 
index d125a37..3a5e4e0 100644 (file)
@@ -11,13 +11,15 @@ require_once 'plc_login.php';
 require_once 'plc_session.php';
 global $plc, $api;
 
-// Common functions
-require_once 'plc_functions.php';
-
 // Print header
 require_once 'plc_drupal.php';
 include 'plc_header.php';
 
+// Common functions
+require_once 'plc_functions.php';
+require_once 'details.php';
+require_once 'nifty.php';
+
 include 'site_form.php';
 
 ////////////////////////////////////////
@@ -83,41 +85,36 @@ function render_join_request_review($api, $site_id) {
 <input type="hidden" name="address_id" value="$address_id">
 <input type="hidden" name="tech_id" value="$tech_id">
 <input type="hidden" name="site_id" value="$site_id">
-
-<table border="0" width="100%" cellspacing="0" cellpadding="3">
 EOF;
 
   $site_form = build_site_form(FALSE);
   $input = array ('site' => $site, 'address'=> $address, 'pi' => $pi, 'tech' => $tech);
   
+  $nifty=new PlekitNifty ('pending','site-pending','medium');
+
+  $nifty->start();
+  $details = new PlekitDetails(TRUE);
+  $details->start();
+
   // display the buttons 
-  print <<< EOF
-    <tr>
-    <td colspan='2'>
+  $buttons_row =<<<EOF
     <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr> 
     <td align=center><input type="submit" name="submitted" value="Delete"></td>
     <td align=center><input type="submit" name="submitted" value="Update"></td>
     <td align=center><input type="submit" name="submitted" value="Approve"></td>
     </tr> </table>
-    </tr>
 EOF;
 
+  $details->tr($buttons_row,'center');
   // render the form - not supposed to be empty
-  form_render_table2 ($site_form, $input, TRUE);
+  form_render_details ($details,$site_form, $input, TRUE);
 
-  // display the buttons 
-  print <<< EOF
-    <tr>
-    <td colspan='2'>
-    <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr> 
-    <td align=center><input type="submit" name="submitted" value="Delete"></td>
-    <td align=center><input type="submit" name="submitted" value="Update"></td>
-    <td align=center><input type="submit" name="submitted" value="Approve"></td>
-    </tr> </table>
-    </tr>
-EOF;
+  // display the buttons again
+  $details->tr($buttons_row,'center');
+
+  $details->end();
+  $nifty->end();
 
-  print "</table></form>";
 }
 
 function notify_enabled_pi ($api, $pi_id, $pi, $site_id, $site) {
index b9ac957..7a79eda 100644 (file)
@@ -16,7 +16,9 @@ require_once 'plc_drupal.php';
 drupal_set_title('New Site Registration');
 include 'plc_header.php';
 
-include 'site_form.php';
+require_once 'site_form.php';
+require_once 'details.php';
+require_once 'nifty.php';
 
 $verbose = FALSE;
 
@@ -195,23 +197,32 @@ print <<<EOF
 
 <form action="$self" method="post">
 
-<table border="0" width="100%" cellspacing="0" cellpadding="3">
 EOF;
 
+$nifty=new PlekitNifty ('register','site-register','medium');
+$nifty->start();
+$details = new PlekitDetails(TRUE);
+$details -> start();
+
+$register_button='<input type="submit" name="op" value="Register"  class="form-submit" />';
+
 // Do not allow resubmits
 if (empty($site['site_id'])) {
-  print '<tr><td colspan=2 align=center><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
+  $details->tr($register_button,'center');
 }
 
-form_render_table2 ($site_form, $input, ! $empty_form);
+form_render_details ($details, $site_form, $input, ! $empty_form);
 
 // Do not allow resubmits
 if (empty($site['site_id'])) {
-  print '<tr><td colspan=2> &nbsp; </td></tr>';
-  print '<tr><td colspan=2 align=center><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
+  // some space
+  $details->space();
+  $details->tr($register_button,'center');
 }
 
-print "</table></form></div>";
+$details -> end();
+$nifty->end();
+print "</form></div>";
 
 include 'plc_footer.php';
 
index bc54821..e77d781 100644 (file)
@@ -180,16 +180,16 @@ function form_check_required ($form, $input) {
 // if $outline_missing is set, missing required fields are outlined
 // fields typed as 'password' are displayed differently
 // expected to be embedded in a table with 2 columns
-function form_render_table2 ($form, $input, $outline_missing) {
+function form_render_details ($details, $site_form, $input, $outline_missing) {
 
-  foreach ($form as $fullname => $item) {
+  foreach ($site_form as $fullname => $item) {
     
     list($objname,$field) = split(":",$fullname);
     
     // render the comment field
     if ( ! empty($item['comment'])) {
-      $comment=$item['comment'];
-      print "<tr><td colspan='2'> $comment </td></tr>";
+      $details->space();
+      $details->tr ($item['comment'] . ":");
     }
 
     // compute line attributes
@@ -201,10 +201,7 @@ function form_render_table2 ($form, $input, $outline_missing) {
     }
 
     // Label part
-    print "<tr>";
-    print <<<EOF
-      <td> <label class="$class" for="edit-$fullname">$title: $required</label> </td>\n
-EOF;
+    $left_part = "<label class='$class' for='edit-$fullname'>$title: $required</label>";
 
     // input part
     if ($item['type'] == 'boolean') {
@@ -221,25 +218,23 @@ EOF;
        $checkedyes = "";
        $checkedno = "checked='checked'";
       }
-      print <<< EOF
-<td>
+      $right_part = <<<EOF
 <input type='radio' id="check-$fullname" name="$fullname" value="yes" $checkedyes> Yes
 <input type='radio' id="check-$fullname" name="$fullname" value="no"  $checkedno> No
-</td>\n
 EOF;
     } else {
       $type = ($item['type'] == 'password') ? "password" : "text";
       $value = !empty($input[$objname][$field]) ? $input[$objname][$field] : "";
       $maxlength = $item['maxlength'];
       $size = $item['size'];
-      print <<<EOF
-<td><input type="$type" id="edit-$fullname" name="$fullname" value="$value" 
+      $right_part= <<<EOF
+<input type="$type" id="edit-$fullname" name="$fullname" value="$value" 
 size="$size" maxlength="$maxlength" 
-class="form-text $class" /> </td>\n
+class="form-text $class" /> 
 EOF;
     }
 
-    print "</tr>\n";
+    $details->th_td($left_part,$right_part);
   }
 }
 
diff --git a/planetlab/sites/switch_site.php b/planetlab/sites/switch_site.php
deleted file mode 100644 (file)
index b1678ba..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-<?php
-
-// Require login
-require_once 'plc_login.php';
-
-// Get session and API handles
-require_once 'plc_session.php';
-global $plc, $api;
-
-
-// Common functions
-require_once 'plc_functions.php';
-
-// find person roles
-$_person= $plc->person;
-$_roles= $_person['role_ids'];
-
-
-// if no site id redirect
-if( !$_GET['id'] ) 
-  plc_redirect(l_sites());
-
-// get site_id
-$site_id= $_GET['id'];
-
-// if submitted
-if( $_POST['submitted'] ) {
-  $new_site= $_POST['new_site'];
-  
-  // no primary site anymore..........
-
-}
-
-
-// Print header
-require_once 'plc_drupal.php';
-drupal_set_title('Sites');
-include 'plc_header.php';
-
-
-// if admin list all sites, else list just persons sites
-if( in_array( '10', $_roles ) ) 
-  $site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
-else 
-  $site_info= $api->GetSites( $_person['site_ids'], array( "site_id", "name" ) );
-
-
-// start form
-echo "<from method=post action='switch_site.php?id=$site_id'>\n";
-echo "<h2>Switch Site</h2>\n";
-echo "Change active site to: \n";
-echo "<p><select name='new_site'>\n";
-
-// out puts site names and ids
-foreach( $site_info as $site ) {
-  echo "<option value='". $site['site_id'] ."'>". $site['name'] ."</option>\n";
-
-}
-
-echo "</select>\n";
-echo "<p>This will change your Primary Site.\n";
-echo "<p><input type=submit name='submitted' value='Switch Site'>\n";
-
-echo "<p><a href='index.php?id=$site_id'>Back to Site</a>\n";
-
-echo "</form>\n";
-
-
-
-// Print footer
-include 'plc_footer.php';
-
-?>
diff --git a/planetlab/sites/test.php b/planetlab/sites/test.php
deleted file mode 100644 (file)
index 87bd436..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-// $Id$
-
-// Get session and API handles
-require_once 'plc_session.php';
-global $plc, $api, $adm;
-
-// input 
-$input = $_GET['input'];
-$len = strlen($input);
-
-// init result
-$aResults = array();
-
-// dont query the db on empty input
-if ($len) {
-  // query db
-  $input .= "%";
-  $sites= $adm->GetSites( array( "name" => $input ), array("name") );
-  if (count($sites)) {
-    foreach ( $sites as $site ) {
-      $aResults[] = $site['name'];
-    }
-  }
-}
-
-header("Content-Type: text/xml");
-
-echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
-<results>";
-for ($i=0;$i<count($aResults);$i++)
-  echo"        <rs>".$aResults[$i]."</rs>";
-
-echo "
-</results>
-";
-
-?>