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