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