Permalink
Browse files

feature(icons): user avatars are now handled by the icon service

Adds a handler to update icon file location to its conventional placement
under /profile.
Updates avatar upload/crop/remove actions to use new icon service
  • Loading branch information...
hypeJunction authored and mrclay committed Apr 11, 2016
1 parent 72b8a2c commit 36c8b465e0f8c39a1f83f74fb9cf3562d1467c17
View
@@ -12,58 +12,24 @@
forward(REFERER);
}
$x1 = (int) get_input('x1', 0);
$y1 = (int) get_input('y1', 0);
$x2 = (int) get_input('x2', 0);
$y2 = (int) get_input('y2', 0);
$filehandler = new ElggFile();
$filehandler->owner_guid = $owner->getGUID();
$filehandler->setFilename("profile/" . $owner->guid . "master" . ".jpg");
$filename = $filehandler->getFilenameOnFilestore();
$coords = [
'x1' => (int) get_input('x1', 0),
'y1' => (int) get_input('y1', 0),
'x2' => (int) get_input('x2', 0),
'y2' => (int) get_input('y2', 0),
];
// ensuring the avatar image exists in the first place
if (!file_exists($filename)) {
if (!$owner->hasIcon('master')) {
register_error(elgg_echo('avatar:crop:fail'));
forward(REFERER);
}
$icon_sizes = elgg_get_config('icon_sizes');
unset($icon_sizes['master']);
// get the images and save their file handlers into an array
// so we can do clean up if one fails.
$files = array();
foreach ($icon_sizes as $name => $size_info) {
$resized = get_resized_image_from_existing_file($filename, $size_info['w'], $size_info['h'], $size_info['square'], $x1, $y1, $x2, $y2, $size_info['upscale']);
if ($resized) {
//@todo Make these actual entities. See exts #348.
$file = new ElggFile();
$file->owner_guid = $guid;
$file->setFilename("profile/{$guid}{$name}.jpg");
$file->open('write');
$file->write($resized);
$file->close();
$files[] = $file;
} else {
// cleanup on fail
foreach ($files as $file) {
$file->delete();
}
register_error(elgg_echo('avatar:resize:fail'));
forward(REFERER);
}
if (!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) {
register_error(elgg_echo('avatar:crop:fail'));
forward(REFERER);
}
$owner->icontime = time();
$owner->x1 = $x1;
$owner->x2 = $x2;
$owner->y1 = $y1;
$owner->y2 = $y2;
system_message(elgg_echo('avatar:crop:success'));
$view = 'river/user/default/profileiconupdate';
elgg_delete_river(array('subject_guid' => $owner->guid, 'view' => $view));
View
@@ -11,26 +11,7 @@
forward(REFERER);
}
// Delete all icons from diskspace
$icon_sizes = elgg_get_config('icon_sizes');
foreach ($icon_sizes as $name => $size_info) {
$file = new ElggFile();
$file->owner_guid = $user_guid;
$file->setFilename("profile/{$user_guid}{$name}.jpg");
$filepath = $file->getFilenameOnFilestore();
if (!$file->delete()) {
elgg_log("Avatar file remove failed. Remove $filepath manually, please.", 'WARNING');
}
}
// Remove crop coords
unset($user->x1);
unset($user->x2);
unset($user->y1);
unset($user->y2);
// Remove icon
unset($user->icontime);
$user->deleteIcon();
system_message(elgg_echo('avatar:remove:success'));
forward(REFERER);
View
@@ -17,41 +17,11 @@
forward(REFERER);
}
$icon_sizes = elgg_get_config('icon_sizes');
// get the images and save their file handlers into an array
// so we can do clean up if one fails.
$files = array();
foreach ($icon_sizes as $name => $size_info) {
$resized = get_resized_image_from_uploaded_file('avatar', $size_info['w'], $size_info['h'], $size_info['square'], $size_info['upscale']);
if ($resized) {
//@todo Make these actual entities. See exts #348.
$file = new ElggFile();
$file->owner_guid = $guid;
$file->setFilename("profile/{$guid}{$name}.jpg");
$file->open('write');
$file->write($resized);
$file->close();
$files[] = $file;
} else {
// cleanup on fail
foreach ($files as $file) {
$file->delete();
}
register_error(elgg_echo('avatar:resize:fail'));
forward(REFERER);
}
if (!$owner->saveIconFromUploadedFile('avatar')) {
register_error(elgg_echo('avatar:resize:fail'));
forward(REFERER);
}
// reset crop coordinates
$owner->x1 = 0;
$owner->x2 = 0;
$owner->y1 = 0;
$owner->y2 = 0;
$owner->icontime = time();
if (elgg_trigger_event('profileiconupdate', $owner->type, $owner)) {
system_message(elgg_echo("avatar:upload:success"));
View
@@ -752,6 +752,26 @@ function users_pagesetup() {
}
}
/**
* Set user icon file
*
* @param string $hook "entity:icon:file"
* @param string $type "user"
* @param \ElggIcon $icon Icon file
* @param array $params Hook params
* @return \ElggIcon
*/
function _elgg_user_set_icon_file($hook, $type, $icon, $params) {
$entity = elgg_extract('entity', $params);
$size = elgg_extract('size', $params, 'medium');
$icon->owner_guid = $entity->guid;
$icon->setFilename("profile/{$entity->guid}{$size}.jpg");
return $icon;
}
/**
* Users initialisation function, which establishes the page handler
*
@@ -787,6 +807,8 @@ function users_init() {
elgg_register_plugin_hook_handler('register', 'menu:entity', 'elgg_users_setup_entity_menu', 501);
elgg_register_event_handler('create', 'user', 'user_create_hook_add_site_relationship');
elgg_register_plugin_hook_handler('entity:icon:file', 'user', '_elgg_user_set_icon_file');
}
/**
@@ -17,15 +17,16 @@
$user = elgg_extract('entity', $vars, elgg_get_logged_in_user_entity());
$size = elgg_extract('size', $vars, 'medium');
$icon_sizes = elgg_get_config('icon_sizes');
if (!array_key_exists($size, $icon_sizes)) {
$size = 'medium';
}
if (!($user instanceof ElggUser)) {
return;
}
$icon_sizes = elgg_get_icon_sizes('user');
if (!array_key_exists($size, $icon_sizes)) {
$size = 'medium';
}
$name = htmlspecialchars($user->name, ENT_QUOTES, 'UTF-8', false);
$username = $user->username;
@@ -20,7 +20,7 @@
$content = elgg_view('core/avatar/upload', array('entity' => $entity));
// only offer the crop view if an avatar has been uploaded
if (isset($entity->icontime)) {
if ($entity->hasIcon('master')) {
$content .= elgg_view('core/avatar/crop', array('entity' => $entity));
}

0 comments on commit 36c8b46

Please sign in to comment.