diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-08-22 13:52:59 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-08-22 13:52:59 +0000 |
commit | 4a7bb638fb3243a35dd2ce82a4cafc78d2f6b9d2 (patch) | |
tree | 3e17684510b739dafb359a17a1d26027fe168ec6 /modules/user/user.module | |
parent | b36d4959ef2244298fd28d02575c88b0259555b4 (diff) | |
download | brdo-4a7bb638fb3243a35dd2ce82a4cafc78d2f6b9d2.tar.gz brdo-4a7bb638fb3243a35dd2ce82a4cafc78d2f6b9d2.tar.bz2 |
- Patch #353458 by quicksketch, drewish, jpetso, sun, noahb, aaron, chx, mikey_p, dhthwy: hook_file_references() was not designed for a highly flexible field storage.
Diffstat (limited to 'modules/user/user.module')
-rw-r--r-- | modules/user/user.module | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/modules/user/user.module b/modules/user/user.module index e48b5f3e3..e85ceff35 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -427,7 +427,7 @@ function user_save($account, $edit = array(), $category = 'account') { } // Process picture uploads. - if (!empty($edit['picture']->fid)) { + if (!$delete_previous_picture = empty($edit['picture']->fid)) { $picture = $edit['picture']; // If the picture is a temporary file move it to its final location and // make it permanent. @@ -439,12 +439,22 @@ function user_save($account, $edit = array(), $category = 'account') { file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY); $destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '-' . REQUEST_TIME . '.' . $info['extension']); + // Move the temporary file into the final location. if ($picture = file_move($picture, $destination, FILE_EXISTS_RENAME)) { + $delete_previous_picture = TRUE; $picture->status = FILE_STATUS_PERMANENT; $edit['picture'] = file_save($picture); + file_usage_add($picture, 'user', 'user', $account->uid); } } } + + // Delete the previous picture if it was deleted or replaced. + if ($delete_previous_picture && !empty($account->picture->fid)) { + file_usage_delete($account->picture, 'user', 'user', $account->uid); + file_delete($account->picture); + } + $edit['picture'] = empty($edit['picture']->fid) ? 0 : $edit['picture']->fid; // Do not allow 'uid' to be changed. @@ -457,13 +467,6 @@ function user_save($account, $edit = array(), $category = 'account') { return FALSE; } - // If the picture changed or was unset, remove the old one. This step needs - // to occur after updating the {users} record so that user_file_references() - // doesn't report it in use and block the deletion. - if (!empty($account->picture->fid) && ($edit['picture'] != $account->picture->fid)) { - file_delete($account->picture); - } - // Reload user roles if provided. if (isset($edit['roles']) && is_array($edit['roles'])) { db_delete('users_roles') @@ -829,15 +832,18 @@ function user_file_download($uri) { } /** - * Implements hook_file_references(). + * Implements hook_file_move(). */ -function user_file_references($file) { - // Determine if the file is used by this module. - $file_used = (bool) db_query_range('SELECT 1 FROM {users} WHERE picture = :fid', 0, 1, array(':fid' => $file->fid))->fetchField(); - if ($file_used) { - // Return the name of the module and how many references it has to the file. - // If file is still used then 1 is enough to indicate this. - return array('user' => 1); +function user_file_move($file, $source) { + // If a user's picture is replaced with a new one, update the record in + // the users table. + if (isset($file->fid) && isset($source->fid) && $file->fid != $source->fid) { + db_update('users') + ->fields(array( + 'picture' => $file->fid, + )) + ->condition('picture', $source->fid) + ->execute(); } } |