ckp
[plewww.git] / planetlab / persons / person.php
index 0fafa41..3419150 100644 (file)
@@ -57,13 +57,14 @@ $slice_ids= $person['slice_ids'];
 $key_ids= $person['key_ids'];
 
 // gets more data from API calls
-$sites= $api->GetSites( $site_ids, array( "site_id", "name", "login_base" ) );
+$site_columns=array( "site_id", "name", "login_base" );
+$sites= $api->GetSites( $site_ids, $site_columns);
 $slices= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
 $keys= $api->GetKeys( $key_ids );
 
 drupal_set_title("Details for account " . $first_name . " " . $last_name);
 
-$plc_hash=plc_peer_get_hash($api);
+$plc_hash=plc_peer_global_hash($api);
 
 $local_peer = plc_peer_block_start ($peer_hash,$peer_id);
 $is_my_account = plc_my_person_id() == $person_id;
@@ -81,14 +82,14 @@ if ($privileges || $is_my_account)
 if ($local_peer && $privileges) 
   if ($enabled) 
     $tabs['Disable'] = array ('method'=>'POST',
-                             'url'=>l_person_actions(),
+                             'url'=>l_actions(),
                              'values'=> array ('person_id'=>$person_id,
                                                'action'=>'disable-person'),
                              'bubble'=>"Disable $first_name $last_name",
                              'confirm'=>"Are you sure you want to disable $first_name $last_name");
   else 
     $tabs['Enable'] = array ('method'=>'POST',
-                            'url'=>l_person_actions(),
+                            'url'=>l_actions(),
                             'values'=> array ('person_id'=>$person_id,
                                               'action'=>'enable-person'),
                             'bubble'=>"Enable $first_name $last_name",
@@ -96,7 +97,8 @@ if ($local_peer && $privileges)
 
 // become
 if (plc_is_admin() && ! $is_my_account) 
-  $tabs['Become'] = array('url'=>l_person_actions(),
+  $tabs['Become'] = array('method'=>'POST',
+                         'url'=>l_actions(),
                          'values'=>array('action'=>'become-person',
                                          'person_id'=>$person_id),
                          'bubble'=>"Become $first_name $last_name",
@@ -105,7 +107,7 @@ if (plc_is_admin() && ! $is_my_account)
 // delete
 if ($local_peer && $privileges) 
   $tabs['Delete'] = array ('method'=>'POST',
-                          'url'=>l_person_actions(),
+                          'url'=>l_actions(),
                           'values'=> array ('person_id'=>$person_id,
                                             'action'=>'delete-person'),
                           'bubble'=>"Delete $first_name $last_name",
@@ -142,14 +144,15 @@ plc_details_line("Bio",wordwrap($bio,50,"<br/>"));
 plc_details_end();
 
 //////////////////// slices
-echo "<hr />\n";
-plc_table_title('Slices');
+plc_section('Slices');
 
-if( empty( $slices ) ) {
-  drupal_set_message ("User has no slice");
+if( ! $slices) {
+  plc_warning ("User has no slice");
  } else {
-  $columns=array('Slice name'=>'string');
-  plc_table_start("person_slices",$columns,1,$table_options);
+  $headers=array('Slice name'=>'string');
+  $table_options=array('notes_area'=>false,
+                      'pagesize'=>5);
+  plc_table_start("person-slices",$headers,1,$table_options);
 
   foreach( $slices as $slice ) {
     $slice_name= $slice['name'];
@@ -158,168 +161,160 @@ if( empty( $slices ) ) {
     plc_table_cell(l_slice_t($slice_id,$slice_name));
     plc_table_row_end();
   }
-  plc_table_end("person_slices");
+  plc_table_end("person-slices");
  }
 
-////////// keys          
-echo "<hr />\n";
-plc_table_title ("Keys");
+// we don't set 'action', but use the submit button name instead
+plc_form_start(l_actions(),
+              array("person_id"=>$person_id,
+                    // uncomment this to run the 'debug' action 
+                    //"action"=>"debug",
+                    ));
+
+//////////////////// keys
+plc_section ("Keys");
                
 $can_manage_keys = ( $local_peer && ( plc_is_admin() || $is_my_account) );
 if ( empty( $key_ids ) ) {
   plc_warning("This user has no known key");
- } else {
-  // we don't set 'action', but use the submit button name instead
-  plc_form_start(l_person_actions(),
-                array("person_id"=>$person_id));
-
-  // the headers
-  $columns=array("Type"=>"string",
-                "Key"=>"string");
-  if ($can_manage_keys) $columns['Remove']="none";
-  // table overall options
-  $table_options=array("search_area"=>false,"notes_area"=>false);
-  // add the 'remove site' button and key upload areas as the table footer
-  if ($can_manage_keys) {
-    $remove_keys_area=plc_form_submit_text ("delete-keys","Remove keys");
-    $upload_key_left_area= plc_form_label_text("Upload new key","key") . plc_form_file_text("key",60);
-    $upload_key_right_area=plc_form_submit_text("upload-key","Upload key");
-    $table_options['footer']="";
-    $table_options['footer'].="<tr><td colspan=3 style='text-align:right'> $remove_keys_area </td></tr>";
-    $table_options['footer'].="<tr><td colspan=2 style='text-align:right'> $upload_key_left_area </td>".
-      "<td> $upload_key_right_area </td></tr>";
-  }
-  plc_table_start("person_keys",$columns,"1",$table_options);
+ } 
+
+// headers
+$headers=array("Type"=>"string",
+              "Key"=>"string");
+if ($can_manage_keys) $headers['Remove']="none";
+// table overall options
+$table_options=array("search_area"=>false,"notes_area"=>false);
+plc_table_start("person-keys",$headers,"1",$table_options);
     
-  foreach( $keys as $key ) {
-    $key_id=$key['key_id'];
-    plc_table_row_start($key_id);
-    plc_table_cell ($key['key_type']);
-    plc_table_cell(wordwrap( $key['key'], 60, "<br />\n", 1 ));
-    if ($can_manage_keys) 
-      plc_table_cell (plc_form_checkbox_text('key_ids[]',$key_id));
-    plc_table_row_end();
-  }
-  plc_table_end("person_keys");
-  plc_form_end();
- }
+if ($keys) foreach ($keys as $key) {
+  $key_id=$key['key_id'];
+  plc_table_row_start($key_id);
+  plc_table_cell ($key['key_type']);
+  plc_table_cell(wordwrap( $key['key'], 60, "<br />\n", 1 ));
+  if ($can_manage_keys) 
+    plc_table_cell (plc_form_checkbox_text('key_ids[]',$key_id));
+  plc_table_row_end();
+}
+// the footer area is used for displaying key-management buttons
+$footers=array();
+// add the 'remove keys' button and key upload areas as the table footer
+if ($can_manage_keys) {
+  $remove_keys_area=plc_form_submit_text ("delete-keys","Remove keys");
+  $upload_key_left_area= plc_form_label_text("key","Upload new key") . plc_form_file_text("key",60);
+  $upload_key_right_area=plc_form_submit_text("upload-key","Upload key");
+  // no need to remove if there's no key
+  if ($keys) 
+    $footers[]="<td colspan=3 style='text-align:right'> $remove_keys_area </td>";
+  // upload a new key
+  $footers []="<td colspan=2 style='text-align:right'> $upload_key_left_area </td>".
+    "<td> $upload_key_right_area </td>";
+}
 
-// sites
-echo "<hr />\n";
-plc_table_title('Sites');
+plc_table_end("person-keys",array("footers"=>$footers));
+
+//////////////////// sites
+plc_section('Sites');
   
 // sites
 if (empty( $sites ) ) {
   plc_warning('This user is not affiliated with a site !!');
- } else {
-  $columns=array();
-  $columns['Name']="string";
-  $columns['Login_base']="string";
-  $columns['Remove']="string";
-  $table_options = array('notes_area'=>false,'search_area'=>false);
-  plc_table_start ("person_sites",$columns,1,$table_options);
-  foreach( $sites as $site ) {
-    $site_name= $site['name'];
-    $site_id= $site['site_id'];
-    $login_base=$site['login_base'];
-    plc_table_row_start();
-    plc_table_cell (l_site_t($site_id,$site_name));
-    plc_table_cell ($login_base);
-    plc_table_cell ("<input id=" . $login_base . " type=checkbox name='rem_site[]' value=" . $site_id . ">");
-    plc_table_row_end ();
-  }
-  plc_table_end("person_sites");
- }
+ } 
+$can_manage_sites = $local_peer && plc_is_admin() || $is_my_account;
+$headers=array();
+$headers['Login_base']="string";
+$headers['Name']="string";
+if ($can_manage_sites) 
+  $headers['Remove']="string";
+$table_options = array('notes_area'=>false,'search_area'=>false);
+plc_table_start ("person-sites",$headers,0,$table_options);
+foreach( $sites as $site ) {
+  $site_name= $site['name'];
+  $site_id= $site['site_id'];
+  $login_base=$site['login_base'];
+  plc_table_row_start();
+  plc_table_cell ($login_base);
+  plc_table_cell (l_site_t($site_id,$site_name));
+  if ($can_manage_sites)
+    plc_table_cell (plc_form_checkbox_text('site_ids[]',$site_id));
+  plc_table_row_end ();
+}
+// footers : the remove and add buttons
+$footers=array();
+if ($can_manage_sites) {
+  // remove selected sites
+  $remove_sites_area = plc_form_submit_text("remove-person-from-sites","Remove Sites");
 
-echo "<input type=submit name='Remove_Sites' value='Remove Sites'>\n";
-  
-       
-// diplay site select list to add another site for user
-if ($local_peer && plc_is_admin()) {
-  // get site info
-  $all_sites= $api->GetSites( NULL, array( "site_id", "name" ) );
-    
-  if( $sites )
-    $person_site= arr_diff( $all_sites, $sites );
-  else
-    $person_site= $all_sites;
-    
-  //    sort_sites( $person_site );
-    
-  echo "<p>Select a site to add this user to: ";
-  echo "<select name='site_add' onChange='submit()'>\n<option value=''>Choose a site to add:</option>\n";
-    
-  foreach( $person_site as $site ) {
-    echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
-      
-  }
-    
-  echo "</select>";
-    
+  // add a site : the button
+  $add_site_left_area=plc_form_submit_text("add-person-to-site","Add in site");
+  // get list of local sites that the person is not in
+  $person_site_ids=array_map("get_site_id",$sites);
+  $relevant_sites= $api->GetSites( array("peer_id"=>NULL,"~site_id"=>$person_site_ids), $site_columns);
+
+  // xxx cannot use onchange=submit() - would need to somehow pass action name 
+  $selector=array();
+  foreach ($relevant_sites as $site) 
+    $selector[]= array('display'=>$site['name'],"value"=>$site['site_id']);
+  $add_site_right_area=plc_form_select_text("site_id",$selector,"Choose a site to add");
+  if ($sites) 
+    $footers[]="<td colspan=3 style='text-align:right'> $remove_sites_area </td>";
+  // add a new site
+  $footers []="<td style='text-align:right'> $add_site_left_area </td>".
+    "<td colspan=2> $add_site_right_area </td>";
  }
-echo "<hr />\n";
-  
-// roles
-echo "<h3>Roles</h3>\n";
-echo "<p><table>\n";
-echo "<thead><tr><th>Role</th>";
-if( plc_is_admin())
-  echo "<th>Remove</th>";
-echo "</tr></thead><tbody>\n";
+plc_table_end("person-sites",array("footers"=>$footers));
+
+//////////////////// roles
+plc_section("Roles");
+if (! $roles) plc_warning ("This user has no role !");
+
+$can_manage_roles= ($local_peer && plc_is_admin());
+$table_options=array("search_area"=>false,"notes_area"=>false);
+
+$headers=array("Role"=>"none");
+if ($can_manage_roles) $headers ["Remove"]="none";
+
+plc_table_start("person-roles",$headers,0,$table_options);  
   
-// construct role array
-for( $n=0; $n<count($roles); $n++ ) {
-  $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
+// construct array of role objs
+$role_objs=array();
+for ($n=0; $n<count($roles); $n++) {
+  $role_objs[]= array('role_id'=>$role_ids[$n], 'name'=>$roles[$n]);
  }
-    
-$button_shown=0;
-if ( !empty ($roles) ) {
-  foreach( $proles as $role ) {
-    $role_name= $role['name'];
-    $role_id= $role['role_id'];
-      
-    echo "<tr><td>$role_name";
-             
-
-    if( plc_is_admin()) {
-      echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
-      if ( ! $button_shown ) {
-       $rowspan=count($roles);
-       echo "</td><td rowspan=$rowspan valign=center><input type=submit name='Remove_Roles' value='Remove Roles'></td></tr>\n";
-       $button_shown=1;
-      }
-    }
-      
-    echo "</td></tr>\n";
-  }
- } else {
-  echo "<span class='plc-warning'> This user has no known role !!</span>";
+
+if ($role_objs) foreach ($role_objs as $role_obj) {
+  plc_table_row_start();
+  plc_table_cell($role_obj['name']);
+  if ($can_manage_roles) plc_table_cell (plc_form_checkbox_text('role_ids[]',$role_obj['role_id']));
+  plc_table_row_end();
  }
-echo "</tbody></table>\n";
-         
-// if admin show roles to add
-if( plc_is_admin()) {
-  $all_roles= $api->GetRoles();
-  $addable_roles= arr_diff( $all_roles, $proles );
-  ##when the proles array is empty strangely the method arr_diff($all_roles, $proles )
-  ##return an empty array and the scrolling roles list is not displayed in this case
-  ##assign to addablerole all the roles
-  if (count($proles)==0)
-    $addable_roles=$all_roles;
-    
-  if( !empty( $addable_roles ) ) {
-    echo "<p>Add role: <select name='add_role' onChange='submit()'>\n<option value=''>Choose a Role to add:</option>\n";
-      
-    foreach( $addable_roles as $arole ) {
-      echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
-       
-    }
-      
-    echo "</select>\n";
-      
-  }
+
+// footers : the remove and add buttons
+$footers=array();
+if ($can_manage_roles) {
+  // remove selected roles
+  $remove_roles_area = plc_form_submit_text("remove-roles-from-person","Remove Roles");
+
+  // add a role : the button
+  $add_role_left_area=plc_form_submit_text("add-role-to-person","Add role");
+  // get list of local roles that the person has not yet
+  // xxx this does not work because GetRoles does not support filters
+  $relevant_roles = $api->GetRoles( array("~role_id"=>$role_ids));
+
+  $selector=array();
+  foreach ($relevant_roles as $role) 
+    $selector[]= array('display'=>$role['name'],"value"=>$role['role_id']);
+  $add_role_right_area=plc_form_select_text("role_id",$selector,"Choose a role to add");
+  if ($roles) 
+    $footers[]="<td colspan=3 style='text-align:right'> $remove_roles_area </td>";
+  // add a new role
+  $footers []="<td style='text-align:right'> $add_role_left_area </td>".
+    "<td colspan=2> $add_role_right_area </td>";
  }
-  
+plc_table_end("person-roles",array("footers"=>$footers));
+
+//////////////////////////////
+plc_form_end();
 plc_peer_block_end();
   
 // Print footer