Contributions API

Calling all Drupal developers!

Help us get this on the first page of Digg. DIGG NOW!

Modules in 6

avatar_selection.module

<?php
// $Id: avatar_selection.module,v 1.1.2.22.2.31 2008/04/28 15:08:55 snpower Exp $

/**
 * @file
 * The Avatar Selection module allows the user to pick an avatar image from a
 * list already loaded by an administrative user, and to the administrator to
 * disable uploading other avatar files by the user.
 */


/**
 * Implementation of hook_help().
 */
function avatar_selection_help($path, $arg) {

  $output = '';

  switch ($path) {
    case "admin/help#avatar_selection":
      $output .= '<p>'. t("Allows the user to pick an avatar from a list.") .'</p>';
      return $output;
    case "admin/modules#description":
      return t("Allows the user to pick an avatar from a list.");
    case "admin/settings/avatar_selection/images":
      return t("Upload images to display as a user avatar.  These images can be anything you like, but it is recommended that you maintain a uniform icon size for all of your avatars.  Maximum dimensions are 85x85 and the maximum size is 30 kB.");
  }

}

/**
 * Implementation of hook_perm().
 *
 * Define the permissions this module uses.
 */
function avatar_selection_perm() {
  return array('administer avatar selection', 'access avatars');
}


/**
 * Implementation of hook_access().
 *
 * Define what does each permission means :
 * 'view' is included in the 'access avatars' permission; while 'create',
 * 'update' and 'delete' are included in the 'administrer avatar selection'.
 *
 * @param $op
 *   The action the user wants to do after the function checks the permission.
 * @param $node
 *   The node where the specific permission is requested.
 * @return
 *   The access needed to perform a certain operation.
 */
function avatar_selection_access($op, $node, $account) {
  if ($op == 'view') {
    return user_access('access avatars');
  }
  else if ($op == 'create' || $op == 'update' || $op == 'delete') {
    return user_access('administer avatar selection');
  }
}

/**
 * Implementation of hook_menu().
 */
function avatar_selection_menu() {
  $items = array();

  $items['admin/settings/avatar_selection'] = array(
    'title' => 'Avatar Selection',
    'description' => 'Allows the user to upload and delete avatars.',
    'page callback' => 'avatar_selection_settings_page',
    'access callback' => 'user_access',
    'access arguments' => array('administer avatar selection'),
  );
  $items['admin/settings/avatar_selection/config'] = array(
    'title' => 'Configure',
    'description' => 'Allows the user to configure avatar settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('avatar_selection_config_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer avatar selection'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/settings/avatar_selection/upload'] = array(
    'title' => 'Upload',
    'description' => 'Allows the user to upload avatars.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('avatar_selection_upload_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer avatar selection'),
    'type' => MENU_LOCAL_TASK,
    'weight' => -9,
  );
  $items['admin/settings/avatar_selection/edit'] = array(
    'title' => 'Manage avatars',
    'description' => 'Allows the user to modify or delete an avatar from a list.',
    'page callback' => 'avatar_selection_roles_page',
    'access callback' => 'user_access',
    'access arguments' => array('administer avatar selection'),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/settings/avatar_selection/edit/role/%'] = array(
    'title' => 'Manage avatars',
    'description' => 'Allows the user to modify or delete an avatar from a list.',
    'page callback' => 'avatar_selection_roles_page',
    'page arguments' => array('op' => 'role'),
    'access callback' => 'user_access',
    'access arguments' => array('administer avatar selection'),
  );
  if (module_exists("og")) {
    $items['admin/settings/avatar_selection/edit/og/%'] = array(
      'title' => 'Manage avatars',
      'description' => 'Allows the user to modify or delete an avatar from a list.',
      'callback' => 'avatar_selection_roles_page',
      'callback arguments' => array('op' => 'og'),
      'access callback' => 'user_access',
      'access arguments' => array('administer avatar selection'),
    );
  }

  return $items;
}

/**
 * Implementation of hook_form_alter().
 *
 * Create the form structure for adding an avatar in the user registration page.
 *
 * @param &$form
 *   General reference used in drupal, defining the structure & the fields of
 *   a form.
 * @param $form_state
 *   General variable, used to control the processing of the form.
 * @param $form_id
 *   The default is "user_register"; hold the page where the function is being
 *   used.
 * @return
 *   Return the structure of the form.
 */
function avatar_selection_form_user_register_alter(&$form, $form_state, $form_id="user_register") {

  // If user pictures aren't enabled, nothing to do here.
  if (!variable_get('user_pictures', 0)) {
    return;
  }

  $anon_user = drupal_anonymous_user();

  // See if user has access to avatars.
  $disable_upload = variable_get('avatar_selection_disable_user_upload', 0);
  if (!user_access('access avatars')) {
    // If uploads also disabled, remove the field altogether.
    if ($disable_upload) {
      unset($form['picture']);
    }
    return;
  }

  $force_choose = variable_get('avatar_selection_force_user_avatar_reg', 0);
  $selects = _avatar_selection_image_list($anon_user);
  if (count($selects['avatars'])) {
    drupal_add_css(drupal_get_path('module', 'avatar_selection') .'/avatar_selection.css');
    $form['picture'] = array(
      '#type' => 'fieldset',
      '#title' => t('Picture'),
      '#weight' => 1,
    );
    $form['picture']['select_avatar'] = array(
      '#type' => 'radios',
      '#title' => t('Select an avatar'),
      '#options' => $selects['avatars'],
      '#required' => $force_choose ? TRUE : FALSE,
      '#attributes' => array('class' => 'user_avatar_select'),
    );
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
  }
  else {
    $form['#validate'][] = 'avatar_selection_validate_user_avatar';
  }
  $images_per_page = variable_get('avatar_selection_avatar_per_page', 30);
  $js_settings = array(
    'num_images_per_page' => ($images_per_page ? $images_per_page : 1),
    'num_images' => $selects['total'],
    'image_url' => file_create_url(file_create_path('avatar_selection')),
    'images' => $selects['avatars_indexed'],
  );
  drupal_add_js(array('avatar_selection' => $js_settings), 'setting');
  drupal_add_js(drupal_get_path('module', 'avatar_selection') .'/js/avatar_selection.js', 'module', 'header');

  return $form;
}


/**
 * Implementation of hook_form_alter().
 *
 * Create the form structure for adding an avatar in the user profile page.
 *
 * @param &$form
 *   General reference used in drupal, defining the structure & the fields of
 *   a form.
 * @param $form_state
 *   General variable, used to control the processing of the form.
 * @param $form_id
 *   The default is "user_profile_form"; holds the form name.
 * @return
 *   Return the structure of the form.
 */
function avatar_selection_form_user_profile_form_alter(&$form, $form_state, $form_id="user_profile_form") {

  // If user pictures aren't enabled, nothing to do here.
  if (!variable_get('user_pictures', 0)) {
    return;
  }
  $user = user_load(array("uid" => $form['#uid']));

  // See if user has access to avatars.
  $disable_upload = variable_get('avatar_selection_disable_user_upload', 0);
  if (!user_access('access avatars')) {
    // If uploads also disabled, remove the field altogether.
    if ($disable_upload) {
      unset($form['picture']);
    }
    return;
  }

  if (is_array($form['picture'])) {
    drupal_add_css(drupal_get_path('module', 'avatar_selection') .'/avatar_selection.css');
    $force_choose = variable_get('avatar_selection_force_user_avatar', 0);

    // If upload support has been disabled, remove the ability to upload and
    // delete pictures.
    if ($disable_upload) {
      unset($form['picture']['picture_delete']);
      unset($form['picture']['picture_upload']);
    }
    else {
      $force_choose = 0;
    }

    // Show selection options.
    if (!isset($user->og_groups)) $user->og_groups = '';
    $selects = _avatar_selection_image_list($user);

    if (count($selects['avatars'])) {
      $current_avatar = basename($user->picture);
      $form['picture']['select_avatar'] = array(
        '#type' => 'radios',
        '#title' => $disable_upload ? t('Select an avatar') : t('Or simply select an icon'),
        '#options' => $selects['avatars'],
        '#default_value' => array_key_exists($current_avatar, $selects['avatars']) ? $current_avatar : '',
        '#required' => $force_choose ? TRUE : FALSE,
        '#attributes' => array('class' => 'user_avatar_select'),
      );
      if (array_key_exists($current_avatar, $selects['avatars'])) {
        $form['_account']['#value']->picture = null;
      }
      $form['#validate'][] = 'avatar_selection_validate_user_avatar';
    }
    else {
      $form['#validate'][] = 'avatar_selection_validate_user_avatar';
    }

    // Don't allow user to delete a selected avatar.
    $path = '/avatar_selection/';
    if (!empty($form['picture']['current_picture']['#value'])
      && preg_match($path, $form['picture']['current_picture']['#value'])) {
      unset($form['picture']['picture_delete']);
    }

    $images_per_page = variable_get('avatar_selection_avatar_per_page', 30);
    $js_settings = array(
      'num_images_per_page' => ($images_per_page ? $images_per_page : 1),
      'num_images' => $selects['total'],
      'image_url' => file_create_url(file_create_path('avatar_selection')),
      'images' => $selects['avatars_indexed'],
    );
    drupal_add_js(array('avatar_selection' => $js_settings), 'setting');
    drupal_add_js(drupal_get_path('module', 'avatar_selection') .'/js/avatar_selection.js', 'module', 'header');
  }

  return $form;
}

/**
 * Validate and upload the user's picture.
 *
 * @param &$form
 *   General reference used in drupal, defining the structure & the fields of a
 *   form.
 * @param &$form_state
 *   General reference, used to control the processing of the form.
 */
function avatar_selection_validate_user_avatar(&$form, &$form_state) {
  // If required, validate the uploaded picture.
  $validators = array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
    'file_validate_size' => array(variable_get('user_picture_file_size', 30) * 1024),
  );

  $file = file_save_upload('picture_upload', $validators);

  if (!$file && !empty($form_state['values']['select_avatar'])) {
    $path = file_create_path('avatar_selection') .'/';
    $form_state['values']['picture'] = $path . $form_state['values']['select_avatar'];
  }

  else if (!$file && variable_get('avatar_selection_set_random_default', FALSE)) {

    if (isset($form_state['values']['_account'])) {
      $user = $form_state['values']['_account'];
    }
    else {
      $user = drupal_anonymous_user();
    }
    $form_state['values']['picture'] = avatar_selection_get_random_image($user);
  }
}

/**
 * Select a random avatar picture for a certain user.
 *
 * @param $user
 *   User object.
 * @return
 *   Return the path to the image to be shown as avatar.
 */
function avatar_selection_get_random_image($user) {
  $avatars = _avatar_selection_image_list($user);
  if ($avatars['total'] > 0) {
    $avatar = array_rand($avatars['avatars'], 1);
    return $avatar;
  }
  return ' ';
}


/**
 * Get the list of avatars available to a certain user.
 *
 * @param $user
 *   User object (optional).
 * @param $set_type
 *   Set type, can be 'role' or 'og' (optional).
 * @param $set_id
 *   The unique identifier of the set (optional).
 * @return
 *   Return an array with the list of avatars for the current user, together
 *   with the number of avatars.
 */
function _avatar_selection_image_list($user = "", $set_type = "", $set_id = 0) {
  $avatars = array();
  $avatars_indexed = array();

  $result = db_query("SELECT avatar, access, og_access FROM {avatar_selection} avs");
  while ($avatar = db_fetch_object($result)) {
    $avs_image = $avatar->avatar;
    $avs_access = preg_split('/\s*,\s*/', $avatar->access);
    $og_access = preg_split('/\s*,\s*/', $avatar->og_access);
    $del_avatar = 1;

    $avatars[$avs_image] = "";

    // Check the organic groups.
    if (module_exists("og")) {
      if (!empty($avatar->og_access)) {
        if ($set_type == 'og' && $set_id != 0) {
          if (in_array($set_id, $og_access)) {
            $del_avatar = 2;
          }
        }
        else if (!empty($user->og_groups) && is_array($user->og_groups)) {
          foreach ($user->og_groups as $gid => $grp) {
            if (in_array($gid, $og_access)) {
              $del_avatar = 2;
              break;
            }
          }
        }
        // Delete it if it's not in one of the valid groups.
        if ($del_avatar == 1 && !empty($user) && $user->uid != 1) {
          unset($avatars[$avs_image]);
        }
        else if ($del_avatar == 1 && $set_type == 'og') {
          unset($avatars[$avs_image]);
        }
      }
      // Delete it if no groups are configured and we're searching on a
      // specific group id.
      else if ($set_type == 'og' && $set_id != 0) {
        unset($avatars[$avs_image]);
      }
    }

    // Check the user roles.
    if (!empty($avatar->access)) {
      if ($set_type == 'role' && $set_id != 0) {
        if (in_array($set_id, $avs_access)) {
          $del_avatar = 0;
        }
      }
      else if (!empty($user->roles) && is_array($user->roles)) {
        foreach ($user->roles as $rid => $role) {
          if (in_array($rid, $avs_access)) {
            $del_avatar = 0;
            break;
          }
        }
      }
      // Delete it if it's not in a valid role.
      if ($del_avatar != 0 && !empty($user) && $user->uid != 1) {
        unset($avatars[$avs_image]);
      }
      else if ($del_avatar != 0 && $set_type == 'role') {
        unset($avatars[$avs_image]);
      }
    }
    // Delete it if no roles are configured and we're searching on a
    // specific role id.
    else if ($set_type == 'role' && $set_id != 0) {
      unset($avatars[$avs_image]);
    }
  }

  // Remove ones already in use if necessary.
  if (!empty($user) && variable_get('avatar_selection_distinctive_avatars', FALSE)) {
    $dir = file_create_path('avatar_selection');
    $result = db_query("SELECT DISTINCT avs.avatar FROM {avatar_selection} avs LEFT JOIN {users} u ON u.picture = concat('%s/', avs.avatar) WHERE u.picture IS NOT NULL AND u.uid <> %d", $dir, $user->uid);
    while ($avatar = db_fetch_object($result)) {
      $avs_image = $avatar->avatar;
      unset($avatars[$avs_image]);
    }
  }

  $total_avatars = count($avatars);

  $c = 0;
  foreach ($avatars as $image => $value) {
    $avatars_indexed[$c] = $image;
    $c++;
  }

  $selects['avatars_indexed'] = $avatars_indexed;
  $selects['avatars'] = $avatars;
  $selects['total'] = $total_avatars;

  return $selects;
}

/**
 * Scan the directory for new files.
 *
 * @param $access
 *   The permission - the value determines which user roles have rights to see
 *   the avatar.
 * @param $og
 *   Organic group (optional).
 * @return
 *   Number of images found.
 */
function _avatar_selection_scan_images($access, $og = "") {
  $avatars = array();
  $count = 0;

  $dir = file_create_path('avatar_selection');
  $mask = '.*\.(gif|GIF|Gif|jpg|JPG|Jpg|jpeg|JPEG|Jpeg|png|PNG|Png)';
  $listings = file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, FALSE);

  $result = db_query("SELECT avatar FROM {avatar_selection} avs");
  while ($avatar = db_fetch_object($result)) {
    $avatars[$avatar->avatar] = $avatar->avatar;
  }

  foreach ($listings as $listing) {
    if (in_array($listing->basename, $avatars)) {
      unset($avatars[$listing->basename]);
    }
    else {
      _avatar_selection_save_avatar_info($listing->basename, $access, $og);
      $count++;
    }
  }

  return $count;
}


/**
 * Select which form will be shown to the user, according to the permissions.
 *
 * @param $op
 *   Default NULL; the action the user wants to do after the function checks
 &   the permission.
 * @return
 *   Return the structure of the form.
 */
function avatar_selection_settings_page($op = NULL) {

  switch ($op) {
    case 'edit':
      $output = drupal_get_form('avatar_selection_config_form');
      break;
    case 'upload':
      $output = drupal_get_form('avatar_selection_upload_form');
      break;
    case 'list' :
      $output = drupal_get_form('avatar_selection_edit_form');
      break;
    default:
      $form[] = array(
        '#type' => 'fieldset',
        '#title' => t('Add another'),
      );
      $output = drupal_get_form('avatar_selection_config_form');
      break;
  }
  return $output;
}


/**
 * Create the form structure for configuring the avatar module settings; seen
 * in the 'Configure' tab under the Avatar Selection administration page.
 *
 * @return
 *   Return the structure of the form.
 */
function avatar_selection_config_form() {
  if (!variable_get('user_pictures', 0)) {
    drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting on the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  }

  // To store how many avatars per page are displayed.
  $form['update']['avatar_per_page'] = array(
    '#type' => 'textfield',
    '#title' => t('How many avatars per page'),
    '#description' => t('The number of avatars to show per page.'),
    '#default_value' => variable_get('avatar_selection_avatar_per_page', 30),
    '#size' => 3,
  );

  $form['update']['disable_user_upload'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable users uploading pictures to profile'),
    '#description' => t('Allow users to pick their avatar from the selection but prevent them from uploading new avatars when editing their account.'),
    '#default_value' => variable_get('avatar_selection_disable_user_upload', FALSE),
    );
  $form['update']['force_set_image_reg'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force users to select an avatar image on user registration.'),
    '#description' => t('This only applies on the user registration screen.'),
    '#default_value' => variable_get('avatar_selection_force_user_avatar_reg', FALSE),
    );
  $form['update']['force_set_image'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force users to select an avatar image when editing their account'),
    '#description' => t('This only applies when the user is editing their account details and image uploads are disabled.'),
    '#default_value' => variable_get('avatar_selection_force_user_avatar', FALSE),
    );
  $form['update']['set_random_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable random default avatar image.'),
    '#description' => t('Automatically set a random avatar image to be used when the user doesn\'t set one for their account.'),
    '#default_value' => variable_get('avatar_selection_set_random_default', FALSE),
    );
  $form['update']['distinctive_avatars'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable distinctive avatars.'),
    '#description' => t('Only allow users to pick an avatar that isn\'t already in use by another user.  If there are no available avatars left, the default avatar image will be used.'),
    '#default_value' => variable_get('avatar_selection_distinctive_avatars', FALSE),
    );


  $form['update']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  return $form;
}

/**
 * Create the form structure for uploading an avatar.
 *
 * @return
 *   Return the structure of the form.
 */
function avatar_selection_upload_form() {
  if (!variable_get('user_pictures', 0)) {
    drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting on the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  }

  $form['#attributes']['enctype'] = 'multipart/form-data';

  $form['picture_upload'] = array('#type' => 'file',
    '#title' => t('Upload image'),
    '#size' => 48,
    '#description' => t('A new avatar image.  Maximum dimensions are %dimensions and the maximum size is %size kB.  Images must have one of the following extensions (case sensitive): png, jpg, jpeg, gif, PNG, JPG, JPEG, GIF.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', 30))) .' '.  variable_get('user_picture_guidelines', ''),
  );

  $form['bulk_upload'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bulk Upload'),
    '#description' => t('To upload a large number of avatars, first copy the images manually to the %dir folder, using ftp for example.  To make these new avatar images available, check the \'Scan for new avatars\' option. All new images will then be added to the list.', array('%dir' => file_create_path('avatar_selection'))),
    '#weight' => 1,
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
  );
  $form['bulk_upload']['scan_avatars'] = array(
    '#type' => 'checkbox',
    '#title' => t('Scan for new avatars'),
    '#description' => t('All new avatar images found will be added to the list of available avatars with the permissions defined below.  Scanning for new avatars may be slow depending on the number of files.'),
    '#default_value' => 0,
  );


  $form['permissions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Permissions'),
    '#weight' => 2,
  );
  $form['permissions']['access'] = array(
    '#type' => 'checkboxes',
    '#title' => t('User Roles'),
    '#options' => avatar_selection_handler_filter_role(),
    '#description' => t('Only the checked roles will be able to see this avatar icon; if no roles are checked, access will not be restricted.'),
  );
  if (module_exists("og")) {
    $form['permissions']['og_access'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Organic Groups'),
      '#options' => og_all_groups_options(),
      '#description' => t('Only users in the checked organic groups will be able to see this avatar icon; if no groups are checked, access will not be restricted.'),
    );
  }

  $form['upload'] = array(
    '#type' => 'submit',
    '#value' => t('Upload'),
    '#weight' => 10,
  );

  return $form;
}

function avatar_selection_roles_page($op = NULL) {
  $output = "";

  // Display the form where appropriate.
  if (isset($op) && ($op == 'role' || $op == 'og')) {
    $output .= drupal_get_form('avatar_selection_edit_form');
  }

  // Display the number of avatars per role / group.
  else {
    $avs_access = array();
    $og_access = array();
    $avs_access[0] = 0;
    $og_access[0] = 0;

    // Get the list of access roles and initialise the count to 0.
    $roles = avatar_selection_handler_filter_role();
    foreach ($roles as $rid => $role_name) {
      $avs_access[$rid] = 0;
    }

    // Get the list of organic groups and initialise the count to 0.
    if (module_exists("og")) {
      $ogroups = og_all_groups_options();
      foreach ($ogroups as $ogid => $ogroup_name) {
        $og_access[$ogid] = 0;
      }
    }

    // Get the total number of avatars available on the system.
    $total_count = 0;
    $result = db_query("SELECT count(*) AS count FROM {avatar_selection} avs");
    while ($avatar = db_fetch_object($result)) {
      $total_count = $avatar->count;
    }
    $output .= '<p>'. t('There is a total of %count avatars configured.', array('%count' => $total_count)) .'</p>';


    // Get the count of avatars per role and organic group.
    $result = db_query("SELECT avs.access, avs.og_access, count(*) AS count FROM {avatar_selection} avs GROUP BY avs.access, avs.og_access");
    while ($avatar = db_fetch_object($result)) {
      $rids = preg_split('/\s*,\s*/', $avatar->access);
      $ogids = preg_split('/\s*,\s*/', $avatar->og_access);

      // Handle role access.
      if (empty($avatar->access)) {
        $avs_access[0] += $avatar->count;
      }
      foreach ($rids as $rid) {
        $avs_access[$rid] += $avatar->count;
      }

      // Handle organic groups access.
      if (empty($avatar->og_access)) {
        $og_access[0] += $avatar->count;
      }
      foreach ($ogids as $ogid) {
        $og_access[$ogid] += $avatar->count;
      }
    }


    // Format the user roles table.
    $avs_rows = array();
    $header = array(t('User Role'), t('Number of Avatars'));
    $edit = l(t('edit'), 'admin/settings/avatar_selection/edit/role/0');
    $avs_rows[] = array(t('Available to all roles'), $avs_access[0], $edit);
    foreach ($roles as $rid => $role_name) {
      $edit = l(t('edit'), 'admin/settings/avatar_selection/edit/role/'. $rid);
      $avs_rows[] = array($role_name, $avs_access[$rid], $edit);
    }
    $output .= theme('table', $header, $avs_rows);


    // Format the organic groups table.
    if (module_exists("og")) {
      $og_rows = array();
      $header = array(t('Organic Group'), t('Number of Avatars'));
      $edit = l(t('edit'), 'admin/settings/avatar_selection/edit/og/0');
      $og_rows[] = array(t('Available to all groups'), $og_access[0], $edit);
      foreach ($ogroups as $ogid => $ogroup_name) {
        $edit = l(t('edit'), 'admin/settings/avatar_selection/edit/og/'. $ogid);
        $og_rows[] = array($ogroup_name, $og_access[$ogid], $edit);
      }
      $output .= theme('table', $header, $og_rows);
    }
  }

  return $output;
}


/**
 * Create the form structure for listing the avatars and managing them in the
 * 'Manage Avatars' tab under the Avatar Selection administration page.
 *
 * @param $form_state
 *   General variable, used to control the processing of the form.
 * @return
 *   Return the structure of the form.
 */
function avatar_selection_edit_form($form_state) {
  $form = array();

  if (!variable_get('user_pictures', 0)) {
    drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting on the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  }

  drupal_add_css(drupal_get_path('module', 'avatar_selection') .'/avatar_selection.css');

  $set_type = arg(4);
  $set_id = arg(5);

  $selects = _avatar_selection_image_list("", $set_type, $set_id);
  if (!count($selects['avatars'])) {
    drupal_set_message(t('There are no avatars configured.'));
  }

  else {

    if (!isset($form_state['values'])) {
      $step = "list";
    }
    else {
      $step = "edit";
    }
    $form['step'] = array(
      '#type' => 'value',
      '#value' => $step,
    );


    if ($step == "list") {

      if ($set_type == 'role') {
        $sets = avatar_selection_handler_filter_role();
      }
      else if ($set_type == 'og') {
        $sets = og_all_groups_options();
      }
      drupal_set_title(t('Manage Avatars - %name', array('%name' => $sets[$set_id])));

      $form['select_avatar'] = array(
        '#type' => 'radios',
        '#title' => t('Select an avatar to edit'),
        '#options' => $selects['avatars'],
        '#required' => TRUE,
        '#attributes' => array('class' => 'user_avatar_select'),
      );

      $form['search'] = array(
        '#type' => 'submit',
        '#value' => t('Edit'),
        '#submit' => array('avatar_selection_edit_list_form_submit'),
      );

      $images_per_page = variable_get('avatar_selection_avatar_per_page', 30);
      $js_settings = array(
        'num_images_per_page' => ($images_per_page ? $images_per_page : 1),
        'num_images' => $selects['total'],
        'image_url' => file_create_url(file_create_path('avatar_selection')),
        'images' => $selects['avatars_indexed'],
      );
      drupal_add_js(array('avatar_selection' => $js_settings), 'setting');
      drupal_add_js(drupal_get_path('module', 'avatar_selection') .'/js/avatar_selection.js', 'module', 'header');

    }
    else if ($step == "edit") {
      drupal_set_title(t('Manage Avatars'));
      $form['#redirect'] = array('admin/settings/avatar_selection/edit');
      $roles = avatar_selection_handler_filter_role();
      $avs_access = $og_access = array();

      $result = db_query("SELECT avatar, access, og_access FROM {avatar_selection} avs WHERE avatar = '%s'", $form_state['values']['select_avatar']);
      while ($avatar = db_fetch_object($result)) {
        $avs_access = preg_split('/\s*,\s*/', $avatar->access);
        $og_access = preg_