From d9c1f3f15e6f0b012481c99313803b2fb501dfd1 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 22 May 2020 14:55:27 +0200 Subject: [PATCH] Add UserTemplatePlugin (#3322) * Add UserTemplatePlugin Close #3321 * Change name of user online marking in language items * Apply suggestions from code review Co-authored-by: Alexander Ebert Co-authored-by: Alexander Ebert --- .../UserFunctionTemplatePlugin.class.php | 95 +++++++++++++++++++ wcfsetup/install/lang/de.xml | 4 +- wcfsetup/install/lang/en.xml | 4 +- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/template/plugin/UserFunctionTemplatePlugin.class.php diff --git a/wcfsetup/install/files/lib/system/template/plugin/UserFunctionTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/UserFunctionTemplatePlugin.class.php new file mode 100644 index 0000000000..ad6d9ec58a --- /dev/null +++ b/wcfsetup/install/files/lib/system/template/plugin/UserFunctionTemplatePlugin.class.php @@ -0,0 +1,95 @@ + + * @package WoltLabSuite\Core\System\Template\Plugin + * @since 5.3 + */ +class UserFunctionTemplatePlugin implements IFunctionTemplatePlugin { + /** + * @inheritDoc + */ + public function execute($tagArgs, TemplateEngine $tplObj) { + if (!isset($tagArgs['object'])) { + throw new \InvalidArgumentException("Missing 'object' attribute."); + } + + $object = $tagArgs['object']; + unset($tagArgs['object']); + if (!($object instanceof UserProfile) && !ClassUtil::isDecoratedInstanceOf($object, UserProfile::class)) { + throw new \InvalidArgumentException("'object' attribute is no '" . UserProfile::class . "' object."); + } + + $additionalParameters = ''; + $content = ''; + if (isset($tagArgs['type'])) { + $type = $tagArgs['type']; + unset($tagArgs['type']); + + if ($type === 'plain') { + $content = $object->getTitle(); + } + else if (preg_match('~^avatar(\d+)$~', $type, $matches)) { + $content = $object->getAvatar()->getImageTag($matches[1]); + } + else if ($type !== 'default') { + throw new \InvalidArgumentException("Unknown 'type' value '{$type}'."); + } + } + + // default case + if ($content === '') { + $additionalParameters = ' data-object-id="' . $object->getObjectID() . '"'; + $content = $object->getFormattedUsername(); + if (isset($tagArgs['class'])) { + $tagArgs['class'] = 'userLink ' . $tagArgs['class']; + } + else { + $tagArgs['class'] = 'userLink'; + } + } + + $append = ''; + if (isset($tagArgs['append'])) { + $append = $tagArgs['append']; + unset($tagArgs['append']); + } + + foreach ($tagArgs as $name => $value) { + if (!preg_match('~^[a-z]+([A-z]+)+$~', $name)) { + throw new \InvalidArgumentException("Invalid additional argument name '{$name}'."); + } + + $additionalParameters .= ' ' . strtolower(preg_replace('~([A-Z])~', '-$1', $name)) + . '="' . StringUtil::encodeHTML($value) . '"'; + } + + return '' . $content . ''; + } +} diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 029f779a29..592bc9db11 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -768,8 +768,8 @@ ACHTUNG: Die oben genannten Meldungen sind stark gekürzt. Sie können Details z nicht verwendet werden.]]> - - <strong>%s</strong> stellt Mitglieder dieser Gruppe beispielsweise in Fettdruck dar.]]> + + <strong>%s</strong> stellt Mitglieder dieser Gruppe beispielsweise in Fettdruck dar.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index b8013f987a..de44fcfeec 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -744,8 +744,8 @@ ATTENTION: The messages listed above are greatly shortened. You can view details cannot be used in the signature.]]> - - <strong>%s</strong> results in a bolder appearance.]]> + + <strong>%s</strong> results in a bolder appearance.]]> -- 2.20.1