removed unused var
[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 'plc_objects.php';
20 require_once 'plc_visibletags.php';
21 require_once 'linetabs.php';
22 require_once 'table.php';
23 require_once 'details.php';
24 require_once 'toggle.php';
25 require_once 'form.php';
26 require_once 'raphael.php';
27
28 // keep css separate for now
29 drupal_set_html_head('
30 <link href="/planetlab/css/my_slice.css" rel="stylesheet" type="text/css" />
31 <script src="/planetlab/slices/leases.js" type="text/javascript" charset="utf-8"></script>
32 ');
33
34 // -------------------- admins potentially need to get full list of users
35 ini_set('memory_limit','32M');
36
37 $profiling=false;
38 if ($_GET['profiling']) $profiling=true;
39
40 if ($profiling)  plc_debug_prof_start();
41
42 // -------------------- 
43 // recognized URL arguments
44 $slice_id=intval($_GET['id']);
45 if ( ! $slice_id ) { plc_error('Malformed URL - id not set'); return; }
46
47 ////////////////////
48 // have to name columns b/c we need the non-native 'omf_control' column
49 $slice_columns=array('slice_id','name','peer_id','site_id','person_ids','node_ids','expires',
50                      'url','description','instantiation','omf_control');
51 $slices= $api->GetSlices( array($slice_id), $slice_columns);
52
53 if (empty($slices)) {
54   drupal_set_message ("Slice " . $slice_id . " not found");
55   return;
56  }
57
58 $slice=$slices[0];
59
60 if ($profiling) plc_debug_prof('2: slice',count($slices));
61 // pull all node info to vars
62 $name= $slice['name'];
63 $expires = date( "d/m/Y", $slice['expires'] );
64 $site_id= $slice['site_id'];
65
66 $person_ids=$slice['person_ids'];
67
68 // get peers
69 $peer_id= $slice['peer_id'];
70 $peers=new Peers ($api);
71 $local_peer = ! $peer_id;
72
73 if ($profiling) plc_debug_prof('3: peers',count($peers));
74
75 // gets site info
76 $sites= $api->GetSites( array( $site_id ) );
77 $site=$sites[0];
78 $site_name= $site['name'];
79
80 if ($profiling) plc_debug_prof('4: sites',count($sites));
81 //////////////////////////////////////// building blocks for the renew area
82 // Constants
83 global $DAY;            $DAY = 24*60*60;
84 global $WEEK;           $WEEK = 7 * $DAY; 
85 global $MAX_WEEKS;      $MAX_WEEKS= 8;          // weeks from today
86 global $GRACE_DAYS;     $GRACE_DAYS=10;         // days for renewal promoted on top
87 global $NOW;            $NOW=mktime();
88
89 ////////////////////////////////////////////////////////////
90 // make the renew area on top and open if the expiration time is less than 10 days from now
91 function renew_needed ($slice) {
92   global $DAY, $NOW, $GRACE_DAYS;
93   $current_exp=$slice['expires'];
94
95   $time_left = $current_exp - $NOW;
96   $visible = $time_left/$DAY <= $GRACE_DAYS;
97   return $visible;
98 }
99
100 function renew_area ($slice,$site,$visible) {
101   global $DAY, $WEEK, $MAX_WEEKS, $GRACE_DAYS, $NOW;
102  
103   $current_exp=$slice['expires'];
104   $current_text = gmstrftime("%A %b-%d-%y %T %Z", $current_exp);
105   $max_exp= $NOW + ($MAX_WEEKS * $WEEK); // seconds since epoch
106   $max_text = gmstrftime("%A %b-%d-%y %T %Z", $max_exp);
107
108   // xxx some extra code needed to enable this area only if the slice description is OK:
109   // description and url must be non void
110   $toggle=
111     new PlekitToggle('renew',"Expires $current_text - Renew this slice",
112                      array("bubble"=>
113                            "Enter this zone if you wish to renew your slice",
114                            'visible'=>$visible));
115   $toggle->start();
116
117   // xxx message could take roles into account
118   if ($site['max_slices']<=0) {
119      $message= <<< EOF
120 <p class='my-slice-renewal'>Slice creation and renewal have been temporarily disabled for your
121 <site. This may have occurred because your site's nodes have been down
122 or unreachable for several weeks, and multiple attempts to contact
123 your site's PI(s) and Technical Contact(s) have all failed. If so,
124 contact your site's PI(s) and Technical Contact(s) and ask them to
125 bring up your site's nodes. Please visit your <a
126 href='/db/sites/index.php?id=$site_id'>site details</a> page to find
127 out more about your site's nodes, and how to contact your site's PI(s)
128 and Technical Contact(s).</p>
129 EOF;
130      echo $message;
131  
132   } else {
133     // xxx this is a rough cut and paste from the former UI
134     // showing a datepicker view could be considered as well with some extra work
135     // calculate possible extension lengths
136     $selectors = array();
137     foreach ( array ( 1 => "One more week", 
138                       2 => "Two more weeks", 
139                       3 => "Three more weeks", 
140                       4 => "One more month" ) as $weeks => $text ) {
141       $candidate_exp = $current_exp + $weeks*$WEEK;
142       if ( $candidate_exp < $max_exp) {
143         $selectors []= array('display'=>"$text (" . gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp) . ")",
144                              'value'=>$candidate_exp);
145         $max_renewal_weeks=$weeks;
146         $max_renewal_date= gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp);
147       }
148     }
149
150     if ( empty( $selectors ) ) {
151       print <<< EOF
152 <div class='my-slice-renewal'>
153 Slices annot be renewed more than $MAX_WEEKS weeks from now, i.e. not beyond $max_text. 
154 For this reason, the current slice cannot be renewed any further into the future, try again closer to expiration date.
155 </div>
156 EOF;
157      } else {
158       print <<< EOF
159 <div class='my-slice-renewal'>
160 <p>You <span class='bold'>must</span> provide a short description, 
161 as well as a link to a project website, before renewing it.
162
163 <br/> Please make sure to provide reasonable details on <span class='bold'>
164 the kind of traffic</span>, and <span class='bold'>copyrights</span> if relevant. 
165 Do <span class='bold'>not</span> provide bogus information; if a complaint is lodged against 
166 your slice  and your PlanetLab Operations Center is unable to determine what the normal behavior 
167 of your slice is, your slice may be deleted to resolve the complaint.</p>
168
169 <p><span class='bold'>NOTE:</span> 
170 Slices cannot be renewed beyond another $max_renewal_weeks week(s) ($max_renewal_date).
171 </p>
172 </div>
173 EOF;
174
175       $form = new PlekitForm (l_actions(),
176                               array('action'=>'renew-slice',
177                                     'slice_id'=>$slice['slice_id']));
178       $form->start();
179       print $form->label_html('expires','Duration');
180       print $form->select_html('expires',$selectors,array('label'=>'Pick one'));
181       print $form->submit_html('renew-button','Renew');
182       $form->end();
183     }
184   }
185  
186   $toggle->end();
187 }
188
189 ////////////////////////////////////////////////////////////
190
191 $am_in_slice = in_array(plc_my_person_id(),$person_ids);
192
193 if ($am_in_slice) {
194   drupal_set_title("My slice " . $name);
195  } else {
196   drupal_set_title("Slice " . $name);
197 }
198
199 $privileges = ( $local_peer && (plc_is_admin()  || plc_is_pi() || $am_in_slice));
200 $tags_privileges = $privileges || plc_is_admin();
201
202 $tabs=array();
203 $tabs [] = tab_nodes_slice($slice_id);
204 $tabs [] = tab_site($site);
205
206 // are these the right privileges for deletion ?
207 if ($privileges) {
208   $tabs ['Delete']= array('url'=>l_actions(),
209                           'method'=>'post',
210                           'values'=>array('action'=>'delete-slice','slice_id'=>$slice_id),
211                           'bubble'=>"Delete slice $name",
212                           'confirm'=>"Are you sure to delete slice $name");
213
214   $tabs["Events"]=array_merge(tablook_event(),
215                               array('url'=>l_event("Slice","slice",$slice_id),
216                                     'bubble'=>"Events for slice $name"));
217   $tabs["Comon"]=array_merge(tablook_comon(),
218                              array('url'=>l_comon("slice_id",$slice_id),
219                                    'bubble'=>"Comon page about slice $name"));
220 }
221
222 plekit_linetabs($tabs);
223
224 ////////////////////////////////////////
225 $peers->block_start($peer_id);
226
227 //////////////////////////////////////// renewal area 
228 // (1) close to expiration : show on top and open
229
230 if ($local_peer ) {
231   $renew_visible = renew_needed ($slice);
232   if ($renew_visible) renew_area ($slice,$site,true);
233  }
234
235
236 //////////////////// details
237 // default for opening the details section or not ?
238 if ($local_peer) {
239   $default_show_details = true;
240  } else {
241   $default_show_details = ! $renew_visible;
242  }
243   
244 $toggle = 
245   new PlekitToggle ('my-slice-details',"Details",
246                     array('bubble'=>
247                           'Display and modify details for that slice',
248                           'visible'=>get_arg('show_details',$default_show_details)));
249 $toggle->start();
250
251 $details=new PlekitDetails($privileges);
252 $details->form_start(l_actions(),array('action'=>'update-slice',
253                                        'slice_id'=>$slice_id,
254                                        'name'=>$name));
255
256 $details->start();
257 if (! $local_peer) {
258   $details->th_td("Peer",$peers->peer_link($peer_id));
259   $details->space();
260  }
261
262
263 $details->th_td('Name',$slice['name']);
264 $details->th_td('Description',$slice['description'],'description',
265                 array('input_type'=>'textarea',
266                       'width'=>50,'height'=>5));
267 $details->th_td('URL',$slice['url'],'url',array('width'=>50));
268 $details->tr_submit("submit","Update Slice");
269 $details->th_td('Expires',$expires);
270 $details->th_td('Instantiation',$slice['instantiation']);
271 $details->th_td("OMF-friendly", ($slice['omf_control'] ? 'Yes' : 'No') . " [to change: see 'omf_control' in the tags section below]");
272 $details->th_td('Site',l_site_obj($site));
273 // xxx show the PIs here
274 //$details->th_td('PIs',...);
275 $details->end();
276
277 $details->form_end();
278 $toggle->end();
279
280 //////////////////// persons
281 $person_columns = array('email','person_id','first_name','last_name','roles');
282 // get persons in slice
283 if (!empty($person_ids))
284   $persons=$api->GetPersons(array('person_id'=>$slice['person_ids']),$person_columns);
285 // just propose to add everyone else
286 // xxx this is maybe too much for admins as it slows stuff down 
287 // as regular persons can see only a fraction of the db anyway
288 $potential_persons=
289   $api->GetPersons(array('~person_id'=>$slice['person_ids'],
290                          'peer_id'=>NULL,
291                          'enabled'=>true),
292                    $person_columns);
293 $count=count($persons);
294
295 if ($profiling) plc_debug_prof('4: persons',count($persons));
296 $toggle=
297   new PlekitToggle ('my-slice-persons',"$count Users",
298                     array('bubble'=>
299                           'Manage accounts attached to this slice',
300                           'visible'=>get_arg('show_persons',false)));
301 $toggle->start();
302
303 ////////// people currently in
304 // visible:
305 // hide if both current+add are included
306 // so user can chose which section is of interest
307 // show otherwise
308 $toggle_persons = new PlekitToggle ('my-slice-persons-current',
309                                     "$count people currently in $name",
310                                     array('visible'=>get_arg('show_persons_current',!$privileges)));
311 $toggle_persons->start();
312
313 $headers=array();
314 $headers['email']='string';
315 $headers['first']='string';
316 $headers['last']='string';
317 $headers['R']='string';
318 if ($privileges) $headers[plc_delete_icon()]="none";
319 $table=new PlekitTable('persons',$headers,'0',
320                        array('notes_area'=>false));
321 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
322 $form->start();
323 $table->start();
324 if ($persons) foreach ($persons as $person) {
325   $table->row_start();
326   $table->cell(l_person_obj($person));
327   $table->cell($person['first_name']);
328   $table->cell($person['last_name']);
329   $table->cell(plc_vertical_table ($person['roles']));
330   if ($privileges) $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
331   $table->row_end();
332 }
333 // actions area
334 if ($privileges) {
335
336   // remove persons
337   $table->tfoot_start();
338
339   $table->row_start();
340   $table->cell($form->submit_html ("remove-persons-from-slice","Remove selected"),
341                array('hfill'=>true,'align'=>'right'));
342   $table->row_end();
343  }
344 $table->end();
345 $toggle_persons->end();
346
347 ////////// people to add
348 if ($privileges) {
349   $count=count($potential_persons);
350   $toggle_persons = new PlekitToggle ('my-slice-persons-add',
351                                       "$count people may be added to $name",
352                                       array('visible'=>get_arg('show_persons_add',false)));
353   $toggle_persons->start();
354   if ( ! $potential_persons ) {
355     // xxx improve style
356     echo "<p class='not-relevant'>No person to add</p>";
357   } else {
358     $headers=array();
359     $headers['email']='string';
360     $headers['first']='string';
361     $headers['last']='string';
362     $headers['R']='string';
363     $headers['+']="none";
364     $options = array('notes_area'=>false,
365                      'search_width'=>15,
366                      'pagesize'=>8);
367     // show search for admins only as other people won't get that many names to add
368     if ( ! plc_is_admin() ) $options['search_area']=false;
369     
370     $table=new PlekitTable('add_persons',$headers,'0',$options);
371     $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
372     $form->start();
373     $table->start();
374     if ($potential_persons) foreach ($potential_persons as $person) {
375         $table->row_start();
376         $table->cell(l_person_obj($person));
377         $table->cell($person['first_name']);
378         $table->cell($person['last_name']);
379         $table->cell(plc_vertical_table ($person['roles']));
380         $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
381         $table->row_end();
382       }
383     // add users
384     $table->tfoot_start();
385     $table->row_start();
386     $table->cell($form->submit_html ("add-persons-in-slice","Add selected"),
387                  array('hfill'=>true,'align'=>'right'));
388     $table->row_end();
389     $table->end();
390     $form->end();
391   }
392   $toggle_persons->end();
393 }
394 $toggle->end();
395
396 //////////////////////////////////////////////////////////// Nodes
397 // the nodes details to display here
398 // (1) we search for the tag types for which 'category' matches 'node*/ui*'
399 // all these tags will then be tentatively displayed in this area
400 // (2) further information can also be optionally specified in the category:
401 //     (.) we split the category with '/' and search for assignments of the form var=value
402 //     (.) header can be set to supersede the column header (default is tagname)
403 //     (.) rank can be used for ordering the columns (default is tagname)
404 //     (.) type is passed to the javascript table, for sorting (default is 'string')
405
406 // minimal list as a start
407 $node_fixed_columns = array('hostname','node_id','peer_id','slice_ids_whitelist',
408                             'run_level','boot_state','last_contact','node_type');
409 // create a VisibleTags object : basically the list of tag columns to show
410 $visibletags = new VisibleTags ($api, 'node');
411 $visiblecolumns = $visibletags->column_names();
412 $node_columns=array_merge($node_fixed_columns,$visiblecolumns);
413
414 // optimizing calls to GetNodes
415 $all_nodes=$api->GetNodes(NULL,$node_columns);
416 //$slice_nodes=$api->GetNodes(array('node_id'=>$slice['node_ids']),$node_columns);
417 //$potential_nodes=$api->GetNodes(array('~node_id'=>$slice['node_ids']),$node_columns);
418 $slice_nodes=array();
419 $potential_nodes=array();
420 $reservable_nodes=array();
421 foreach ($all_nodes as $node) {
422   if (in_array($node['node_id'],$slice['node_ids'])) {
423     $slice_nodes[]=$node;
424     if ($node['node_type']=='reservable') $reservable_nodes[]=$node;
425   } else {
426     $potential_nodes[]=$node;
427   }
428 }
429 if ($profiling) plc_debug_prof('5: nodes',count($slice_nodes));
430 ////////////////////
431 // outline the number of reservable nodes
432 $nodes_message=count_english($slice_nodes,"node");
433 if (count($reservable_nodes)) $nodes_message .= " (" . count($reservable_nodes) . " reservable)";
434 $toggle=new PlekitToggle ('my-slice-nodes',$nodes_message,
435                           array('bubble'=>
436                                 'Manage nodes attached to this slice',
437                                 'visible'=>get_arg('show_nodes',false)));
438 $toggle->start();
439
440 ////////// show a notice to people having attached a reservable node
441 if (count($reservable_nodes) && $privileges) {
442   $mark=reservable_mark();
443   print <<<EOF
444 <p class='note_reservable'>
445 You have attached one or more <span class='bold'>reservable nodes</span> to your slice. 
446 Reservable nodes show up with the '$mark' mark. 
447 Your slice will be available <span class='bold'>only during timeslots
448 where you have obtained leases</span>. 
449 You can manage your leases in the tab below.
450 <br>
451 Please note that as of August 2010 this feature is experimental. 
452 Feedback is appreciated at <a href="mailto:devel@planet-lab.org">devel@planet-lab.org</a>
453 </p>
454 EOF;
455 }  
456
457 //////////////////// reservable nodes area
458 $count=count($reservable_nodes);
459 if ($count && $privileges) {
460   // having reservable nodes in white lists looks a bit off scope for now...
461   $toggle_nodes=new PlekitToggle('my-slice-nodes-reserve',
462                                  "Leases - " . count($reservable_nodes) . " reservable node(s)",
463                                  array('visible'=>get_arg('show_nodes_resa',false)));
464   $toggle_nodes->start();
465   $grain=$api->GetLeaseGranularity();
466   if ($profiling) plc_debug_prof('6 granul',$grain);
467   // where to start from, expressed as an offset in hours from now
468   $resa_offset=$_GET['resa_offset'];
469   if ( ! $resa_offset ) $resa_offset=0;
470   $rough_start=time()+$resa_offset*3600;
471   // xxx should be configurable
472   $resa_slots=$_GET['resa_slots'];
473   if ( ! $resa_slots ) $resa_slots = 36;
474   // for now, show the next 72 hours, or 72 grains, which ever is smaller
475   $duration=$resa_slots*$grain;
476   $steps=$duration/$grain;
477   $start=intval($rough_start/$grain)*$grain;
478   $end=$rough_start+$duration;
479   $lease_columns=array('lease_id','name','t_from','t_until','hostname','name');
480   $leases=$api->GetLeases(array(']t_until'=>$rough_start,'[t_from'=>$end,'-SORT'=>'t_from'),$lease_columns);
481   if ($profiling) plc_debug_prof('7 leases',count($leases));
482   // hash nodes -> leases
483   $host_hash=array();
484   foreach ($leases as $lease) {
485     $hostname=$lease['hostname'];
486     if ( ! $host_hash[$hostname] ) {
487         $host_hash[$hostname]=array();
488     }
489     // resync within the table
490     $lease['nfrom']=($lease['t_from']-$start)/$grain;
491     $lease['nuntil']=($lease['t_until']-$start)/$grain;
492     $host_hash[$hostname] []= $lease;
493   }
494   # leases_data is the name used by leases.js to locate this table
495   echo "<table id='leases_data'>";
496   # pass (slice_id,slicename) as the [0,0] coordinate as thead>tr>td
497   echo "<thead><tr><td>" . $slice['slice_id'] . '&' . $slice['name'] . "</td>";
498   # the timeslot headers read (timestamp,label)
499   $day_names=array('Su','M','Tu','W','Th','F','Sa');
500   for ($i=0; $i<$steps; $i++) {
501     $timestamp=($start+$i*$grain);
502     $day=$day_names[intval(strftime("%w",$timestamp))];
503     $label=$day . strftime(" %H:%M",$timestamp);
504     // expose in each header cell the full timestamp, and how to display it - use & as a separator*/
505     echo "<th>" . implode("&",array($timestamp,$label)) . "</th>";
506   }
507   echo "</tr></thead><tbody>";
508   // todo - sort on hostnames
509   function sort_hostname ($a,$b) { return ($a['hostname']<$b['hostname'])?-1:1;}
510   usort($reservable_nodes,sort_hostname);
511   foreach ($reservable_nodes as $node) {
512     echo "<tr><th scope='row'>". $node['hostname'] . "</th>";
513     $hostname=$node['hostname'];
514     $leases=$host_hash[$hostname];
515     $counter=0;
516     while ($counter<$steps) {
517       if ($leases && ($leases[0]['nfrom']<=$counter)) {
518         $lease=array_shift($leases);
519         /* nicer display, merge two consecutive leases for the same slice 
520            avoid doing that for now, as it might makes things confusing */
521         /* while ($leases && ($leases[0]['name']==$lease['name']) && ($leases[0]['nfrom']==$lease['nuntil'])) {
522           $lease['nuntil']=$leases[0]['nuntil'];
523           array_shift($leases);
524           }*/
525         $duration=$lease['nuntil']-$counter;
526         echo "<td colspan='$duration'>" . $lease['lease_id'] . '&' . $lease['name'] . "</td>";
527         $counter=$lease['nuntil']; 
528       } else {
529         echo "<td></td>";
530         $counter+=1;
531       }
532     }
533     echo "</tr>";
534   }
535   echo "</tbody></table>\n";
536
537   // the general layout for the scheduler
538   echo <<< EOF
539 <div id='leases_area'></div>
540
541 <div id='leases_buttons'>
542   <button id='leases_clear' type='submit'>Clear</button>
543   <button id='leases_submit' type='submit'>Submit</button>
544 </div>
545 EOF;
546
547   $toggle_nodes->end();
548  }
549
550 //////////////////// nodes currently in
551 $toggle_nodes=new PlekitToggle('my-slice-nodes-current',
552                                count_english($slice_nodes,"node") . " currently in $name",
553                                array('visible'=>get_arg('show_nodes_current',!$privileges)));
554 $toggle_nodes->start();
555
556 $headers=array();
557 $notes=array();
558 $headers['peer']='string';
559 $headers['hostname']='string';
560 $short="-S-"; $long=Node::status_footnote(); $type='string'; 
561         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
562 $short=reservable_mark(); $long=reservable_legend(); $type='string';
563         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
564 // the extra tags, configured for the UI
565 $headers=array_merge($headers,$visibletags->headers());
566 $notes=array_merge($notes,$visibletags->notes());
567
568 if ($privileges) $headers[plc_delete_icon()]="none";
569
570 $table_options = array('notes'=>$notes,
571                        'search_width'=>15,
572                        'pagesize'=>20);
573 $table=new PlekitTable('nodes',$headers,'1',$table_options);
574
575 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
576 $form->start();
577 $table->start();
578 if ($slice_nodes) foreach ($slice_nodes as $node) {
579   $table->row_start();
580   $peers->cell($table,$node['peer_id']);
581   $table->cell(l_node_obj($node));
582   $run_level=$node['run_level'];
583   list($label,$class) = Node::status_label_class_($node);
584   $table->cell ($label,array('class'=>$class));
585   $table->cell( ($node['node_type']=='reservable')?reservable_mark():"" );
586   foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
587
588   if ($privileges) $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
589   $table->row_end();
590 }
591 // actions area
592 if ($privileges) {
593
594   // remove nodes
595   $table->tfoot_start();
596
597   $table->row_start();
598   $table->cell($form->submit_html ("remove-nodes-from-slice","Remove selected"),
599                array('hfill'=>true,'align'=>'right'));
600   $table->row_end();
601  }
602 $table->end();
603 $toggle_nodes->end();
604
605 //////////////////// nodes to add
606 if ($privileges) {
607   $new_potential_nodes = array();
608   if ($potential_nodes) foreach ($potential_nodes as $node) {
609       $emptywl=empty($node['slice_ids_whitelist']);
610       $inwl = (!emptywl) and in_array($slice['slice_id'],$node['slice_ids_whitelist']);
611       if ($emptywl or $inwl)
612         $new_potential_nodes[]=$node;
613   }
614   $potential_nodes=$new_potential_nodes;
615
616   $count=count($potential_nodes);
617   $toggle_nodes=new PlekitToggle('my-slice-nodes-add',
618                                  count_english($potential_nodes,"more node") . " available",
619                                  array('visible'=>get_arg('show_nodes_add',false)));
620   $toggle_nodes->start();
621
622   if ( $potential_nodes ) {
623     $headers=array();
624     $notes=array();
625     $headers['peer']='string';
626     $headers['hostname']='string';
627     $short="-S-"; $long=Node::status_footnote(); $type='string'; 
628         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
629         $short=reservable_mark(); $long=reservable_legend(); $type='string';
630         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
631     // the extra tags, configured for the UI
632     $headers=array_merge($headers,$visibletags->headers());
633     $notes=array_merge($notes,$visibletags->notes());
634     $headers['+']="none";
635     
636     $table=new PlekitTable('add_nodes',$headers,'1', $table_options);
637     $form=new PlekitForm(l_actions(),
638                          array('slice_id'=>$slice['slice_id']));
639     $form->start();
640     $table->start();
641     if ($potential_nodes) foreach ($potential_nodes as $node) {
642         $table->row_start();
643         $peers->cell($table,$node['peer_id']);
644         $table->cell(l_node_obj($node));
645         list($label,$class) = Node::status_label_class_($node);
646         $table->cell ($label,array('class'=>$class));
647         $table->cell( ($node['node_type']=='reservable')?reservable_mark():"" );
648         foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
649         $table->cell ($form->checkbox_html('node_ids[]',$node['node_id']));
650         $table->row_end();
651       }
652     // add nodes
653     $table->tfoot_start();
654     $table->row_start();
655     $table->cell($form->submit_html ("add-nodes-in-slice","Add selected"),
656                  array('hfill'=>true,'align'=>'right'));
657     $table->row_end();
658     $table->end();
659     $form->end();
660   }
661   $toggle_nodes->end();
662 }
663
664 $toggle->end();
665
666 // very wide values get abbreviated
667 $tag_value_threshold=24;
668 //////////////////////////////////////////////////////////// Tags
669 //if ( $local_peer ) {
670   $tags=$api->GetSliceTags (array('slice_id'=>$slice_id));
671   if ($profiling) plc_debug_prof('8 slice tags',count($tags));
672   function get_tagname ($tag) { return $tag['tagname'];}
673   $tagnames = array_map ("get_tagname",$tags);
674   
675   $toggle = new PlekitToggle ('slice-tags',count_english_warning($tags,'tag'),
676                               array('bubble'=>'Inspect and set tags on tat slice',
677                                     'visible'=>get_arg('show_tags',false)));
678   $toggle->start();
679   
680   $headers=array(
681     "Name"=>"string",
682     "Value"=>"string",
683     "Node"=>"string",
684     "NodeGroup"=>"string");
685   if ($tags_privileges) $headers[plc_delete_icon()]="none";
686   
687   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
688   $table=new PlekitTable("slice_tags",$headers,'0',$table_options);
689   $form=new PlekitForm(l_actions(),
690                        array('slice_id'=>$slice['slice_id']));
691   $form->start();
692   $table->start();
693   if ($tags) {
694     foreach ($tags as $tag) {
695       $node_name = "ALL";
696       if ($tag['node_id']) {
697         $tag_nodes = $api->GetNodes(array('node_id'=>$tag['node_id']));
698         if ($profiling) plc_debug_prof('9 node for slice tag',count($tag_nodes));
699         if($tag_nodes) {
700           $node = $tag_nodes[0];
701           $node_name = $node['hostname'];
702         }
703       }
704       $nodegroup_name="n/a";
705       if ($tag['nodegroup_id']) { 
706         $nodegroups=$api->GetNodeGroups(array('nodegroup_id'=>$tag['nodegroup_id']));
707         if ($profiling) plc_debug_prof('10 nodegroup for slice tag',$nodegroup);
708         if ($nodegroup) {
709           $nodegroup = $nodegroups[0];
710           $nodegroup_name = $nodegroup['groupname'];
711         }
712       }
713       $table->row_start();
714       $table->cell(l_tag_obj($tag));
715       // very wide values get abbreviated
716       $table->cell(truncate_and_popup($tag['value'],$tag_value_threshold));
717       $table->cell($node_name);
718       $table->cell($nodegroup_name);
719       if ($tags_privileges) $table->cell ($form->checkbox_html('slice_tag_ids[]',$tag['slice_tag_id']));
720       $table->row_end();
721     }
722   }
723   if ($tags_privileges) {
724     $table->tfoot_start();
725     $table->row_start();
726     $table->cell($form->submit_html ("delete-slice-tags","Remove selected"),
727                  array('hfill'=>true,'align'=>'right'));
728     $table->row_end();
729     
730     $table->row_start();
731     function tag_selector ($tag) {
732       return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']);
733     }
734     $all_tags= $api->GetTagTypes( array ("category"=>"slice*","-SORT"=>"+tagname"), array("tagname","tag_type_id"));
735     if ($profiling) plc_debug_prof('11 tagtypes',count($all_tags));
736     $selector_tag=array_map("tag_selector",$all_tags);
737     
738     function node_selector($node) { 
739       return array("display"=>$node["hostname"],"value"=>$node['node_id']);
740     }
741     $selector_node=array_map("node_selector",$slice_nodes);
742     
743     function nodegroup_selector($ng) {
744       return array("display"=>$ng["groupname"],"value"=>$ng['nodegroup_id']);
745     }
746     $all_nodegroups = $api->GetNodeGroups( array("groupname"=>"*"), array("groupname","nodegroup_id"));
747     if ($profiling) plc_debug_prof('13 nodegroups',count($all_nodegroups));
748     $selector_nodegroup=array_map("nodegroup_selector",$all_nodegroups);
749     
750     $table->cell($form->select_html("tag_type_id",$selector_tag,array('label'=>"Choose Tag")));
751     $table->cell($form->text_html("value","",array('width'=>8)));
752     $table->cell($form->select_html("node_id",$selector_node,array('label'=>"All Nodes")));
753     $table->cell($form->select_html("nodegroup_id",$selector_nodegroup,array('label'=>"No Nodegroup")));
754     $table->cell($form->submit_html("add-slice-tag","Set Tag"),array('columns'=>2,'align'=>'left'));
755     $table->row_end();
756   }
757     
758   $table->end();
759   $form->end();
760   $toggle->end();
761 //}
762
763
764 //////////////////////// renew slice
765 if ($local_peer ) {
766   if ( ! $renew_visible) renew_area ($slice,$site,false);
767  }
768
769 $peers->block_end($peer_id);
770
771 if ($profiling) plc_debug_prof_end();
772
773 // Print footer
774 include 'plc_footer.php';
775
776 ?>