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