more inline updates & cleaned up tags
[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_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
24 // -------------------- 
25 // recognized URL arguments
26 $person_id=intval($_GET['id']);
27 if ( ! $person_id ) { 
28   plc_error('Malformed URL - id not set'); 
29   return;
30  }
31
32 ////////////////////
33 // Get all columns as we focus on only one entry
34 $persons= $api->GetPersons( array($person_id));
35
36 if (empty($persons)) {
37   drupal_set_message ("Person " . $person_id . " not found");
38   return;
39  }
40 $person=$persons[0];
41   
42 // vars from api
43 $enabled= $person['enabled'];
44 $first_name= $person['first_name'];
45 $last_name= $person['last_name'];
46 $email= $person['email'];
47 $url= $person['url'];
48 $phone= $person['phone'];
49 $title= $person['title'];
50 $bio= $person['bio'];
51 $peer_id=$person['peer_id'];
52
53 // arrays from api
54 $role_ids= $person['role_ids'];
55 $roles= $person['roles'];
56 $site_ids= $person['site_ids'];
57 $slice_ids= $person['slice_ids'];
58 $key_ids= $person['key_ids'];
59
60 // gets more data from API calls
61 $site_columns=array( "site_id", "name", "login_base" );
62 $sites= $api->GetSites( $site_ids, $site_columns);
63 $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
64 $keys= $api->GetKeys( $key_ids );
65
66 drupal_set_title("Details for account " . $first_name . " " . $last_name);
67 $local_peer = ! $peer_id;
68
69 $peers = new Peers ($api);
70
71 $is_my_account = plc_my_person_id() == $person_id;
72 $privileges = plc_is_admin () || ( plc_in_site($site_id) && plc_is_pi());
73
74 $tabs=array();
75
76 // enable / disable
77 if ($local_peer && $privileges) 
78   if ($enabled) 
79     $tabs['Disable'] = array ('method'=>'POST',
80                               'url'=>l_actions(),
81                               'values'=> array ('person_id'=>$person_id,
82                                                 'action'=>'disable-person'),
83                               'bubble'=>"Disable $first_name $last_name",
84                               'confirm'=>"Are you sure you want to disable $first_name $last_name");
85   else 
86     $tabs['Enable'] = array ('method'=>'POST',
87                              'url'=>l_actions(),
88                              'values'=> array ('person_id'=>$person_id,
89                                                'action'=>'enable-person'),
90                              'bubble'=>"Enable $first_name $last_name",
91                              'confirm'=>"Are you sure you want to enable $first_name $last_name");
92
93 // become
94 if (plc_is_admin() && ! $is_my_account) 
95   $tabs['Become'] = array('method'=>'POST',
96                           'url'=>l_actions(),
97                           'values'=>array('action'=>'become-person',
98                                           'person_id'=>$person_id),
99                           'bubble'=>"Become $first_name $last_name",
100                           'confirm'=>"Are you sure you want to su $first_name $last_name");
101     
102 // delete
103 if ($local_peer && $privileges) 
104   $tabs['Delete'] = array ('method'=>'POST',
105                            'url'=>l_actions(),
106                            'values'=> array ('person_id'=>$person_id,
107                                              'action'=>'delete-person'),
108                            'bubble'=>"Delete $first_name $last_name",
109                            'confirm'=>"Are you sure to delete $first_name $last_name");
110 // events for that person
111 if ( $privileges) 
112   $tabs['Events'] = array('url'=>l_events(),
113                           'values'=>array('type'=>'Person','person'=>$person_id),
114                           'bubble'=>"Events about $first_name $last_name",
115                           'image'=>'/planetlab/icons/event.png','height'=>18);
116
117 // Back button
118 $tabs['All Users'] = array ('url'=>l_persons(),
119                             'bubble'=>'Back to the Users page');
120
121 plc_tabs($tabs);
122     
123 $peers->block_start ($peer_id);
124
125 if ($local_peer && $privileges && ! $enabled ) 
126   drupal_set_message ("$first_name $last_name is not enabled yet, you can enable her/him with the 'Enable' button below");
127
128 $enabled_label="Yes";
129 if ( ! $enabled ) $enabled_label = plc_warning_html("Disabled");
130
131 $can_update = $is_my_account || plc_is_admin();
132 $details = new PlcDetails($can_update);
133
134 $details->form_start(l_actions(),array("action"=>"update-person",
135                                        "person_id"=>$person_id));
136 $details->start();
137 $details->line("Enabled",$enabled_label);
138 $details->line("Peer",$peers->peer_link($peer_id));
139 $details->space();
140 // xxx this needs some more work on the PlcDetails class
141 $details->set_field_width(5);
142 $details->line("Title",$title,"title");
143 $details->set_field_width("");
144 $details->line("First Name",$first_name,"first_name");
145 $details->line("Last Name",$last_name,"last_name");
146 $details->line(href("mailto:$email","Email"),$email,"email");
147 $details->line("Phone",$phone,"phone");
148 $save_w=$details->set_field_width(40);
149 $details->line("URL",$url,"url");
150 $details->set_field_height(4);
151 $details->set_input_type("textarea");
152 $details->line("Bio",$bio,"bio");
153 $details->set_input_type("text");
154 $details->set_field_width($save_w);
155
156 // xxx need to check that this is working
157 if ($can_update) {
158   $save_i=$details->set_input_type("password");
159   $details->line("Password","","password1");
160   $details->line("Repeat","","password2");
161   $details->set_input_type($save_i);
162  }
163 // xxx need fields to reset password ?
164 $details->line("",$details->submit_html("submit","Update Account"));
165
166 $details->end();
167 $details->form_end();
168
169 //////////////////// slices
170 plc_section('Slices');
171
172 if( ! $slices) {
173   plc_warning ("User has no slice");
174  } else {
175   $headers=array('Slice name'=>'string');
176   $reasonable_page=5;
177   $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
178   if (count ($slices) <= $reasonable_page) {
179     $table_options['search_area']=false;
180     $table_options['pagesize_area']=false;
181   }
182   $table=new PlcTable ("person_slices",$headers,1,$table_options);
183   $table->start();
184
185   foreach( $slices as $slice ) {
186     $slice_name= $slice['name'];
187     $slice_id= $slice['slice_id'];
188     $table->row_start();
189     $table->cell(l_slice_t($slice_id,$slice_name));
190     $table->row_end();
191   }
192   $table->end();
193  }
194
195 // we don't set 'action', but use the submit button name instead
196 $form=new PlcForm(l_actions(), array("person_id"=>$person_id));
197 $form->start();
198
199 //////////////////// keys
200 plc_section ("Keys");
201                 
202 $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
203 if ( empty( $key_ids ) ) {
204   plc_warning("This user has no known key");
205  } 
206
207 $headers=array("Type"=>"string",
208                "Key"=>"string");
209 if ($can_manage_keys) $headers[plc_delete_icon()]="none";
210 // table overall options
211 $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
212 $table=new PlcTable("person_keys",$headers,"1",$table_options);
213 $table->start();
214     
215 if ($keys) foreach ($keys as $key) {
216   $key_id=$key['key_id'];
217   $table->row_start();
218   $table->cell ($key['key_type']);
219   $table->cell(wordwrap( $key['key'], 60, "<br />\n", 1 ));
220   if ($can_manage_keys) 
221     $table->cell ($form->checkbox_html('key_ids[]',$key_id));
222   $table->row_end();
223 }
224 // the footer area is used for displaying key-management buttons
225 // add the 'remove keys' button and key upload areas as the table footer
226 if ($can_manage_keys) {
227   $table->tfoot_start();
228   // no need to remove if there's no key
229   if ($keys) {
230     $table->row_start();
231     $table->cell($form->submit_html ("delete-keys","Remove keys"),
232                  $table->columns(),"right");
233     $table->row_end();
234   }
235   $table->row_start();
236   $table->cell($form->label_html("key","Upload new key")
237                . $form->file_html("key",60)
238                . $form->submit_html("upload-key","Upload key"),
239                $table->columns(),"right");
240   $table->row_end();
241 }
242
243 $table->end();
244
245 //////////////////// sites
246 plc_section('Sites');
247   
248 if (empty( $sites ) ) {
249   plc_warning('This user is not affiliated with a site !!');
250  } 
251 $can_manage_sites = $local_peer && plc_is_admin() || $is_my_account;
252 $headers=array();
253 $headers['Login_base']="string";
254 $headers['Name']="string";
255 if ($can_manage_sites) $headers[plc_delete_icon()]="none";
256 $table_options = array('notes_area'=>false,'search_area'=>false, 'pagesize_area'=>false);
257 $table=new PlcTable ("person_sites",$headers,0,$table_options);
258 $table->start();
259 foreach( $sites as $site ) {
260   $site_name= $site['name'];
261   $site_id= $site['site_id'];
262   $login_base=$site['login_base'];
263   $table->row_start();
264   $table->cell ($login_base);
265   $table->cell (l_site_t($site_id,$site_name));
266   if ($can_manage_sites)
267     $table->cell ($form->checkbox_html('site_ids[]',$site_id));
268   $table->row_end ();
269 }
270 if ($can_manage_sites) {
271   $table->tfoot_start();
272
273   if ($sites) {
274     $table->row_start();
275     $table->cell($form->submit_html("remove-person-from-sites","Remove Sites"),
276                  $table->columns(),"right");
277     $table->row_end();
278   }
279
280   $table->row_start();
281
282   // get list of local sites that the person is not in
283   function get_site_id ($site) { return $site['site_id'];}
284   $person_site_ids=array_map("get_site_id",$sites);
285   $relevant_sites= $api->GetSites( array("peer_id"=>NULL,"~site_id"=>$person_site_ids), $site_columns);
286   // xxx cannot use onchange=submit() - would need to somehow pass action name 
287   function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); }
288   $selectors = array_map ("site_selector",$relevant_sites);
289   $table->cell ($form->select_html("site_id",$selectors,"Choose a site to add").
290                 $form->submit_html("add-person-to-site","Add in site"),
291                 $table->columns(),"right");
292   $table->row_end();
293  }
294 $table->end();
295
296 //////////////////// roles
297 plc_section("Roles");
298 if (! $roles) plc_warning ("This user has no role !");
299
300 $can_manage_roles= ($local_peer && plc_is_admin());
301 $table_options=array("search_area"=>false,"notes_area"=>false);
302
303 $headers=array("Role"=>"string");
304 if ($can_manage_roles) $headers [plc_delete_icon()]="none";
305
306 $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
307 $table=new PlcTable("person_roles",$headers,0,$table_options);  
308 $table->start();
309   
310 // construct array of role objs
311 $role_objs=array();
312 for ($n=0; $n<count($roles); $n++) {
313   $role_objs[]= array('role_id'=>$role_ids[$n], 'name'=>$roles[$n]);
314  }
315
316 if ($role_objs) foreach ($role_objs as $role_obj) {
317   $table->row_start();
318   $table->cell($role_obj['name']);
319   if ($can_manage_roles) $table->cell ($form->checkbox_html('role_ids[]',$role_obj['role_id']));
320   $table->row_end();
321  }
322
323 // footers : the remove and add buttons
324 if ($can_manage_roles) {
325   $table->tfoot_start();
326   if ($roles) {
327     $table->row_start();
328     $table->cell($form->submit_html("remove-roles-from-person","Remove Roles"),
329                  $table->columns(),"right");
330     $table->row_end();
331   }
332
333   $table->row_start();
334   $selectors=$form->role_selectors_excluding($api,$role_ids);
335   $add_role_left_area=$form->select_html("role_id",$selectors,"Choose role");
336   // add a role : the button
337   $add_role_right_area=$form->submit_html("add-role-to-person","Add role");
338   $table->cell ($add_role_left_area . $add_role_right_area,
339                 $table->columns(),"right");
340   $table->row_end();
341  }
342 $table->end();
343
344 //////////////////////////////
345 $form->end();
346 $peers->block_end($peer_id);
347   
348 // Print footer
349 include 'plc_footer.php';
350
351
352 ?>