--- old\user_badges.admin.inc	2011-09-13 16:52:48.000000000 -0400
+++ new\user_badges.admin.inc	2011-10-11 00:44:27.562500000 -0400
@@ -40,30 +40,32 @@ function user_badges_badgelist_form($for
   );
 
   $result = db_select('user_badges_badges', 'ubb')
-    ->fields('ubb', array('bid', 'name', 'image', 'weight', 'href'))    
+    ->fields('ubb', array('bid', 'name', 'image', 'weight', 'href'))
     ->extend('TableSort')
     ->orderByHeader($form['header']['#value'])
     ->extend('PagerDefault')
     ->limit(50)
     ->execute();
 
-  // Build a table listing the appropriate badges.    
-  foreach ($result as $badge) {  
+  // Build a table listing the appropriate badges.
+  foreach ($result as $badge) {
     $badge->class = 'badge ' . _user_badges_class($badge);
     $form['name'][$badge->bid] = array('#value' => check_plain($badge->name));
-    $form['badge'][$badge->bid] = array('#value' => theme('user_badge', array('badge' => $badge)));
+    $form['badge'][$badge->bid] = array(
+      '#value' => theme('user_badge', array('badge' => $badge)));
     $form['weight'][$badge->bid] = array(
       '#type' => 'textfield',
       '#size' => 4,
       '#maxlength' => 255,
       '#default_value' => $badge->weight,
     );
-    $form['edit'][$badge->bid] = array('#value' => l(t('edit'), 'admin/user/user_badges/edit/' . $badge->bid));
-    $form['delete'][$badge->bid] = array('#value' => l(t('delete'), 'admin/user/user_badges/delete/' . $badge->bid));
+
+    $form['edit'][$badge->bid] = array('#value' => l(t('edit'),  'admin/config/people/user_badges/edit/' . $badge->bid));
+    $form['delete'][$badge->bid] = array('#value' => l(t('delete'), 'admin/config/people/user_badges/delete/' . $badge->bid));
   }
 
   $form['submit'] = array(
-    '#submit' => t('Update'),    
+    '#submit' => t('Update'),
   );
 
   $form['pager'] = array('#value' => theme('pager', array('tags' => NULL, 'element' => 0)));
@@ -93,9 +95,9 @@ function user_badges_badgelist_form_vali
  */
 function user_badges_badgelist_form_submit($form, &$form_state) {
   if (isset($form['weight']) && is_array($form['weight'])) {
-    foreach (element_children($form['weight']) as $bid) {    
+    foreach (element_children($form['weight']) as $bid) {
       db_update('user_badges_badges')
-        ->fields(array('weight' => $form_state['values'][$bid],))
+        ->fields(array('weight' => $form_state['values'][$bid]))
         ->condition('bid', $bid)
         ->execute();
     }
@@ -128,7 +130,7 @@ function theme_user_badges_badgelist_for
       $row[] = $form['badge'][$key]['#value'];
       $row[] = $form['weight'][$key]['#value'];
       $row[] = $form['edit'][$key]['#value'];
-      $row[] = $form['delete'][$key]['#value'];      
+      $row[] = $form['delete'][$key]['#value'];
       $rows[] = $row;
     }
 
@@ -139,7 +141,7 @@ function theme_user_badges_badgelist_for
         'align' => 'center',
         'colspan' => '20',
       ),
-    );    
+    );
   }
   else {
     $rows[] = array(array(
@@ -172,7 +174,7 @@ function user_badges_edit_form($form, $f
       ->fields('ubb')
       ->condition('bid', $bid)
       ->execute();
-                
+
     if ($edit->rowCount() > 0) {
       $form['bid'] = array(
         '#type' => 'value',
@@ -186,10 +188,10 @@ function user_badges_edit_form($form, $f
   // Are we using a library image or an image URL?
   $libraryimage = '';
   $imageurl = '';
-  if ( isset($edit) && valid_url($edit->image, TRUE) ) {
+  if (isset($edit) && $edit->image) {
     $imageurl = $edit->image;
     $libraryimage = '';
-  }  
+  }
 
   $form['name'] = array(
     '#type' => 'textfield',
@@ -252,25 +254,28 @@ function user_badges_edit_form($form, $f
       '#title' => t('Replacement patterns'),
       '#type' => 'fieldset',
       '#collapsible' => TRUE,
-      '#collapsed' => FALSE,
+      '#collapsed' => TRUE,
     );
     $form['token_help']['help'] = array(
       // Ignore Coder flag on the link.
-      '#value' => t('Install the !link module if you want this URL to include dynamic elements such as badge ID numbers.', array('!link' => l('Token', 'http://drupal.org/project/token', array('absolute' => TRUE)))),
+      '#value' => t('Install the !link module if you want this URL to include dynamic elements such as badge ID numbers.',
+      array('!link' => l('Token', 'http://drupal.org/project/token',
+        array('absolute' => TRUE))
+      )),
     );
   }
 
   if (module_exists('taxonomy')) {
     $badges_vid = variable_get('user_badges_vid', '');
-        
+
     if (!empty($badges_vid)) {
-      $terms = user_badges_get_term_from_vid($badges_vid);      
+      $terms = user_badges_get_term_from_vid($badges_vid);
       $form['tid'] = array(
         '#title' => t('Taxonomy Relation'),
         '#type' => 'select',
         '#options' => !empty($terms) ? $terms : array(t('There are no terms in the selected vocabulery')),
         '#default_value' => !empty($edit) ? $edit->tid : '',
-      );         
+      );
     }
     else {
       $form['tid']['help'] = array(
@@ -297,19 +302,21 @@ function user_badges_edit_form($form, $f
     '#default_value' => !empty($edit) ? $edit->doesnotcounttolimit : '',
     '#description' => t('If this is set, the badge does not count towards the limit for number of badges to display per user.'),
   );
+
   $form['submit'] = array(
     '#type' => 'submit',
-    '#value' => t('Submit'),
+    '#value' => t('Save badge'),
   );
+
   $selects = array('' => t('Use the image URL above instead of a library image.')) + user_badges_image_selects();
 
   if (count($selects)) {
     $form['image'] = array(
       '#type' => 'radios',
       '#title' => t('Image Library'),
-      '#default_value' => $libraryimage,
+      '#default_value' => $imageurl,
       '#options' => $selects,
-      '#description' => t('If you have not entered an image URL above, you can select an image from this user badges image library to associate with this badge. You can upload additional images to the library under the <a href="@url">images</a> tab.', array('@url' => url("admin/user/user_badges/images"))),
+      '#description' => t('If you have not entered an image URL above, you can select an image from this user badges image library to associate with this badge. You can upload additional images to the library under the <a href="@url">images</a> tab.', array('@url' => url("admin/config/people/user_badges/images"))),
     );
   }
   else {
@@ -317,12 +324,12 @@ function user_badges_edit_form($form, $f
       '#type' => 'item',
       '#title' => t('Image'),
       '#value' => t('No badge images uploaded.'),
-      '#description' => t('You can enter an image URL directly above, but if you want to instead upload your image to the user badges image library, use <a href="@upload_link">this link</a>. Note that private download is incompatible with this library and should use direct URL entry instead.', array('@upload_link' => url("admin/user/user_badges/images"))),
+      '#description' => t('You can enter an image URL directly above, but if you want to instead upload your image to the user badges image library, use <a href="@upload_link">this link</a>. Note that private download is incompatible with this library and should use direct URL entry instead.', array('@upload_link' => url("admin/config/people/user_badges/images"))),
     );
   }
   $form['submit2'] = array(
     '#type' => 'submit',
-    '#value' => t('Submit'),
+    '#value' => t('Save badge'),
   );
   return $form;
 }
@@ -360,22 +367,30 @@ function user_badges_edit_form_submit($f
   $edit = $form_state['values'];
   $edit = (object) $edit;
 
-  $image = $edit->imageurl ? trim($edit->imageurl) : 'public://' . $edit->image;
+  if (isset($edit->bid)) {
+    // If the badge already exists, delete it and re-insert it.
+    $result = db_merge('user_badges_badges')
+      ->key(array('bid' => $edit->bid));
+  }
+  else {
+    // If the badge does not exist, insert it.
+    $result = db_insert('user_badges_badges');
+  }
 
-  // If the badge already exists, delete it and re-insert it.  
-  $result = db_merge('user_badges_badges')
-    ->key(array('bid' => $edit->bid))
-    ->fields(array(
-      'name' => array(trim($edit->name), 
-      'image' => $image, 
-      'weight' => $edit->weight, 
-      'href' => (!empty($edit->href) ? trim($edit->href) : NULL), 
-      'unhideable' => $edit->unhideable, 
-      'fixedweight' => $edit->fixedweight, 
-      'doesnotcounttolimit' => $edit->doesnotcounttolimit, 
-      'tid' => (!empty($edit->tid) ? $edit->tid : NULL))
-    ))      
-    ->execute();  
+  $image = $edit->imageurl ? trim($edit->imageurl) : $edit->image;
+
+  // Add the rest of the fields.
+  $result->fields(array(
+      'name' => trim($edit->name),
+      'image' => $image,
+      'weight' => $edit->weight,
+      'href' => (!empty($edit->href) ? trim($edit->href) : NULL),
+      'unhideable' => $edit->unhideable,
+      'fixedweight' => $edit->fixedweight,
+      'doesnotcounttolimit' => $edit->doesnotcounttolimit,
+      'tid' => (!empty($edit->tid) ? $edit->tid : NULL),
+    ))
+    ->execute();
 
   if ($result) {
     drupal_set_message(t('Badge %badgename saved.', array('%badgename' => $edit->name)));
@@ -383,30 +398,29 @@ function user_badges_edit_form_submit($f
   else {
     drupal_set_message(t('There was a problem saving the badge information into the database.'));
   }
-  $form_state['redirect'] = 'admin/user/user_badges';
 }
 
 /**
  * form to upload the badge images or to delete existing ones
  */
 function user_badges_images_form($form, $form_state) {
-  $form = array('#skip_duplicate_check' => TRUE);  
+  $form = array('#skip_duplicate_check' => TRUE);
   $form['new']['upload'] = array(
     '#type' => 'file',
     '#title' => t('Upload image'),
-    '#size' => 40,
   );
+
   $form['new']['attach'] = array(
     '#type' => 'submit',
     '#value' => t('Upload'),
-  );    
+  );
 
   $form['#attributes']['enctype'] = 'multipart/form-data';
 
   $selects = user_badges_image_selects();
   if (count($selects)) {
     $form['images'] = array('#tree' => TRUE);
-    foreach ($selects as $imagepath => $imageimg) {      
+    foreach ($selects as $imagepath => $imageimg) {
       $form['images'][$imagepath] = array(
         '#type' => 'checkbox',
         '#title' => $imageimg,
@@ -433,16 +447,16 @@ function user_badges_images_form($form, 
  */
 function user_badges_images_form_validate($form, &$form_state) {
   $op = $form_state['clicked_button']['#value'];
-  if ($op == t('Upload')) {    
-    $dir = 'public://badges';    
+  if ($op == t('Upload')) {
+    $dir = 'public://badges';
 
-    $is_writable = file_prepare_directory($dir, 1);    
+    $is_writable = file_prepare_directory($dir, 1);
     if ($is_writable) {
       $validators = array(
         'file_validate_extensions' => array('png jpg jpeg gif'),
       );
 
-      if ($file = file_save_upload('upload', $validators, $dir)) {                
+      if ($file = file_save_upload('upload', $validators, $dir)) {
         if (!image_get_info($file->uri)) {
           file_delete($file->uri);
           form_set_error('upload', t('Uploaded image file does not appear to be a valid image file.  Please try again'));
@@ -486,21 +500,24 @@ function user_badges_hold_temporary_file
  */
 function user_badges_images_form_submit($form, &$form_state) {
   $op = $form_state['clicked_button']['#value'];
-  // Save uploaded files.
-  if ($op == t('Upload')) {
-    $file = $form_state['values']['file_image'];
-    $file->status &= FILE_STATUS_PERMANENT;
-    $file = file_save($file);
-  }
-  elseif ($op == t('Delete')) {
-    foreach ($form_state['values']['images'] as $path => $is_removed) {
-      if ($is_removed) {
-        $to_delete[] = $path;
+  switch ($op) {
+    case t('Upload'):
+      // Save uploaded files.
+      $file = $form_state['values']['file_image'];
+      $file->status |= FILE_STATUS_PERMANENT;
+      $file = file_save($file);
+      break;
+
+    case t('Delete'):
+      foreach ($form_state['values']['images'] as $path => $is_removed) {
+        if ($is_removed) {
+          $to_delete[] = $path;
+        }
       }
-    }
-    if (is_array($to_delete)) {
-      user_badges_image_delete($to_delete);
-    }
+      if (is_array($to_delete)) {
+        user_badges_image_delete($to_delete);
+      }
+      break;
   }
 }
 
@@ -508,27 +525,25 @@ function user_badges_images_form_submit(
  * Delete the specified image
  */
 function user_badges_image_delete($to_delete) {
-  $dir = 'public://badges/';  
+  $dir = 'public://badges/';
 
-  $is_writable = file_prepare_directory($dir, 1);    
+  $is_writable = file_prepare_directory($dir, 1);
   if ($is_writable) {
-    foreach ($to_delete as $path) {  
-      $uri = $dir . $path;
-
+    foreach ($to_delete as $path) {
       $fid = db_select('file_managed', 'f')
         ->fields('f', array('fid'))
-        ->condition('f.uri', $uri)
+        ->condition('f.uri', $path)
         ->execute()
         ->fetchField();
-      
-      $file = file_load($fid);      
-      file_delete($file);  
+
+      $file = file_load($fid);
+      file_delete($file);
     }
   }
 }
 
 /**
- * form to associated badges with roles
+ * Form to associated badges with roles.
  */
 function user_badges_roles_form($form, &$form_state) {
   // No badges for the anonymous role.
@@ -563,7 +578,7 @@ function user_badges_roles_form($form, &
   foreach ($roles as $rid => $role) {
     $form['roles'][$rid] = array(
       '#type' => 'textfield',
-      '#title' => $role,
+      '#title' => check_plain($role),
       '#maxlength' => 255,
       '#autocomplete_path' => 'user_badges/autocomplete',
       '#default_value' => isset($badges[$rid]) ? $badges[$rid]->name . ' '
@@ -653,7 +668,7 @@ function user_badges_settings_form($form
     '#description' => t('If checked, only the badge associated to blocked users will be shown, overriding other badges the user eventually has as well as any other settings.') . ' <br />' .
       t('Note that if there is no badge associated to blocked users, no badges will appear.') . ' <br />' .
       t('This option only acts on blocked users and has no repercussions on active user badges.'),
-    '#attributes' => array('class' => 'container-inline'),
+    '#attributes' => array('class' => array('container-inline')),
   );
 
   $form['userweight'] = array(
@@ -663,25 +678,17 @@ function user_badges_settings_form($form
     '#default_value' => variable_get('user_badges_userweight', 0),
     '#description' => t('If checked, users will have the ability to reweight their badges in their profile, enabling them to set what order their badges display, and also which badges will display if a limit is set above.') . ' <br />' .
       t('Note that you can make individual badges exempt from this function, allowing you to force them to the top or bottom of the list by giving them a very high or low weight value.') . ' <br />',
-    '#attributes' => array('class' => 'container-inline'),
+    '#attributes' => array('class' => array('container-inline')),
   );
 
-  if (module_exists('imagecache')) {
-    $form['imagecache'] = array(
-      '#type' => 'radios',
-      '#options' => $noyes,
-      '#title' => t('Allow ImageCache to size badges'),
-      '#default_value' => variable_get('user_badges_imagecache', 0),
-      '#description' => t('The ImageCache module is available, if you choose this option, it will be used to format the badge image. The preset name is "user-badges".'),
-      '#attributes' => array('class' => 'container-inline'),
-    );
-  }
-  else {
-    $form['imagecache'] = array(
-      '#type' => 'value',
-      '#value' => 0,
+  $form['imagecache'] = array(
+    '#type' => 'select',
+    '#options' => image_style_options(),
+    '#title' => t('Image style to size badges'),
+    '#default_value' => variable_get('user_badges_imagecache', ''),
+    '#description' => t('This determines the size of the image display.'),
+    '#attributes' => array('class' => array('container-inline')),
     );
-  }
 
   $form['nobadges'] = array(
     '#type' => 'textfield',
@@ -702,6 +709,7 @@ function user_badges_settings_form($form
   );
 
   // Tokens help.
+  // @TODO: use new core tokens stuff.
   if (module_exists('token')) {
     $form['token_help'] = array(
       '#title' => t('Replacement patterns'),
@@ -736,7 +744,7 @@ function user_badges_settings_form($form
       '#default_value' => variable_get('user_badges_vid', ''),
       '#options' => $selects,
       '#description' => t('Optional. Select a vocabulary to associate badges with. You can optionally associate each badge with one term in this vocabulary.'),
-      '#attributes' => array('class' => 'container-inline'),
+      '#attributes' => array('class' => array('container-inline')),
     );
   }
 
@@ -797,17 +805,25 @@ function user_badges_settings_form_submi
  */
 function user_badges_image_selects() {
   $selects = array();
-  
-  //$dir = file_create_path('badges');
-  
+
   $path = 'public://badges';
   file_prepare_directory($path, FILE_CREATE_DIRECTORY);
   $dir = drupal_realpath($path);
 
   $files = file_scan_directory($dir, '/.*\.(((J|j)(p|P)(g|G))|((p|P)(n|N)(g|G))|((g|G)(i|I)(f|F)))/', array('recurse' => FALSE));
   foreach ($files as $file) {
-    $image = image_get_info($file->uri);    
-    $selects[$file->filename] = theme('image', array('path' => $path . '/' . $file->filename, 'width' => $image['width'], 'height' => $image['height']));
+    $image = image_get_info($file->uri);
+    $file->filepath = $path . '/' . $file->filename;
+
+    $style_name = variable_get('user_badges_imagecache', '');
+    $variables = array(
+      'style_name' => $style_name,
+      'path' => $file->filepath,
+      'width' => $image['width'],
+      'height' => $image['height'],
+      );
+    $theme = $style_name ? 'image_style' : 'image';
+    $selects[$file->filepath] = theme($theme, $variables);
   }
 
   return $selects;
