add an admin option to update the site:enabled field on the site view page.
[plewww.git] / planetlab / sites / site.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 'form.php';
23 require_once 'toggle.php';
24
25 // -------------------- 
26 // recognized URL arguments
27 $site_id=intval($_GET['id']);
28 if ( ! $site_id ) { plc_error('Malformed URL - id not set'); return; }
29
30 ////////////////////
31 // Get all columns as we focus on only one entry
32 $sites = $api->GetSites( array($site_id));
33
34 if (empty($sites)) {
35   drupal_set_message ("Site " . $site_id . " not found");
36   return;
37  }
38
39 $site=$sites[0];
40 // var names to api return
41 $sitename= htmlentities($site['name']);
42 $abbreviated_name= htmlentities($site['abbreviated_name']);
43 $site_url= $site['url'];
44 $login_base= $site['login_base'];
45 $site_lat= $site['latitude'];
46 $site_long= $site['longitude'];
47 $max_slivers= $site['max_slivers'];
48 $max_slices= $site['max_slices'];
49
50 $enabled = $site['enabled'];
51
52 // get peer 
53 $peer_id= $site['peer_id'];
54 $peers = new Peers ($api);
55 $local_peer = ! $peer_id;
56
57 // extra privileges to admins, and (pi||tech) on this site
58 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
59 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
60 $is_site_admin = ($local_peer && plc_is_admin());
61   
62 $address_ids= $site['address_ids'];
63 $pcu_ids= $site['pcu_ids'];
64 $node_ids= $site['node_ids'];
65 $person_ids= $site['person_ids'];
66 $slice_ids= $site['slice_ids'];
67
68 $api->begin();
69 // gets address info
70 $api->GetAddresses( $address_ids );
71
72 // gets pcu info
73 // GetPCUs is not accessible to the 'user' role
74 //$api->GetPCUs( $pcu_ids );
75
76 // gets node info
77 $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state" ) );
78
79 // gets person info
80 $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
81
82 $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
83
84 //list( $addresses, $pcus, $nodes, $persons, $slices )= $api->commit();
85 list( $addresses, $nodes, $persons, $slices )= $api->commit();
86   
87 $techs = array();
88 $pis = array();
89 $disabled_persons = array();
90 foreach( $persons as $person ) {
91   $role_ids= $person['role_ids'];
92
93   if ( in_array( '20', $role_ids ))     $pis[] = $person;
94   if ( in_array( '40', $role_ids ))     $techs[] = $person;
95   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
96   
97 }
98
99 $has_disabled_persons = count ($disabled_persons) !=0;
100
101 // get number of slivers
102 $slivers_count=0;
103 if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_ids']);
104
105 ////////////////////////////////////////
106 drupal_set_title("Details for site " . $sitename);
107   
108 $tabs=array();
109
110 // available actions
111 if ( $is_site_admin)
112   $tabs['Expire slices'] = array('url'=>l_actions(),
113                                  'method'=>'POST',
114                                  'values'=>array('site_id'=>$site_id,
115                                                  'action'=>'expire-all-slices-in-site'),
116                                  'bubble'=>"Expire all slices and prevent creation of new slices",
117                                  'confirm'=>"Suspend all slices in $login_base");
118 if ( $is_site_admin)
119   $tabs['Delete']=array('url'=>l_actions(),
120                         'method'=>'POST',
121                         'values'=>array('site_id'=>$site_id,
122                                         'action'=>'delete-site'),
123                         'bubble'=>"Delete site $sitename",
124                         'confirm'=>"Are you sure you want to delete site $login_base");
125
126 if ( $is_site_pi ) 
127   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
128                               'method'=>'post',
129                               'bubble'=>'Create new slice in site');
130
131 if (plc_is_admin() || plc_in_site($site_id))
132   $tabs["Events"]=array_merge (tablook_event(),
133                                array('url'=>l_event("Site","site",$site_id),
134                                      'bubble'=>"Events for site $sitename"));
135 if (plc_is_admin() || plc_in_site($site_id))
136   $tabs["Comon"]=array_merge(tablook_comon(),
137                              array('url'=>l_comon("site_id",$site_id),
138                                    'bubble'=>"Comon page for $sitename"));
139
140
141 plekit_linetabs($tabs);
142
143 // show gray background on foreign objects : start a <div> with proper class
144 $peers->block_start ($peer_id);
145
146 if ( $local_peer && ( ! $enabled ) )
147   plc_warning ("This site is not enabled - Please visit " . 
148                href (l_sites_pending(),"this page") . 
149                " to review pending applications.");
150
151 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
152
153
154 $toggle = new PlekitToggle ('site',"Details",
155                             array('visible'=>get_arg('show_details',true),
156                                   'bubble'=>'Display and modify details for that site'));
157 $toggle->start();
158
159 $details = new PlekitDetails($can_update);
160
161 if ( ! $site['is_public']) 
162   plc_warning("This site is not public!");
163
164 $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
165
166 $details->start();
167
168 $details->th_td("Full name",$sitename,'name',array('width'=>50));
169 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
170 $details->th_td("URL",$site_url,'url',array('width'=>40));
171 $details->th_td("Latitude",$site_lat,'latitude');
172 $details->th_td("Longitude",$site_long,'longitude');
173
174 // modifiable by admins only
175 if (plc_is_admin()) 
176   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
177 else
178   $details->th_td("Login base",$login_base);
179 if (plc_is_admin())
180   $details->th_td("Max slices",$max_slices,'max_slices');
181 else
182   $details->th_td("Max slices",$max_slices);
183 if (plc_is_admin())
184 {
185   $selectors=array(array('display'=>"False",'value'=>'0'), 
186                                    array('display'=>"True",'value'=>'1'));
187   $selectors[intval($enabled)]['selected'] = 'selected';
188
189   $enable_select = $f->select_html ("enabled", $selectors);
190
191   $details->th_td("Enabled",$enable_select,'enabled', array('input_type' => 'select', 'value'=>$enabled));
192 } else
193   $details->th_td("Enabled",$enabled);
194
195 $details->tr_submit("submit","Update Site");
196
197 if ( ! $local_peer) {
198   $details->space();
199   $details->th_td("Peer",$peers->peer_link($peer_id));
200  }
201 $details->end();
202 $details->form_end();
203 $toggle->end();
204
205 //////////////////// mode details - for local object
206 if ( $local_peer ) {
207
208   //////////////////// nodes
209   // xxx missing : would need to add columns on attached PCU name and port if avail
210   $nb_boot = 0;
211   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
212
213   $nodes_title = "Nodes : ";
214   $nodes_title .= count($nodes) . " total";
215   $nodes_title .= " / " . $nb_boot . " boot";
216   if ($nb_boot < 2 ) 
217     $nodes_title = plc_warning_html ($nodes_title);
218   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
219
220   $toggle=new PlekitToggle ('nodes',$nodes_title,
221                             array('visible'=>get_arg('show_nodes',false)));
222   $toggle->start();
223
224   $headers=array();
225   $headers['hostname']='string';
226   $headers['state']='string';
227
228   $table = new PlekitTable ('nodes',$headers,'0',array('search_area'=>false,
229                                                     'notes_area'=>false,
230                                                     'pagesize_area'=>false));
231   $table->start();
232   foreach ($nodes as $node) {
233     $table->row_start();
234     $table->cell (l_node_obj($node));
235     $table->cell ($node['boot_state']);
236     $table->row_end();
237   }
238   $table->tfoot_start();
239   $table->row_start();
240   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
241   $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
242   $table->row_end();
243   $table->end();
244   $toggle->end();
245     
246   //////////////////// Users
247   $persons_title = "Users : ";
248   $persons_title .= count($person_ids) . " total";
249   $persons_title .= " / " . count ($pis) . " PIs";
250   $persons_title .= " / " . count ($techs) . " Techs";
251   if ($has_disabled_persons) 
252     $persons_title .= " / " . count($disabled_persons) . " Disabled";
253   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
254     $persons_title = plc_warning_html ($persons_title);
255   $persons_title .= href(l_persons_site($site_id)," (See as users)");
256
257   $toggle=new PlekitToggle ('persons',$persons_title,
258                             array('visible'=>get_arg('show_persons',false)));
259   $toggle->start();
260
261   $headers = array ();
262   $headers["email"]='string';
263   $headers["S"]='int';
264   $headers["PI"]='string';
265   $headers['User']='string';
266   $headers["Tech"]='string';
267   if ($has_disabled_persons) $headers["Disabled"]='string';
268   $table=new PlekitTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
269                                                             'notes_area'=>false,
270                                                             'pagesize_area'=>false));
271   $table->start();
272   if ($persons) foreach ($persons as $person) {
273     $table->row_start();
274     $table->cell(l_person_obj($person));
275     $table->cell(count($person['slice_ids']));
276     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
277     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
278     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
279     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : plc_warning_html("yes"));
280     $table->row_end();
281   }
282   $table->end();
283   $toggle->end();
284
285   //////////////////// Slices
286   $slices_title="Slices : ";
287   $slices_title .= $max_slices . " max";
288   $slices_title .= " / " . count($slice_ids) . " running";
289   $slices_title .= " / $slivers_count slivers";
290   if (count($slice_ids) >= $max_slices) 
291     $slices_title = plc_warning_html($slices_title);
292   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
293   
294   $toggle=new PlekitToggle ('slices',$slices_title,
295                             array('visible'=>get_arg('show_slices',false)));
296   $toggle->start();
297
298   $headers = array ();
299   $headers ['name']='string';
300   $headers ['I'] = 'string';
301   $headers ['N']='int';
302   $headers ['U']='int';
303   $notes=array('I column shows instantiation type',
304                'N column shows number of nodes',
305                'U column shows number of users');
306   $table=new PlekitTable ('slices',$headers,0,array('search_area'=>false,
307                                                     'pagesize_area'=>false,
308                                                     'notes'=>$notes));
309
310   $table->start();
311   if ($slices) foreach ($slices as $slice) {
312       $table->row_start();
313       $table->cell(l_slice_obj($slice));
314       $table->cell(instantiation_label($slice));
315       $table->cell (href(l_nodes_slice($slice['slice_id']),count($slice['node_ids'])));
316       $table->cell (count($slice['person_ids']));
317       $table->row_end();
318     }
319   if ($is_site_pi) {
320     $button=new PlekitFormButton (l_slice_add(),"slice_add","Add slice","post");
321     $table->tfoot_start();
322     $table->row_start();
323     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
324   }
325     
326   $table->end();
327   $toggle->end();
328
329   //////////////////// Addresses
330   $toggle=new PlekitToggle ('addresses',"Addresses",
331                             array('visible'=>get_arg('show_addresses',false)));
332   $toggle->start();
333   if ( ! $addresses) {
334     print "<p class='addresses'>No known address for this site</p>";
335   } else {
336     $details=new PlekitDetails (false);
337     $details->start();
338     $details->th_td("Addresses","");
339     foreach ($addresses as $address) {
340       $details->th_td(plc_vertical_table($address['address_types']),
341                        plc_vertical_table(array($address['line1'],
342                                                 $address['line2'],
343                                                 $address['line3'],
344                                                 $address['city'],
345                                                 $address['state'],
346                                                 $address['postalcode'],
347                                                 $address['country'])));
348     }
349     $details->end();
350   }
351   $toggle->end();
352
353  }
354
355 ////////////////////////////////////////
356 $peers->block_end($peer_id);
357
358 //plekit_linetabs ($tabs,"bottom");
359
360 // Print footer
361 include 'plc_footer.php';
362
363 ?>