mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-06 22:45:02 +02:00
[ticket/15538] Style inheritance for PNG and a default 'no icon'
PHPBB3-15538
This commit is contained in:
parent
8bac1daba7
commit
5a1be4611c
@ -74,10 +74,13 @@ class icon extends \Twig\Extension\AbstractExtension
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$not_found = false;
|
||||||
|
$source = '';
|
||||||
|
|
||||||
switch ($type)
|
switch ($type)
|
||||||
{
|
{
|
||||||
case 'font':
|
case 'font':
|
||||||
$source = '';
|
// Nothing to do here..
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'iconify':
|
case 'iconify':
|
||||||
@ -86,19 +89,39 @@ class icon extends \Twig\Extension\AbstractExtension
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'png':
|
case 'png':
|
||||||
$board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH;
|
$filesystem = $environment->get_filesystem();
|
||||||
$web_path = $board_url ? generate_board_url() . '/' : $environment->get_web_root_path();
|
$root_path = $environment->get_web_root_path();
|
||||||
$style_path = $this->user->style['style_path'];
|
|
||||||
|
|
||||||
$source = "{$web_path}styles/{$style_path}/theme/icons/png/{$icon}.png";
|
$board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH;
|
||||||
|
$base_path = $board_url ? generate_board_url() . '/' : $root_path;
|
||||||
|
|
||||||
|
// Iterate over the user's styles and check for icon existance
|
||||||
|
foreach ($this->get_style_list() as $style_path)
|
||||||
|
{
|
||||||
|
if ($filesystem->exists("{$root_path}styles/{$style_path}/theme/icons/png/{$icon}.png"))
|
||||||
|
{
|
||||||
|
$source = "{$base_path}styles/{$style_path}/theme/icons/png/{$icon}.png";
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the icon was found or not
|
||||||
|
$not_found = empty($source);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'svg':
|
case 'svg':
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Try to load and prepare the SVG icon
|
||||||
$file = $environment->load('icons/svg/' . $icon . '.svg');
|
$file = $environment->load('icons/svg/' . $icon . '.svg');
|
||||||
$source = $this->prepare_svg($file);
|
$source = $this->prepare_svg($file);
|
||||||
}
|
}
|
||||||
|
catch (\Twig\Error\LoaderError $e)
|
||||||
|
{
|
||||||
|
// Icon was not found
|
||||||
|
$not_found = true;
|
||||||
|
}
|
||||||
catch (\Twig\Error\Error $e)
|
catch (\Twig\Error\Error $e)
|
||||||
{
|
{
|
||||||
return '';
|
return '';
|
||||||
@ -110,6 +133,20 @@ class icon extends \Twig\Extension\AbstractExtension
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no PNG or SVG icon was found, display a default 404 PNG icon.
|
||||||
|
if ($not_found)
|
||||||
|
{
|
||||||
|
if (empty($base_path))
|
||||||
|
{
|
||||||
|
$board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH;
|
||||||
|
$base_path = $board_url ? generate_board_url() . '/' : $environment->get_web_root_path();
|
||||||
|
}
|
||||||
|
|
||||||
|
$source = "{$base_path}styles/chameleon/theme/icons/png/404.png";
|
||||||
|
$type = 'png';
|
||||||
|
$icon = '404';
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return $environment->render("macros/icons/{$type}.html", [
|
return $environment->render("macros/icons/{$type}.html", [
|
||||||
@ -130,8 +167,11 @@ class icon extends \Twig\Extension\AbstractExtension
|
|||||||
/**
|
/**
|
||||||
* Prepare an SVG for usage in the template icon.
|
* Prepare an SVG for usage in the template icon.
|
||||||
*
|
*
|
||||||
|
* This removes any <?xml ?> and <!DOCTYPE> elements,
|
||||||
|
* aswell as any <svg> and <title> elements.
|
||||||
|
*
|
||||||
* @param \Twig\TemplateWrapper $file The SVG file loaded from the environment
|
* @param \Twig\TemplateWrapper $file The SVG file loaded from the environment
|
||||||
* @return string
|
* @return string The cleaned SVG
|
||||||
*/
|
*/
|
||||||
protected function prepare_svg(\Twig\TemplateWrapper $file)
|
protected function prepare_svg(\Twig\TemplateWrapper $file)
|
||||||
{
|
{
|
||||||
@ -195,4 +235,21 @@ class icon extends \Twig\Extension\AbstractExtension
|
|||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the style tree of the style preferred by the current user.
|
||||||
|
*
|
||||||
|
* @return array Style tree, most specific first
|
||||||
|
*/
|
||||||
|
protected function get_style_list()
|
||||||
|
{
|
||||||
|
$style_list = [$this->user->style['style_path']];
|
||||||
|
|
||||||
|
if ($this->user->style['style_parent_id'])
|
||||||
|
{
|
||||||
|
$style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $style_list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,15 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
|||||||
$auth->method('acl_get')
|
$auth->method('acl_get')
|
||||||
->willReturn(true);
|
->willReturn(true);
|
||||||
|
|
||||||
$filesystem = new \phpbb\filesystem\filesystem();
|
$filesystem = $this->createMock('\phpbb\filesystem\filesystem');
|
||||||
|
$filesystem->expects($this->any())
|
||||||
|
->method('exists')
|
||||||
|
->with($this->stringContains('theme/icons/png/'))
|
||||||
|
->will($this->returnValueMap([
|
||||||
|
['phpBB/styles/chameleon/theme/icons/png/phone.png', true],
|
||||||
|
['phpBB/styles/chameleon/theme/icons/png/pencil.png', true],
|
||||||
|
['phpBB/styles/chameleon/theme/icons/png/user.png', false],
|
||||||
|
]));
|
||||||
$request = new phpbb_mock_request;
|
$request = new phpbb_mock_request;
|
||||||
$symfony_request = new \phpbb\symfony_request(
|
$symfony_request = new \phpbb\symfony_request(
|
||||||
$request
|
$request
|
||||||
@ -365,6 +373,21 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
|||||||
],
|
],
|
||||||
'<img class="o-icon o-icon-png png-pencil my-class" src="phpBB/styles/chameleon/theme/icons/png/pencil.png" alt="Pencil icon" data-url="my-test-url/test-page.php?u=2" />',
|
'<img class="o-icon o-icon-png png-pencil my-class" src="phpBB/styles/chameleon/theme/icons/png/pencil.png" alt="Pencil icon" data-url="my-test-url/test-page.php?u=2" />',
|
||||||
],
|
],
|
||||||
|
/** PNG: Not found */
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'type' => 'png',
|
||||||
|
'icon' => 'user',
|
||||||
|
'title' => 'ICON_USER',
|
||||||
|
'hidden' => false,
|
||||||
|
'classes' => 'my-class',
|
||||||
|
'attributes' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'ICON_USER' => 'User icon',
|
||||||
|
],
|
||||||
|
'<img class="o-icon o-icon-png png-404 my-class" src="phpBB/styles/chameleon/theme/icons/png/404.png" alt="User icon" />',
|
||||||
|
],
|
||||||
/** SVG: default */
|
/** SVG: default */
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
@ -405,6 +428,19 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
|||||||
<path d="M0 0h24v24H0z" fill="none"></path>
|
<path d="M0 0h24v24H0z" fill="none"></path>
|
||||||
</svg>',
|
</svg>',
|
||||||
],
|
],
|
||||||
|
/** SVG: Not found */
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'type' => 'svg',
|
||||||
|
'icon' => 'not-existent',
|
||||||
|
'title' => 'Just a title',
|
||||||
|
'hidden' => false,
|
||||||
|
'classes' => '',
|
||||||
|
'attributes' => [],
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
'<img class="o-icon o-icon-png png-404" src="phpBB/styles/chameleon/theme/icons/png/404.png" alt="Just a title" />',
|
||||||
|
],
|
||||||
/** SVG: Sanitization */
|
/** SVG: Sanitization */
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user