ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BuildChat.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Chatroom;
22
24use ilLanguage;
26use ilChatroom;
28use ilUtil;
29use ilTemplate;
30use ilObjUser;
32use ILIAS\UI\Factory as UIFactory;
33use ILIAS\UI\Renderer as UIRenderer;
34
36{
37 public function __construct(
38 private readonly ilCtrlInterface $ilCtrl,
39 private readonly ilLanguage $ilLng,
40 private readonly ilChatroomObjectGUI $gui,
41 private readonly ilChatroom $room,
42 private readonly ilChatroomServerSettings $settings,
43 private readonly ilObjUser $user,
44 private readonly UIFactory $ui_factory,
45 private readonly UIRenderer $ui_renderer,
46 ) {
47 }
48
49 public function template(bool $read_only, array $initial, string $input, string $output): ilTemplate
50 {
51 $room_tpl = new ilTemplate('tpl.chatroom.html', true, true, 'components/ILIAS/Chatroom');
52 $set_json_var = fn($var, $value) => $room_tpl->setVariable($var, json_encode($value));
53 $set_json_var('BASEURL', $this->settings->generateClientUrl());
54 $set_json_var('INSTANCE', $this->settings->getInstance());
55 $set_json_var('SCOPE', $this->room->getRoomId());
56 $set_json_var('POSTURL', ILIAS_HTTP_PATH . '/' . $this->ilCtrl->getLinkTarget($this->gui, 'postMessage', '', true));
57 $room_tpl->setVariable('JS_CALL', 'il.Chatroom.' . ($read_only ? 'runReadOnly' : 'run'));
58
59 $set_json_var('INITIAL_DATA', $initial);
60 $set_json_var('INITIAL_USERS', $this->room->getConnectedUsers());
61 $set_json_var('DATE_FORMAT', (string) $this->user->getDateFormat());
62 $set_json_var('TIME_FORMAT', $this->timeFormat());
63 $set_json_var('NOTHING_FOUND', $this->ui_renderer->render($this->ui_factory->messageBox()->info($this->ilLng->txt('chat_osc_no_usr_found'))));
64
65 $room_tpl->setVariable('CHAT_OUTPUT', $output);
66 $room_tpl->setVariable('CHAT_INPUT', $input);
67
68 $this->renderLanguageVariables($room_tpl);
69
70 return $room_tpl;
71 }
72
73 public function initialData(array $users, bool $show_auto_messages, ?string $redirect_url, array $userinfo, array $messages): array
74 {
75 $initial = [];
76 $initial['users'] = $users;
77 $initial['redirect_url'] = $redirect_url;
78 $initial['profile_image_url'] = $this->ilCtrl->getLinkTarget($this->gui, 'view-getUserProfileImages', '', true);
79 $initial['no_profile_image_url'] = ilUtil::getImagePath('placeholder/no_photo_xxsmall.jpg');
80 $initial['subdirectory'] = $this->settings->getSubDirectory();
81
82 $initial['userinfo'] = $userinfo;
83 $initial['messages'] = $messages;
84
85 $initial['state'] = [
86 'scrolling' => true,
87 'show_auto_msg' => $show_auto_messages,
88 'system_message_update_url' => $this->ilCtrl->getFormAction($this->gui, 'view-toggleAutoMessageDisplayState', '', true, false),
89 ];
90
91 return $initial;
92 }
93
94 private function renderLanguageVariables(ilTemplate $room_tpl): void
95 {
96 $set_vars = fn($a) => array_map($room_tpl->setVariable(...), array_keys($a), array_values($a));
97
98 $js_translations = [
99 'LBL_MAINROOM' => 'chat_mainroom',
100 'LBL_JOIN' => 'chat_join',
101 'LBL_INVITE_TO_PRIVATE_ROOM' => 'invite_to_private_room',
102 'LBL_KICK' => 'chat_kick',
103 'LBL_BAN' => 'chat_ban',
104 'LBL_KICK_QUESTION' => 'kick_question',
105 'LBL_BAN_QUESTION' => 'ban_question',
106 'LBL_ADDRESS' => 'chat_address',
107 'LBL_WHISPER' => 'chat_whisper',
108 'LBL_CONNECT' => 'chat_connection_established',
109 'LBL_DISCONNECT' => 'chat_connection_disconnected',
110 'LBL_TO_MAINROOM' => 'chat_to_mainroom',
111 'LBL_WELCOME_TO_CHAT' => 'welcome_to_chat',
112 'LBL_USER_INVITED' => 'user_invited',
113 'LBL_USER_KICKED' => 'user_kicked',
114 'LBL_USER_BANNED' => 'user_banned',
115 'LBL_USER_INVITED_SELF' => 'user_invited_self',
116 'LBL_PRIVATE_ROOM_CLOSED' => 'private_room_closed',
117 'LBL_PRIVATE_ROOM_ENTERED' => 'private_room_entered',
118 'LBL_PRIVATE_ROOM_LEFT' => 'private_room_left',
119 'LBL_PRIVATE_ROOM_ENTERED_USER' => 'private_room_entered_user',
120 'LBL_KICKED_FROM_PRIVATE_ROOM' => 'kicked_from_private_room',
121 'LBL_OK' => 'ok',
122 'LBL_DELETE' => 'delete',
123 'LBL_INVITE' => 'chat_invite',
124 'LBL_CANCEL' => 'cancel',
125 'LBL_HISTORY_CLEARED' => 'history_cleared',
126 'LBL_CLEAR_ROOM_HISTORY' => 'clear_room_history',
127 'LBL_CLEAR_ROOM_HISTORY_QUESTION' => 'clear_room_history_question',
128 'LBL_END_WHISPER' => 'end_whisper',
129 'LBL_TIMEFORMAT' => 'lang_timeformat_no_sec',
130 'LBL_DATEFORMAT' => 'lang_dateformat',
131 'LBL_START_PRIVATE_CHAT' => 'start_private_chat',
132 ];
133
134 $set_vars(array_map(
135 fn($v) => json_encode($this->ilLng->txt($v), JSON_THROW_ON_ERROR),
136 $js_translations
137 ));
138
139 $this->ilLng->toJSMap([
140 'chat_user_x_is_typing' => $this->ilLng->txt('chat_user_x_is_typing'),
141 'chat_users_are_typing' => $this->ilLng->txt('chat_users_are_typing'),
142 ]);
143
144 $vars = [
145 'LBL_LAYOUT' => 'layout',
146 'LBL_SHOW_SETTINGS' => 'show_settings',
147 'LBL_USER_IN_ROOM' => 'user_in_room',
148 'LOADING_IMAGE' => 'media/loader.svg',
149 ];
150
151 $set_vars(array_map($this->ilLng->txt(...), $vars));
152 $room_tpl->setVariable('LOADING_IMAGE', ilUtil::getImagePath('media/loader.svg'));
153 }
154
155 private function timeFormat(): string
156 {
157 return match ($this->user->getTimeFormat()) {
158 (string) ilCalendarSettings::TIME_FORMAT_12 => 'h:ia',
159 default => 'H:i',
160 };
161 }
162}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
renderLanguageVariables(ilTemplate $room_tpl)
Definition: BuildChat.php:94
initialData(array $users, bool $show_auto_messages, ?string $redirect_url, array $userinfo, array $messages)
Definition: BuildChat.php:73
__construct(private readonly ilCtrlInterface $ilCtrl, private readonly ilLanguage $ilLng, private readonly ilChatroomObjectGUI $gui, private readonly ilChatroom $room, private readonly ilChatroomServerSettings $settings, private readonly ilObjUser $user, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer,)
Definition: BuildChat.php:37
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Stores all calendar relevant settings.
Class ilChatroomServerSettings.
Class ilChatroom.
Class ilCtrl provides processing control methods.
getFormAction(object $a_gui_obj, ?string $a_fallback_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
getLinkTarget(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
language handling
User class.
special template class to simplify handling of ITX/PEAR
Util class various functions, usage as namespace.
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$messages
Definition: xapiexit.php:21