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