interface form
[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
76 // enable / disable
77 if ($local_peer && $privileges) 
78   if ($enabled) 
79     $tabs['Disable'] = array ('method'=>'POST',
80                               'url'=>l_actions(),
81                               'values'=> array ('person_id'=>$person_id,
82                                                 'action'=>'disable-person'),
83                               'bubble'=>"Disable $first_name $last_name",
84                               'confirm'=>"Are you sure you want to disable $first_name $last_name");
85   else 
86     $tabs['Enable'] = array ('method'=>'POST',
87                              'url'=>l_actions(),
88                              'values'=> array ('person_id'=>$person_id,
89                                                'action'=>'enable-person'),
90                              'bubble'=>"Enable $first_name $last_name",
91                              'confirm'=>"Are you sure you want to enable $first_name $last_name");
92
93 // become
94 if (plc_is_admin() && ! $is_my_account) 
95   $tabs['Become'] = array('method'=>'POST',
96                           'url'=>l_actions(),
97                           'values'=>array('action'=>'become-person',
98                                           'person_id'=>$person_id),
99                           'bubble'=>"Become $first_name $last_name",
100                           'confirm'=>"Are you sure you want to su $first_name $last_name");
101     
102 // delete
103 if ($local_peer && $privileges) 
104   $tabs['Delete'] = array ('method'=>'POST',
105                            'url'=>l_actions(),
106                            'values'=> array ('person_id'=>$person_id,
107                                              'action'=>'delete-person'),
108                            'bubble'=>"Delete $first_name $last_name",
109                            'confirm'=>"Are you sure to delete $first_name $last_name");
110 // events for that person
111 if ( $privileges) 
112   $tabs['Events'] = array('url'=>l_events(),
113                           'values'=>array('type'=>'Person','person'=>$person_id),
114                           'bubble'=>"Events about $first_name $last_name",
115                           'image'=>'/planetlab/icons/event.png','height'=>18);
116
117 // Back button
118 $tabs['All Users'] = array ('url'=>l_persons(),
119                             'bubble'=>'Back to the Users page');
120
121 plc_tabs($tabs);
122     
123 $peers->block_start ($peer_id);
124
125 if ($local_peer && $privileges && ! $enabled ) 
126   drupal_set_message ("$first_name $last_name is not enabled yet, you can enable her/him with the 'Enable' button below");
127
128 $enabled_label="Yes";
129 if ( ! $enabled ) $enabled_label = plc_warning_html("Disabled");
130
131 $can_update = (plc_is_admin() && $local_peer) || $is_my_account;
132 $details = new PlcDetails($can_update);
133
134 $details->form_start(l_actions(),array("action"=>"update-person",
135                                        "person_id"=>$person_id));
136 $details->start();
137
138
139 $details->th_td("Title",$title,"title",array('width'=>5));
140 $details->th_td("First Name",$first_name,"first_name");
141 $details->th_td("Last Name",$last_name,"last_name");
142 $details->th_td(href("mailto:$email","Email"),$email,"email");
143 $details->th_td("Phone",$phone,"phone");
144 $details->th_td("URL",$url,"url",array('width'=>40));
145 $details->th_td("Bio",$bio,"bio",array('input_type'=>'textarea','height'=>4));
146
147 // xxx need to check that this is working
148 if ($can_update) {
149   $details->th_td("Password","","password1",array('input_type'=>'password'));
150   $details->th_td("Repeat","","password2",array('input_type'=>'password'));
151   $details->tr_submit("submit","Update Account");
152   $details->space();
153  }
154
155 $details->th_td("Enabled",$enabled_label);
156 if ( ! $local_peer ) {
157   $details->th_td("Peer",$peers->peer_link($peer_id));
158   $details->space();
159  }
160
161 $details->end();
162 $details->form_end();
163
164 //////////////////// slices
165 plc_section('Slices');
166
167 if( ! $slices) {
168   plc_warning ("User has no slice");
169  } else {
170   $headers=array('Slice name'=>'string');
171   $reasonable_page=5;
172   $table_options = array('notes_area'=>false,"search_width"=>10,'pagesize'=>$reasonable_page);
173   if (count ($slices) <= $reasonable_page) {
174     $table_options['search_area']=false;
175     $table_options['pagesize_area']=false;
176   }
177   $table=new PlcTable ("person_slices",$headers,1,$table_options);
178   $table->start();
179
180   foreach( $slices as $slice ) {
181     $slice_name= $slice['name'];
182     $slice_id= $slice['slice_id'];
183     $table->row_start();
184     $table->cell(l_slice_t($slice_id,$slice_name));
185     $table->row_end();
186   }
187   $table->end();
188  }
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 plc_section ("Keys");
196                 
197 $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
198 if ( empty( $key_ids ) ) {
199   plc_warning("This user has no known key");
200  } 
201
202 $headers=array("Type"=>"string",
203                "Key"=>"string");
204 if ($can_manage_keys) $headers[plc_delete_icon()]="none";
205 // table overall options
206 $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
207 $table=new PlcTable("person_keys",$headers,"1",$table_options);
208 $table->start();
209     
210 if ($keys) foreach ($keys as $key) {
211   $key_id=$key['key_id'];
212   $table->row_start();
213   $table->cell ($key['key_type']);
214   $table->cell(wordwrap( $key['key'], 60, "<br />\n", 1 ));
215   if ($can_manage_keys) 
216     $table->cell ($form->checkbox_html('key_ids[]',$key_id));
217   $table->row_end();
218 }
219 // the footer area is used for displaying key-management buttons
220 // add the 'remove keys' button and key upload areas as the table footer
221 if ($can_manage_keys) {
222   $table->tfoot_start();
223   // no need to remove if there's no key
224   if ($keys) {
225     $table->row_start();
226     $table->cell($form->submit_html ("delete-keys","Remove keys"),
227                  $table->columns(),"right");
228     $table->row_end();
229   }
230   $table->row_start();
231   $table->cell($form->label_html("key","Upload new key")
232                . $form->file_html("key",60)
233                . $form->submit_html("upload-key","Upload key"),
234                $table->columns(),"right");
235   $table->row_end();
236 }
237
238 $table->end();
239
240 //////////////////// sites
241 plc_section('Sites');
242   
243 if (empty( $sites ) ) {
244   plc_warning('This user is not affiliated with a site !!');
245  } 
246 $can_manage_sites = $local_peer && plc_is_admin() || $is_my_account;
247 $headers=array();
248 $headers['Login_base']="string";
249 $headers['Name']="string";
250 if ($can_manage_sites) $headers[plc_delete_icon()]="none";
251 $table_options = array('notes_area'=>false,'search_area'=>false, 'pagesize_area'=>false);
252 $table=new PlcTable ("person_sites",$headers,0,$table_options);
253 $table->start();
254 foreach( $sites as $site ) {
255   $site_name= $site['name'];
256   $site_id= $site['site_id'];
257   $login_base=$site['login_base'];
258   $table->row_start();
259   $table->cell ($login_base);
260   $table->cell (l_site_t($site_id,$site_name));
261   if ($can_manage_sites)
262     $table->cell ($form->checkbox_html('site_ids[]',$site_id));
263   $table->row_end ();
264 }
265 if ($can_manage_sites) {
266   $table->tfoot_start();
267
268   if ($sites) {
269     $table->row_start();
270     $table->cell($form->submit_html("remove-person-from-sites","Remove Sites"),
271                  $table->columns(),"right");
272     $table->row_end();
273   }
274
275   $table->row_start();
276
277   // get list of local sites that the person is not in
278   function get_site_id ($site) { return $site['site_id'];}
279   $person_site_ids=array_map("get_site_id",$sites);
280   $relevant_sites= $api->GetSites( array("peer_id"=>NULL,"~site_id"=>$person_site_ids), $site_columns);
281   // xxx cannot use onchange=submit() - would need to somehow pass action name 
282   function site_selector($site) { return array('display'=>$site['name'],"value"=>$site['site_id']); }
283   $selectors = array_map ("site_selector",$relevant_sites);
284   $table->cell ($form->select_html("site_id",$selectors,array('label'=>"Choose a site to add")).
285                 $form->submit_html("add-person-to-site","Add in site"),
286                 $table->columns(),"right");
287   $table->row_end();
288  }
289 $table->end();
290
291 //////////////////// roles
292 plc_section("Roles");
293 if (! $roles) plc_warning ("This user has no role !");
294
295 $can_manage_roles= ($local_peer && plc_is_admin());
296 $table_options=array("search_area"=>false,"notes_area"=>false);
297
298 $headers=array("Role"=>"string");
299 if ($can_manage_roles) $headers [plc_delete_icon()]="none";
300
301 $table_options=array('search_area'=>false,'pagesize_area'=>false,'notes_area'=>false);
302 $table=new PlcTable("person_roles",$headers,0,$table_options);  
303 $table->start();
304   
305 // construct array of role objs
306 $role_objs=array();
307 for ($n=0; $n<count($roles); $n++) {
308   $role_objs[]= array('role_id'=>$role_ids[$n], 'name'=>$roles[$n]);
309  }
310
311 if ($role_objs) foreach ($role_objs as $role_obj) {
312   $table->row_start();
313   $table->cell($role_obj['name']);
314   if ($can_manage_roles) $table->cell ($form->checkbox_html('role_ids[]',$role_obj['role_id']));
315   $table->row_end();
316  }
317
318 // footers : the remove and add buttons
319 if ($can_manage_roles) {
320   $table->tfoot_start();
321   if ($roles) {
322     $table->row_start();
323     $table->cell($form->submit_html("remove-roles-from-person","Remove Roles"),
324                  $table->columns(),"right");
325     $table->row_end();
326   }
327
328   $table->row_start();
329   $selectors=$form->role_selectors_excluding($api,$role_ids);
330   $add_role_left_area=$form->select_html("role_id",$selectors,array('label'=>"Choose role"));
331   // add a role : the button
332   $add_role_right_area=$form->submit_html("add-role-to-person",array('label'=>"Add role"));
333   $table->cell ($add_role_left_area . $add_role_right_area,
334                 $table->columns(),"right");
335   $table->row_end();
336  }
337 $table->end();
338
339 //////////////////////////////
340 $form->end();
341 $peers->block_end($peer_id);
342   
343 // Print footer
344 include 'plc_footer.php';
345
346
347 ?>