From f2725c5a5c2f451e292c4dce2b46cfc8087b5e72 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Thu, 13 Aug 2020 14:55:13 +0200 Subject: [PATCH] Add $bAllowDefaultPicture parameter to UserRights::GetContactPictureAbsUrl() to return null instead of the default picture --- core/userrights.class.inc.php | 30 ++++++++++++------- .../Layout/NavigationMenu/NavigationMenu.php | 9 +++++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/core/userrights.class.inc.php b/core/userrights.class.inc.php index 4a2738f14..283f6cc68 100644 --- a/core/userrights.class.inc.php +++ b/core/userrights.class.inc.php @@ -1113,23 +1113,24 @@ class UserRights * Return the absolute URL of the contact picture * * @param string $sLogin Login of the user from which we return the picture URL + * @param bool $bAllowDefaultPicture Set to false if you want it to return null instead of the default picture URL when the contact has no picture defined. This can be useful when we want to display something else than the default picture (eg. initials) * - * @return mixed|string - * @throws \Exception + * @return null|string + * @throws \ArchivedObjectException + * @throws \CoreException * @since 2.8.0 */ - public static function GetContactPictureAbsUrl($sLogin = '') + public static function GetContactPictureAbsUrl($sLogin = '', $bAllowDefaultPicture = true) { - // First, retrieve the default picture from preferences (users without contact) - if(empty($sLogin)) + // First, the default picture + if($bAllowDefaultPicture === true) { - $sDefaultPictureFilename = appUserPreferences::GetPref('user_picture_placeholder', 'user-profile-default-256px.png'); + $sPictureUrl = utils::GetAbsoluteUrlAppRoot().'images/user-pictures/' . 'user-profile-default-256px.png'; } else { - $sDefaultPictureFilename = 'user-profile-default-256px.png'; + $sPictureUrl = null; } - $sPictureUrl = utils::GetAbsoluteUrlAppRoot().'images/user-pictures/' . $sDefaultPictureFilename; // Then check if the user has a contact attached and if it has an picture defined $sContactId = UserRights::GetContactId($sLogin); @@ -1145,9 +1146,16 @@ class UserRights $oPicture = $oContact->Get(static::DEFAULT_CONTACT_PICTURE_ATTCODE); if($oPicture->IsEmpty()) { - /** @var \AttributeImage $oAttDef */ - $oAttDef = MetaModel::GetAttributeDef($sContactClass, static::DEFAULT_CONTACT_PICTURE_ATTCODE); - $sPictureUrl = $oAttDef->Get('default_image'); + if($bAllowDefaultPicture === true) + { + /** @var \AttributeImage $oAttDef */ + $oAttDef = MetaModel::GetAttributeDef($sContactClass, static::DEFAULT_CONTACT_PICTURE_ATTCODE); + $sPictureUrl = $oAttDef->Get('default_image'); + } + else + { + $sPictureUrl = null; + } } else { diff --git a/sources/application/UI/Layout/NavigationMenu/NavigationMenu.php b/sources/application/UI/Layout/NavigationMenu/NavigationMenu.php index 828f221d5..68d1e4675 100644 --- a/sources/application/UI/Layout/NavigationMenu/NavigationMenu.php +++ b/sources/application/UI/Layout/NavigationMenu/NavigationMenu.php @@ -224,11 +224,18 @@ class NavigationMenu extends UIBlock */ protected function ComputeUserData() { + // Use a picture set in the preferences is there is none in the user's contact + $sPictureUrl = UserRights::GetContactPictureAbsUrl('', false); + if(empty($sPictureUrl)) + { + $sPictureUrl = utils::GetAbsoluteUrlAppRoot().'images/user-pictures/' . appUserPreferences::GetPref('user_picture_placeholder', 'user-profile-default-256px.png'); + } + //Todo : what do we show if no contact is linked to the user ? $aData = [ 'sOrganization' => UserRights::GetContactOrganizationFriendlyname(), 'sFirstname' => UserRights::GetContactFirstname(), - 'sPictureUrl' => UserRights::GetContactPictureAbsUrl(), + 'sPictureUrl' => $sPictureUrl, 'sWelcomeMessage' => Dict::Format('UI:Layout:NavigationMenu:UserInfo:WelcomeMessage:Text', UserRights::GetContactFirstname()) ];