Skip to content

Commit

Permalink
Allows profile to override the avatar of employee
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Nov 4, 2020
1 parent c020552 commit ff9bad2
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ npm-debug.log.*
!/img/m/index.php
!/img/os/index.php
!/img/p/index.php
!/img/pr/index.php
!/img/s/index.php
!/img/scenes
!/img/st/index.php
Expand Down
28 changes: 19 additions & 9 deletions classes/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,23 @@ public function isSuperAdmin()
*/
public function getImage()
{
$default = Tools::getAdminImageUrl('prestashop-avatar.png');
$imageUrl = '';
$defaultSystem = Tools::getAdminImageUrl('prestashop-avatar.png');
$imageUrl = null;

// Default from Profile
$profile = new Profile($this->id_profile);
$profilePath = $profile ? ($profile->image_dir . $profile->id . '.jpg') : '';
$defaultProfile = file_exists($profilePath)
? Context::getContext()->link->getMediaLink(
str_replace($profile->image_dir, _THEME_PROFILE_DIR_, $profilePath)
)
: null;
$imageUrl = $imageUrl ?? $defaultProfile;

// Gravatar
if ($this->has_enabled_gravatar) {
$imageUrl = $imageUrl ?? 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email))) . '?d=' . urlencode($defaultSystem);
}

// Local Image
$imagePath = $this->image_dir . $this->id . '.jpg';
Expand All @@ -610,13 +625,8 @@ public function getImage()
);
}

// Default Image
$imageUrl = $imageUrl ?? $default;

// Gravatar
if ($this->has_enabled_gravatar) {
$imageUrl = 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email))) . '?d=' . urlencode($default);
}
// Default from System
$imageUrl = $imageUrl ?? $defaultSystem;

// Hooks
Hook::exec(
Expand Down
10 changes: 10 additions & 0 deletions classes/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ class ProfileCore extends ObjectModel

protected static $_cache_accesses = [];

/**
* {@inheritdoc}
*/
public function __construct($id = null, $idLang = null, $idShop = null, $translator = null)
{
parent::__construct($id, $idLang, $idShop, $translator);

$this->image_dir = _PS_PROFILE_IMG_DIR_;
}

/**
* Get all available profiles.
*
Expand Down
1 change: 1 addition & 0 deletions config/defines.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
define('_PS_MANU_IMG_DIR_', _PS_IMG_DIR_.'m/');
define('_PS_ORDER_STATE_IMG_DIR_', _PS_IMG_DIR_.'os/');
define('_PS_PROD_IMG_DIR_', _PS_IMG_DIR_.'p/');
define('_PS_PROFILE_IMG_DIR_', _PS_IMG_DIR_.'pr/');
define('_PS_SHIP_IMG_DIR_', _PS_IMG_DIR_.'s/');
define('_PS_STORE_IMG_DIR_', _PS_IMG_DIR_.'st/');
define('_PS_SUPP_IMG_DIR_', _PS_IMG_DIR_.'su/');
Expand Down
1 change: 1 addition & 0 deletions config/defines_uri.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
define('_THEME_CAT_DIR_', _PS_IMG_.'c/');
define('_THEME_EMPLOYEE_DIR_', _PS_IMG_.'e/');
define('_THEME_PROD_DIR_', _PS_IMG_.'p/');
define('_THEME_PROFILE_DIR_', _PS_IMG_.'pr/');
define('_THEME_MANU_DIR_', _PS_IMG_.'m/');
define('_THEME_SUP_DIR_', _PS_IMG_.'su/');
define('_THEME_SHIP_DIR_', _PS_IMG_.'s/');
Expand Down
35 changes: 35 additions & 0 deletions img/pr/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header("Location: ../");
exit;
90 changes: 90 additions & 0 deletions src/Adapter/Image/Uploader/ProfileImageUploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\Image\Uploader;

use Profile;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Uploads profile logo image
*/
final class ProfileImageUploader extends AbstractImageUploader
{
/**
* @var string
*/
private $profileImageDir;

/**
* @var string
*/
private $tmpImageDir;

/**
* @param string $profileImageDir
* @param string $tmpImageDir
*/
public function __construct(
string $profileImageDir = _PS_PROFILE_IMG_DIR_,
string $tmpImageDir = _PS_TMP_IMG_DIR_
) {
$this->profileImageDir = $profileImageDir;
$this->tmpImageDir = $tmpImageDir;
}

/**
* {@inheritdoc}
*/
public function upload($profileId, UploadedFile $image)
{
$this->checkImageIsAllowedForUpload($image);
$tempImageName = $this->createTemporaryImage($image);
$this->deleteOldImage($profileId);

$destination = $this->profileImageDir . $profileId . '.jpg';
$this->uploadFromTemp($tempImageName, $destination);
}

/**
* Deletes old image
*
* @param $id
*/
private function deleteOldImage($id)
{
$profile = new Profile($id);
$profile->deleteImage();

$currentImage = $this->tmpImageDir . 'profile_mini_' . $id . '.jpg';

if (file_exists($currentImage)) {
unlink($currentImage);
}
}
}
49 changes: 45 additions & 4 deletions src/Adapter/Profile/QueryHandler/GetProfileForEditingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,48 @@

namespace PrestaShop\PrestaShop\Adapter\Profile\QueryHandler;

use PrestaShop\PrestaShop\Adapter\Domain\AbstractObjectModelHandler;
use PrestaShop\PrestaShop\Core\Domain\Profile\Exception\ProfileNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Profile\Query\GetProfileForEditing;
use PrestaShop\PrestaShop\Core\Domain\Profile\QueryHandler\GetProfileForEditingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Profile\QueryResult\EditableProfile;
use PrestaShop\PrestaShop\Core\Domain\Profile\ValueObject\ProfileId;
use PrestaShop\PrestaShop\Core\Image\Parser\ImageTagSourceParser;
use PrestaShop\PrestaShop\Core\Image\Parser\ImageTagSourceParserInterface;
use Profile;

/**
* Gets Profile for editing using legacy object model
*/
final class GetProfileForEditingHandler implements GetProfileForEditingHandlerInterface
final class GetProfileForEditingHandler extends AbstractObjectModelHandler implements GetProfileForEditingHandlerInterface
{
/**
* @var ImageTagSourceParserInterface
*/
private $imageTagSourceParser;

/**
* @param ImageTagSourceParserInterface $imageTagSourceParser
*/
public function __construct(ImageTagSourceParserInterface $imageTagSourceParser = null)
{
$this->imageTagSourceParser = $imageTagSourceParser ?? new ImageTagSourceParser(__PS_BASE_URI__);
}

/**
* {@inheritdoc}
*/
public function handle(GetProfileForEditing $query)
{
$profile = $this->getProfile($query->getProfileId());
$profileId = $query->getProfileId();
$profile = $this->getProfile($profileId);

$avatarUrl = $this->getAvatarUrl($profileId->getValue());

return new EditableProfile(
$query->getProfileId(),
$profile->name
$profileId,
$profile->name,
$avatarUrl ? $avatarUrl['path'] : null
);
}

Expand All @@ -68,4 +88,25 @@ private function getProfile(ProfileId $profileId)

return $profile;
}

/**
* @param int $imageId
*
* @return array|null
*/
private function getAvatarUrl(int $imageId): ?array
{
$imagePath = _PS_PROFILE_IMG_DIR_ . $imageId . '.jpg';
$imageTag = $this->getTmpImageTag($imagePath, $imageId, 'profile');
$imageSize = $this->getImageSize($imagePath);

if (empty($imageTag) || null === $imageSize) {
return null;
}

return [
'size' => sprintf('%skB', $imageSize),
'path' => $this->imageTagSourceParser->parse($imageTag),
];
}
}
22 changes: 20 additions & 2 deletions src/Core/Domain/Profile/QueryResult/EditableProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ class EditableProfile
*/
private $localizedNames;

/**
* @var string|null
*/
private $avatarUrl;

/**
* @param ProfileId $profileId
* @param string[] $localizedNames
* @param string|null $avatarUrl
*/
public function __construct(ProfileId $profileId, array $localizedNames)
{
public function __construct(
ProfileId $profileId,
array $localizedNames,
?string $avatarUrl = null
) {
$this->profileId = $profileId;
$this->localizedNames = $localizedNames;
$this->avatarUrl = $avatarUrl;
}

/**
Expand All @@ -68,4 +78,12 @@ public function getLocalizedNames()
{
return $this->localizedNames;
}

/**
* @return string|null
*/
public function getAvatarUrl()
{
return $this->avatarUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function create(array $data)
$data['active'],
$data['profile'],
isset($data['shop_association']) ? $data['shop_association'] : $this->defaultShopAssociation,
$data['has_enabled_gravatar']
$data['has_enabled_gravatar'] ?? false
));

/** @var UploadedFile $uploadedAvatar */
Expand Down
Loading

0 comments on commit ff9bad2

Please sign in to comment.