checkpoint
[plewww.git] / planetlab / persons / person.php
1 <?php
2
3 // $Id: index.php 11645 2009-01-21 23:09:49Z 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_minitabs.php';
19 require_once 'plc_tables.php';
20 require_once 'plc_details.php';
21
22 // -------------------- 
23 // recognized URL arguments
24 $person_id=intval($_GET['id']);
25 if ( ! $person_id ) { plc_error('Malformed URL - id not set'); return; }
26
27 ////////////////////
28 // Get all columns as we focus on only one entry
29 $persons= $api->GetPersons( array($person_id));
30
31 if (empty($persons)) {
32   drupal_set_message ("Person " . $person_id . " not found");
33  } else {
34   $person=$persons[0];
35   
36   // vars from api
37   $first_name= $person['first_name'];
38   $last_name= $person['last_name'];
39   $title= $person['title'];
40   $url= $person['url'];
41   $phone= $person['phone'];
42   $email= $person['email'];
43   $enabled= $person['enabled'];
44   $peer_id=$person['peer_id'];
45   
46   // arrays from api
47   $role_ids= $person['role_ids'];
48   $roles= $person['roles'];
49   $site_ids= $person['site_ids'];
50   $slice_ids= $person['slice_ids'];
51   $key_ids= $person['key_ids'];
52   
53   // gets more data from API calls
54   $sites= $api->GetSites( $site_ids, array( "site_id", "name", "login_base" ) );
55   $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
56   $keys= $api->GetKeys( $key_ids );
57   
58   drupal_set_title("Details for account " . $first_name . " " . $last_name);
59
60   $plc_hash=plc_peer_get_hash($api);
61
62   $local_peer = plc_peer_block_start ($peer_hash,$peer_id);
63   $is_my_account = plc_my_person_id() == $person_id;
64   $privileges = plc_is_admin () || ( plc_in_site($site_id) && plc_is_pi());
65
66   $tabs=array();
67
68   if ($local_peer && $privileges) {
69     if (plc_is_admin())
70         $tabs['Events'] = array('url'=>l_event("Person","person",$person_id),
71                                 'bubble'=>'Related events',
72                                 'image'=>'/planetlab/icons/event.png',
73                                 'height'=>18);
74     if ($enabled) 
75       $tabs['Disable'] = array ('method'=>'POST',
76                                 'url'=>'/db/persons/person_actions.php',
77                                 'values'=> array ('person_id'=>$person_id,
78                                                   'action'=>'disable'),
79                                 'bubble'=>"Disable $first_name");
80     else 
81       $tabs['Enable'] = array ('method'=>'POST',
82                                 'url'=>'/db/persons/person_actions.php',
83                                 'values'=> array ('person_id'=>$person_id,
84                                                   'action'=>'enable'),
85                                 'bubble'=>"Enable $first_name");
86       $tabs['Delete'] = array ('method'=>'POST',
87                                 'url'=>'/db/persons/person_actions.php',
88                                 'values'=> array ('person_id'=>$person_id,
89                                                   'action'=>'delete'),
90                                'bubble'=>"Delete $first_name",
91                                'confirm'=>"Are you sure to delete $first_name $last_name");
92   }
93
94   if ($privileges || $is_my_account) 
95     $tabs['Update'] = array('url'=>'/db/persons/update.php',
96                             'values'=>array('id'=>$person_id),
97                             'bubble'=>"Update $first_name");
98
99   $tabs['Persons'] = array ('url'=>l_persons());
100
101   plc_tabs($tabs);
102     
103   plc_details_start();
104   plc_details_line("First Name",$first_name);
105   plc_details_line("Last Name",$last_name);
106   plc_details_line("Title",$title);
107   plc_details_line("Email",href("mailto:$email",$email));
108   plc_details_line("Phone",$phone);
109   plc_details_line("URL",$url);
110   plc_details_end();
111
112           
113   echo "<hr />\n";
114                 
115   // keys
116   $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
117   plc_table_title('Keys');
118   if ( empty( $key_ids ) ) {
119     echo "<span class='plc-warning'> This user has no known key</span>";
120   } else {
121     echo "<p><table border=0 width=450>\n";
122     echo "<thead><tr><th>Type</th><th>Key</th>";
123     if ( $can_manage_keys )
124       echo "<th>Remove</th>";
125     echo "</tr></thead><tbody>\n";
126     
127     foreach( $keys as $key ) {
128       $key_type= $key['key_type'];
129       $key_id= $key['key_id'];
130       $key_text= wordwrap( $key['key'], 70, "<br />\n", 1 );
131       echo "<tr><td>$key_type</td><td>$key_text";
132       
133       if ( $can_manage_keys ) 
134         echo "</td><td><input type=checkbox name='rem_key[]' value='$key_id'>";
135       
136       echo "</td></tr>\n";
137     }
138     
139     echo "</tbody></table>\n";
140     if ($can_manage_keys)
141       echo "<p><input type=submit name='Remove_keys' value='Remove keys'><br />\n";
142   }
143                 
144   if( $can_manage_keys ){
145     echo "<br /> Upload new key: <input type='file' name='key' size=30>\n
146         <input type='submit' name='Upload' value='Upload'>\n
147         <br /><hr />\n";
148   }
149
150   // sites
151   plc_table_title('Sites');
152   
153   // sites
154   if (empty( $sites ) ) {
155     plc_warning('This user is not affiliated with a site !!');
156     } else {
157     $columns=array();
158     $columns['Name']="string";
159     $columns['Login_base']="string";
160     $columns['Remove']="string";
161     $table_options = array('notes_area'=>false,'search_area'=>false);
162     plc_table_start ("person_slices",$columns,1,$table_options);
163     foreach( $sites as $site ) {
164       $site_name= $site['name'];
165       $site_id= $site['site_id'];
166       $login_base=$site['login_base'];
167       plc_table_row_start();
168       plc_table_cell (l_site_t($site_id,$site_name));
169       plc_table_cell ($login_base);
170       plc_table_cell ("<input id=" . $login_base . " type=checkbox name='rem_site[]' value=" . $site_id . ">");
171       plc_table_row_end ();
172     }
173     plc_table_end($table_options);
174   }
175
176   echo "<input type=submit name='Remove_Sites' value='Remove Sites'>\n";
177   
178         
179   // diplay site select list to add another site for user
180   if ($local_peer && plc_is_admin()) {
181     // get site info
182     $all_sites= $api->GetSites( NULL, array( "site_id", "name" ) );
183     
184     if( $sites )
185       $person_site= arr_diff( $all_sites, $sites );
186     else
187       $person_site= $all_sites;
188     
189     //    sort_sites( $person_site );
190     
191     echo "<p>Select a site to add this user to: ";
192     echo "<select name='site_add' onChange='submit()'>\n<option value=''>Choose a site to add:</option>\n";
193     
194     foreach( $person_site as $site ) {
195       echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
196       
197     }
198     
199     echo "</select>";
200     
201   }
202   echo "<hr />\n";
203   
204   // roles
205   echo "<h3>Roles</h3>\n";
206   echo "<p><table>\n";
207   echo "<thead><tr><th>Role</th>";
208   if( plc_is_admin())
209     echo "<th>Remove</th>";
210   echo "</tr></thead><tbody>\n";
211   
212   // construct role array
213   for( $n=0; $n<count($roles); $n++ ) {
214     $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
215   }
216     
217   $button_shown=0;
218   if ( !empty ($roles) ) {
219     foreach( $proles as $role ) {
220       $role_name= $role['name'];
221       $role_id= $role['role_id'];
222       
223       echo "<tr><td>$role_name";
224               
225
226       if( plc_is_admin()) {
227         echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
228         if ( ! $button_shown ) {
229           $rowspan=count($roles);
230           echo "</td><td rowspan=$rowspan valign=center><input type=submit name='Remove_Roles' value='Remove Roles'></td></tr>\n";
231           $button_shown=1;
232         }
233       }
234       
235       echo "</td></tr>\n";
236     }
237   } else {
238     echo "<span class='plc-warning'> This user has no known role !!</span>";
239   }
240   echo "</tbody></table>\n";
241           
242   // if admin show roles to add
243   if( plc_is_admin()) {
244     $all_roles= $api->GetRoles();
245     $addable_roles= arr_diff( $all_roles, $proles );
246     ##when the proles array is empty strangely the method arr_diff($all_roles, $proles )
247     ##return an empty array and the scrolling roles list is not displayed in this case
248     ##assign to addablerole all the roles
249     if (count($proles)==0)
250       $addable_roles=$all_roles;
251     
252     if( !empty( $addable_roles ) ) {
253       echo "<p>Add role: <select name='add_role' onChange='submit()'>\n<option value=''>Choose a Role to add:</option>\n";
254       
255       foreach( $addable_roles as $arole ) {
256         echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
257         
258       }
259       
260       echo "</select>\n";
261       
262     }
263   }
264   
265   echo "<hr />\n";
266   
267   // slices
268   plc_table_title('Slices');
269
270   if( empty( $slices ) ) {
271     drupal_set_message ("User has no slice");
272   } else {
273     $columns=array('Slice name'=>'string');
274     $table_options=array();
275     plc_table_start("person_slices",$columns,1,$table_options);
276
277     foreach( $slices as $slice ) {
278       $slice_name= $slice['name'];
279       $slice_id= $slice['slice_id'];
280       plc_table_row_start();
281       plc_table_cell(l_slice_t($slice_id,$slice_name));
282       plc_table_row_end();
283     }
284     plc_table_end($table_options);
285   }
286
287   plc_peer_block_end();
288   
289  }
290
291 // Print footer
292 include 'plc_footer.php';
293
294
295 ?>