1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-11 03:04:09 +02:00

Merge pull request #6470 from marc1706/ticket/17100

[ticket/17100] Introduce twig macros for commonly used form elements
This commit is contained in:
Marc Alexander
2023-05-23 20:46:31 +02:00
committed by GitHub
42 changed files with 1390 additions and 409 deletions

View File

@@ -17,9 +17,11 @@ use phpbb\auth\auth;
use phpbb\avatar\helper as avatar_helper;
use phpbb\cache\service as cache;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\language\language;
use phpbb\event\dispatcher_interface;
use phpbb\path_helper;
use phpbb\template\template;
use phpbb\user;
class helper
@@ -294,8 +296,56 @@ class helper
*
* @return array Avatar data
*/
function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false)
public function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false)
{
return $this->avatar_helper->get_group_avatar($group_row, $alt, $ignore_config, $lazy);
}
/**
* Display groups legend
*
* @param driver_interface $db
* @param template $template
* @return void
*/
public function display_legend(driver_interface $db, template $template): void
{
$order_legend = $this->config['legend_sort_groupname'] ? 'group_name' : 'group_legend';
// Grab group details for legend display
if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
{
$sql = 'SELECT group_id, group_name, group_colour, group_type, group_legend
FROM ' . GROUPS_TABLE . '
WHERE group_legend > 0
ORDER BY ' . $order_legend . ' ASC';
}
else
{
$sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend
FROM ' . GROUPS_TABLE . ' g
LEFT JOIN ' . USER_GROUP_TABLE . ' ug
ON (
g.group_id = ug.group_id
AND ug.user_id = ' . $this->user->data['user_id'] . '
AND ug.user_pending = 0
)
WHERE g.group_legend > 0
AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $this->user->data['user_id'] . ')
ORDER BY g.' . $order_legend . ' ASC';
}
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$show_group_url = $row['group_name'] != 'BOTS' && $this->auth->acl_get('u_viewprofile');
$template->assign_block_vars('LEGEND', [
'GROUP_COLOR' => $row['group_colour'] ?: '',
'GROUP_NAME' => $this->get_name($row['group_name']),
'GROUP_URL' => $show_group_url ? append_sid("{$this->path_helper->get_phpbb_root_path()}memberlist.{$this->path_helper->get_php_ext()}", 'mode=group&amp;g=' . $row['group_id']) : '',
]);
}
$db->sql_freeresult($result);
}
}

View File

@@ -153,10 +153,17 @@ class topic_form extends form
parent::render($template);
$this->user->add_lang('viewtopic');
$lang_options = phpbb_language_select($this->db, $this->recipient_lang);
$template->assign_vars(array(
'EMAIL' => $this->recipient_address,
'NAME' => $this->recipient_name,
'S_LANG_OPTIONS' => language_select($this->recipient_lang),
'LANG_OPTIONS' => [
'id' => 'lang',
'name' => 'lang',
'options' => $lang_options,
],
'MESSAGE' => $this->body,
'L_EMAIL_BODY_EXPLAIN' => $this->user->lang['EMAIL_TOPIC_EXPLAIN'],

View File

@@ -0,0 +1,221 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\twig\extension;
use phpbb\template\twig\environment;
use phpbb\user;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class forms extends AbstractExtension
{
/** @var user */
protected $user;
/**
* Constructor.
*
* @param user $user User object
*/
public function __construct(user $user)
{
$this->user = $user;
}
/**
* Returns the name of this extension.
*
* @return string The extension name
*/
public function getName()
{
return 'forms';
}
/**
* Returns a list of functions to add to the existing list.
*
* @return TwigFunction[] Array of twig functions
*/
public function getFunctions(): array
{
return [
new TwigFunction('FormsBuildTemplate', [$this, 'build_template'], ['needs_environment' => true]),
new TwigFunction('FormsDimension', [$this, 'dimension'], ['needs_environment' => true]),
new TwigFunction('FormsInput', [$this, 'input'], ['needs_environment' => true]),
new TwigFunction('FormsRadioButtons', [$this, 'radio_buttons'], ['needs_environment' => true]),
new TwigFunction('FormsSelect', [$this, 'select'], ['needs_environment' => true]),
new TwigFunction('FormsTextarea', [$this, 'textarea'], ['needs_environment' => true]),
];
}
/**
* Renders a form template
*
* @param environment $environment
* @param array $form_data
*
* @return string Rendered form template
*/
public function build_template(environment $environment, array $form_data): string
{
try
{
return $environment->render('macros/forms/build_template.twig', [
'form_data' => $form_data ?? [],
]);
}
catch (\Twig\Error\Error $e)
{
return '';
}
}
/**
* Renders form dimension fields
*
* @param environment $environment The twig environment
* @param array $form_data The form data
*
* @return string Form dimension fields
*/
public function dimension(environment $environment, array $form_data): string
{
try
{
return $environment->render('macros/forms/dimension.twig', [
'WIDTH' => $form_data['width'],
'HEIGHT' => $form_data['height'],
]);
}
catch (\Twig\Error\Error $e)
{
return '';
}
}
/**
* Renders a form input field
*
* @param environment $environment The twig environment
* @param array $form_data The form data
*
* @return string Form input field
*/
public function input(environment $environment, array $form_data): string
{
try
{
return $environment->render('macros/forms/input.twig', [
'ID' => (string) ($form_data['id'] ?? ''),
'TYPE' => (string) $form_data['type'],
'NAME' => (string) $form_data['name'],
'SIZE' => (int) ($form_data['size'] ?? 0),
'MAXLENGTH' => (int) ($form_data['maxlength'] ?? 0),
'MIN' => (int) ($form_data['min'] ?? 0),
'MAX' => (int) ($form_data['max'] ?? 0),
'STEP' => (int) ($form_data['step'] ?? 0),
'CHECKED' => (bool) ($form_data['checked'] ?? false),
'CLASS' => (string) ($form_data['class'] ?? ''),
'VALUE' => (string) ($form_data['value']),
'DATA' => $form_data['data'] ?? [],
]);
}
catch (\Twig\Error\Error $e)
{
return '';
}
}
/**
* Renders form radio buttons
*
* @param environment $environment The twig environment
* @param array $form_data The form data
*
* @return string Form radio buttons
*/
public function radio_buttons(environment $environment, array $form_data): string
{
try
{
return $environment->render('macros/forms/radio_buttons.twig', [
'FIRST_BUTTON' => $form_data['buttons'][0],
'FIRST_BUTTON_LABEL' => $form_data['buttons'][0]['label'],
'SECOND_BUTTON' => $form_data['buttons'][1],
'SECOND_BUTTON_LABEL' => $form_data['buttons'][1]['label'],
]);
}
catch (\Twig\Error\Error $e)
{
return '';
}
}
/**
* Renders a form select field
*
* @param environment $environment The twig environment
* @param array $form_data The form data
*
* @return string Form select field
*/
public function select(environment $environment, array $form_data): string
{
try
{
return $environment->render('macros/forms/select.twig', [
'ID' => (string) ($form_data['id'] ?? ''),
'CLASS' => (string) ($form_data['class'] ?? ''),
'NAME' => (string) $form_data['name'],
'TOGGLEABLE' => (bool) ($form_data['toggleable'] ?? false),
'OPTIONS' => $form_data['options'] ?? [],
'GROUP_ONLY' => (bool) ($form_data['group_only'] ?? false),
'DATA' => $form_data['data'] ?? [],
'SIZE' => (int) ($form_data['size'] ?? 0),
'MULTIPLE' => (bool) ($form_data['multiple'] ?? false),
]);
}
catch (\Twig\Error\Error $e)
{
return '';
}
}
/**
* Renders a form textarea field
*
* @param environment $environment
* @param array $form_data
*
* @return string Form textarea field
*/
public function textarea(environment $environment, array $form_data): string
{
try
{
return $environment->render('macros/forms/textarea.twig', [
'ID' => (string) $form_data['id'],
'NAME' => (string) $form_data['name'],
'ROWS' => (int) $form_data['rows'],
'COLS' => (int) $form_data['cols'],
'CONTENT' => (string) $form_data['content'],
]);
}
catch (\Twig\Error\Error $e)
{
return '';
}
}
}