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