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