function _avatar_selection_scan_images in Avatar Selection 7
Same name and namespace in other branches
- 5.2 avatar_selection.module \_avatar_selection_scan_images()
- 6 avatar_selection.admin.inc \_avatar_selection_scan_images()
Scan the directory for new files.
Parameters
$name: The name of the 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.admin.inc - Submit the image upload form in the Avatar Selection administration page.
File
- ./
avatar_selection.admin.inc, line 24 - Administrative page callbacks for the avatar_selection module.
Code
function _avatar_selection_scan_images($name, $access, $og = array(), $weight = 0) {
$avatars = array();
$add_count = 0;
$delete_count = 0;
$dir = file_build_uri('avatar_selection');
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
$mask = '/.*\\.(gif|GIF|Gif|jpg|JPG|Jpg|jpeg|JPEG|Jpeg|png|PNG|Png)/';
$listings = file_scan_directory($dir, $mask);
$result = db_select('avatar_selection', 'avs')
->fields('avs', array(
'avatar',
))
->execute();
foreach ($result as $avatar) {
$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 {
// Manage the file and add it to the avatar table.
$file = file_save(_avatar_selection_uri_to_object($dir . '/' . $listing->filename));
_avatar_selection_save_avatar_info(0, $filename, empty($name) ? $filename : $name, $access, $og, $weight, $file->fid);
$add_count++;
}
}
// Remove records from database where we have an avatar entry but no
// corresponding file.
foreach ($avatars as $avatar) {
$file = _avatar_selection_uri_to_object($dir . '/' . $avatar, TRUE);
avatar_selection_image_delete($file->fid);
$delete_count++;
}
$count['add'] = $add_count;
$count['delete'] = $delete_count;
return $count;
}