You are here

function _user_badges_images_from_library in User Badges 7.3

Same name and namespace in other branches
  1. 7 user_badges.admin.inc \_user_badges_images_from_library()
  2. 7.2 user_badges.admin.inc \_user_badges_images_from_library()

Scan the badges image directory.

Return value

An array of images from the image library, in the form $filepath => object('image' => $image_html, 'file' => $file_unmanaged)

2 calls to _user_badges_images_from_library()
user_badges_edit_form in ./user_badges.admin.inc
Define the edit form for userbadges.
user_badges_images_form in ./user_badges.admin.inc
Form to upload the badge images or to delete existing ones.

File

./user_badges.admin.inc, line 993
@brief User Badges admin functions

Code

function _user_badges_images_from_library() {
  $selects = array();
  $path = 'public://badges';
  if (file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {

    // Scan directory for images in supported formats.
    $files = file_scan_directory($path, '/.*\\.(jpe?g|png|gif)/i', array(
      'recurse' => FALSE,
    ));
    foreach ($files as $file) {
      $image = image_get_info($file->uri);
      if ($image) {
        $file->filepath = $path . '/' . $file->filename;
        $style_name = variable_get('user_badges_imagecache', '');
        $variables = array(
          'path' => $file->filepath,
        );
        if ($style_name) {
          $theme = 'image_style';
          $variables['style_name'] = $style_name;
        }
        else {
          $theme = 'image';
          $variables['width'] = $image['width'];
          $variables['height'] = $image['height'];
        }
        $selects[$file->filepath] = (object) array(
          'image' => theme($theme, $variables),
          'file' => $file,
        );

        // Ensures this file is managed in database.
        if (!_user_badges_managed_file($file->filepath)) {
          watchdog('user_badges', 'Failed to manage detected image file: @file', array(
            '@file' => $file->filepath,
          ), WATCHDOG_ERROR);
        }
      }
    }
  }
  else {

    // 'public://badges' directory could not be created or has wrong permissions.
    watchdog('user_badges', 'Scanning for badge images library failed.  Badges directory could not be created or has wrong permissions: @dir', array(
      '@dir' => $path,
    ), WATCHDOG_ERROR);
  }
  return $selects;
}