svn:keywords
[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_minitabs.php';
19 require_once 'plc_tables.php';
20 require_once 'plc_details.php';
21
22 // -------------------- 
23 // recognized URL arguments
24 $person_id=intval($_GET['id']);
25 if ( ! $person_id ) { plc_error('Malformed URL - id not set'); return; }
26
27 ////////////////////
28 // Get all columns as we focus on only one entry
29 $persons= $api->GetPersons( array($person_id));
30
31 if (empty($persons)) {
32   drupal_set_message ("Person " . $person_id . " not found");
33  } else {
34   $person=$persons[0];
35   
36   // vars from api
37   $first_name= $person['first_name'];
38   $last_name= $person['last_name'];
39   $title= $person['title'];
40   $url= $person['url'];
41   $phone= $person['phone'];
42   $email= $person['email'];
43   $enabled= $person['enabled'];
44   $peer_id=$person['peer_id'];
45   
46   // arrays from api
47   $role_ids= $person['role_ids'];
48   $roles= $person['roles'];
49   $site_ids= $person['site_ids'];
50   $slice_ids= $person['slice_ids'];
51   $key_ids= $person['key_ids'];
52   
53   // gets more data from API calls
54   $sites= $api->GetSites( $site_ids, array( "site_id", "name", "login_base" ) );
55   $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
56   $keys= $api->GetKeys( $key_ids );
57   
58   drupal_set_title("Details for account " . $first_name . " " . $last_name);
59
60   $plc_hash=plc_peer_get_hash($api);
61
62   $local_peer = plc_peer_block_start ($peer_hash,$peer_id);
63   $is_my_account = plc_my_person_id() == $person_id;
64   $privileges = plc_is_admin () || ( plc_in_site($site_id) && plc_is_pi());
65
66   $tabs=array();
67
68   if ($local_peer && $privileges) {
69     if (plc_is_admin())
70       $tabs['Events'] = array('url'=>l_events(),
71                               'values'=>array('type'=>'Person','person'=>$person_id),
72                               'bubble'=>'Related events',
73                               'image'=>'/planetlab/icons/event.png',
74                               'height'=>18);
75     if ($enabled) 
76       $tabs['Disable'] = array ('method'=>'POST',
77                                 'url'=>'/db/persons/person_actions.php',
78                                 'values'=> array ('person_id'=>$person_id,
79                                                   'action'=>'disable'),
80                                 'bubble'=>"Disable $first_name",
81                                 'confirm'=>"Are you sure you want to disable $first_name $last_name");
82     else 
83       $tabs['Enable'] = array ('method'=>'POST',
84                                 'url'=>'/db/persons/person_actions.php',
85                                 'values'=> array ('person_id'=>$person_id,
86                                                   'action'=>'enable'),
87                                 'bubble'=>"Enable $first_name");
88       $tabs['Delete'] = array ('method'=>'POST',
89                                 'url'=>'/db/persons/person_actions.php',
90                                 'values'=> array ('person_id'=>$person_id,
91                                                   'action'=>'delete'),
92                                'bubble'=>"Delete $first_name",
93                                'confirm'=>"Are you sure to delete $first_name $last_name");
94   }
95
96   if ($privileges || $is_my_account) 
97     $tabs['Update'] = array('url'=>'/db/persons/update.php',
98                             'values'=>array('id'=>$person_id),
99                             'bubble'=>"Update $first_name");
100
101   $tabs['All Users'] = array ('url'=>l_persons());
102
103   plc_tabs($tabs);
104     
105   if ($privileges && ! $enabled ) 
106     drupal_set_message ("$first_name $last_name is not enabled yet, please use the 'Enable' button below");
107
108   $enabled_text="Enabled";
109   if ( ! $enabled ) $enabled_text = plc_warning_div("Disabled");
110
111   plc_details_start();
112   plc_details_line("Enabled",$enabled_text);
113   plc_details_line("First Name",$first_name);
114   plc_details_line("Last Name",$last_name);
115   plc_details_line("Title",$title);
116   plc_details_line("Email",href("mailto:$email",$email));
117   plc_details_line("Phone",$phone);
118   plc_details_line("URL",$url);
119   plc_details_end();
120
121           
122   echo "<hr />\n";
123                 
124   // keys
125   $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
126   plc_table_title('Keys');
127   if ( empty( $key_ids ) ) {
128     echo "<span class='plc-warning'> This user has no known key</span>";
129   } else {
130     echo "<p><table border=0 width=450>\n";
131     echo "<thead><tr><th>Type</th><th>Key</th>";
132     if ( $can_manage_keys )
133       echo "<th>Remove</th>";
134     echo "</tr></thead><tbody>\n";
135     
136     foreach( $keys as $key ) {
137       $key_type= $key['key_type'];
138       $key_id= $key['key_id'];
139       $key_text= wordwrap( $key['key'], 70, "<br />\n", 1 );
140       echo "<tr><td>$key_type</td><td>$key_text";
141       
142       if ( $can_manage_keys ) 
143         echo "</td><td><input type=checkbox name='rem_key[]' value='$key_id'>";
144       
145       echo "</td></tr>\n";
146     }
147     
148     echo "</tbody></table>\n";
149     if ($can_manage_keys)
150       echo "<p><input type=submit name='Remove_keys' value='Remove keys'><br />\n";
151   }
152                 
153   if( $can_manage_keys ){
154     echo "<br /> Upload new key: <input type='file' name='key' size=30>\n
155         <input type='submit' name='Upload' value='Upload'>\n
156         <br /><hr />\n";
157   }
158
159   // sites
160   plc_table_title('Sites');
161   
162   // sites
163   if (empty( $sites ) ) {
164     plc_warning('This user is not affiliated with a site !!');
165     } else {
166     $columns=array();
167     $columns['Name']="string";
168     $columns['Login_base']="string";
169     $columns['Remove']="string";
170     $table_options = array('notes_area'=>false,'search_area'=>false);
171     plc_table_start ("person_slices",$columns,1,$table_options);
172     foreach( $sites as $site ) {
173       $site_name= $site['name'];
174       $site_id= $site['site_id'];
175       $login_base=$site['login_base'];
176       plc_table_row_start();
177       plc_table_cell (l_site_t($site_id,$site_name));
178       plc_table_cell ($login_base);
179       plc_table_cell ("<input id=" . $login_base . " type=checkbox name='rem_site[]' value=" . $site_id . ">");
180       plc_table_row_end ();
181     }
182     plc_table_end($table_options);
183   }
184
185   echo "<input type=submit name='Remove_Sites' value='Remove Sites'>\n";
186   
187         
188   // diplay site select list to add another site for user
189   if ($local_peer && plc_is_admin()) {
190     // get site info
191     $all_sites= $api->GetSites( NULL, array( "site_id", "name" ) );
192     
193     if( $sites )
194       $person_site= arr_diff( $all_sites, $sites );
195     else
196       $person_site= $all_sites;
197     
198     //    sort_sites( $person_site );
199     
200     echo "<p>Select a site to add this user to: ";
201     echo "<select name='site_add' onChange='submit()'>\n<option value=''>Choose a site to add:</option>\n";
202     
203     foreach( $person_site as $site ) {
204       echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
205       
206     }
207     
208     echo "</select>";
209     
210   }
211   echo "<hr />\n";
212   
213   // roles
214   echo "<h3>Roles</h3>\n";
215   echo "<p><table>\n";
216   echo "<thead><tr><th>Role</th>";
217   if( plc_is_admin())
218     echo "<th>Remove</th>";
219   echo "</tr></thead><tbody>\n";
220   
221   // construct role array
222   for( $n=0; $n<count($roles); $n++ ) {
223     $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
224   }
225     
226   $button_shown=0;
227   if ( !empty ($roles) ) {
228     foreach( $proles as $role ) {
229       $role_name= $role['name'];
230       $role_id= $role['role_id'];
231       
232       echo "<tr><td>$role_name";
233               
234
235       if( plc_is_admin()) {
236         echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
237         if ( ! $button_shown ) {
238           $rowspan=count($roles);
239           echo "</td><td rowspan=$rowspan valign=center><input type=submit name='Remove_Roles' value='Remove Roles'></td></tr>\n";
240           $button_shown=1;
241         }
242       }
243       
244       echo "</td></tr>\n";
245     }
246   } else {
247     echo "<span class='plc-warning'> This user has no known role !!</span>";
248   }
249   echo "</tbody></table>\n";
250           
251   // if admin show roles to add
252   if( plc_is_admin()) {
253     $all_roles= $api->GetRoles();
254     $addable_roles= arr_diff( $all_roles, $proles );
255     ##when the proles array is empty strangely the method arr_diff($all_roles, $proles )
256     ##return an empty array and the scrolling roles list is not displayed in this case
257     ##assign to addablerole all the roles
258     if (count($proles)==0)
259       $addable_roles=$all_roles;
260     
261     if( !empty( $addable_roles ) ) {
262       echo "<p>Add role: <select name='add_role' onChange='submit()'>\n<option value=''>Choose a Role to add:</option>\n";
263       
264       foreach( $addable_roles as $arole ) {
265         echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
266         
267       }
268       
269       echo "</select>\n";
270       
271     }
272   }
273   
274   echo "<hr />\n";
275   
276   // slices
277   plc_table_title('Slices');
278
279   if( empty( $slices ) ) {
280     drupal_set_message ("User has no slice");
281   } else {
282     $columns=array('Slice name'=>'string');
283     $table_options=array();
284     plc_table_start("person_slices",$columns,1,$table_options);
285
286     foreach( $slices as $slice ) {
287       $slice_name= $slice['name'];
288       $slice_id= $slice['slice_id'];
289       plc_table_row_start();
290       plc_table_cell(l_slice_t($slice_id,$slice_name));
291       plc_table_row_end();
292     }
293     plc_table_end($table_options);
294   }
295
296   plc_peer_block_end();
297   
298  }
299
300 // Print footer
301 include 'plc_footer.php';
302
303
304 ?>