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