call NotifyPersons() using the admin account instead for the client's acocunt
[plewww.git] / planetlab / sites / join_request.php
index ce602b4..f0b5562 100644 (file)
@@ -9,32 +9,57 @@ 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';
+require_once 'plc_api.php'; 
+global $plc, $api, $adm;
 
 // 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';
+require_once 'table.php';
+require_once 'nifty.php';
+
 include 'site_form.php';
 
 ////////////////////////////////////////
 function render_all_join_requests($api) {
-  $sites = $api->GetSites( array("enabled" => False, "peer_id" => NULL));
-  if (!empty($sites)) {
-    print("<table width=100%>");
-    print("<tr><th>Site Name</th><th>site_id</th><th>Submitted</th></tr>");
-    foreach($sites as $site) {
-      printf("<tr><td><a href=/db/sites/join_request.php?review=t&site_id=%d> %s </a> </td>", $site['site_id'], $site['name']);
-      printf("<td><a href=/db/sites/join_request.php?review=t&site_id=%d> %d </a> </td>", $site['site_id'], $site['site_id']);
-      printf("<td> %s </td> </tr>", date("d F Y, G:i",$site['date_created']));
-    }
-    print("</table>");
-  } else {
+  global $PENDING_CONSORTIUM_ID;
+  $filter = array("peer_id" => NULL, "ext_consortium_id" => $PENDING_CONSORTIUM_ID);
+  $columns=array('site_id','name','enabled','date_created');
+  $sites = $api->GetSites( $filter, $columns);
+  if (empty($sites)) {
     print("<p> No open join requests </p>");
+    return;
   }
+  $headers = array();
+  $headers['id']='int';
+  $headers['Site Name']='string';
+  # trying the sortEnglishDateTime stuff - not too lucky so far
+  # http://www.frequency-decoder.com/demo/table-sort-revisited/custom-sort-functions/
+  $headers['submitted']='sortEnglishDateTime';
+  $headers['enabled']='string';
+  
+  $nifty=new PlekitNifty ('pending','sites-pending','medium');
+  $nifty->start();
+  $table=new PlekitTable ('pending',$headers,2,
+                         array('pagesize_area'=>FALSE,'pagesize'=>10000));
+  $table->start();
+  foreach($sites as $site) {
+    $site_id=$site['site_id'];
+    $link = $site['enabled'] ? l_site($site_id) : l_site_review_pending($site_id);
+    $table->row_start();
+    $table->cell(href($link,$site['site_id']));
+    $table->cell(href($link,$site['name']));
+    $table->cell(date("dS F Y",$site['date_created']) . " at " . date("G:i",$site['date_created']));
+    $table->cell( $site['enabled'] ? plc_warning_html('yes') : 'no');
+    $table->row_end();
+  }
+  $table->end();
+  $nifty->end();
 }
 
 function render_join_request_review($api, $site_id) {
@@ -44,23 +69,25 @@ function render_join_request_review($api, $site_id) {
       return;
   }
   $site = $sites[0];
-  if ($site['enabled']) {
+  if ($site['enabled'] && $site['ext_consortium_id'] === $PENDING_CONSORTIUM_ID) {
     print("<p class='plc-warning'> This site is already enabled </p>");
     return;
   }
   $addresses = $api->GetAddresses ($site['address_ids']);
   if (empty ($addresses)) {
-      print("<p class='plc-warning'> No address found for site_id=$site_id</p> ");
-      return ;
+    drupal_set_message("Site $site_id has no address - never mind");
+    $address=array('line1'=>'');
+    $address_id=0;
+  } else {
+    $address = $addresses[0];
+    $address_id=$address['address_id'];
   }
-  $address = $addresses[0];
-  $address_id=$address['address_id'];
 # just in case there is no person attached yet
   if (empty ($site['person_ids'])) {
     $persons=array();
   } else {
     $person_ids = $site['person_ids'];
-    $persons = $api->GetPersons( $person_ids, array( "person_id", "role_ids", "first_name", "last_name", "email" , "phone") );
+    $persons = $api->GetPersons( $person_ids, array( "person_id", "role_ids", "first_name", "last_name", "title", "email" , "phone") );
   }
   $tech = Null;
   $pi = Null;
@@ -74,7 +101,7 @@ function render_join_request_review($api, $site_id) {
   }
   $pi_id = $pi['person_id'];
   $tech_id = $tech['person_id'];
-    
+
   print <<< EOF
 <p> Please review the join request below.</p>
     <p> <b> Warning:</b> the PI email address that was provided in this form will <b> not be checked</b> automatically. We expect that as part of the handshake with the site, the support team has had an opportunity to use this address so it can be considered safe. Please check it manually if this is not the case.</p>
@@ -83,41 +110,37 @@ 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;
 
+  // don't render the tech part if that was the same as the pi
   $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) {
@@ -153,7 +176,7 @@ EOF;
               PLC_MAIL_SUPPORT_ADDRESS);
    
  $subject="Site registration for " . $site['name'] . " has been approved by " . PLC_NAME;
- $api->NotifyPersons(array($pi_id),$subject,$body);
+ $adm->NotifyPersons(array($pi_id),$subject,$body);
 }
 
 
@@ -218,9 +241,10 @@ else if ($_POST['submitted'] )
     case 'Update': {
       $api->begin();
       $api->UpdateSite($site_id,$site);
-      $api->UpdateAddress($address_id,$address);
+      if ($address_id) $api->UpdateAddress($address_id,$address);
+      else if(!empty($address)) $api->AddSiteAddress($site_id,$address);
       $api->UpdatePerson($pi_id,$pi);
-      $api->UpdatePerson($tech_id,$tech);
+      if ($tech_id != $pi_id) $api->UpdatePerson($tech_id,$tech);
       $api->commit();
       $api_error=$api->error();
       if (!empty($api_error)) {
@@ -237,16 +261,18 @@ else if ($_POST['submitted'] )
       // the PI's email address, which makes the whole thing *a lot* simpler
       // enable the site, enable the PI, and VerifyPerson the thec if different from the PI
       $site['enabled'] = True;
+      global $APPROVED_CONSORTIUM_ID;
+      $site['ext_consortium_id'] = $APPROVED_CONSORTIUM_ID;
       $api->UpdateSite ($site_id,$site);
       $api_error=$api->error();
       if (!empty($api_error)) {
        $error .= $api->error();
        $messages [] = "Could not enable site";
       } else {
-       $messages[] = "Site " . $site['name'] . " enabled";
+       $messages[] = l_site_t ($site_id,"Site '" . $site['name'] . "' enabled");
       }
       
-      if (empty ($error)) {
+      if (empty ($error) && $address_id) {
        // Update Address
        $api->UpdateAddress($address_id,$address);
        $api_error=$api->error();