diff --git a/config/packages/liip_imagine.yaml b/config/packages/liip_imagine.yaml index 130e574b..7338b189 100644 --- a/config/packages/liip_imagine.yaml +++ b/config/packages/liip_imagine.yaml @@ -11,9 +11,17 @@ liip_imagine: size: [150, 150] mode: inset + thumbnail_md: quality: 75 filters: thumbnail: size: [250, 250] mode: inset + + thumbnail_xs: + quality: 70 + filters: + thumbnail: + size: [50, 50] + mode: inset \ No newline at end of file diff --git a/config/packages/twig.yaml b/config/packages/twig.yaml index 1e2104c2..18de5917 100644 --- a/config/packages/twig.yaml +++ b/config/packages/twig.yaml @@ -17,6 +17,7 @@ twig: error_page_show_help: '%partdb.error_pages.show_help%' sidebar_items: '%partdb.sidebar.items%' sidebar_tree_updater: '@App\Services\Trees\SidebarTreeUpdater' + avatar_helper: '@App\Services\UserSystem\UserAvatarHelper' when@test: twig: diff --git a/config/services.yaml b/config/services.yaml index aa71168c..b5fe8112 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -182,6 +182,10 @@ services: tags: - { name: 'translation.extractor', alias: 'permissionExtractor'} + App\Services\UserSystem\UserAvatarHelper: + arguments: + $use_gravatar: '%partdb.users.use_gravatar%' + #################################################################################################################### # Label system diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 826510b5..88e39c12 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -220,13 +220,6 @@ class UserController extends AdminPages\BaseAdminController return $table->getResponse(); } - if ($this->getParameter('partdb.users.use_gravatar')) { - //If no email is existing just set some string to show a gravatar - $avatar = $this->getGravatar($user->getEmail() ?? 'partdb', 200, 'identicon'); - } else { - $avatar = $packages->getUrl('/img/default_avatar.png'); - } - //Show permissions to user $builder = $this->createFormBuilder()->add('permissions', PermissionsType::class, [ 'mapped' => false, @@ -237,42 +230,8 @@ class UserController extends AdminPages\BaseAdminController return $this->renderForm('Users/user_info.html.twig', [ 'user' => $user, - 'avatar' => $avatar, 'form' => $builder->getForm(), 'datatable' => $table, ]); } - - /** - * Get either a Gravatar URL or complete image tag for a specified email address. - * - * @param string|null $email The email address - * @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ] - * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] - * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] - * @param bool $img True to return a complete IMG tag False for just the URL - * @param array $atts Optional, additional key/value attributes to include in the IMG tag - * - * @return string containing either just a URL or a complete image tag - * @source https://gravatar.com/site/implement/images/php/ - */ - public function getGravatar(?string $email, int $s = 80, string $d = 'mm', string $r = 'g', bool $img = false, array $atts = []): string - { - if (null === $email) { - return ''; - } - - $url = 'https://www.gravatar.com/avatar/'; - $url .= md5(strtolower(trim($email))); - $url .= "?s=${s}&d=${d}&r=${r}"; - if ($img) { - $url = ' $val) { - $url .= ' '.$key.'="'.$val.'"'; - } - $url .= ' />'; - } - - return $url; - } } diff --git a/src/DataTables/LogDataTable.php b/src/DataTables/LogDataTable.php index 493cc91c..1cb77e3d 100644 --- a/src/DataTables/LogDataTable.php +++ b/src/DataTables/LogDataTable.php @@ -44,6 +44,7 @@ use App\Exceptions\EntityNotSupportedException; use App\Repository\LogEntryRepository; use App\Services\ElementTypeNameGenerator; use App\Services\EntityURLGenerator; +use App\Services\UserSystem\UserAvatarHelper; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\QueryBuilder; use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider; @@ -66,9 +67,11 @@ class LogDataTable implements DataTableTypeInterface protected EntityURLGenerator $entityURLGenerator; protected LogEntryRepository $logRepo; protected Security $security; + protected UserAvatarHelper $userAvatarHelper; public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator, - UrlGeneratorInterface $urlGenerator, EntityURLGenerator $entityURLGenerator, EntityManagerInterface $entityManager, Security $security) + UrlGeneratorInterface $urlGenerator, EntityURLGenerator $entityURLGenerator, EntityManagerInterface $entityManager, + Security $security, UserAvatarHelper $userAvatarHelper) { $this->elementTypeNameGenerator = $elementTypeNameGenerator; $this->translator = $translator; @@ -76,6 +79,7 @@ class LogDataTable implements DataTableTypeInterface $this->entityURLGenerator = $entityURLGenerator; $this->logRepo = $entityManager->getRepository(AbstractLogEntry::class); $this->security = $security; + $this->userAvatarHelper = $userAvatarHelper; } public function configureOptions(OptionsResolver $optionsResolver): void @@ -227,8 +231,11 @@ class LogDataTable implements DataTableTypeInterface ); } + $img_url = $this->userAvatarHelper->getAvatarSmURL($user); + return sprintf( - '%s', + ' %s', + $img_url, $this->urlGenerator->generate('user_info', ['id' => $user->getID()]), htmlentities($user->getFullName(true)) ); diff --git a/src/Services/UserSystem/UserAvatarHelper.php b/src/Services/UserSystem/UserAvatarHelper.php new file mode 100644 index 00000000..4c104a01 --- /dev/null +++ b/src/Services/UserSystem/UserAvatarHelper.php @@ -0,0 +1,111 @@ +. + */ + +namespace App\Services\UserSystem; + +use App\Entity\UserSystem\User; +use App\Services\Attachments\AttachmentURLGenerator; +use Liip\ImagineBundle\Service\FilterService; +use Symfony\Component\Asset\Packages; + +class UserAvatarHelper +{ + private bool $use_gravatar; + private Packages $packages; + private AttachmentURLGenerator $attachmentURLGenerator; + private FilterService $filterService; + + public function __construct(bool $use_gravatar, Packages $packages, AttachmentURLGenerator $attachmentURLGenerator, FilterService $filterService) + { + $this->use_gravatar = $use_gravatar; + $this->packages = $packages; + $this->attachmentURLGenerator = $attachmentURLGenerator; + $this->filterService = $filterService; + } + + + /** + * Returns the URL to the profile picture of the given user (in big size) + * @param User $user + * @return string + */ + public function getAvatarURL(User $user): string + { + //Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture) + if ($user->getMasterPictureAttachment() !== null) { + return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment()); + } + + //If not check if gravatar is enabled (then use gravatar URL) + if ($this->use_gravatar) { + return $this->getGravatar($user, 200); //200px wide picture + } + + //Fallback to the default avatar picture + return $this->packages->getUrl('/img/default_avatar.png'); + } + + public function getAvatarSmURL(User $user): string + { + //Check if the user has a master attachment defined (meaning he has explicitly defined a profile picture) + if ($user->getMasterPictureAttachment() !== null) { + return $this->attachmentURLGenerator->getThumbnailURL($user->getMasterPictureAttachment(), 'thumbnail_xs'); + } + + //If not check if gravatar is enabled (then use gravatar URL) + if ($this->use_gravatar) { + return $this->getGravatar($user, 50); //50px wide picture + } + + try { + //Otherwise we can serve the relative path via Asset component + return $this->filterService->getUrlOfFilteredImage('/img/default_avatar.png', 'thumbnail_xs'); + } catch (\Imagine\Exception\RuntimeException $e) { + //If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning + return $this->packages->getUrl('/img/default_avatar.png'); + } + } + + + /** + * Get either a Gravatar URL or complete image tag for a specified email address. + * + * @param User $user The user for which the gravator should be generated + * @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ] + * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] + * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] + * + * @return string containing either just a URL or a complete image tag + * @source https://gravatar.com/site/implement/images/php/ + */ + private function getGravatar(User $user, int $s = 80, string $d = 'identicon', string $r = 'g'): string + { + $email = $user->getEmail(); + if (empty($email)) { + $email = 'Part-DB'; + } + + $url = 'https://www.gravatar.com/avatar/'; + $url .= md5(strtolower(trim($email))); + $url .= "?s=${s}&d=${d}&r=${r}"; + + return $url; + } +} \ No newline at end of file diff --git a/templates/Users/user_info.html.twig b/templates/Users/user_info.html.twig index ab9790a0..f18df094 100644 --- a/templates/Users/user_info.html.twig +++ b/templates/Users/user_info.html.twig @@ -9,7 +9,7 @@
- User avatar + User avatar
diff --git a/templates/_navbar.html.twig b/templates/_navbar.html.twig index 56e57d0c..14e40151 100644 --- a/templates/_navbar.html.twig +++ b/templates/_navbar.html.twig @@ -1,3 +1,5 @@ +{% import "helper.twig" as helper %} +