Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ticket/17153] Use avatar classes and twig to render avatars #6504

Merged
merged 8 commits into from
Jun 6, 2024
2 changes: 1 addition & 1 deletion phpBB/config/default/container/services_avatar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ services:
class: phpbb\avatar\driver\upload
arguments:
- '@config'
- '@controller.helper'
- '%core.root_path%'
- '%core.php_ext%'
- '@storage.avatar'
- '@path_helper'
- '@routing.helper'
- '@event_dispatcher'
- '@files.factory'
- '@php_ini'
Expand Down
2 changes: 2 additions & 0 deletions phpBB/config/default/container/services_twig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ services:

template.twig.extensions.avatar:
class: phpbb\template\twig\extension\avatar
arguments:
- '@avatar.helper'
tags:
- { name: twig.extension }

Expand Down
1 change: 1 addition & 0 deletions phpBB/config/installer/container/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: services_installer.yml }
- { resource: services_avatar.yml }
- { resource: ../../default/container/services_event.yml }
- { resource: ../../default/container/services_filesystem.yml }
- { resource: ../../default/container/services_http.yml }
Expand Down
25 changes: 25 additions & 0 deletions phpBB/config/installer/container/services_avatar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
avatar.manager:
class: phpbb\avatar\manager
arguments:
- '@config'
- '@dispatcher'
- '@avatar.driver_collection'

avatar.helper:
class: phpbb\avatar\helper
arguments:
- '@config'
- '@dispatcher'
- '@language'
- '@avatar.manager'
- '@path_helper'
- '@user'

# ----- Avatar drivers -----
avatar.driver_collection:
class: phpbb\di\service_collection
arguments:
- '@service_container'
tags:
- { name: service_collection, tag: avatar.driver }
121 changes: 0 additions & 121 deletions phpBB/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3541,127 +3541,6 @@ function phpbb_quoteattr($data, $entities = null)
return $data;
}

/**
* Get user avatar
*
* @deprecated 4.0.0 Use \phpbb\avatar\helper::get_user_avatar() instead
*
* @param array $user_row Row from the users table
* @param string $alt Optional language string for alt tag within image, can be a language key or text
* @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
* @param bool $lazy If true, will be lazy loaded (requires JS)
*
* @return string Avatar html
*/
function phpbb_get_user_avatar($user_row, $alt = 'USER_AVATAR', $ignore_config = false, $lazy = false)
{
$row = \phpbb\avatar\manager::clean_row($user_row, 'user');
return phpbb_get_avatar($row, $alt, $ignore_config, $lazy);
}

/**
* Get group avatar
*
* @deprecated 4.0.0 Use \phpbb\avatar\helper::get_group_avatar() instead
*
* @param array $group_row Row from the groups table
* @param string $alt Optional language string for alt tag within image, can be a language key or text
* @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
* @param bool $lazy If true, will be lazy loaded (requires JS)
*
* @return string Avatar html
*/
function phpbb_get_group_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false)
{
$row = \phpbb\avatar\manager::clean_row($group_row, 'group');
return phpbb_get_avatar($row, $alt, $ignore_config, $lazy);
}

/**
* Get avatar
*
* @deprecated 4.0.0 Use \phpbb\avatar\helper::get_avatar() instead
*
* @param array $row Row cleaned by \phpbb\avatar\manager::clean_row
* @param string $alt Optional language string for alt tag within image, can be a language key or text
* @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
* @param bool $lazy If true, will be lazy loaded (requires JS)
*
* @return string Avatar html
*/
function phpbb_get_avatar($row, $alt, $ignore_config = false, $lazy = false)
{
global $user, $config;
global $phpbb_container, $phpbb_dispatcher;

if (!$config['allow_avatar'] && !$ignore_config)
{
return '';
}

$avatar_data = array(
'src' => $row['avatar'],
'width' => $row['avatar_width'],
'height' => $row['avatar_height'],
);

/* @var $phpbb_avatar_manager \phpbb\avatar\manager */
$phpbb_avatar_manager = $phpbb_container->get('avatar.manager');
$driver = $phpbb_avatar_manager->get_driver($row['avatar_type'], !$ignore_config);
$html = '';

if ($driver)
{
$html = $driver->get_custom_html($user, $row, $alt);
$avatar_data = $driver->get_data($row);
}
else
{
$avatar_data['src'] = '';
}

if (empty($html) && !empty($avatar_data['src']))
{
if ($lazy)
{
// This path is sent with the base template paths in the assign_vars()
// call below. We need to correct it in case we are accessing from a
// controller because the web paths will be incorrect otherwise.
$phpbb_path_helper = $phpbb_container->get('path_helper');
$web_path = $phpbb_path_helper->get_web_root_path();

$theme = "{$web_path}styles/" . rawurlencode($user->style['style_path']) . '/theme';

$src = 'src="' . $theme . '/images/no_avatar.gif" data-src="' . $avatar_data['src'] . '"';
}
else
{
$src = 'src="' . $avatar_data['src'] . '"';
}

$html = '<img class="avatar" ' . $src . ' ' .
($avatar_data['width'] ? ('width="' . $avatar_data['width'] . '" ') : '') .
($avatar_data['height'] ? ('height="' . $avatar_data['height'] . '" ') : '') .
'alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
}

/**
* Event to modify HTML <img> tag of avatar
*
* @event core.get_avatar_after
* @var array row Row cleaned by \phpbb\avatar\manager::clean_row
* @var string alt Optional language string for alt tag within image, can be a language key or text
* @var bool ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
* @var array avatar_data The HTML attributes for avatar <img> tag
* @var string html The HTML <img> tag of generated avatar
* @since 3.1.6-RC1
*/
$vars = array('row', 'alt', 'ignore_config', 'avatar_data', 'html');
extract($phpbb_dispatcher->trigger_event('core.get_avatar_after', compact($vars)));

return $html;
}

/**
* Generate page header
*/
Expand Down
28 changes: 0 additions & 28 deletions phpBB/includes/functions_compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,6 @@
exit;
}

/**
* Get user avatar
*
* @deprecated 3.1.0-a1 (To be removed: 4.0.0)
*
* @param string $avatar Users assigned avatar name
* @param int $avatar_type Type of avatar
* @param string $avatar_width Width of users avatar
* @param string $avatar_height Height of users avatar
* @param string $alt Optional language string for alt tag within image, can be a language key or text
* @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
* @param bool $lazy If true, will be lazy loaded (requires JS)
*
* @return string Avatar image
*/
function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR', $ignore_config = false, $lazy = false)
{
// map arguments to new function phpbb_get_avatar()
$row = array(
'avatar' => $avatar,
'avatar_type' => $avatar_type,
'avatar_width' => $avatar_width,
'avatar_height' => $avatar_height,
);

return phpbb_get_avatar($row, $alt, $ignore_config, $lazy);
}

/**
* Hash the password
*
Expand Down
12 changes: 6 additions & 6 deletions phpBB/phpbb/avatar/driver/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

use bantu\IniGetWrapper\IniGetWrapper;
use phpbb\config\config;
use phpbb\controller\helper;
use phpbb\event\dispatcher_interface;
use phpbb\files\factory;
use phpbb\path_helper;
use phpbb\routing\helper;
use phpbb\storage\exception\exception as storage_exception;
use phpbb\storage\storage;

Expand All @@ -30,7 +30,7 @@ class upload extends \phpbb\avatar\driver\driver
/**
* @var helper
*/
private $controller_helper;
private $routing_helper;

/**
* @var storage
Expand All @@ -56,23 +56,23 @@ class upload extends \phpbb\avatar\driver\driver
* Construct a driver object
*
* @param config $config phpBB configuration
* @param helper $controller_helper
* @param string $phpbb_root_path Path to the phpBB root
* @param string $php_ext PHP file extension
* @param storage $storage phpBB avatar storage
* @param path_helper $path_helper phpBB path helper
* @param helper $routing_helper phpBB routing helper
* @param dispatcher_interface $dispatcher phpBB Event dispatcher object
* @param factory $files_factory File classes factory
* @param IniGetWrapper $php_ini ini_get() wrapper
*/
public function __construct(config $config, helper $controller_helper, string $phpbb_root_path, string $php_ext, storage $storage, path_helper $path_helper, dispatcher_interface $dispatcher, factory $files_factory, IniGetWrapper $php_ini)
public function __construct(config $config, string $phpbb_root_path, string $php_ext, storage $storage, path_helper $path_helper, helper $routing_helper, dispatcher_interface $dispatcher, factory $files_factory, IniGetWrapper $php_ini)
{
$this->config = $config;
$this->controller_helper = $controller_helper;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->storage = $storage;
$this->path_helper = $path_helper;
$this->routing_helper = $routing_helper;
$this->dispatcher = $dispatcher;
$this->files_factory = $files_factory;
$this->php_ini = $php_ini;
Expand All @@ -84,7 +84,7 @@ public function __construct(config $config, helper $controller_helper, string $p
public function get_data($row)
{
return array(
'src' => $this->controller_helper->route('phpbb_storage_avatar', ['file' => $row['avatar']]),
'src' => $this->routing_helper->route('phpbb_storage_avatar', ['file' => $row['avatar']]),
'width' => $row['avatar_width'],
'height' => $row['avatar_height'],
);
Expand Down
73 changes: 44 additions & 29 deletions phpBB/phpbb/template/twig/extension/avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,50 @@

namespace phpbb\template\twig\extension;

use phpbb\avatar\helper;
use phpbb\avatar\manager;
use phpbb\template\twig\environment;
use Twig\Error\Error;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class avatar extends AbstractExtension
{
/**
* @var helper
*/
private $avatar_helper;

/**
* Constructor for avatar extension
*
* @param helper $avatar_helper
*/
public function __construct(helper $avatar_helper)
{
$this->avatar_helper = $avatar_helper;
}

/**
* Get the name of this extension
*
* @return string
*/
public function getName()
public function getName(): string
{
return 'avatar';
}

/**
* Returns a list of global functions to add to the existing list.
*
* @return \Twig\TwigFunction[] An array of global functions
* @return TwigFunction[] An array of global functions
*/
public function getFunctions(): array
{
return array(
new \Twig\TwigFunction('avatar', array($this, 'get_avatar')),
);
return [
new TwigFunction('avatar', [$this, 'get_avatar'], ['needs_environment' => true]),
];
}

/**
Expand All @@ -48,35 +68,30 @@ public function getFunctions(): array
* The mode and row (group_row or user_row) are required.
* The other fields (alt|ignore_config|lazy) are optional.
*
* @uses \phpbb_get_group_avatar()
* @uses \phpbb_get_user_avatar()
*
* @return string The avatar HTML for the specified mode
*/
public function get_avatar()
public function get_avatar(environment $environment, string $mode, array $row, ?string $alt, ?bool $ignore_config, ?bool $lazy): string
{
$args = func_get_args();
$alt = $alt ?? false;
$ignore_config = $ignore_config ?? false;
$lazy = $lazy ?? false;
$row = manager::clean_row($row, $mode);
$avatar = $this->avatar_helper->get_avatar($row, $alt, $ignore_config, $lazy);

$mode = (string) $args[0];
$row = (array) $args[1];
$alt = isset($args[2]) ? (string) $args[2] : false;
$ignore_config = isset($args[3]) ? (bool) $args[3] : false;
$lazy = isset($args[4]) ? (bool) $args[4] : false;

// To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided
switch ($mode)
try
{
case 'group':
return $alt ? phpbb_get_group_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_group_avatar($row);
break;

case 'user':
return $alt ? phpbb_get_user_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_user_avatar($row);
break;

default:
return '';
break;
return $environment->render('macros/avatar.twig', [
'SRC' => $avatar['lazy'] ? $this->avatar_helper->get_no_avatar_source() : $avatar['src'],
'DATA_SRC' => $avatar['lazy'] ? $avatar['src'] : '',
'WIDTH' => $avatar['width'],
'HEIGHT' => $avatar['height'],
'TITLE' => $avatar['title'],
'LAZY' => $avatar['lazy'],
]);
}
catch (Error $e)
{
return '';
}
}
}
1 change: 1 addition & 0 deletions phpBB/styles/all/template/macros/avatar.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% if SRC %}<img class="avatar" src="{{ SRC }}"{% if DATA_SRC %} data-src="{{ DATA_SRC }}"{% endif %}{% if WIDTH %} width="{{ WIDTH }}"{% endif %}{% if HEIGHT %} height="{{ HEIGHT }}"{% endif %} alt="{{ lang(TITLE) }}">{% endif %}
Loading