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