You are here

function _avatar_selection_scan_images in Avatar Selection 5.2

Same name and namespace in other branches
  1. 6 avatar_selection.admin.inc \_avatar_selection_scan_images()
  2. 7 avatar_selection.admin.inc \_avatar_selection_scan_images()

Scan the directory for new files.

Parameters

$name: The name to assign the new avatar(s).

$access: The permission - the value determines which user roles have rights to see the avatar.

$og: Organic group (optional).

$weight: Avatar weight to assign.

Return value

Number of images found.

1 call to _avatar_selection_scan_images()
avatar_selection_upload_form_submit in ./avatar_selection.module
Submit the image upload form in the Avatar Selection administration page.

File

./avatar_selection.module, line 412
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.

Code

function _avatar_selection_scan_images($name, $access, $og = array(), $weight = 0) {
  $avatars = array();
  $add_count = 0;
  $delete_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, TRUE);
  $result = db_query("SELECT avatar FROM {avatar_selection} avs");
  while ($avatar = db_fetch_object($result)) {
    $avatars[$avatar->avatar] = $avatar->avatar;
  }

  // Search for new files.  Remove matching records from avatars array.
  foreach ($listings as $listing) {
    $filename = str_replace("{$dir}/", '', $listing->filename);
    if (in_array($filename, $avatars)) {
      unset($avatars[$filename]);
    }
    else {
      _avatar_selection_save_avatar_info(0, $filename, empty($name) ? $filename : $name, $access, $og, $weight);
      $add_count++;
    }
  }

  // Remove records from database where we have an avatar entry but no
  // corresponding file.
  foreach ($avatars as $avatar) {
    avatar_selection_image_delete($avatar);
    $delete_count++;
  }
  $count['add'] = $add_count;
  $count['delete'] = $delete_count;
  return $count;
}