split in two
[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) 
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) 
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 plc_tabs($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 $details = new PlcDetails($can_update);
131
132 $details->form_start(l_actions(),array("action"=>"update-person",
133                                        "person_id"=>$person_id));
134 $details->start();
135
136
137 $details->th_td("Title",$title,"title",array('width'=>5));
138 $details->th_td("First Name",$first_name,"first_name");
139 $details->th_td("Last Name",$last_name,"last_name");
140 $details->th_td(href("mailto:$email","Email"),$email,"email");
141 $details->th_td("Phone",$phone,"phone");
142 $details->th_td("URL",$url,"url",array('width'=>40));
143 $details->th_td("Bio",$bio,"bio",array('input_type'=>'textarea','height'=>4));
144
145 // xxx need to check that this is working
146 if ($can_update) {
147   $details->th_td("Password","","password1",array('input_type'=>'password'));
148   $details->th_td("Repeat","","password2",array('input_type'=>'password'));
149   $details->tr_submit("submit","Update Account");
150   $details->space();
151  }
152
153 $details->th_td("Enabled",$enabled_label);
154 if ( ! $local_peer ) {
155   $details->th_td("Peer",$peers->peer_link($peer_id));
156   $details->space();
157  }
158
159 $details->end();
160 $details->form_end();
161
162 //////////////////// slices
163 $toggle=new PlcToggle ('slices','Slices',array('trigger-tagname'=>'h2'));
164 $toggle->start();
165
166 if( ! $slices) {
167   plc_warning ("User has no slice");
168  } else {
169   $headers=array('Slice name'=>'string');
170   $reasonable_page=5;
171   $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
172   if (count ($slices) <= $reasonable_page) {
173     $table_options['search_area']=false;
174     $table_options['pagesize_area']=false;
175   }
176   $table=new PlcTable ("person_slices",$headers,1,$table_options);
177   $table->start();
178
179   foreach( $slices as $slice ) {
180     $slice_name= $slice['name'];
181     $slice_id= $slice['slice_id'];
182     $table->row_start();
183     $table->cell(l_slice_t($slice_id,$slice_name));
184     $table->row_end();
185   }
186   $table->end();
187  }
188 $toggle->end();
189
190 // we don't set 'action', but use the submit button name instead
191 $form=new PlcForm(l_actions(), array("person_id"=>$person_id));
192 $form->start();
193
194 //////////////////// keys
195 $toggle=new PlcToggle ('keys',"Keys",array('trigger-tagname'=>'h2'));
196 $toggle->start();
197                 
198 $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
199 if ( empty( $key_ids ) ) {
200   plc_warning("This user has no known key");
201  } 
202
203 $headers=array("Type"=>"string",
204                "Key"=>"string");
205 if ($can_manage_keys) $headers[plc_delete_icon()]="none";
206 // table overall options
207 $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
208 $table=new PlcTable("person_keys",$headers,"1",$table_options);
209 $table->start();
210     
211 if ($keys) foreach ($keys as $key) {
212   $key_id=$key['key_id'];
213   $table->row_start();
214   $table->cell ($key['key_type']);
215   $table->cell(wordwrap( $key['key'], 60, "<br />\n", 1 ));
216   if ($can_manage_keys) 
217     $table->cell ($form->checkbox_html('key_ids[]',$key_id));
218   $table->row_end();
219 }
220 // the footer area is used for displaying key-management buttons
221 // add the 'remove keys' button and key upload areas as the table footer
222 if ($can_manage_keys) {
223   $table->tfoot_start();
224   // no need to remove if there's no key
225   if ($keys) {
226     $table->row_start();
227     $table->cell($form->submit_html ("delete-keys","Remove keys"),
228                  $table->columns(),"right");
229     $table->row_end();
230   }
231   $table->row_start();
232   $table->cell($form->label_html("key","Upload new key")
233                . $form->file_html("key","upload",array('size'=>60))
234                . $form->submit_html("upload-key","Upload key"),
235                $table->columns(),"right");
236   $table->row_end();
237 }
238
239 $table->end();
240 $toggle->end();
241
242 //////////////////// sites
243 $toggle=new PlcToggle('sites','Sites',array('trigger-tagname'=>'h2'));
244 $toggle->start();
245   
246 if (empty( $sites ) ) {
247   plc_warning('This user is not affiliated with a site !!');
248  } 
249 $can_manage_sites = $local_peer && plc_is_admin() || $is_my_account;
250 $headers=array();
251 $headers['Login_base']="string";
252 $headers['Name']="string";
253 if ($can_manage_sites) $headers[plc_delete_icon()]="none";
254 $table_options = array('notes_area'=>false,'search_area'=>false, 'pagesize_area'=>false);
255 $table=new PlcTable ("person_sites",$headers,0,$table_options);
256 $table->start();
257 foreach( $sites as $site ) {
258   $site_name= $site['name'];
259   $site_id= $site['site_id'];
260   $login_base=$site['login_base'];
261   $table->row_start();
262   $table->cell ($login_base);
263   $table->cell (l_site_t($site_id,$site_name));
264   if ($can_manage_sites)
265     $table->cell ($form->checkbox_html('site_ids[]',$site_id));
266   $table->row_end ();
267 }
268 if ($can_manage_sites) {
269   $table->tfoot_start();
270
271   if ($sites) {
272     $table->row_start();
273     $table->cell($form->submit_html("remove-person-from-sites","Remove Sites"),
274                  $table->columns(),"right");
275     $table->row_end();
276   }
277
278   $table->row_start();
279
280   // get list of local sites that the person is not in
281   function get_site_id ($site) { return $site['site_id'];}
282   $person_site_ids=array_map("get_site_id",$sites);
283   $relevant_sites= $api->GetSites( array("peer_id"=>NULL,"~site_id"=>$person_site_ids), $site_columns);
284   // xxx cannot use onchange=submit() - would need to somehow pass action name 
285   function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); }
286   $selectors = array_map ("site_selector",$relevant_sites);
287   $table->cell ($form->select_html("site_id",$selectors,array('label'=>"Choose a site to add")).
288                 $form->submit_html("add-person-to-site","Add in site"),
289                 $table->columns(),"right");
290   $table->row_end();
291  }
292 $table->end();
293 $toggle->end();
294
295 //////////////////// roles
296 $toggle=new PlcToggle ('roles','Roles',array('trigger-tagname'=>'h2'));
297 $toggle->start();
298
299 if (! $roles) plc_warning ("This user has no role !");
300
301 $can_manage_roles= ($local_peer && plc_is_admin());
302 $table_options=array("search_area"=>false,"notes_area"=>false);
303
304 $headers=array("Role"=>"string");
305 if ($can_manage_roles) $headers [plc_delete_icon()]="none";
306
307 $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
308 $table=new PlcTable("person_roles",$headers,0,$table_options);  
309 $table->start();
310   
311 // construct array of role objs
312 $role_objs=array();
313 for ($n=0; $n<count($roles); $n++) {
314   $role_objs[]= array('role_id'=>$role_ids[$n], 'name'=>$roles[$n]);
315  }
316
317 if ($role_objs) foreach ($role_objs as $role_obj) {
318   $table->row_start();
319   $table->cell($role_obj['name']);
320   if ($can_manage_roles) $table->cell ($form->checkbox_html('role_ids[]',$role_obj['role_id']));
321   $table->row_end();
322  }
323
324 // footers : the remove and add buttons
325 if ($can_manage_roles) {
326   $table->tfoot_start();
327   if ($roles) {
328     $table->row_start();
329     $table->cell($form->submit_html("remove-roles-from-person","Remove Roles"),
330                  $table->columns(),"right");
331     $table->row_end();
332   }
333
334   $table->row_start();
335   $selectors=$form->role_selectors_excluding($api,$role_ids);
336   $add_role_left_area=$form->select_html("role_id",$selectors,array('label'=>"Choose role"));
337   // add a role : the button
338   $add_role_right_area=$form->submit_html("add-role-to-person",array('label'=>"Add role"));
339   $table->cell ($add_role_left_area . $add_role_right_area,
340                 $table->columns(),"right");
341   $table->row_end();
342  }
343 $table->end();
344 $toggle->end();
345
346 //////////////////////////////
347 $form->end();
348 $peers->block_end($peer_id);
349   
350 //plc_tabs ($tabs,"bottom");
351
352 // Print footer
353 include 'plc_footer.php';
354
355 ?>