togglable sections everywhere
[plewww.git] / planetlab / sites / site.php
1 <?php
2
3   // $Id: index.php 11750 2009-01-29 10:11:53Z thierry $
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_minitabs.php';
20 require_once 'plc_tables.php';
21 require_once 'plc_details.php';
22 require_once 'plc_forms.php';
23 require_once 'plc_toggles.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 // extra privileges to admins, and (pi||tech) on this site
53 $privileges = plc_is_admin () || ( plc_in_site($site_id) && ( plc_is_pi() || plc_is_tech()));
54   
55 // get peer 
56 $peer_id= $site['peer_id'];
57 $peers = new Peers ($api);
58
59 $address_ids= $site['address_ids'];
60 $pcu_ids= $site['pcu_ids'];
61 $node_ids= $site['node_ids'];
62 $person_ids= $site['person_ids'];
63 $slice_ids= $site['slice_ids'];
64
65 $api->begin();
66 // gets address info
67 $api->GetAddresses( $address_ids );
68
69 // gets pcu info
70 // GetPCUs is not accessible to the 'user' role
71 //$api->GetPCUs( $pcu_ids );
72
73 // gets node info
74 $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state" ) );
75
76 // gets person info
77 $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" ) );
78
79 $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation" ) );
80
81 //list( $addresses, $pcus, $nodes, $persons, $slices )= $api->commit();
82 list( $addresses, $nodes, $persons, $slices )= $api->commit();
83   
84 $techs = array();
85 $pis = array();
86 $disabled_persons = array();
87 foreach( $persons as $person ) {
88   $role_ids= $person['role_ids'];
89
90   if ( in_array( '20', $role_ids ))     $pis[] = $person;
91   if ( in_array( '40', $role_ids ))     $techs[] = $person;
92   if ( ! $person['enabled'] )           $disabled_persons[] = $person;
93   
94 }
95
96 $has_disabled_persons = count ($disabled_persons) !=0;
97
98 drupal_set_title("Details for site " . $sitename);
99 $local_peer = ! $peer_id;
100   
101 // extra privileges to admins, and pi on this site
102 $privileges = plc_is_admin () || ( plc_in_site($site_id) && plc_is_pi());
103   
104 $tabs=array();
105
106 $tabs []= tab_sites_local();
107
108 // available actions
109 if ( $local_peer  && $privileges ) {
110   
111   $tabs['Expire slices'] = array('url'=>l_actions(),
112                                  'method'=>'POST',
113                                  'values'=>array('site_id'=>$site_id,
114                                                  'action'=>'expire-all-slices-in-site'),
115                                  'bubble'=>"Expire all slices and prevent creation of new slices",
116                                  'confirm'=>"Suspend all slices in $login_base");
117   if (plc_is_admin())
118     $tabs['Delete']=array('url'=>l_actions(),
119                           'method'=>'POST',
120                           'values'=>array('site_id'=>$site_id,
121                                           'action'=>'delete-site'),
122                           'bubble'=>"Delete site $sitename",
123                           'confirm'=>"Are you sure you want to delete site $login_base");
124   $tabs["Events"]=array_merge (tablook_event(),
125                                array('url'=>l_event("Site","site",$site_id),
126                                      'bubble'=>"Events for site $sitename"));
127   $tabs["Comon"]=array_merge(tablook_comon(),
128                              array('url'=>l_comon("site_id",$site_id),
129                                    'bubble'=>"Comon page for $sitename"));
130
131   if (plc_is_admin()) 
132     $tabs['Pending'] = array ('url'=>l_sites_pending(),
133                               'bubble'=>'Review pending join requests');
134  }
135
136 plc_tabs($tabs);
137
138 // show gray background on foreign objects : start a <div> with proper class
139 $peers->block_start ($peer_id);
140
141 if ( ! $enabled ) 
142   plc_warning ("This site is not enabled - Please visit " . 
143                href (l_sites_pending(),"this page") . 
144                " to review pending applications.");
145
146 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
147 $details = new PlcDetails($can_update);
148
149 if ( ! $site['is_public']) 
150   plc_warning("This site is not public!");
151
152 $details->start();
153
154 $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$site_id));
155 $details->th_td("Full name",$sitename,'name',array('width'=>50));
156 $details->th_td("Abbreviated name",$abbreviated_name,'abbreviated_name',array('width'=>15));
157 $details->th_td("URL",$site_url,'url',array('width'=>40));
158 $details->th_td("Latitude",$site_lat,'latitude');
159 $details->th_td("Longitude",$site_long,'longitude');
160
161 // modifiable by admins only
162 if (plc_is_admin()) 
163   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
164 else
165   $details->th_td("Login base",$login_base);
166 if (plc_is_admin())
167   $details->th_td("Max slices",$max_slices,'max_slices');
168 else
169   $details->th_td("Max slices",$max_slices);
170 $details->tr_submit("submit","Update Site");
171 $details->form_end();
172
173 if ( ! $local_peer) {
174   $details->space();
175   $details->th_td("Peer",$peers->peer_link($peer_id));
176  }
177 $details->end();
178
179 //////////////////// mode details - for local object
180 if ( $local_peer ) {
181
182   //////////////////// nodes
183   // xxx missing : would need to add columns on attached PCU name and port if avail
184   $nb_boot = 0;
185   if ($nodes) foreach ($nodes as $node) if ($node['boot_state'] == 'boot') $nb_boot ++;
186
187   $nodes_title = "# Nodes : ";
188   $nodes_title .= count($nodes) . " total";
189   $nodes_title .= " / " . $nb_boot . " boot";
190   if ($nb_boot < 2 ) 
191     $nodes_title = plc_warning_html ($nodes_title);
192   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
193
194   $toggle=new PlcToggle ('nodes',$nodes_title,array('trigger-tagname'=>'h2'));
195   $toggle->start();
196
197   $headers=array();
198   $headers['hostname']='string';
199   $headers['state']='string';
200
201   $table = new PlcTable ('nodes',$headers,'0',array('search_area'=>false,
202                                                     'notes_area'=>false,
203                                                     'pagesize_area'=>false));
204   $table->start();
205   foreach ($nodes as $node) {
206     $table->row_start();
207     $table->cell (l_node_obj($node));
208     $table->cell ($node['boot_state']);
209     $table->row_end();
210   }
211   $table->tfoot_start();
212   $table->row_start();
213   $button=new PlcFormButton (l_node_add(),"node_add","Add node","POST");
214   $table->cell($button->html(),$table->columns(),"right");
215   $table->row_end();
216   $table->end();
217   $toggle->end();
218     
219   //////////////////// Users
220   $persons_title = "# Users : ";
221   $persons_title .= count($person_ids) . " total";
222   $persons_title .= " / " . count ($pis) . " PIs";
223   $persons_title .= " / " . count ($techs) . " Techs";
224   if ($has_disabled_persons) 
225     $persons_title .= " / " . ($disabled_persons) . " Disabled";
226   if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
227     $persons_title = plc_warning_html ($persons_title);
228   $persons_title .= href(l_persons_site($site_id)," (See as users)");
229
230   $toggle=new PlcToggle ('persons',$persons_title,array('trigger-tagname'=>'h2'));
231   $toggle->start();
232
233   $headers = array ();
234   $headers["email"]='string';
235   $headers["PI"]='string';
236   $headers['User']='string';
237   $headers["Tech"]='string';
238   if ($has_disabled_persons) $headers["Disabled"]='string';
239   $table=new PlcTable('persons',$headers,'1r-3r-0',array('search_area'=>false,
240                                                          'notes_area'=>false,
241                                                          'pagesize_area'=>false));
242   $table->start();
243   foreach ($persons as $person) {
244     $table->row_start();
245     $table->cell(l_person_obj($person));
246     $table->cell( in_array ('20',$person['role_ids']) ? "yes" : "no");
247     $table->cell( in_array ('30',$person['role_ids']) ? "yes" : "no");
248     $table->cell( in_array ('40',$person['role_ids']) ? "yes" : "no");
249     if ($has_disabled_persons) $table->cell( $person['enabled'] ? "no" : "yes");
250     $table->row_end();
251   }
252   $table->end();
253   $toggle->end();
254
255   //////////////////// Slices
256   // xxx to review after slices gets reworked
257   $toggle=new PlcToggle ('slices',"Slices",array('trigger-tagname'=>'h2'));
258   $toggle->start();
259   $details=new PlcDetails (false);
260   $details->start();
261   // summary on slices
262   $slice_label = count($slice_ids) . " running / " . $max_slices . " max";
263   if (count($slice_ids) >= $max_slices) 
264     $slice_label = plc_warning_html ($slice_label);
265   $details->th_td("# Slices", href(l_slices_site($site_id),$slice_label));
266   if ($slices) foreach ($slices as $slice)
267      $details->th_td($slice['instantiation'],l_slice_obj($slice));
268   $button=new PlcFormButton (l_slice_add(),"slice_add","Add slice","POST");
269   $details->tr($button->html(),"right");
270   $details->end();
271   $toggle->end();
272
273   // Addresses
274   $toggle=new PlcToggle ('addresses',"Addresses",array('trigger-tagname'=>'h2',
275                                                        'start-visible'=>false));
276   $toggle->start();
277   if ( ! $addresses) {
278     print "No known address for this site";
279   } else {
280     $details=new PlcDetails (false);
281     $details->start();
282     $details->th_td("Addresses","");
283     foreach ($addresses as $address) {
284       $details->th_td(plc_vertical_table($address['address_types']),
285                        plc_vertical_table(array($address['line1'],
286                                                 $address['line2'],
287                                                 $address['line3'],
288                                                 $address['city'],
289                                                 $address['state'],
290                                                 $address['postalcode'],
291                                                 $address['country'])));
292     }
293     $details->end();
294   }
295   $toggle->end();
296
297  }
298
299 ////////////////////////////////////////
300 $peers->block_end($peer_id);
301
302 //plc_tabs ($tabs,"bottom");
303
304 // Print footer
305 include 'plc_footer.php';
306
307 ?>