filter out not enabled users in slice page
[plewww.git] / planetlab / slices / slice.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 'toggle.php';
23 require_once 'form.php';
24
25 // keep css separate for now
26 drupal_set_html_head('
27 <link href="/planetlab/css/my_slice.css" rel="stylesheet" type="text/css" />
28 ');
29
30 // -------------------- admins potentially need to get full list of users
31 ini_set('memory_limit','32M');
32
33 // -------------------- 
34 // recognized URL arguments
35 $slice_id=intval($_GET['id']);
36 if ( ! $slice_id ) { plc_error('Malformed URL - id not set'); return; }
37
38 ////////////////////
39 // Get all columns as we focus on only one entry
40 $slices= $api->GetSlices( array($slice_id));
41
42 if (empty($slices)) {
43   drupal_set_message ("Slice " . $slice_id . " not found");
44   return;
45  }
46
47 $slice=$slices[0];
48
49 // pull all node info to vars
50 $name= $slice['name'];
51 $expires = date( "d/m/Y", $slice['expires'] );
52 $site_id= $slice['site_id'];
53
54 //$node_ids=$slice['node_ids'];
55 $person_ids=$slice['person_ids'];
56 //$slice_tag_ids= $slice['slice_tag_ids'];
57
58 // get peers
59 $peer_id= $slice['peer_id'];
60 $peers=new Peers ($api);
61 $local_peer = ! $peer_id;
62
63 // gets site info
64 $sites= $api->GetSites( array( $site_id ) );
65 $site=$sites[0];
66 $site_name= $site['name'];
67 $max_slices = $site['max_slices'];
68 // xxx PIs
69 //$pis=$api->GetPersons(...)
70
71 // get all persons info
72 if (!empty($person_ids))
73   $persons=$api->GetPersons($person_ids,array('email','enabled'));
74
75
76 //////////////////////////////////////// building blocks for the renew area
77 // Constants
78 global $DAY;            $DAY = 24*60*60;
79 global $WEEK;           $WEEK = 7 * $DAY; 
80 global $MAX_WEEKS;      $MAX_WEEKS= 8;          // weeks from today
81 global $GRACE_DAYS;     $GRACE_DAYS=10;         // days for renewal promoted on top
82 global $NOW;            $NOW=mktime();
83
84
85 // make the renew area on top and open if the expiration time is less than 10 days from now
86 function renew_needed ($slice) {
87   global $DAY, $NOW, $GRACE_DAYS;
88   $current_exp=$slice['expires'];
89
90   $time_left = $current_exp - $NOW;
91   $visible = $time_left/$DAY <= $GRACE_DAYS;
92   return $visible;
93 }
94
95 function renew_area ($slice,$site,$visible) {
96   global $DAY, $WEEK, $MAX_WEEKS, $GRACE_DAYS, $NOW;
97  
98   $current_exp=$slice['expires'];
99   $max_exp= $NOW + ($MAX_WEEKS * $WEEK); // seconds since epoch
100
101   // xxx some extra code needed to enable this area only if the slice description is OK:
102   // description and url must be non void
103   $toggle=
104     new PlekitToggle('renew',"Renew this slice",
105                      array("bubble"=>
106                            "Enter this zone if you wish to renew your slice",
107                            'visible'=>$visible));
108   $toggle->start();
109
110   // xxx message could take roles into account
111   if ($site['max_slices']<=0) {
112      $message= <<< EOF
113 <p class='my-slice-renewal'>Slice creation and renewal have been temporarily disabled for your
114 <site. This may have occurred because your site's nodes have been down
115 or unreachable for several weeks, and multiple attempts to contact
116 your site's PI(s) and Technical Contact(s) have all failed. If so,
117 contact your site's PI(s) and Technical Contact(s) and ask them to
118 bring up your site's nodes. Please visit your <a
119 href='/db/sites/index.php?id=$site_id'>site details</a> page to find
120 out more about your site's nodes, and how to contact your site's PI(s)
121 and Technical Contact(s).</p>
122 EOF;
123      echo $message;
124  
125   } else {
126     // xxx this is a rough cut and paste from the former UI
127     // showing a datepicker view could be considered as well with some extra work
128     // calculate possible extension lengths
129     $selectors = array();
130     foreach ( array ( 1 => "One more week", 
131                       2 => "Two more weeks", 
132                       3 => "Three more weeks", 
133                       4 => "One more month" ) as $weeks => $text ) {
134       $candidate_exp = $current_exp + $weeks*$WEEK;
135       if ( $candidate_exp < $max_exp) {
136         $selectors []= array('display'=>"$text (" . gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp) . ")",
137                              'value'=>$candidate_exp);
138         $max_renewal_weeks=$weeks;
139         $max_renewal_date= gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp);
140       }
141     }
142
143     if ( empty( $selectors ) ) {
144       print <<< EOF
145 <div class='plc-warning renewal'>
146 Slice cannot be renewed any further into the future, try again closer to expiration date.
147 </div>
148 EOF;
149      } else {
150       print <<< EOF
151 <div class='my-slice-renewal'>
152 <p>You must provide a short description as well as a link to a project website before renewing it.
153 Do <span class='bold'>not</span> provide bogus information; if a complaint is lodged against your slice 
154 and PlanetLab Operations is unable to determine what the normal behavior of your slice is, 
155 your slice may be deleted to resolve the complaint.</p>
156 <p><span class='bold'>NOTE:</span> 
157 Slices cannot be renewed beyond another $max_renewal_weeks week(s) ($max_renewal_date).
158 </p>
159 </div>
160 EOF;
161
162       $form = new PlekitForm (l_actions(),
163                               array('action'=>'renew-slice',
164                                     'slice_id'=>$slice['slice_id']));
165       $form->start();
166       print $form->label_html('expires','Duration');
167       print $form->select_html('expires',$selectors,array('label'=>'Pick one'));
168       print $form->submit_html('renew-button','Renew');
169       $form->end();
170     }
171   }
172  
173   $toggle->end();
174 }
175
176 ////////// 
177
178 $am_in_slice = in_array(plc_my_person_id(),$person_ids);
179
180 if ($am_in_slice) {
181   drupal_set_title("My slice " . $name);
182  } else {
183   drupal_set_title("Slice " . $name);
184 }
185
186 $privileges = ( $local_peer && (plc_is_admin()  || plc_is_pi() || $am_in_slice));
187 $tags_privileges = $privileges || plc_is_admin();
188
189 $tabs=array();
190 $tabs [] = tab_nodes_slice($slice_id);
191 $tabs [] = tab_site($site_id);
192
193 // are these the right privileges for deletion ?
194 if ($privileges) {
195   $tabs ['Delete']= array('url'=>l_actions(),
196                           'method'=>'post',
197                           'values'=>array('action'=>'delete-slice','slice_id'=>$slice_id),
198                           'bubble'=>"Delete slice $name",
199                           'confirm'=>"Are you sure to delete slice $name");
200
201   $tabs["Events"]=array_merge(tablook_event(),
202                               array('url'=>l_event("Slice","slice",$slice_id),
203                                     'bubble'=>"Events for slice $name"));
204   $tabs["Comon"]=array_merge(tablook_comon(),
205                              array('url'=>l_comon("slice_id",$slice_id),
206                                    'bubble'=>"Comon page about slice $name"));
207 }
208
209 plekit_linetabs($tabs);
210
211 ////////////////////////////////////////
212 $peers->block_start($peer_id);
213
214 //////////////////////////////////////// renewal area 
215 // (1) close to expiration : show on top and open
216
217 if ($local_peer ) {
218   $renew_visible = renew_needed ($slice);
219   if ($renew_visible) renew_area ($slice,$site,true);
220  }
221
222
223 //////////////////// details
224 // default for opening the details section or not ?
225 if ($local_peer) {
226   $default_show_details = true;
227  } else {
228   $default_show_details = ! $renew_visible;
229  }
230   
231 $toggle = 
232   new PlekitToggle ('my-slice-details',"Details",
233                     array('bubble'=>
234                           'Display and modify details for that slice',
235                           'visible'=>get_arg('show_details',$default_show_details)));
236 $toggle->start();
237
238 $details=new PlekitDetails($privileges);
239 $details->form_start(l_actions(),array('action'=>'update-slice',
240                                        'slice_id'=>$slice_id,
241                                        'name'=>$name));
242
243 $details->start();
244 if (! $local_peer) {
245   $details->th_td("Peer",$peers->peer_link($peer_id));
246   $details->space();
247  }
248
249
250 $details->th_td('Name',$slice['name']);
251 $details->th_td('Description',$slice['description'],'description',
252                 array('input_type'=>'textarea',
253                       'width'=>50,'height'=>5));
254 $details->th_td('URL',$slice['url'],'url',array('width'=>50));
255 $details->tr_submit("submit","Update Slice");
256 $details->th_td('Expires',$expires);
257 $details->th_td('Instantiation',$slice['instantiation']);
258 $details->th_td('Site',l_site_obj($site));
259 // xxx show the PIs here
260 //$details->th_td('PIs',...);
261 $details->end();
262
263 $details->form_end();
264 $toggle->end();
265
266 //////////////////// persons
267 $person_columns = array('email','person_id','first_name','last_name','roles');
268 $persons=$api->GetPersons(array('person_id'=>$slice['person_ids']));
269 // just propose to add everyone else, 
270 // as regular persons can see only a fraction of the db anyway
271 $potential_persons=
272   $api->GetPersons(array('~person_id'=>$slice['person_ids'],'peer_id'=>NULL,'enabled'=>true),
273                    $person_columns);
274 $count=count($persons);
275
276 $toggle=
277   new PlekitToggle ('my-slice-persons',"$count Users",
278                     array('bubble'=>
279                           'Manage accounts attached to this slice',
280                           'visible'=>get_arg('show_persons',false)));
281 $toggle->start();
282
283 ////////// people currently in
284 // visible:
285 // hide if both current+add are included
286 // so user can chose which section is of interest
287 // show otherwise
288 $toggle_persons = new PlekitToggle ('my-slice-persons-current',
289                                     "$count people currently in $name",
290                                     array('visible'=>get_arg('show_persons_current',!$privileges)));
291 $toggle_persons->start();
292
293 $headers=array();
294 $headers['email']='string';
295 $headers['first']='string';
296 $headers['last']='string';
297 $headers['R']='string';
298 if ($privileges) $headers[plc_delete_icon()]="none";
299 $table=new PlekitTable('persons',$headers,'0',
300                        array('notes_area'=>false));
301 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
302 $form->start();
303 $table->start();
304 if ($persons) foreach ($persons as $person) {
305   $table->row_start();
306   $table->cell(l_person_obj($person));
307   $table->cell($person['first_name']);
308   $table->cell($person['last_name']);
309   $table->cell(plc_vertical_table ($person['roles']));
310   if ($privileges) $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
311   $table->row_end();
312 }
313 // actions area
314 if ($privileges) {
315
316   // remove persons
317   $table->tfoot_start();
318
319   $table->row_start();
320   $table->cell($form->submit_html ("remove-persons-from-slice","Remove selected"),
321                array('hfill'=>true,'align'=>'right'));
322   $table->row_end();
323  }
324 $table->end();
325 $toggle_persons->end();
326
327 ////////// people to add
328 if ($privileges) {
329   $count=count($potential_persons);
330   $toggle_persons = new PlekitToggle ('my-slice-persons-add',
331                                       "$count people may be added to $name",
332                                       array('visible'=>get_arg('show_persons_add',false)));
333   $toggle_persons->start();
334   if ( ! $potential_persons ) {
335     // xxx improve style
336     echo "<p class='not-relevant'>No person to add</p>";
337   } else {
338     $headers=array();
339     $headers['email']='string';
340     $headers['first']='string';
341     $headers['last']='string';
342     $headers['R']='string';
343     $headers['+']="none";
344     $options = array('notes_area'=>false,
345                      'search_width'=>15,
346                      'pagesize'=>8);
347     // show search for admins only as other people won't get that many names to add
348     if ( ! plc_is_admin() ) $options['search_area']=false;
349     
350     $table=new PlekitTable('add_persons',$headers,'0',$options);
351     $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
352     $form->start();
353     $table->start();
354     if ($potential_persons) foreach ($potential_persons as $person) {
355         $table->row_start();
356         $table->cell(l_person_obj($person));
357         $table->cell($person['first_name']);
358         $table->cell($person['last_name']);
359         $table->cell(plc_vertical_table ($person['roles']));
360         $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
361         $table->row_end();
362       }
363     // add users
364     $table->tfoot_start();
365     $table->row_start();
366     $table->cell($form->submit_html ("add-persons-in-slice","Add selected"),
367                  array('hfill'=>true,'align'=>'right'));
368     $table->row_end();
369     $table->end();
370     $form->end();
371   }
372   $toggle_persons->end();
373 }
374 $toggle->end();
375
376 //////////////////// nodes
377 // minimal list as a start
378 $node_columns = array('hostname','node_id','arch','peer_id','slice_ids_whitelist');
379 $nodes=$api->GetNodes(array('node_id'=>$slice['node_ids']),$node_columns);
380 $potential_nodes=$api->GetNodes(array('~node_id'=>$slice['node_ids']),$node_columns);
381 $count=count($nodes);
382
383 $toggle=new PlekitToggle ('my-slice-nodes',"$count Nodes",
384                           array('bubble'=>
385                                 'Manage nodes attached to this slice',
386                                 'visible'=>get_arg('show_nodes',false)));
387 $toggle->start();
388
389 ////////// nodes currently in
390 $count=count($nodes);
391 $toggle_nodes=new PlekitToggle('my-slice-nodes-current',
392                                "$count nodes currently in $name",
393                                array('visible'=>get_arg('show_nodes_current',!$privileges)));
394 $toggle_nodes->start();
395
396 $headers=array();
397 $headers['peer']='string';
398 $headers['hostname']='string';
399 $headers['arch']='string';
400 if ($privileges) $headers[plc_delete_icon()]="none";
401
402 $table_options = array('notes_area'=>false,
403                        'search_width'=>15,
404                        'pagesize'=>20);
405 $table=new PlekitTable('nodes',$headers,'0',$table_options);
406
407 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
408 $form->start();
409 $table->start();
410 if ($nodes) foreach ($nodes as $node) {
411   $table->row_start();
412   $peers->cell($table,$node['peer_id']);
413   $table->cell(l_node_obj($node));
414   $table->cell($node['arch']);
415   if ($privileges) $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
416   $table->row_end();
417 }
418 // actions area
419 if ($privileges) {
420
421   // remove nodes
422   $table->tfoot_start();
423
424   $table->row_start();
425   $table->cell($form->submit_html ("remove-nodes-from-slice","Remove selected"),
426                array('hfill'=>true,'align'=>'right'));
427   $table->row_end();
428  }
429 $table->end();
430 $toggle_nodes->end();
431
432 ////////// nodes to add
433 if ($privileges) {
434   $new_potential_nodes = array();
435   if ($potential_nodes) foreach ($potential_nodes as $node) {
436       $emptywl=empty($node['slice_ids_whitelist']);
437       $inwl = (!emptywl) and in_array($slice['slice_id'],$node['slice_ids_whitelist']);
438       if ($emptywl or $inwl)
439         $new_potential_nodes[]=$node;
440   }
441   $potential_nodes=$new_potential_nodes;
442
443   $count=count($potential_nodes);
444   $toggle_nodes=new PlekitToggle('my-slice-nodes-add',
445                                  "$count more nodes available",
446                                  array('visible'=>get_arg('show_persons_add',false)));
447   $toggle_nodes->start();
448
449   if ( ! $potential_nodes ) {
450     // xxx improve style
451     echo "<p class='not-relevant'>No node to add</p>";
452   } else {
453     $headers=array();
454     $headers['peer']='string';
455     $headers['hostname']='string';
456     $headers['arch']='string';
457     $headers['+']="none";
458     
459     $table=new PlekitTable('add_nodes',$headers,'1', $table_options);
460     $form=new PlekitForm(l_actions(),
461                          array('slice_id'=>$slice['slice_id']));
462     $form->start();
463     $table->start();
464     if ($potential_nodes) foreach ($potential_nodes as $node) {
465         $table->row_start();
466         $peers->cell($table,$node['peer_id']);
467         $table->cell(l_node_obj($node));
468         $table->cell($node['arch']);
469         $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
470         $table->row_end();
471       }
472     // add nodes
473     $table->tfoot_start();
474     $table->row_start();
475     $table->cell($form->submit_html ("add-nodes-in-slice","Add selected"),
476                  array('hfill'=>true,'align'=>'right'));
477     $table->row_end();
478     $table->end();
479     $form->end();
480   }
481   $toggle_nodes->end();
482 }
483 $toggle->end();
484
485 //////////////////////////////////////////////////////////// Tags
486 //if ( $local_peer ) {
487   $tags=$api->GetSliceTags (array('slice_id'=>$slice_id));
488   function get_tagname ($tag) { return $tag['tagname'];}
489   $tagnames = array_map ("get_tagname",$tags);
490   
491   $toggle = new PlekitToggle ('slice-tags',count_english_warning($tags,'tag'),
492                               array('bubble'=>'Inspect and set tags on tat slice',
493                                     'visible'=>get_arg('show_tags',false)));
494   $toggle->start();
495   
496   $headers=array(
497     "Name"=>"string",
498     "Value"=>"string",
499     "Node"=>"string",
500     "NodeGroup"=>"string");
501   if ($tags_privileges) $headers[plc_delete_icon()]="none";
502   
503   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
504   $table=new PlekitTable("slice_tags",$headers,'0',$table_options);
505   $form=new PlekitForm(l_actions(),
506                        array('slice_id'=>$slice['slice_id']));
507   $form->start();
508   $table->start();
509   if ($tags) {
510     foreach ($tags as $tag) {
511       $node_name = "ALL";
512       if ($tag['node_id']) {
513         $nodes = $api->GetNodes(array('node_id'=>$tag['node_id']));
514         if($nodes) {
515           $node = $nodes[0];
516           $node_name = $node['hostname'];
517         }
518       }
519       $nodegroup_name="n/a";
520       if ($tag['nodegroup_id']) { 
521         $nodegroup=$api->GetNodeGroups(array('nodegroup_id'=>$tag['nodegroup_id']));
522         if ($nodegroup) {
523           $nodegroup = $nodegroup[0];
524           $nodegroup_name = $nodegroup['groupname'];
525         }
526       }
527       $table->row_start();
528       $table->cell(l_tag_obj($tag));
529       $table->cell($tag['value']);
530       $table->cell($node_name);
531       $table->cell($nodegroup_name);
532       if ($tags_privileges) $table->cell ($form->checkbox_html('slice_tag_ids[]',$tag['slice_tag_id']));
533       $table->row_end();
534     }
535   }
536   if ($tags_privileges) {
537     $table->tfoot_start();
538     $table->row_start();
539     $table->cell($form->submit_html ("delete-slice-tags","Remove selected"),
540                  array('hfill'=>true,'align'=>'right'));
541     $table->row_end();
542     
543     $table->row_start();
544     function tag_selector ($tag) {
545       return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']);
546     }
547     $all_tags= $api->GetTagTypes( array ("category"=>"slice*","-SORT"=>"+tagname"), array("tagname","tag_type_id"));
548     $selector_tag=array_map("tag_selector",$all_tags);
549     
550     function node_selector($node) { 
551       return array("display"=>$node["hostname"],"value"=>$node['node_id']);
552     }
553     $all_nodes = $api->GetNodes( array ("node_id" => $slice['node_ids']), array("hostname","node_id"));
554     $selector_node=array_map("node_selector",$all_nodes);
555     
556     function nodegroup_selector($ng) {
557       return array("display"=>$ng["groupname"],"value"=>$ng['nodegroup_id']);
558     }
559     $all_nodegroups = $api->GetNodeGroups( array("groupname"=>"*"), array("groupname","nodegroup_id"));
560     $selector_nodegroup=array_map("nodegroup_selector",$all_nodegroups);
561     
562     $table->cell($form->select_html("tag_type_id",$selector_tag,array('label'=>"Choose Tag")));
563     $table->cell($form->text_html("value","",array('width'=>8)));
564     $table->cell($form->select_html("node_id",$selector_node,array('label'=>"All Nodes")));
565     $table->cell($form->select_html("nodegroup_id",$selector_nodegroup,array('label'=>"No Nodegroup")));
566     $table->cell($form->submit_html("add-slice-tag","Set Tag"),array('columns'=>2,'align'=>'left'));
567     $table->row_end();
568   }
569     
570   $table->end();
571   $form->end();
572   $toggle->end();
573 //}
574
575
576 //////////////////////// renew slice
577 if ($local_peer ) {
578   if ( ! $renew_visible) renew_area ($slice,$site,false);
579  }
580
581 $peers->block_end($peer_id);
582
583 // Print footer
584 include 'plc_footer.php';
585
586 ?>