From efb3f7f9cdf4bad59e6d25fee763e7b673d6cc74 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Mon, 10 Aug 2020 11:52:06 +0200 Subject: [PATCH] Internal - Rework on User and UserRights classes: - PHPDoc - Add User::GetInitials() method - Fix several calls to UserRights::FindUser() - Add UserRights::GetUserInitials() - Change usage of DBObject->Get('friendlyname') to DBObject->GetRawName() --- core/userrights.class.inc.php | 69 ++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/core/userrights.class.inc.php b/core/userrights.class.inc.php index bdb12ce7c..2bb733baf 100644 --- a/core/userrights.class.inc.php +++ b/core/userrights.class.inc.php @@ -275,7 +275,11 @@ abstract class User extends cmdbAbstractObject /* * Compute a name in best effort mode - */ + * + * @return string + * @throws \ArchivedObjectException + * @throws \CoreException + */ public function GetFriendlyName() { if (!MetaModel::IsValidAttCode(get_class($this), 'contactid')) @@ -303,6 +307,32 @@ abstract class User extends cmdbAbstractObject return $this->Get('login'); } + /* + * Compute the initials in best effort mode + * + * @return string + * @throws \ArchivedObjectException + * @throws \CoreException + * @since 2.8.0 + */ + public function GetInitials() + { + $sInitials = ''; + + if (MetaModel::IsValidAttCode(get_class($this), 'contactid') && ($this->Get('contactid') != 0)) + { + $sInitials .= mb_substr($this->Get('first_name'), 0, 1); + $sInitials .= mb_substr($this->Get('last_name'), 0, 1); + } + + if (empty($sInitials)) + { + $sInitials = mb_substr($this->Get('login'), 0, 1); + } + + return $sInitials; + } + protected $oContactObject; /** @@ -1063,7 +1093,7 @@ class UserRights } else { - $oUser = FindUser($sName); + $oUser = self::FindUser($sName); } if (is_null($oUser)) { @@ -1096,7 +1126,7 @@ class UserRights } else { - $oUser = FindUser($sName); + $oUser = self::FindUser($sName); } // Check that user exists (in case we try to get it for another contact) @@ -1181,9 +1211,9 @@ class UserRights $sFriendlyname = null; $oContact = static::GetContactObject(); - if(!is_null($oContact) && MetaModel::IsValidAttCode(get_class($oContact), 'friendlyname')) + if(!is_null($oContact)) { - $sFriendlyname = $oContact->Get('friendlyname'); + $sFriendlyname = $oContact->GetRawName(); } return $sFriendlyname; @@ -1210,6 +1240,7 @@ class UserRights * @param string $sName * * @return string + * @throws \OQLException */ public static function GetUserFriendlyName($sName = '') { @@ -1219,7 +1250,7 @@ class UserRights } else { - $oUser = FindUser($sName); + $oUser = self::FindUser($sName); } if (is_null($oUser)) { @@ -1228,6 +1259,32 @@ class UserRights return $oUser->GetFriendlyName(); } + /** + * Render the user initials in best effort mode + * + * @param string $sName + * + * @return string + * @throws \OQLException + * @since 2.8.0 + */ + public static function GetUserInitials($sName = '') + { + if (empty($sName)) + { + $oUser = self::$m_oUser; + } + else + { + $oUser = self::FindUser($sName); + } + if (is_null($oUser)) + { + return ''; + } + return $oUser->GetInitials(); + } + /** * @return bool */