display person's hrn if set
[plewww.git] / planetlab / persons / person.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 // Print header
11 require_once 'plc_drupal.php';
12 include 'plc_header.php'; 
13
14 // Common functions
15 require_once 'plc_functions.php';
16 require_once 'plc_peers.php';
17 require_once 'linetabs.php';
18 require_once 'table.php';
19 require_once 'details.php';
20 require_once 'form.php';
21 require_once 'toggle.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 // we need to mention them explicitly because we want hrn that is a tag..
34 $columns=array('enabled','first_name','last_name','email','url','phone','title','bio','peer_id',
35                'role_ids','roles','site_ids','slice_ids','key_ids','hrn');
36 $persons= $api->GetPersons( array($person_id), $columns);
37
38 if (empty($persons)) {
39   drupal_set_message ("Person " . $person_id . " not found");
40   return;
41  }
42 $person=$persons[0];
43   
44 // vars from api
45 $enabled= $person['enabled'];
46 $first_name= $person['first_name'];
47 $last_name= $person['last_name'];
48 $email= $person['email'];
49 $url= $person['url'];
50 $phone= $person['phone'];
51 $title= $person['title'];
52 $bio= $person['bio'];
53 $peer_id=$person['peer_id'];
54
55 // arrays from api
56 $role_ids= $person['role_ids'];
57 $roles= $person['roles'];
58 $site_ids= $person['site_ids'];
59 $slice_ids= $person['slice_ids'];
60 $key_ids= $person['key_ids'];
61 $hrn=$person['hrn'];
62
63 // gets more data from API calls
64 $site_columns=array( "site_id", "name", "login_base" );
65 $sites= $api->GetSites( $site_ids, $site_columns);
66 $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
67 $keys= $api->GetKeys( $key_ids );
68
69 drupal_set_title("Details for account " . $first_name . " " . $last_name);
70 $local_peer = ! $peer_id;
71
72 $peers = new Peers ($api);
73
74 if (count($site_ids))
75     $site_id = $site_ids[0];
76 $is_my_account = plc_my_person_id() == $person_id;
77 $privileges = plc_is_admin () || ( plc_in_site($site_ids[0]) && plc_is_pi());
78
79 $tabs=array();
80
81 // enable / disable
82 // become
83 if (plc_is_admin() && ! $is_my_account && $local_peer && $enabled) 
84   $tabs['Become'] = array('method'=>'POST',
85                           'url'=>l_actions(),
86                           'values'=>array('action'=>'become-person',
87                                           'person_id'=>$person_id),
88                           'bubble'=>"Become $first_name $last_name",
89                           'confirm'=>"Are you sure you want to become $first_name $last_name");
90     
91 if ($local_peer && $privileges && ! $is_my_account) 
92   if ($enabled) 
93     $tabs['Disable'] = array ('method'=>'POST',
94                               'url'=>l_actions(),
95                               'values'=> array ('person_id'=>$person_id,
96                                                 'action'=>'disable-person'),
97                               'bubble'=>"Disable $first_name $last_name",
98                               'confirm'=>"Are you sure you want to disable $first_name $last_name");
99   else 
100     $tabs['Enable'] = array ('method'=>'POST',
101                              'url'=>l_actions(),
102                              'values'=> array ('person_id'=>$person_id,
103                                                'action'=>'enable-person'),
104                              'bubble'=>"Enable $first_name $last_name",
105                              'confirm'=>"Are you sure you want to enable $first_name $last_name");
106
107 // delete
108 if ($local_peer && $privileges && $local_peer && ! $is_my_account) 
109   $tabs['Delete'] = array ('method'=>'POST',
110                            'url'=>l_actions(),
111                            'values'=> array ('person_id'=>$person_id,
112                                              'action'=>'delete-person'),
113                            'bubble'=>"Delete $first_name $last_name",
114                            'confirm'=>"Are you sure to delete $first_name $last_name");
115 // events for that person
116 if ( $privileges) 
117   $tabs['Events'] = array('url'=>l_events(),
118                           'values'=>array('type'=>'Person','person'=>$person_id),
119                           'bubble'=>"Events about $first_name $last_name",
120                           'image'=>'/planetlab/icons/event.png','height'=>18);
121
122 plekit_linetabs($tabs);
123     
124 $peers->block_start ($peer_id);
125
126 if ($local_peer && $privileges && ! $enabled ) 
127   drupal_set_message ("$first_name $last_name is not enabled yet, you can enable her/him with the 'Enable' button below");
128
129 $enabled_label="Yes";
130 if ( ! $enabled ) $enabled_label = plc_warning_html("Disabled");
131
132 $can_update = (plc_is_admin() && $local_peer) || $is_my_account;
133
134 $toggle = new PlekitToggle ('person',"Details",
135                             array('bubble'=>'Display and modify details for that account',
136                                   'visible'=>get_arg('show_details')));
137 $toggle->start();
138
139 $details = new PlekitDetails($can_update);
140
141 $details->form_start(l_actions(),array("action"=>"update-person",
142                                        "person_id"=>$person_id));
143 $details->start();
144
145
146 $details->th_td("Title",$title,"title",array('width'=>10));
147 $details->th_td("First Name",$first_name,"first_name");
148 $details->th_td("Last Name",$last_name,"last_name");
149 $details->th_td(href("mailto:$email","Email"),$email,"email",array("width"=>30));
150 if ($hrn) $details->th_td("SFA hrn",$hrn);
151 else $details->tr("SFA hrn not set","center");
152 $details->th_td("Phone",$phone,"phone");
153 $details->th_td("URL",$url,"url",array('width'=>40));
154 $details->th_td("Bio",$bio,"bio",array('input_type'=>'textarea','height'=>4));
155
156 // xxx need to check that this is working
157 if ($can_update) {
158   $details->th_td("Password","","password1",array('input_type'=>'password'));
159   $details->th_td("Repeat","","password2",array('input_type'=>'password'));
160   $details->tr_submit("submit","Update Account");
161   $details->space();
162  }
163
164 $details->th_td("Enabled",$enabled_label);
165 if ( ! $local_peer ) {
166   $details->th_td("Peer",$peers->peer_link($peer_id));
167   $details->space();
168  }
169
170 $details->end();
171 $details->form_end();
172 $toggle->end();
173
174 //////////////////// slices
175 if ($local_peer) {
176   $slices_title=count_english_warning($slices,'slice');
177   $toggle=new PlekitToggle ('slices',$slices_title,
178                             array('visible'=>get_arg('show_slices')));
179   $toggle->start();
180   
181   if( ! $slices) {
182     plc_warning ("User has no slice");
183   } else {
184     $headers=array('Slice name'=>'string');
185     $reasonable_page=5;
186     $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
187     if (count ($slices) <= $reasonable_page) {
188       $table_options['search_area']=false;
189       $table_options['pagesize_area']=false;
190     }
191     $table=new PlekitTable ("person_slices",$headers,1,$table_options);
192     $table->start();
193     
194     foreach( $slices as $slice ) {
195       $slice_name= $slice['name'];
196       $slice_id= $slice['slice_id'];
197       $table->row_start();
198       $table->cell(l_slice_t($slice_id,$slice_name));
199       $table->row_end();
200     }
201     $table->end();
202   }
203   $toggle->end();
204  }
205
206 ////////////////////////////////////////
207 // we don't set 'action', but use the submit button name instead
208 $form=new PlekitForm(l_actions(), array("person_id"=>$person_id));
209 $form->start();
210
211 //////////////////// keys
212 if ($local_peer) {
213   $keys_title = count_english_warning($keys,'key');
214   $toggle=new PlekitToggle ('keys',$keys_title,array('visible'=>get_arg('show_keys')));
215   $toggle->start();
216                 
217   $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
218   if ( empty( $key_ids ) ) {
219     plc_warning("This user has no known key");
220   } 
221
222   $headers=array("Type"=>"string",
223                  "Key"=>"string");
224   if ($can_manage_keys) $headers[plc_delete_icon()]="none";
225   // table overall options
226   $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
227   $table=new PlekitTable("person_keys",$headers,"1",$table_options);
228   $table->start();
229     
230   if ($keys) foreach ($keys as $key) {
231       $key_id=$key['key_id'];
232       $table->row_start();
233       $table->cell ($key['key_type']);
234       $table->cell(wordwrap( $key['key'], 60, "<br />\n", 1 ));
235       if ($can_manage_keys) 
236         $table->cell ($form->checkbox_html('key_ids[]',$key_id));
237       $table->row_end();
238     }
239   // the footer area is used for displaying key-management buttons
240   // add the 'remove keys' button and key upload areas as the table footer
241   if ($can_manage_keys) {
242     $table->tfoot_start();
243     // no need to remove if there's no key
244     if ($keys) {
245       $table->row_start();
246       $table->cell($form->submit_html ("delete-keys","Remove keys"),
247                    array('hfill'=>true,'align'=>'right'));
248       $table->row_end();
249     }
250     $table->row_start();
251     $table->cell($form->label_html("key","Upload new key")
252                  . $form->file_html("key","upload",array('size'=>60))
253                  . $form->submit_html("upload-key","Upload key"),
254                  array('hfill'=>true,'align'=>'right'));
255     $table->row_end();
256   }
257
258   $table->end();
259   $toggle->end();
260  }
261
262 //////////////////// sites
263 if ($local_peer) {
264   $sites_title = count_english_warning($sites,'site');
265   $toggle=new PlekitToggle('sites',$sites_title,
266                            array('visible'=>get_arg('show_sites')));
267   $toggle->start();
268   
269   if (empty( $sites ) ) {
270     plc_warning('This user is not affiliated with a site !!');
271   } 
272   $can_manage_sites = $local_peer && plc_is_admin() || $is_my_account;
273   $headers=array();
274   $headers['Login_base']="string";
275   $headers['Name']="string";
276   if ($can_manage_sites) $headers[plc_delete_icon()]="none";
277   $table_options = array('notes_area'=>false,'search_area'=>false, 'pagesize_area'=>false);
278   $table=new PlekitTable ("person_sites",$headers,0,$table_options);
279   $table->start();
280   foreach( $sites as $site ) {
281     $site_name= $site['name'];
282     $site_id= $site['site_id'];
283     $login_base=$site['login_base'];
284     $table->row_start();
285     $table->cell ($login_base);
286     $table->cell (l_site_t($site_id,$site_name));
287     if ($can_manage_sites)
288       $table->cell ($form->checkbox_html('site_ids[]',$site_id));
289     $table->row_end ();
290   }
291   if ($can_manage_sites) {
292     $table->tfoot_start();
293
294     if ($sites) {
295       $table->row_start();
296       $table->cell($form->submit_html("remove-person-from-sites","Remove Sites"),
297                    array('hfill'=>true,'align'=>'right'));
298       $table->row_end();
299     }
300
301     if (plc_is_admin()) 
302     {
303         // NOTE: only admins can add users to different sites.
304         $table->row_start();
305         // get list of local sites that the person is not in
306         function get_site_id ($site) { return $site['site_id'];}
307         $person_site_ids=array_map("get_site_id",$sites);
308         $relevant_sites= $api->GetSites( array("peer_id"=>NULL,"~site_id"=>$person_site_ids, '-SORT'=>'name'), $site_columns);
309         // xxx cannot use onchange=submit() - would need to somehow pass action name 
310         function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); }
311         $selectors = array_map ("site_selector",$relevant_sites);
312         $table->cell ($form->select_html("site_id",$selectors,array('label'=>"Choose a site to add")).
313               $form->submit_html("add-person-to-site","Add in site"),
314               array('hfill'=>true,'align'=>'right'));
315         $table->row_end();
316     }
317   }
318   $table->end();
319   $toggle->end();
320  }
321 //////////////////// roles
322 if ($local_peer) {
323   $toggle=new PlekitToggle ('roles',count_english($roles,"role"),array('visible'=>get_arg('show_roles')));
324   $toggle->start();
325
326   if (! $roles) plc_warning ("This user has no role !");
327
328   $is_pi_of_the_site = ( plc_in_site($site_ids[0]) && plc_is_pi() );
329   $can_manage_roles= ( ($local_peer && plc_is_admin()) || $is_pi_of_the_site );
330
331   $headers=array("Role"=>"string");
332   if ($can_manage_roles) $headers [plc_delete_icon()]="none";
333
334   $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
335   $table=new PlekitTable("person_roles",$headers,0,$table_options);  
336   $table->start();
337   
338   // construct array of role objs
339   $role_objs=array();
340   for ($n=0; $n<count($roles); $n++) {
341     $role_objs[]= array('role_id'=>$role_ids[$n], 'name'=>$roles[$n]);
342   }
343
344   if ($role_objs) foreach ($role_objs as $role_obj) {
345       $table->row_start();
346       $table->cell($role_obj['name']);
347       if ($can_manage_roles) $table->cell ($form->checkbox_html('role_ids[]',$role_obj['role_id']));
348       $table->row_end();
349     }
350
351   // footers : the remove and add buttons
352   if ($can_manage_roles) {
353
354     // remove
355     $table->tfoot_start();
356     if ($roles) {
357       $table->row_start();
358       $table->cell($form->submit_html("remove-roles-from-person","Remove Roles"),
359                    array('hfill'=>true,'align'=>'right'));
360       $table->row_end();
361     }
362
363     // add
364     // compute the roles that can be added
365     if (plc_is_admin()) 
366       // all roles, except 'node' that does not make sense for a person
367       $exclude_role_ids=array(50);
368     else
369     // all roles except admin and pi, and node to avoid confusing people
370       $exclude_role_ids=array(10,20,50);
371     $possible_roles = roles_except($api->GetRoles(),$exclude_role_ids);
372     $roles_to_add = roles_except ($possible_roles,$role_ids);
373     if ( $roles_to_add ) {
374       $selectors=$form->role_selectors($roles_to_add);
375       $table->row_start();
376       $add_role_left_area=$form->select_html("role_id",$selectors,array('label'=>"Choose role"));
377       // add a role : the button
378       $add_role_right_area=$form->submit_html("add-role-to-person","Add role");
379       $table->cell ($add_role_left_area . $add_role_right_area,
380                     array('hfill'=>true,'align'=>'right'));
381       $table->row_end();
382     }
383   }
384   $table->end();
385   $toggle->end();
386  }
387
388 //////////////////////////////////////////////////////////// Tags
389 // tags section
390 if ($local_peer) {
391   $tags=$api->GetPersonTags (array('person_id'=>$person_id));
392   function get_tagname ($tag) { return $tag['tagname'];}
393   // xxx looks like tech-only see an error here, 
394   // might be that GetPersonTags is not accessible or something
395   $tagnames = array_map ("get_tagname",$tags);
396   
397   $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
398                               array('bubble'=>'Inspect and set tags on that person',
399                                     'visible'=>get_arg('show_tags')));
400   $toggle->start();
401
402   $headers=array("Name"=>"string",
403                  "Value"=>"string",
404                  );
405   if (plc_is_admin()) $headers[plc_delete_icon()]="none";
406   
407   $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
408   $table=new PlekitTable("person_tags",$headers,0,$table_options);
409   $table->start();
410   if ($tags) foreach ($tags as $tag) {
411       $table->row_start();
412       $table->cell(l_tag_obj($tag));
413       $table->cell($tag['value']);
414       // the remove checkbox
415       if (plc_is_admin()) $table->cell ($form->checkbox_html('person_tag_ids[]',$tag['person_tag_id']));
416       $table->row_end();
417     }
418   
419   if ($privileges) {
420     $table->tfoot_start();
421
422     // remove tag 
423     $table->row_start();
424     $table->cell($form->submit_html("delete-person-tags","Remove Tags"),
425                  // use the whole columns and right adjust
426                  array('hfill'=>true,'align'=>'right'));
427     $table->row_end();
428
429     // set tag area
430     $table->row_start();
431     // get list of tag names in the person/* category    
432     $all_tags= $api->GetTagTypes( array ("category"=>"person*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
433     // xxx cannot use onchange=submit() - would need to somehow pass action name 
434     function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
435     $selector=array_map("tag_selector",$all_tags);
436     $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
437     $table->cell($form->text_html("value","",array('width'=>8)));
438     $table->cell($form->submit_html("set-tag-on-person","Set Tag"),array('columns'=>2,'align'=>'left'));
439     $table->row_end();
440   }
441   
442   $table->end();
443   $toggle->end();
444
445 }
446
447 //////////////////////////////
448 $form->end();
449 $peers->block_end($peer_id);
450   
451 //plekit_linetabs ($tabs,"bottom");
452
453 // Print footer
454 include 'plc_footer.php';
455
456 ?>