show # of nodes and users in toggle header
[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("trigger-bubble"=>
106                            "Enter this zone if you wish to renew your slice",
107                            'start-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 => "Two 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 drupal_set_title("My slice " . $name);
178
179 $am_in_slice = in_array(plc_my_person_id(),$person_ids);
180
181 $privileges = ( $local_peer && (plc_is_admin()  || $am_in_slice));
182
183 $tabs=array();
184 $tabs [] = tab_nodes_slice($slice_id);
185 $tabs [] = tab_site($site_id);
186
187 // are these the right privileges for deletion ?
188 if ($privileges) {
189   $tabs ['Delete']= array('url'=>l_actions(),
190                           'method'=>'post',
191                           'values'=>array('action'=>'delete-slice','slice_id'=>$slice_id),
192                           'bubble'=>"Delete slice $name",
193                           'confirm'=>'Are you sure to delete $name');
194
195   $tabs["Events"]=array_merge(tablook_event(),
196                               array('url'=>l_event("Slice","slice",$slice_id),
197                                     'bubble'=>"Events for slice $name"));
198   $tabs["Comon"]=array_merge(tablook_comon(),
199                              array('url'=>l_comon("slice_id",$slice_id),
200                                    'bubble'=>"Comon page about slice $name"));
201 }
202
203 plekit_linetabs($tabs);
204
205 ////////////////////////////////////////
206 $peers->block_start($peer_id);
207
208 //////////////////////////////////////// renewal area 
209 // (1) close to expiration : show on top and open
210
211 if ($local_peer ) {
212   $renew_visible = renew_needed ($slice);
213   if ($renew_visible) renew_area ($slice,$site,true);
214  }
215
216
217 //////////////////// details
218 $show_details=false;
219 if (isset ($_GET['show_details'])) $show_details=$_GET['show_details'];
220 $toggle = 
221   new PlekitToggle ('my-slice-details',"Details",
222                     array('trigger-bubble'=>
223                           'Display and modify details for that slice',
224                           'start-visible'=>$show_details));
225 $toggle->start();
226
227 $details=new PlekitDetails($privileges);
228 $details->form_start(l_actions(),array('action'=>'update-slice',
229                                        'slice_id'=>$slice_id,
230                                        'name'=>$name));
231
232 $details->start();
233 if (! $local_peer) {
234   $details->th_td("Peer",$peers->peer_link($peer_id));
235   $details->space();
236  }
237
238
239 $details->th_td('Name',$slice['name']);
240 $details->th_td('Description',$slice['description'],'description',
241                 array('input_type'=>'textarea',
242                       'width'=>50,'height'=>5));
243 $details->th_td('URL',$slice['url'],'url',array('width'=>50));
244 $details->tr_submit("submit","Update Slice");
245 $details->th_td('Expires',$expires);
246 $details->th_td('Instantiation',$slice['instantiation']);
247 $details->th_td('Site',l_site_obj($site));
248 // xxx show the PIs here
249 //$details->th_td('PIs',...);
250 $details->end();
251
252 $details->form_end();
253 $toggle->end();
254
255 //////////////////// persons
256 $persons=$api->GetPersons(array('person_id'=>$slice['person_ids']));
257 // just propose to add everyone else, 
258 // as regular persons can see only a fraction of the db anyway
259 $potential_persons=
260   $api->GetPersons(array('~person_id'=>$slice['person_ids'],'peer_id'=>NULL),
261                    array('email','person_id','first_name','last_name','roles'));
262 $show_persons=false;
263 $count=count($persons);
264
265 if (isset ($_GET['show_persons'])) $show_persons=$_GET['show_persons'];
266 $toggle=
267   new PlekitToggle ('my-slice-persons',"$count Users",
268                     array('trigger-bubble'=>
269                           'Manage accounts attached to this slice',
270                           'start-visible'=>$show_persons));
271 $toggle->start();
272
273 ////////// people currently in
274 // visible:
275 // hide if both current+add are included
276 // so user can chose which section is of interest
277 // show otherwise
278 $toggle_persons = new PlekitToggle ('my-slice-persons-current',
279                                     "$count people currently in $name",
280                                     array('start-visible'=>!$privileges));
281 $toggle_persons->start();
282
283 $headers=array();
284 $headers['email']='string';
285 $headers['first']='string';
286 $headers['last']='string';
287 $headers['R']='string';
288 if ($privileges) $headers[plc_delete_icon()]="none";
289 $table=new PlekitTable('persons',$headers,'0',
290                        array('notes_area'=>false));
291 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
292 $form->start();
293 $table->start();
294 if ($persons) foreach ($persons as $person) {
295   $table->row_start();
296   $table->cell(l_person_obj($person));
297   $table->cell($person['first_name']);
298   $table->cell($person['last_name']);
299   $table->cell(plc_vertical_table ($person['roles']));
300   if ($privileges) $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
301   $table->row_end();
302 }
303 // actions area
304 if ($privileges) {
305
306   // remove persons
307   $table->tfoot_start();
308
309   $table->row_start();
310   $table->cell($form->submit_html ("remove-persons-from-slice","Remove selected"),
311                $table->columns(),"right");
312   $table->row_end();
313  }
314 $table->end();
315 $toggle_persons->end();
316
317 ////////// people to add
318 if ($privileges) {
319   $count=count($potential_persons);
320   $toggle_persons = new PlekitToggle ('my-slice-persons-add',
321                                       "$count people may be added to $name",
322                                       array('start-visible'=>false));
323   $toggle_persons->start();
324   if ( ! $potential_persons ) {
325     // xxx improve style
326     echo "<p class='not-relevant'>No person to add</p>";
327   } else {
328     $headers=array();
329     $headers['email']='string';
330     $headers['first']='string';
331     $headers['last']='string';
332     $headers['R']='string';
333     $headers['Add']="none";
334     $options = array('notes_area'=>false,
335                      'search_width'=>15,
336                      'pagesize'=>8);
337     // show search for admins only as other people won't get that many names to add
338     if ( ! plc_is_admin() ) $options['search_area']=false;
339     
340     $table=new PlekitTable('add_persons',$headers,'0',$options);
341     $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
342     $form->start();
343     $table->start();
344     if ($potential_persons) foreach ($potential_persons as $person) {
345         $table->row_start();
346         $table->cell(l_person_obj($person));
347         $table->cell($person['first_name']);
348         $table->cell($person['last_name']);
349         $table->cell(plc_vertical_table ($person['roles']));
350         $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
351         $table->row_end();
352       }
353     // add users
354     $table->tfoot_start();
355     $table->row_start();
356     $table->cell($form->submit_html ("add-persons-in-slice","Add selected"),
357                  $table->columns(),"right");
358     $table->row_end();
359     $table->end();
360     $form->end();
361   }
362   $toggle_persons->end();
363 }
364 $toggle->end();
365
366 //////////////////// nodes
367 // minimal list as a start
368 $node_columns = array('hostname','node_id','arch');
369 $nodes=$api->GetNodes(array('node_id'=>$slice['node_ids']),$node_columns);
370 $potential_nodes=$api->GetNodes(array('~node_id'=>$slice['node_ids']),$node_columns);
371 $count=count($nodes);
372
373 $show_nodes=true;
374 if (isset ($_GET['show_nodes'])) $show_nodes=$_GET['show_nodes'];
375 $toggle=new PlekitToggle ('my-slice-nodes',"$count Nodes",
376                           array('trigger-bubble'=>
377                                 'Manage nodes attached to this slice',
378                                 'start-visible'=>$show_nodes));
379 $toggle->start();
380
381 ////////// nodes currently in
382 $count=count($nodes);
383 $toggle_nodes=new PlekitToggle('my-slice-nodes-current',
384                                "$count nodes currently in $name",
385                                array('start-visible'=>!$privileges));
386 $toggle_nodes->start();
387
388 $headers=array();
389 $headers['hostname']='string';
390 $headers['arch']='string';
391 if ($privileges) $headers[plc_delete_icon()]="none";
392 $table=new PlekitTable('nodes',$headers,'0',
393                        array('notes_area'=>false));
394 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
395 $form->start();
396 $table->start();
397 if ($nodes) foreach ($nodes as $node) {
398   $table->row_start();
399   $table->cell(l_node_obj($node));
400   $table->cell($node['arch']);
401   if ($privileges) $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
402   $table->row_end();
403 }
404 // actions area
405 if ($privileges) {
406
407   // remove nodes
408   $table->tfoot_start();
409
410   $table->row_start();
411   $table->cell($form->submit_html ("remove-nodes-from-slice","Remove selected"),
412                $table->columns(),"right");
413   $table->row_end();
414  }
415 $table->end();
416 $toggle_nodes->end();
417
418 ////////// nodes to add
419 if ($privileges) {
420   $count=count($potential_nodes);
421   $toggle_nodes=new PlekitToggle('my-slice-nodes-add',
422                                  "$count more nodes available",
423                                  array('start-visible'=>false));
424   $toggle_nodes->start();
425
426   if ( ! $potential_nodes ) {
427     // xxx improve style
428     echo "<p class='not-relevant'>No node to add</p>";
429   } else {
430     $headers=array();
431     $headers['hostname']='string';
432     $headers['arch']='string';
433     $headers['Add']="none";
434     $options = array('notes_area'=>false,
435                      'search_width'=>15,
436                      'pagesize'=>20);
437     
438     $table=new PlekitTable('add_nodes',$headers,'1',$options);
439     $form=new PlekitForm(l_actions(),
440                          array('slice_id'=>$slice['slice_id']));
441     $form->start();
442     $table->start();
443     if ($potential_nodes) foreach ($potential_nodes as $node) {
444         $table->row_start();
445         $table->cell(l_node_obj($node));
446         $table->cell($node['arch']);
447         $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
448         $table->row_end();
449       }
450     // add nodes
451     $table->tfoot_start();
452     $table->row_start();
453     $table->cell($form->submit_html ("add-nodes-in-slice","Add selected"),
454                  $table->columns(),"right");
455     $table->row_end();
456     $table->end();
457     $form->end();
458   }
459   $toggle_nodes->end();
460 }
461 $toggle->end();
462
463 //////////////////// tags
464
465 if ($local_peer ) {
466   if ( ! $renew_visible) renew_area ($slice,$site,false);
467  }
468
469 $peers->block_end($peer_id);
470
471 // Print footer
472 include 'plc_footer.php';
473
474 ?>