fixed: handles better display of nodes,persons,slices
[plewww.git] / planetlab / sites / site.php
1 <?php
2
3   // $Id$
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_peers.php';
19 require_once 'linetabs.php';
20 require_once 'table.php';
21 require_once 'details.php';
22 require_once 'form.php';
23 require_once 'toggle.php';
24
25 // -------------------- 
26 // recognized URL arguments
27 $site_id=intval($_GET['id']);
28 if ( ! $site_id ) { plc_error('Malformed URL - id not set'); return; }
29
30 ////////////////////
31 // Get all columns as we focus on only one entry
32 $sites = $api->GetSites( array($site_id));
33
34 if (empty($sites)) {
35   drupal_set_message ("Site " . $site_id . " not found");
36   return;
37  }
38
39 $site=$sites[0];
40 // var names to api return
41 // Thomas Dreibholz suggested that utf8_decode could be non-needed
42 // and maybe even harmful on modern systems like f23
43 // $sitename= htmlentities(utf8_decode($site['name']));
44 $sitename= htmlentities($site['name']);
45 $abbreviated_name= htmlentities($site['abbreviated_name']);
46 $site_url= $site['url'];
47 $login_base= $site['login_base'];
48 $site_lat= $site['latitude'];
49 $site_long= $site['longitude'];
50 $max_slivers= $site['max_slivers'];
51 $max_slices= $site['max_slices'];
52
53 $enabled = $site['enabled'];
54 $ext_consortium_id = $site ['ext_consortium_id'];
55
56 // get peer 
57 $peer_id= $site['peer_id'];
58 $peers = new Peers ($api);
59 $local_peer = ! $peer_id;
60
61 // extra privileges to admins, and (pi||tech) on this site
62 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
63 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
64 $is_site_admin = ($local_peer && plc_is_admin());
65   
66 $address_ids= $site['address_ids'];
67 $pcu_ids= $site['pcu_ids'];
68 $node_ids= $site['node_ids'];
69 $person_ids= $site['person_ids'];
70 $slice_ids= $site['slice_ids'];
71
72 //$api->begin();
73 // gets address info
74 $addresses = $api->GetAddresses( $address_ids );
75
76 // gets node info
77 if ($node_ids) {
78         $nodes = $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
79 } else {
80         $nodes = array();
81 }
82 // gets person info
83 if ($person_ids) {
84         $persons = $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
85 } else {
86         $persons = array();
87 }
88
89 if ($slice_ids) {
90         $slices = $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
91 } else {
92         $slices = array();
93 }
94 ////////////////////
95 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
96 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
97 if ($display_pcus) {
98         if ($pcu_ids) {
99                 $pcus = $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
100         } else {
101                 $pcus = array();
102         }
103 }
104 // get results
105 //if ($display_pcus)
106 //  list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
107 //else
108 //  list( $addresses, $nodes, $persons, $slices )= $api->commit();
109
110 $techs = array();
111 $pis = array();
112 $disabled_persons = array();
113 if ($persons) foreach( $persons as $person ) {
114   $role_ids= $person['role_ids'];
115
116   if ( in_array( '20', $role_ids ))     $pis[] = $person;
117   if ( in_array( '40', $role_ids ))     $techs[] = $person;
118   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
119   
120 }
121
122 $has_disabled_persons = count ($disabled_persons) !=0;
123
124 // get number of slivers
125 $slivers_count=0;
126 if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_ids']);
127
128 ////////////////////////////////////////
129 drupal_set_title("Details for site " . $sitename);
130   
131 $tabs=array();
132
133 $tabs []= tab_mysite();
134
135 // available actions
136 if ( $is_site_admin)
137   $tabs['Expire slices'] = array('url'=>l_actions(),
138                                  'method'=>'POST',
139                                  'values'=>array('site_id'=>$site_id,
140                                                  'action'=>'expire-all-slices-in-site'),
141                                  'bubble'=>"Expire all slices and prevent creation of new slices",
142                                  'confirm'=>"Suspend all slices in $login_base");
143 if ( $is_site_admin)
144   $tabs['Delete']=array('url'=>l_actions(),
145                         'method'=>'POST',
146                         'values'=>array('site_id'=>$site_id,
147                                         'action'=>'delete-site'),
148                         'bubble'=>"Delete site $sitename",
149                         'confirm'=>"Are you sure you want to delete site $login_base");
150
151 if ( $is_site_pi ) 
152   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
153                               'method'=>'post',
154                               'bubble'=>'Create new slice in site');
155
156 if (plc_is_admin() || plc_in_site($site_id))
157   $tabs["Events"]=array_merge (tablook_event(),
158                                array('url'=>l_event("Site","site",$site_id),
159                                      'bubble'=>"Events for site $sitename"));
160 if (plc_is_admin() || plc_in_site($site_id))
161   $tabs["Comon"]=array_merge(tablook_comon(),
162                              array('url'=>l_comon("site_id",$site_id),
163                                    'bubble'=>"Comon page for $sitename"));
164
165
166 plekit_linetabs($tabs);
167
168 // show gray background on foreign objects : start a <div> with proper class
169 $peers->block_start ($peer_id);
170
171 // sanity checks
172 if ( $local_peer ) {
173   // pending site
174   global $PENDING_CONSORTIUM_ID;
175   if ( $ext_consortium_id === $PENDING_CONSORTIUM_ID) {
176     if ( ! $enabled ) 
177       plc_warning ("This site is under pending registration - Please visit " . 
178                    href (l_sites_pending(),"this page") . 
179                    " to review pending applications.");
180     else 
181       plc_warning ("This site is pending but is also enabled - something is wrong. You should fix the issue with plcsh");
182   } else {
183     if ( ! $enabled) 
184       plc_warning ("This site is disabled.");
185   }
186 }
187
188 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
189
190
191 $toggle = new PlekitToggle ('site',"Details",
192                             array('visible'=>get_arg('show_details'),
193                                   'bubble'=>'Display and modify details for that site'));
194 $toggle->start();
195
196 $details = new PlekitDetails($can_update);
197
198 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
199
200 $details->start();
201
202 if ( ! $site['is_public']) 
203   $details->tr(plc_warning_html("This site is not public!"));
204
205 $details->th_td("Full name",$sitename,'name',array('width'=>50));
206 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
207 $details->th_td("URL",$site_url,'url',array('width'=>40));
208 $details->th_td("Latitude",$site_lat,'latitude');
209 $details->th_td("Longitude",$site_long,'longitude');
210
211 // modifiable by admins only
212 if (plc_is_admin()) 
213   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
214 else
215   $details->th_td("Login base",$login_base);
216 if (plc_is_admin())
217   $details->th_td("Max slices",$max_slices,'max_slices');
218 else
219   $details->th_td("Max slices",$max_slices);
220 if (plc_is_admin())
221 {
222   $selectors=array(array('display'=>"False",'value'=>'0'), 
223                                    array('display'=>"True",'value'=>'1'));
224   $selectors[intval($enabled)]['selected'] = 'selected';
225
226   $enable_select = $f->select_html ("enabled", $selectors);
227
228   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
229 } else
230   $details->th_td("Enabled",$enabled);
231
232 $details->tr_submit("submit","Update Site");
233
234 if ( ! $local_peer) {
235   $details->space();
236   $details->th_td("Peer",$peers->peer_link($peer_id));
237  }
238 $details->end();
239 $details->form_end();
240 $toggle->end();
241
242 //////////////////// mode details - for local object
243 if ( $local_peer ) {
244
245   //////////////////// nodes
246   $nb_boot = 0;
247   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
248
249   $nodes_title = "Nodes : ";
250   $nodes_title .= count($nodes) . " total";
251   $nodes_title .= " / " . $nb_boot . " boot";
252   if ($nb_boot < 2 ) 
253     $nodes_title = plc_warning_html ($nodes_title);
254   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
255
256   $toggle=new PlekitToggle ('nodes',$nodes_title,
257                             array('visible'=>get_arg('show_nodes')));
258   $toggle->start();
259
260   $headers=array();
261   $sort_column = '0';
262   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
263   $headers['hostname']='string'; 
264   $headers['state']='string';
265
266   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
267                                                                 'notes_area'=>false,
268                                                                 'pagesize_area'=>false));
269   // hash pcus on pcu_id
270   if ($display_pcus) {
271     global $pcu_hash;
272     $pcu_hash= array();
273     if ($pcus) foreach ($pcus as $pcu) $pcu_hash[$pcu['pcu_id']]=$pcu;
274   }
275   // search the pcu, return the string to display and mark the pcu as displayed
276   //  function display_and_mark ($pcu_hash,$pcu_ids,$ports) {
277   function display_and_mark ($pcu_ids,$ports) {
278     global $pcu_hash;
279     if (empty($pcu_ids)) return plc_warning_html('None');
280     $pcu_id=$pcu_ids[0];
281     if (empty($ports)) return plc_error_html('???');
282     $port=$ports[0];
283     $pcu=$pcu_hash[$pcu_id];
284     $display= l_pcu_href($pcu_id, $pcu['hostname'] . ' : ' . $port);
285     $pcu_hash[$pcu_id]['displayed']=true;
286     return $display;
287   }
288
289   $table->start();
290   foreach ($nodes as $node) {
291     $table->row_start();
292     if ($display_pcus) {
293       //      $table->cell(display_and_mark($pcu_hash,$node['pcu_ids'],$node['ports']));
294       $table->cell(display_and_mark($node['pcu_ids'],$node['ports']));
295     }
296     $table->cell (l_node_obj($node));
297     $table->cell ($node['boot_state']);
298     $table->row_end();
299   }
300   // show undisplayed PCU's if any
301   if ($display_pcus) 
302     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
303         if (!$pcu['displayed']) {
304           $table->row_start();
305           $table->cell($pcu['hostname']); $table->cell(''); $table->cell('');
306           $table->row_end();
307         }
308       }
309     
310   $table->tfoot_start();
311   $table->row_start();
312   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
313   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
314   $table->row_end();
315   $table->end();
316   $toggle->end();
317     
318   //////////////////// Users
319   $persons_title = "Users : ";
320   $persons_title .= count($person_ids) . " total";
321   $persons_title .= " / " . count ($pis) . " PIs";
322   $persons_title .= " / " . count ($techs) . " Techs";
323   if ($has_disabled_persons) 
324     $persons_title .= " / " . count($disabled_persons) . " Disabled";
325   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
326     $persons_title = plc_warning_html ($persons_title);
327   $persons_title .= href(l_persons_site($site_id)," (See as users)");
328
329   $toggle=new PlekitToggle ('persons',$persons_title,
330                             array('visible'=>get_arg('show_persons')));
331   $toggle->start();
332
333   $headers = array ();
334   $headers["email"]='string';
335   $headers["S"]='int';
336   $headers["PI"]='string';
337   $headers['User']='string';
338   $headers["Tech"]='string';
339   if ($has_disabled_persons) $headers["Disabled"]='string';
340   $notes=array('S = slices');
341   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
342                                                             'notes'=>$notes,
343                                                             'pagesize_area'=>false));
344   $table->start();
345   if ($persons) foreach ($persons as $person) {
346     $table->row_start();
347     $table->cell(l_person_obj($person));
348     $table->cell(count($person['slice_ids']));
349     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
350     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
351     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
352     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
353     $table->row_end();
354   }
355   $table->end();
356   $toggle->end();
357
358   //////////////////// Slices
359   $slices_title="Slices : ";
360   $slices_title .= $max_slices . " max";
361   $slices_title .= " / " . count($slice_ids) . " running";
362   $slices_title .= " / $slivers_count slivers";
363   if (count($slice_ids) >= $max_slices) 
364     $slices_title = plc_warning_html($slices_title);
365   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
366   
367   $toggle=new PlekitToggle ('slices',$slices_title,
368                             array('visible'=>get_arg('show_slices')));
369   $toggle->start();
370
371   $headers = array ();
372   $headers ['name']='string';
373   $headers ['I'] = 'string';
374   $headers ['N']='int';
375   $headers ['U']='int';
376   $notes=array('I = instantiation type',
377                'N = number of nodes',
378                'U = number of users');
379   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
380                                                     'pagesize_area'=>false,
381                                                     'notes'=>$notes));
382
383   $table->start();
384   if ($slices) foreach ($slices as $slice) {
385       $table->row_start();
386       $table->cell(l_slice_obj($slice));
387       $table->cell(instantiation_label($slice));
388       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
389       $table->cell (count($slice['person_ids']));
390       $table->row_end();
391     }
392   if ($is_site_pi) {
393     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
394     $table->tfoot_start();
395     $table->row_start();
396     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
397   }
398     
399   $table->end();
400   $toggle->end();
401
402   $form=new PlekitForm (l_actions(), array('site_id'=>$site_id));
403   $form->start();
404   //////////////////////////////////////////////////////////// Tags
405   // tags section
406   // already inside a if ( $local_peer )...
407   
408   $tags=$api->GetSiteTags (array('site_id'=>$site_id));
409   function get_tagname ($tag) { return $tag['tagname'];}
410   // xxx looks like tech-only see an error here, 
411   // might be that GetSiteTags is not accessible or something
412   $tagnames = array_map ("get_tagname",$tags);
413   
414   $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
415                               array('bubble'=>'Inspect and set tags on that site',
416                                     'visible'=>get_arg('show_tags')));
417   $toggle->start();
418
419   $headers=array("Name"=>"string",
420                  "Value"=>"string",
421                  );
422   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
423   
424   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
425   $table=new PlekitTable("site_tags",$headers,0,$table_options);
426   $table->start();
427   if ($tags) foreach ($tags as $tag) {
428       $table->row_start();
429       $table->cell(l_tag_obj($tag));
430       $table->cell($tag['value']);
431       // the remove checkbox
432       if (plc_is_admin()) $table->cell ($form->checkbox_html('site_tag_ids[]',$tag['site_tag_id']));
433       $table->row_end();
434     }
435   
436   if ($is_site_pi || $is_site_admin) {
437     $table->tfoot_start();
438
439     // remove tag 
440     $table->row_start();
441     $table->cell($form->submit_html("delete-site-tags","Remove Tags"),
442                  // use the whole columns and right adjust
443                  array('hfill'=>true,'align'=>'right'));
444     $table->row_end();
445
446     // set tag area
447     $table->row_start();
448     // get list of tag names in the site/* category    
449     $all_tags= $api->GetTagTypes( array ("category"=>"site*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
450     // xxx cannot use onchange=submit() - would need to somehow pass action name 
451     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
452     $selector=array_map("tag_selector",$all_tags);
453     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
454     $table->cell($form->text_html("value","",array('width'=>8)));
455     $table->cell($form->submit_html("set-tag-on-site","Set Tag"),array('columns'=>2,'align'=>'left'));
456     $table->row_end();
457   }
458   
459   $table->end();
460   $toggle->end();
461   $form->end();
462
463   //////////////////// Addresses
464   $toggle=new PlekitToggle ('addresses',"Addresses",
465                             array('visible'=>get_arg('show_addresses')));
466   $toggle->start();
467   if ( ! $addresses) {
468     print "<p class='addresses'>No known address for this site</p>";
469   } else {
470     $details=new PlekitDetails (false);
471     $details->start();
472     $details->th_td("Addresses","");
473     foreach ($addresses as $address) {
474       $details->th_td(plc_vertical_table($address['address_types']),
475                        plc_vertical_table(array($address['line1'],
476                                                 $address['line2'],
477                                                 $address['line3'],
478                                                 $address['city'],
479                                                 $address['state'],
480                                                 $address['postalcode'],
481                                                 $address['country'])));
482     }
483     $details->end();
484   }
485   $toggle->end();
486
487  }
488
489 ////////////////////////////////////////
490 $peers->block_end($peer_id);
491
492 //plekit_linetabs ($tabs,"bottom");
493
494 // Print footer
495 include 'plc_footer.php';
496
497 ?>