Skip to content

Commit

Permalink
Fix user photo upload and management
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Feb 6, 2019
1 parent c88fe12 commit b678866
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
35 changes: 18 additions & 17 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ public function afterSave($data, $settings)
if ($groupsIds) {
Craft::$app->users->assignUserToGroups($this->element->id, $groupsIds);
}

if ($profilePhoto) {
$filename = basename($profilePhoto);

Craft::$app->users->saveUserPhoto($profilePhoto, $this->element, $filename);
}
}

public function disable($elementIds)
Expand Down Expand Up @@ -197,7 +191,7 @@ protected function parseGroups($feedData, $fieldInfo)
return $groupIds;
}

protected function parsePhoto($feedData, $fieldInfo)
protected function parsePhotoId($feedData, $fieldInfo)
{
$value = $this->fetchSimpleValue($feedData, $fieldInfo);

Expand All @@ -218,26 +212,33 @@ protected function parsePhoto($feedData, $fieldInfo)
}
}

// See if its a default asset
if (is_array($value) && isset($value[0])) {
return $value[0];
}

$folderId = $this->_prepareUserPhotosFolder($this->element);

// Search anywhere in Craft
$foundElement = AssetElement::findOne(['filename' => $value]);
$foundElement = AssetElement::find()
->filename($value)
->folderId($folderId)
->one();

// Do we want to match existing elements, and was one found?
if ($foundElement && $conflict === AssetElement::SCENARIO_INDEX) {
// If so, we still need to make a copy temporarily, as the Users service needs to add it in properly
return $foundElement->getCopyOfFile();
return $foundElement->id;
}

// We can't find an existing asset, we need to download it, or plain ignore it
if ($urlToUpload) {
$folderId = $this->_prepareUserPhotosFolder($this->element);

$uploadedElementIds = AssetHelper::fetchRemoteImage([$urlToUpload], $fieldInfo, $this->feed, null, $this->element, $folderId);
$uploadedElements = AssetHelper::fetchRemoteImage([$urlToUpload], $fieldInfo, $this->feed, null, $this->element, $folderId);

if ($uploadedElementIds) {
$uploadedAsset = AssetElement::findOne(['id' => $uploadedElementIds[0]]);

// We still need to make a copy temporarily, as the Users service needs to add it in properly
return $uploadedAsset->getCopyOfFile();
return $uploadedElementIds[0];
}
}
}
Expand All @@ -261,10 +262,10 @@ private function _prepareUserPhotosFolder($user)
$assetsService = Craft::$app->getAssets();
$volumes = Craft::$app->getVolumes();

$volumeId = Craft::$app->getSystemSettings()->getSetting('users', 'photoVolumeId');
$volume = $volumes->getVolumeById($volumeId);
$volumeUid = Craft::$app->getProjectConfig()->get('users.photoVolumeUid');
$volume = $volumes->getVolumeByUid($volumeUid);

$subpath = (string)Craft::$app->getSystemSettings()->getSetting('users', 'photoSubpath');
$subpath = (string)Craft::$app->getProjectConfig()->get('users.photoSubpath');

if ($subpath) {
$subpath = Craft::$app->getView()->renderObjectTemplate($subpath, $user);
Expand Down
9 changes: 7 additions & 2 deletions src/templates/_includes/elements/user/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
}, {
type: 'assets',
name: 'Photo',
handle: 'photo',
handle: 'photoId',
default: {
type: 'text',
type: 'elementselect',
options: {
limit: 1,
elementType: 'craft\\elements\\asset',
selectionLabel: "Default Asset" | t('feed-me'),
},
},
}, {
name: 'Preferred Locale',
Expand Down

0 comments on commit b678866

Please sign in to comment.