cleanup for plc_sorts.php removal.
[plewww.git] / planetlab / sites / join_request.php
1 <?php
2 // $Id$
3 //
4 // page for administration of pending site registration requests
5 //
6
7 // Require login
8 require_once 'plc_login.php';
9
10 // Get session and API handles
11 require_once 'plc_session.php';
12 global $plc, $api;
13
14 // Common functions
15 require_once 'plc_functions.php';
16
17 // Print header
18 require_once 'plc_drupal.php';
19 include 'plc_header.php';
20
21 include 'site_form.php';
22
23 ////////////////////////////////////////
24 function render_all_join_requests($api) {
25   $sites = $api->GetSites( array("enabled" => False, "peer_id" => NULL));
26   if (!empty($sites)) {
27     print("<table width=100%>");
28     print("<tr><th>Site Name</th><th>site_id</th><th>Submitted</th></tr>");
29     foreach($sites as $site) {
30       printf("<tr><td><a href=/db/sites/join_request.php?review=t&site_id=%d> %s </a> </td>", $site['site_id'], $site['name']);
31       printf("<td><a href=/db/sites/join_request.php?review=t&site_id=%d> %d </a> </td>", $site['site_id'], $site['site_id']);
32       printf("<td> %s </td> </tr>", date("d F Y, G:i",$site['date_created']));
33     }
34     print("</table>");
35   } else {
36     print("<p> No open join requests </p>");
37   }
38 }
39
40 function render_join_request_review($api, $site_id) {
41   $sites = $api->GetSites( array(intval($site_id)) );
42   if (empty($sites)) {
43       print("<p class='plc-warning'> Invalid request with site_id=$site_id</p> ");
44       return;
45   }
46   $site = $sites[0];
47   if ($site['enabled']) {
48     print("<p class='plc-warning'> This site is already enabled </p>");
49     return;
50   }
51   $addresses = $api->GetAddresses ($site['address_ids']);
52   if (empty ($addresses)) {
53       print("<p class='plc-warning'> No address found for site_id=$site_id</p> ");
54       return ;
55   }
56   $address = $addresses[0];
57   $address_id=$address['address_id'];
58 # just in case there is no person attached yet
59   if (empty ($site['person_ids'])) {
60     $persons=array();
61   } else {
62     $person_ids = $site['person_ids'];
63     $persons = $api->GetPersons( $person_ids, array( "person_id", "role_ids", "first_name", "last_name", "email" , "phone") );
64   }
65   $tech = Null;
66   $pi = Null;
67   foreach($persons as $person) {
68     if ( in_array('20',  $person['role_ids']) ) {
69       $pi = $person;
70     }
71     if ( in_array('40',  $person['role_ids']) ) {
72       $tech = $person;
73     }
74   }
75   $pi_id = $pi['person_id'];
76   $tech_id = $tech['person_id'];
77     
78   print <<< EOF
79 <p> Please review the join request below.</p>
80     <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>
81 <form name="join_request" method="post" action="/db/sites/join_request.php">
82 <input type="hidden" name="pi_id" value="$pi_id">
83 <input type="hidden" name="address_id" value="$address_id">
84 <input type="hidden" name="tech_id" value="$tech_id">
85 <input type="hidden" name="site_id" value="$site_id">
86
87 <table border="0" width="100%" cellspacing="0" cellpadding="3">
88 EOF;
89
90   $site_form = build_site_form(FALSE);
91   $input = array ('site' => $site, 'address'=> $address, 'pi' => $pi, 'tech' => $tech);
92   
93   // display the buttons 
94   print <<< EOF
95     <tr>
96     <td colspan='2'>
97     <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr> 
98     <td align=center><input type="submit" name="submitted" value="Delete"></td>
99     <td align=center><input type="submit" name="submitted" value="Update"></td>
100     <td align=center><input type="submit" name="submitted" value="Approve"></td>
101     </tr> </table>
102     </tr>
103 EOF;
104
105   // render the form - not supposed to be empty
106   form_render_table2 ($site_form, $input, TRUE);
107
108   // display the buttons 
109   print <<< EOF
110     <tr>
111     <td colspan='2'>
112     <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr> 
113     <td align=center><input type="submit" name="submitted" value="Delete"></td>
114     <td align=center><input type="submit" name="submitted" value="Update"></td>
115     <td align=center><input type="submit" name="submitted" value="Approve"></td>
116     </tr> </table>
117     </tr>
118 EOF;
119
120   print "</table></form>";
121 }
122
123 function notify_enabled_pi ($api, $pi_id, $pi, $site_id, $site) {
124   // notify the PI
125   $template= <<<EOF
126 You have filed a site registration with the %s platform.
127
128 This registration has been approved, and your account was enabled
129 You were granted a PI role, meaning that you will be responsible 
130 for managing personal accounts and slices for your site
131
132 You can now enter the system at
133 https://%s:%d/
134 with %s as a login, 
135 and the password that you provided at registration-time
136
137 You can directly access your site information at
138 https://%s:%d/db/sites/index.php?id=%d
139
140 Please start with adding nodes for this site with
141 https://%s:%d/db/nodes/node_add.php
142
143 Our support team will be glad to answer any question that you might have
144 They are reachable at mailto:%s
145 EOF;
146  
147  $body=sprintf($template,
148                PLC_NAME,
149                PLC_WWW_HOST,PLC_WWW_SSL_PORT,
150                $pi['email'],
151                PLC_WWW_HOST,PLC_WWW_SSL_PORT,$site_id,
152                PLC_WWW_HOST,PLC_WWW_SSL_PORT,
153                PLC_MAIL_SUPPORT_ADDRESS);
154    
155  $subject="Site registration for " . $site['name'] . " has been approved by " . PLC_NAME;
156  $api->NotifyPersons(array($pi_id),$subject,$body);
157 }
158
159
160 // find person roles
161 $_person= $plc->person;
162 $_roles= $_person['role_ids'];
163
164 // only admins are allowed to view this page
165 if( !in_array( '10', $_roles ) ) {
166
167     print("<p> not allowed to view this page </p>");
168 }
169 else if ($_GET['review'])
170 {
171
172     //print review page
173     drupal_set_title('Join Request - Review');
174     render_join_request_review($api, $_GET['site_id']);
175         
176 }
177 else if ($_POST['submitted'] )
178 {
179
180   // parse the form
181   $site_form = build_site_form(FALSE);
182   $input = parse_form ($site_form, $_REQUEST, $input);
183   // xxx should not happen ?
184   $empty_form = $input['is_empty'];
185   $error = "";
186   $messages= array();
187   if ( $empty_form ) {
188     $error .= '<div class-"plc-warning">Internal error - empty form !?!</div>';
189   }
190   if (empty ($error)) {
191     $site=$input['site'];
192     $address=$input['address'];
193     $pi=$input['pi'];
194     $tech=$input['tech'];
195
196     // Look for missing/blank entries
197     $error .= form_check_required ($site_form, $input);
198   }
199
200   if (empty($error)) {
201     // get objects id from the request
202     $site_id = intval(trim($_POST['site_id']));
203     $address_id = intval(trim($_POST['address_id']));
204     $pi_id = intval(trim($_POST['pi_id']));
205     $tech_id = intval(trim($_POST['tech_id']));
206
207     switch ($_POST['submitted']) {
208     case 'Delete': {
209       $api->DeleteSite ($site_id);
210       $api_error=$api->error();
211       if (!empty($api_error)) {
212         $error .= $api->error();
213       } else {
214         $messages [] = "Site " . $site['name'] . " deleted";
215       }
216       break;
217     }
218     case 'Update': {
219       $api->begin();
220       $api->UpdateSite($site_id,$site);
221       $api->UpdateAddress($address_id,$address);
222       $api->UpdatePerson($pi_id,$pi);
223       $api->UpdatePerson($tech_id,$tech);
224       $api->commit();
225       $api_error=$api->error();
226       if (!empty($api_error)) {
227         $error .= $api->error();
228       } else {
229         $messages [] = "Join request updated for site " . $site['name'] ;
230       }
231       
232       break;
233     }
234     case 'Approve': {
235       // Thierry - august 22 2007
236       // keep it simple - the admin who approves is now supposed to check 
237       // the PI's email address, which makes the whole thing *a lot* simpler
238       // enable the site, enable the PI, and VerifyPerson the thec if different from the PI
239       $site['enabled'] = True;
240       $api->UpdateSite ($site_id,$site);
241       $api_error=$api->error();
242       if (!empty($api_error)) {
243         $error .= $api->error();
244         $messages [] = "Could not enable site";
245       } else {
246         $messages[] = "Site " . $site['name'] . " enabled";
247       }
248       
249       if (empty ($error)) {
250         // Update Address
251         $api->UpdateAddress($address_id,$address);
252         $api_error=$api->error();
253         if ( ! empty($api_error)) {
254           $error .= $api->error();
255           $messages [] = "Could not update address";
256         }
257         
258         foreach ( array("Billing","Shipping") as $address_type) {
259           $api->AddAddressTypeToAddress($address_type,$address_id);
260           $api_error=$api->error();
261           if ( ! empty($api_error)) {
262             $error .= $api->error();
263             $messages [] = "Could not add address type " . $address_type;
264           }
265         }
266           
267         // Update pi, and enable him
268         $api->UpdatePerson ($pi_id,$pi);
269         if ( $pi ['enabled' ] ) {
270           $messages [] = "PI already enabled";
271         } else {
272           $api->UpdatePerson ($pi_id,array("enabled"=>True));
273           $api_error=$api->error();
274           if (empty($api_error)) {
275             $messages[] = "Enabled PI as " . $pi['email'] ;
276             notify_enabled_pi ($api, $pi_id,$pi,$site_id, $site);
277             $messages[] = "Notified PI by email";
278           } else {
279             $error .= $api->error();
280             $messages [] = "Could not update PI";
281           }
282         }
283
284         if ($pi_id != $tech_id) {
285           // Update tech, and VerifyPerson him if needed
286           $api->UpdatePerson ($tech_id,$tech);
287           if ( $tech [ 'enabled' ] ) {
288             $messages [] = "Tech already enabled";
289           } else {
290             $api->VerifyPerson($tech_id);
291             $api_error=$api->error();
292             if (empty($api_error)) {
293               $messages[] = "Created account registration for Tech as " . $tech['email'];
294             } else {
295               $error .= $api->error();
296               $messages [] = "Could not verify Tech";
297             }
298           }
299         }
300       }
301
302       break;
303     }
304     default: {
305       $error .= '<div class-"plc-warning">Internal error - unexpected request</div>';
306       break;
307     }
308
309     } // end switch
310   }
311
312   // Show messages
313   if (!empty($messages)) {
314     print '<div class="messages status"><ul>';
315     foreach ($messages as $line) {
316       print "<li> $line";
317     }
318     print "</ul></div>";
319   }
320         
321   // Show errors if any
322   if (!empty($error)) {
323     print '<div class="messages error">' . $error . '</div>';
324     drupal_set_title('Join Request - Review');
325     render_join_request_review($api, $_POST['site_id']);    
326   } else {
327     drupal_set_title('All currently pending join requests');
328     render_all_join_requests($api);
329   }
330
331  }
332  else // list all pending requests
333 {
334
335     drupal_set_title('All currently pending join requests');
336     render_all_join_requests($api);
337 }
338
339 // Print footer
340 include 'plc_footer.php';
341
342 ?>