ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRTE.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
30class ilRTE
31{
34 protected ilObjUser $user;
35 protected ilLanguage $lng;
38 protected ?int $initialWidth = null;
39
44 protected ?string $root_block_element = null;
45
47 protected array $plugins = [];
48
50 protected array $buttons = [];
51
56 protected array $disabled_buttons = [];
57
58 public function __construct()
59 {
60 global $DIC;
61
62 $this->tpl = $DIC['tpl'];
63 $this->ctrl = $DIC['ilCtrl'];
64 $this->lng = $DIC['lng'];
65 $this->browser = $DIC->http()->agent();
66 $this->client_init = $DIC['ilClientIniFile'];
67 $this->user = $DIC['ilUser'];
68 }
69
70 public function addPlugin(string $a_plugin_name): void
71 {
72 $this->plugins[] = $a_plugin_name;
73 }
74
75 public function addButton(string $a_button_name): void
76 {
77 $this->buttons[] = $a_button_name;
78 }
79
80 public function removePlugin(string $a_plugin_name): void
81 {
82 $key = array_search($a_plugin_name, $this->plugins, true);
83 if ($key !== false) {
84 unset($this->plugins[$key]);
85 }
86 }
87
88 public function removeAllPlugins(): void
89 {
90 foreach ($this->plugins as $plugin) {
91 $this->removePlugin($plugin);
92 }
93 }
94
95 public function removeButton(string $a_button_name): void
96 {
97 $key = array_search($a_button_name, $this->buttons, true);
98 if ($key !== false) {
99 unset($this->buttons[$key]);
100 }
101 }
102
103 public function addRTESupport(
106 int $obj_id,
107 string $obj_type,
108 string $a_module = '',
109 bool $allowFormElements = false,
110 ?string $cfg_template = null
111 ): void {
112 }
113
114 public function addUserTextEditor(string $editor_selector): void
115 {
116 }
117
124 public function addCustomRTESupport(int $obj_id, string $obj_type, array $tags): void
125 {
126 }
127
128 public static function _getRTEClassname(): string
129 {
130 global $DIC;
131 $editor = (new ilRTESettings($DIC['lng'], $DIC['ilUser']))->getRichTextEditor();
132 if (strtolower($editor) === 'tinymce') {
133 return ilTinyMCE::class;
134 }
135
136 return self::class;
137 }
138
145 public static function _cleanupMediaObjectUsage(string $a_text, string $a_usage_type, int $a_usage_id): void
146 {
147 $mobs = ilObjMediaObject::_getMobsOfObject($a_usage_type, $a_usage_id);
148 while (preg_match('/src=".*" data-id="([0-9]+)"/', $a_text, $found)) {
149 $a_text = str_replace($found[0], '', $a_text);
150 $found_mob_id = (int) $found[1];
151
152 if (!in_array($found_mob_id, $mobs, true) && ilObjMediaObject::_exists($found_mob_id)) {
153 // save usage if missing
154 ilObjMediaObject::_saveUsage($found_mob_id, $a_usage_type, $a_usage_id);
155 } else {
156 // if already saved everything ok -> take mob out of mobs array
157 unset($mobs[$found_mob_id]);
158 }
159 }
160 // remaining usages are not in text anymore -> delete them
161 // and media objects (note: delete method of ilObjMediaObject
162 // checks whether object is used in another context; if yes,
163 // the object is not deleted!)
164 foreach ($mobs as $mob) {
165 ilObjMediaObject::_removeUsage($mob, $a_usage_type, $a_usage_id);
166 $mob_obj = new ilObjMediaObject($mob);
167 $mob_obj->delete();
168 }
169 }
170
181 public static function replaceLatexSpan(string $text): string
182 {
183 $start = '<span class="latex">';
184 $end = '</span>';
185 $start_len = ilStr::strLen($start);
186 $end_len = ilStr::strLen($end);
187
188 // current position to start the search for delimiters
189 $cpos = 0;
190
191 // find position of start delimiter
192 while (is_int($spos = ilStr::strIPos($text, $start, $cpos))) {
193
194 // find position of end delimiter
195 $epos = ilStr::strIPos($text, $end, $spos + $start_len);
196 if (!is_int($epos)) {
197 break;
198 }
199
200 // extract the tex code inside the delimiters
201 $tex = ilStr::subStr($text, $spos + $start_len, $epos - $spos - $start_len);
202
203 // wrap the tex code in new delimiters
204 $replace = '[tex]' . $tex . '[/tex]';
205
206 // replace the span
207 $text = ilStr::subStr($text, 0, $spos) . $replace . ilStr::subStr($text, $epos + $end_len);
208
209 // continue search behind the replacement
210 $cpos = $spos + ilStr::strLen($replace);
211
212 if ($cpos >= ilStr::strlen($text)) {
213 // current position at the end => stop search
214 break;
215 }
216 }
217
218 return $text;
219 }
220
228 public static function _replaceMediaObjectImageSrc(
229 string $a_text,
230 int $a_direction = 0,
231 string $nic = ''
232 ): string {
233 if ($a_text === '') {
234 return '';
235 }
236
237 if ($nic === '' && defined('IL_INST_ID')) {
238 $nic = (string) IL_INST_ID;
239 }
240
241 if ($a_direction === 0) {
242 $a_text = preg_replace(
243 '/src=".*" data-id="([0-9]+)"/',
244 'src="il_' . $nic . '_mob_\\1"',
245 $a_text
246 );
247 } else {
248 $resulttext = $a_text;
249 if (preg_match_all('/src="(il_[0-9]+_mob_([0-9]+))"/', $a_text, $matches)) {
250 foreach ($matches[2] as $idx => $mob) {
251 if (ilObject::_lookupType((int) $mob) === 'mob') {
252 $mob_obj = new ilObjMediaObject((int) $mob);
253 $path_to_file = $mob_obj->getStandardSrc();
254 $resulttext = str_replace("src=\"{$matches[1][$idx]}\"", "src=\"{$path_to_file}\" data-id=\"{$matches[2][$idx]}\"", $resulttext);
255 }
256 }
257 }
258 $a_text = $resulttext;
259 }
260
261 return $a_text;
262 }
263
270 public static function _getMediaObjects(string $a_text, int $a_direction = 0): array
271 {
272 if ($a_text === '') {
273 return [];
274 }
275
276 $mediaObjects = [];
277 if ($a_direction === 0) {
278 $is_matching = preg_match_all('/src=".*" data-id="([0-9]+)"/', $a_text, $matches);
279 } else {
280 $is_matching = preg_match_all('/src="il_[0-9]+_mob_([0-9]+)"/', $a_text, $matches);
281 }
282
283 if ($is_matching) {
284 foreach ($matches[1] as $mob) {
285 $mob = (int) $mob;
286
287 if (ilObjMediaObject::_exists($mob) && !in_array($mob, $mediaObjects, true)) {
288 $mediaObjects[] = $mob;
289 }
290 }
291 }
292
293 return $mediaObjects;
294 }
295
296 public function setRTERootBlockElement(?string $a_root_block_element): self
297 {
298 $this->root_block_element = $a_root_block_element;
299 return $this;
300 }
301
302 public function getRTERootBlockElement(): ?string
303 {
304 return $this->root_block_element;
305 }
306
312 public function disableButtons($a_button): self
313 {
314 if (is_array($a_button)) {
315 $this->disabled_buttons = array_unique(array_merge($this->disabled_buttons, $a_button));
316 } else {
317 $this->disabled_buttons = array_unique(array_merge($this->disabled_buttons, [$a_button]));
318 }
319
320 return $this;
321 }
322
328 public function getDisabledButtons(bool $as_list = true)
329 {
330 if (!$as_list) {
331 return implode(',', $this->disabled_buttons);
332 }
333
334 return $this->disabled_buttons;
335 }
336
337 public function getInitialWidth(): ?int
338 {
339 return $this->initialWidth;
340 }
341
342 public function setInitialWidth(?int $initialWidth): void
343 {
344 $this->initialWidth = $initialWidth;
345 }
346}
This library is borrowed from the phpGroupWare API http://www.phpgroupware.org/api Modifications made...
INIFile Parser Early access in init proceess! Avoid further dependencies like logging or other servic...
language handling
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
static _removeUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Remove usage of mob in another container.
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
User class.
static _lookupType(int $id, bool $reference=false)
Rich Text Editor base class This class provides access methods to a Rich Text Editor (RTE) integrated...
Definition: class.ilRTE.php:31
addRTESupport(Language $lng, ilObjUser $user, int $obj_id, string $obj_type, string $a_module='', bool $allowFormElements=false, ?string $cfg_template=null)
setInitialWidth(?int $initialWidth)
ilObjUser $user
Definition: class.ilRTE.php:34
__construct()
Definition: class.ilRTE.php:58
addButton(string $a_button_name)
Definition: class.ilRTE.php:75
removePlugin(string $a_plugin_name)
Definition: class.ilRTE.php:80
ilGlobalTemplateInterface $tpl
Definition: class.ilRTE.php:32
static _getRTEClassname()
AgentDetermination $browser
Definition: class.ilRTE.php:36
array $disabled_buttons
Definition: class.ilRTE.php:56
ilLanguage $lng
Definition: class.ilRTE.php:35
getRTERootBlockElement()
array $plugins
Definition: class.ilRTE.php:47
string $root_block_element
Definition: class.ilRTE.php:44
static replaceLatexSpan(string $text)
Replace the latex delimiters used by the rich text editor Unfortunately these can't be processed by M...
getDisabledButtons(bool $as_list=true)
Returns the disabled RTE buttons.
addUserTextEditor(string $editor_selector)
removeButton(string $a_button_name)
Definition: class.ilRTE.php:95
addPlugin(string $a_plugin_name)
Definition: class.ilRTE.php:70
static _cleanupMediaObjectUsage(string $a_text, string $a_usage_type, int $a_usage_id)
Synchronises appearances of media objects in $a_text with media object usage table.
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
int $initialWidth
Definition: class.ilRTE.php:38
removeAllPlugins()
Definition: class.ilRTE.php:88
disableButtons($a_button)
Sets buttons which should be disabled in the RTE.
array $buttons
Definition: class.ilRTE.php:50
getInitialWidth()
addCustomRTESupport(int $obj_id, string $obj_type, array $tags)
Adds custom support for an RTE in an ILIAS form.
setRTERootBlockElement(?string $a_root_block_element)
ilIniFile $client_init
Definition: class.ilRTE.php:37
static _getMediaObjects(string $a_text, int $a_direction=0)
Returns all media objects found in the passed string.
ilCtrlInterface $ctrl
Definition: class.ilRTE.php:33
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:21
static strLen(string $a_string)
Definition: class.ilStr.php:60
static strIPos(string $a_haystack, string $a_needle, int $a_offset=0)
Definition: class.ilStr.php:51
const IL_INST_ID
Definition: constants.php:40
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26