[FOLLOWUP][FEATURE] Introduce AvatarProvider 01/42601/7
authorFrans Saris <franssaris@gmail.com>
Fri, 14 Aug 2015 06:36:29 +0000 (08:36 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Sat, 15 Aug 2015 21:32:25 +0000 (23:32 +0200)
Changed definition of Avatar/Image::url, renamed defaultAvatarProvider
and adjusted some typos in rst file.

Releases: master
Resolves: #68429
Change-Id: Idbef24d94fb604b844e1c64ac54467441c5f98fc
Reviewed-on: http://review.typo3.org/42601
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/backend/Classes/Backend/Avatar/Avatar.php
typo3/sysext/backend/Classes/Backend/Avatar/AvatarProviderInterface.php
typo3/sysext/backend/Classes/Backend/Avatar/DefaultAvatarProvider.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Backend/Avatar/Image.php
typo3/sysext/backend/Classes/Backend/Avatar/ImageProvider.php [deleted file]
typo3/sysext/backend/ext_localconf.php
typo3/sysext/core/Documentation/Changelog/master/Feature-68429-IntroducedAvatarProviderAPI.rst
typo3/sysext/setup/Classes/Controller/SetupModuleController.php

index fa5a6a8..1f71a8f 100644 (file)
@@ -40,7 +40,7 @@ class Avatar {
        /**
         * Render avatar tag
         *
-        * @param array $backendUser be_user record
+        * @param array $backendUser be_users record
         * @param int $size width and height of the image
         * @param bool $showIcon show the record icon
         * @return string
@@ -64,7 +64,7 @@ class Avatar {
        /**
         * Get avatar img tag
         *
-        * @param array $backendUser be_user record
+        * @param array $backendUser be_users record
         * @param int $size
         * @return string
         */
@@ -77,7 +77,7 @@ class Avatar {
                $avatarImage = $this->getImage($backendUser, $size);
 
                if ($avatarImage) {
-                       $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '"' .
+                       $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(TRUE)) . '"' .
                                'width="' . (int)$avatarImage->getWidth() . '" ' .
                                'height="' . (int)$avatarImage->getHeight() . '" />';
                }
@@ -88,7 +88,7 @@ class Avatar {
        /**
         * Get Image from first provider that returns one
         *
-        * @param array $backendUser be_user record
+        * @param array $backendUser be_users record
         * @param int $size
         * @return Image|NULL
         */
index bd826b3..278ec53 100644 (file)
@@ -22,7 +22,7 @@ interface AvatarProviderInterface {
        /**
         * Get Image
         *
-        * @param array $backendUser be_user record
+        * @param array $backendUser be_users record
         * @param int $size
         * @return Image|NULL
         */
diff --git a/typo3/sysext/backend/Classes/Backend/Avatar/DefaultAvatarProvider.php b/typo3/sysext/backend/Classes/Backend/Avatar/DefaultAvatarProvider.php
new file mode 100644 (file)
index 0000000..440971e
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+namespace TYPO3\CMS\Backend\Backend\Avatar;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
+use TYPO3\CMS\Core\Resource\ProcessedFile;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Class DefaultAvatarProvider
+ */
+class DefaultAvatarProvider implements AvatarProviderInterface {
+
+       /**
+        * Get Image
+        *
+        * @param array $backendUser be_users record
+        * @param int $size
+        * @return Image|NULL
+        */
+       public function getImage(array $backendUser, $size) {
+               $fileUid = $this->getAvatarFileUid($backendUser['uid']);
+
+               // Get file object
+               try {
+                       $file = ResourceFactory::getInstance()->getFileObject($fileUid);
+                       $processedImage = $file->process(
+                               ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
+                               array('width' => $size . 'c', 'height' => $size . 'c')
+                       );
+
+                       $image = GeneralUtility::makeInstance(
+                               Image::class,
+                               $processedImage->getPublicUrl(),
+                               $processedImage->getProperty('width'),
+                               $processedImage->getProperty('height')
+                       );
+
+               } catch (FileDoesNotExistException $e) {
+                       // No image found
+                       $image = NULL;
+               }
+
+               return $image;
+       }
+
+       /**
+        * Get Avatar fileUid
+        *
+        * @param int $beUserId
+        * @return int
+        */
+       protected function getAvatarFileUid($beUserId) {
+               $file = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
+                       'uid_local',
+                       'sys_file_reference',
+                       'tablenames = \'be_users\' AND fieldname = \'avatar\' AND ' .
+                       'table_local = \'sys_file\' AND uid_foreign = ' . (int)$beUserId .
+                       BackendUtility::BEenableFields('sys_file_reference') . BackendUtility::deleteClause('sys_file_reference')
+               );
+               return $file ? $file['uid_local'] : 0;
+       }
+
+       /**
+        * @return \TYPO3\CMS\Core\Database\DatabaseConnection
+        */
+       protected function getDatabaseConnection() {
+               return $GLOBALS['TYPO3_DB'];
+       }
+}
index 1e8425d..5ed063c 100644 (file)
@@ -13,6 +13,8 @@ namespace TYPO3\CMS\Backend\Backend\Avatar;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Utility\PathUtility;
 
 /**
  * Class Image
@@ -22,7 +24,7 @@ namespace TYPO3\CMS\Backend\Backend\Avatar;
 class Image {
 
        /**
-        * Url of avatar image. Needs to be relative to the /typo3/ folder or an absolute URL.
+        * Url of avatar image. Needs to be relative to the website root or an absolute URL.
         *
         * @var string
         */
@@ -41,7 +43,7 @@ class Image {
        /**
         * Constructor
         *
-        * @param string $url url of image. Needs to be relative to the /typo3/ folder or an absolute URL.
+        * @param string $url url of image. Needs to be relative to the website root or an absolute URL.
         * @param int $width width of image
         * @param int $height height of image
         */
@@ -54,10 +56,19 @@ class Image {
        /**
         * Get url
         *
+        * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all.
         * @return string
         */
-       public function getUrl() {
-               return $this->url;
+       public function getUrl($relativeToCurrentScript = FALSE) {
+               $url = $this->url;
+
+               if ($relativeToCurrentScript && !GeneralUtility::isValidUrl($url)) {
+                       $absolutePathToContainingFolder = PathUtility::dirname(PATH_site . $url);
+                       $pathPart = PathUtility::getRelativePathTo($absolutePathToContainingFolder);
+                       $filePart = substr(PATH_site . $url, strlen($absolutePathToContainingFolder) + 1);
+                       $url = $pathPart . $filePart;
+               }
+               return $url;
        }
 
        /**
diff --git a/typo3/sysext/backend/Classes/Backend/Avatar/ImageProvider.php b/typo3/sysext/backend/Classes/Backend/Avatar/ImageProvider.php
deleted file mode 100644 (file)
index 4b62d0a..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-namespace TYPO3\CMS\Backend\Backend\Avatar;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-use TYPO3\CMS\Backend\Utility\BackendUtility;
-use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
-use TYPO3\CMS\Core\Resource\ProcessedFile;
-use TYPO3\CMS\Core\Resource\ResourceFactory;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-
-/**
- * Class ImageProvider
- */
-class ImageProvider implements AvatarProviderInterface {
-
-       /**
-        * Get Image
-        *
-        * @param array $backendUser be_user record
-        * @param int $size
-        * @return Image|NULL
-        */
-       public function getImage(array $backendUser, $size) {
-               $fileUid = $this->getAvatarFileUid($backendUser['uid']);
-
-               // Get file object
-               try {
-                       $file = ResourceFactory::getInstance()->getFileObject($fileUid);
-                       $processedImage = $file->process(
-                               ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
-                               array('width' => $size . 'c', 'height' => $size . 'c')
-                       );
-
-                       $image = GeneralUtility::makeInstance(
-                               Image::class,
-                               $processedImage->getPublicUrl(TRUE),
-                               $processedImage->getProperty('width'),
-                               $processedImage->getProperty('height')
-                       );
-
-               } catch (FileDoesNotExistException $e) {
-                       // No image found
-                       $image = NULL;
-               }
-
-               return $image;
-       }
-
-       /**
-        * Get Avatar fileUid
-        *
-        * @param int $beUserId
-        * @return int
-        */
-       protected function getAvatarFileUid($beUserId) {
-               $file = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
-                       'uid_local',
-                       'sys_file_reference',
-                       'tablenames = \'be_users\' AND fieldname = \'avatar\' AND ' .
-                       'table_local = \'sys_file\' AND uid_foreign = ' . (int)$beUserId .
-                       BackendUtility::BEenableFields('sys_file_reference') . BackendUtility::deleteClause('sys_file_reference')
-               );
-               return $file ? $file['uid_local'] : 0;
-       }
-
-       /**
-        * @return \TYPO3\CMS\Core\Database\DatabaseConnection
-        */
-       protected function getDatabaseConnection() {
-               return $GLOBALS['TYPO3_DB'];
-       }
-}
index e107e78..9695704 100644 (file)
@@ -23,8 +23,8 @@ if (TYPO3_MODE === 'BE') {
                'label' => 'LLL:EXT:backend/Resources/Private/Language/locallang.xlf:login.link'
        );
 
-       $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders']['imageProvider'] = array(
-               'provider' => \TYPO3\CMS\Backend\Backend\Avatar\ImageProvider::class
+       $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders']['defaultAvatarProvider'] = array(
+               'provider' => \TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider::class
        );
 }
 
index c2024c9..7cb0917 100644 (file)
@@ -5,29 +5,29 @@ Feature: #68429 - Introduced AvatarProvider API
 Description
 ===========
 
-To make providing a avatar image for BE users more flexible a API it introduced so you can register AvatarProviders.
-The core provides the ``ImageProvider`` by default.
+To make providing an avatar image for BE users more flexible an API is introduced so you can register AvatarProviders.
+The core provides the ``DefaultAvatarProvider`` by default to handle the image defined in the user settings.
 
 When an avatar is rendered in the BE the available AvatarProviders are asked if they can provide an
-``TYPO3\CMS\Backend\Backend\Image`` for given ``be_user`` and requested size. The first ``TYPO3\CMS\Backend\Backend\Image``
+``TYPO3\CMS\Backend\Backend\Avatar\Image`` for given ``be_users`` record and requested size. The first ``TYPO3\CMS\Backend\Backend\Avatar\Image``
 that gets returned is used.
 
-Registering a avatar provider
------------------------------
+Registering an avatar provider
+------------------------------
 
-A avatar provider can be registered within your ``ext_localconf.php`` file like this:
+An avatar provider can be registered within your ``ext_localconf.php`` file like this:
 
 .. code-block:: php
 
        $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders']['myCustomProvider'] = [
                'provider' => \MyVendor\MyExtension\AvatarProvider\CompanyAvatarProvider::class,
-               'before' => ['imageProvider']
+               'before' => ['defaultAvatarProvider']
        ];
 
 The settings are defined as:
 
-* ``provider``: The avatar provider class name, which must implement ``TYPO3\CMS\Backend\Backend\AvatarProviderInterface``.
-* ``before``/``after``: You can define the ordering how providers are executed. This is order is used as fallback of the avatar providers. Each property must be an array of provider names.
+* ``provider``: The avatar provider class name, which must implement ``TYPO3\CMS\Backend\Backend\Avatar\AvatarProviderInterface``.
+* ``before``/``after``: You can define the ordering how providers are executed. This is used to get the order in which the providers are executed.
 
 
 For a new avatar provider you have to register a **new key** in ``$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders']``.
@@ -36,7 +36,7 @@ extend an existing provider and replace its registered 'provider' class with you
 
 .. code-block:: php
 
-       $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders']['imageProvider']['provider'] = \MyVendor\MyExtension\AvatarProvider\CustomImageProvider::class
+       $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders']['defaultAvatarProvider']['provider'] = \MyVendor\MyExtension\AvatarProvider\CustomAvatarProvider::class;
 
 
 AvatarProviderInterface
@@ -48,14 +48,14 @@ The AvatarProviderInterface contains only one method:
 
 The parameters are defined as:
 
-* ``$backendUser``: The record of from ``be_user`` database table.
+* ``$backendUser``: The record from ``be_users`` database table.
 * ``$size``: The requested size of the avatar image.
 
-The return value of the method is expected to be and ``TYPO3\CMS\Backend\Backend\Image`` instance or NULL when the
-provider can not provide an image.
+The return value of the method is expected to be an instance of ``TYPO3\CMS\Backend\Backend\Avatar\Image`` or NULL
+when the provider can not provide an image.
 
-An ``TYPO3\CMS\Backend\Backend\Image`` object holds 3 properties:
+An ``TYPO3\CMS\Backend\Backend\Image`` object has 3 properties:
 
-* ``$url``: Url of avatar image. Needs to be relative to the /typo3/ folder or an absolute URL.
+* ``$url``: Url of avatar image. Needs to be relative to the website root or an absolute URL.
 * ``$width``: The width of the image.
 * ``$height``: The height of the image.
index de0afe7..b3e19e6 100644 (file)
@@ -14,7 +14,7 @@ namespace TYPO3\CMS\Setup\Controller;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Backend\Backend\Avatar\ImageProvider;
+use TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Backend\Utility\IconUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
@@ -605,11 +605,11 @@ class SetupModuleController {
                                        $avatarFileUid = $this->getAvatarFileUid($this->getBackendUser()->user['uid']);
 
                                        if ($avatarFileUid) {
-                                               $imageProvider = GeneralUtility::makeInstance(ImageProvider::class);
-                                               $avatarImage = $imageProvider->getImage($this->getBackendUser()->user, 32);
+                                               $defaultAvatarProvider = GeneralUtility::makeInstance(DefaultAvatarProvider::class);
+                                               $avatarImage = $defaultAvatarProvider->getImage($this->getBackendUser()->user, 32);
                                                if ($avatarImage) {
                                                        $icon = '<span class="avatar"><span class="avatar-image">' .
-                                                               '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '"' .
+                                                               '<img src="' . htmlspecialchars($avatarImage->getUrl(TRUE)) . '"' .
                                                                'width="' . (int)$avatarImage->getWidth() . '" ' .
                                                                'height="' . (int)$avatarImage->getHeight() . '" />' .
                                                                '</span></span>';