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