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