ckp
[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 // -------------------- admins potentially need to get full list of users
26 ini_set('memory_limit','32M');
27
28 // -------------------- 
29 // recognized URL arguments
30 $slice_id=intval($_GET['id']);
31 if ( ! $slice_id ) { plc_error('Malformed URL - id not set'); return; }
32
33 ////////////////////
34 // Get all columns as we focus on only one entry
35 $slices= $api->GetSlices( array($slice_id));
36
37 if (empty($slices)) {
38   drupal_set_message ("Slice " . $slice_id . " not found");
39   return;
40  }
41
42 $slice=$slices[0];
43
44 // pull all node info to vars
45 $name= $slice['name'];
46 $expires = date( "d/m/Y", $slice['expires'] );
47 $site_id= $slice['site_id'];
48
49 //$node_ids=$slice['node_ids'];
50 $person_ids=$slice['person_ids'];
51 //$slice_tag_ids= $slice['slice_tag_ids'];
52
53 // get peers
54 $peer_id= $slice['peer_id'];
55 $peers=new Peers ($api);
56 $local_peer = ! $peer_id;
57
58 // gets site info
59 $sites= $api->GetSites( array( $site_id ) );
60 $site=$sites[0];
61 $site_name= $site['name'];
62 $max_slices = $site['max_slices'];
63 // xxx PIs
64 //$pis=$api->GetPersons(...)
65
66 // get all persons info
67 if (!empty($person_ids))
68   $persons=$api->GetPersons($person_ids,array('email','enabled'));
69
70
71 //////////////////////////////////////// building blocks for the renew area
72 // Constants
73 global $DAY;            $DAY = 24*60*60;
74 global $WEEK;           $WEEK = 7 * $DAY; 
75 global $MAX_WEEKS;      $MAX_WEEKS= 8;                  // weeks from today
76 global $GRACE_DAYS;     $GRACE_DAYS=10;                 // days for renewal promoted on top
77 global $NOW;            $NOW=mktime();
78
79
80 // make the renew area on top and open if the expiration time is less than 10 days from now
81 function renew_needed ($slice) {
82   global $DAY, $NOW, $GRACE_DAYS;
83   $current_exp=$slice['expires'];
84
85   $time_left = $current_exp - $NOW;
86   $visible = $time_left/$DAY <= $GRACE_DAYS;
87   return $visible;
88 }
89
90 function renew_area ($slice,$site,$visible) {
91   global $DAY, $WEEK, $MAX_WEEKS, $GRACE_DAYS, $NOW;
92  
93   $current_exp=$slice['expires'];
94   $max_exp= $NOW + ($MAX_WEEKS * $WEEK); // seconds since epoch
95
96   // xxx some extra code needed to enable this area only if the slice description is OK:
97   // description and url must be non void
98   $toggle=new PlekitToggle('renew',"Renew this slice",
99                            array("trigger-bubble"=>"Enter this zone if you wish to renew your slice",
100                                  'start-visible'=>$visible));
101   $toggle->start();
102
103   // xxx message could take roles into account
104   if ($site['max_slices']<=0) {
105      $message= <<< EOF
106 <p class='renewal'>Slice creation and renewal have been temporarily disabled for your
107 <site. This may have occurred because your site's nodes have been down
108 or unreachable for several weeks, and multiple attempts to contact
109 your site's PI(s) and Technical Contact(s) have all failed. If so,
110 contact your site's PI(s) and Technical Contact(s) and ask them to
111 bring up your site's nodes. Please visit your <a
112 href='/db/sites/index.php?id=$site_id'>site details</a> page to find
113 out more about your site's nodes, and how to contact your site's PI(s)
114 and Technical Contact(s).</p>
115 EOF;
116      echo $message;
117  
118   } else {
119     // xxx this is a rough cut and paste from the former UI
120     // showing a datepicker view could be considered as well with some extra work
121     // calculate possible extension lengths
122     $selectors = array();
123     foreach ( array ( 1 => "One more week", 
124                       2 => "Two more weeks", 
125                       3 => "Two more weeks", 
126                       4 => "One more month" ) as $weeks => $text ) {
127       $candidate_exp = $current_exp + $weeks*$WEEK;
128       if ( $candidate_exp < $max_exp) {
129         $selectors []= array('display'=>"$text (" . gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp) . ")",
130                              'value'=>$candidate_exp);
131         $max_renewal_weeks=$weeks;
132         $max_renewal_date= gmstrftime("%A %b-%d-%y %T %Z", $candidate_exp);
133       }
134     }
135
136     if ( empty( $selectors ) ) {
137       print <<< EOF
138 <div class='plc-warning renewal'>
139 Slice cannot be renewed any further into the future, try again closer to expiration date.
140 </div>
141 EOF;
142      } else {
143       print <<< EOF
144 <div class='renewal'>
145 <p>You must provide a short description as well as a link to a project website before renewing it.
146 Do <span class='bold'>not</span> provide bogus information; if a complaint is lodged against your slice 
147 and PlanetLab Operations is unable to determine what the normal behavior of your slice is, 
148 your slice may be deleted to resolve the complaint.</p>
149 <p><span class='bold'>NOTE:</span> 
150 Slices cannot be renewed beyond another $max_renewal_weeks week(s) ($max_renewal_date).
151 </p>
152 </div>
153 EOF;
154
155       $form = new PlekitForm (l_actions(),
156                               array('action'=>'renew-slice',
157                                     'slice_id'=>$slice['slice_id']));
158       $form->start();
159       print $form->label_html('expires','Duration');
160       print $form->select_html('expires',$selectors,array('label'=>'Pick one'));
161       print $form->submit_html('renew-button','Renew');
162       $form->end();
163     }
164   }
165  
166   $toggle->end();
167 }
168
169 ////////// 
170 drupal_set_title("Details for slice " . $name);
171 $local_peer= ! $peer_id;
172
173 $am_in_slice = in_array(plc_my_person_id(),$person_ids);
174
175 $privileges = (plc_is_admin()  || $am_in_slice);
176
177 $tabs=array();
178 $tabs [] = tab_nodes_slice($slice_id);
179 $tabs [] = tab_site($site_id);
180
181 // are these the right privileges for deletion ?
182 if ($privileges) {
183   $tabs ['Delete']= array('url'=>l_actions(),
184                           'method'=>'post',
185                           'values'=>array('action'=>'delete-slice','slice_id'=>$slice_id),
186                           'bubble'=>"Delete slice $name",
187                           'confirm'=>'Are you sure to delete $name');
188
189   $tabs["Events"]=array_merge(tablook_event(),
190                               array('url'=>l_event("Slice","slice",$slice_id),
191                                     'bubble'=>"Events for slice $name"));
192   $tabs["Comon"]=array_merge(tablook_comon(),
193                              array('url'=>l_comon("slice_id",$slice_id),
194                                    'bubble'=>"Comon page about slice $name"));
195 }
196
197 plekit_linetabs($tabs);
198
199 ////////////////////////////////////////
200 $peers->block_start($peer_id);
201
202 //////////////////////////////////////// renewal area 
203 // (1) close to expiration : show on top and open
204
205 if ($local_peer ) {
206   $renew_visible = renew_needed ($slice);
207   if ($renew_visible) renew_area ($slice,$site,true);
208  }
209
210
211 //////////////////// details
212 $toggle = new PlekitToggle ('slice',"Details",
213                             array('trigger-bubble'=>'Display and modify details for that slice'));
214 $toggle->start();
215
216 $details=new PlekitDetails($privileges);
217 $details->form_start(l_actions(),array('action'=>'update-slice',
218                                        'slice_id'=>$slice_id,
219                                        'name'=>$name));
220
221 $details->start();
222 if (! $local_peer) {
223   $details->th_td("Peer",$peers->peer_link($peer_id));
224   $details->space();
225  }
226
227
228 $details->th_td('Name',$slice['name']);
229 $details->th_td('Description',$slice['description'],'description',
230                 array('input_type'=>'textarea',
231                       'width'=>50,'height'=>5));
232 $details->th_td('URL',$slice['url'],'url',array('width'=>50));
233 $details->th_td('Expires',$expires);
234 $details->th_td('Instantiation',$slice['instantiation']);
235 $details->th_td('Site',l_site_obj($site));
236 // xxx show the PIs here
237 //$details->th_td('PIs',...);
238 $details->tr_submit("submit","Update Slice");
239 $details->end();
240
241 $details->form_end();
242 $toggle->end();
243
244 //////////////////// users
245 $persons=$api->GetPersons(array('person_id'=>$slice['person_ids']));
246 // just propose to add evryone else, regular users can see only a fraction of the db anyway
247 $potential_persons=$api->GetPersons(array('~person_id'=>$slice['person_ids'],'peer_id'=>NULL),
248                                     array('email','person_id','first_name','last_name','roles'));
249 $show_users=false;
250 if ( $_GET['show_users']) $show_users=true;
251 $toggle=new PlekitToggle ('persons',"Users",array('trigger-bubble'=>'Manage users attached to this slice','start-visible'=>$show_users));
252 $toggle->start();
253
254 ////////// people currently in
255 $headers=array();
256 $headers['email']='string';
257 $headers['first']='string';
258 $headers['last']='string';
259 $headers['R']='string';
260 if ($privileges) $headers[plc_delete_icon()]="none";
261 // xxx caption currently broken, messes pagination
262 $table=new PlekitTable('persons',$headers,'1',array(//'caption'=>'Current users',
263                                                     'search_area'=>false,
264                                                     'notes_area'=>false,
265                                                     'pagesize_area'=>false));
266 $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
267 $form->start();
268 $table->start();
269 if ($persons) foreach ($persons as $person) {
270   $table->row_start();
271   $table->cell(l_person_obj($person));
272   $table->cell($person['first_name']);
273   $table->cell($person['last_name']);
274   $table->cell(plc_vertical_table ($person['roles']));
275   if ($privileges) $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
276   $table->row_end();
277 }
278 // actions area
279 if ($privileges) {
280
281   // remove users
282   $table->tfoot_start();
283
284   $table->row_start();
285   $table->cell($form->submit_html ("remove-persons-from-slice","Remove selected"),
286                $table->columns(),"right");
287   $table->row_end();
288  }
289 $table->end();
290
291 ////////// people to add
292 if ($privileges) {
293   $headers=array();
294   $headers['email']='string';
295   $headers['first']='string';
296   $headers['last']='string';
297   $headers['R']='string';
298   $headers['Add']="none";
299   // xxx caption currently broken, messes pagination
300   $options = array(//'caption'=>'Users to add',
301                    'notes_area'=>false,
302                    'search_width'=>15,
303                    'pagesize'=>8);
304   // show search for admins only as other people won't get that many names to add
305   if ( ! plc_is_admin() ) $options['search_area']=false;
306   
307   $table=new PlekitTable('add_persons',$headers,'1',$options);
308   $form=new PlekitForm(l_actions(),array('slice_id'=>$slice['slice_id']));
309   $form->start();
310   $table->start();
311   if ($potential_persons) foreach ($potential_persons as $person) {
312       $table->row_start();
313       $table->cell(l_person_obj($person));
314       $table->cell($person['first_name']);
315       $table->cell($person['last_name']);
316       $table->cell(plc_vertical_table ($person['roles']));
317       $table->cell ($form->checkbox_html('person_ids[]',$person['person_id']));
318       $table->row_end();
319     }
320   // add users
321   $table->tfoot_start();
322   
323   $table->row_start();
324   $table->cell($form->submit_html ("add-persons-in-slice","Add selected"),
325                $table->columns(),"right");
326   $table->row_end();
327  }
328 $table->end();
329
330 $toggle->end();
331
332 //////////////////// nodes
333
334 //////////////////// tags
335
336 if ($local_peer ) {
337   if ( ! $renew_visible) renew_area ($slice,$site,false);
338  }
339
340 if ($renew_visible) renew_area ($slice,$site,true);
341
342 $peers->block_end($peer_id);
343
344 // Print footer
345 include 'plc_footer.php';
346
347 return;
348
349 ?>
350