ILIAS  release_8 Revision v8.23
class.ilRTE.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
29 class ilRTE
30 {
31  public const ILIAS_IMG_MANAGER_PLUGIN = 'ilias_image_manager_plugin';
32 
35  protected ilObjUser $user;
36  protected ilLanguage $lng;
39  protected ?int $initialWidth = null;
40 
45  protected ?string $root_block_element = null;
46 
48  protected array $plugins = [];
49 
51  protected array $buttons = [];
52 
57  protected array $disabled_buttons = [];
58 
59  public function __construct()
60  {
61  global $DIC;
62 
63  $this->tpl = $DIC['tpl'];
64  $this->ctrl = $DIC['ilCtrl'];
65  $this->lng = $DIC['lng'];
66  $this->browser = $DIC->http()->agent();
67  $this->client_init = $DIC['ilClientIniFile'];
68  $this->user = $DIC['ilUser'];
69  }
70 
71  public function addPlugin(string $a_plugin_name): void
72  {
73  $this->plugins[] = $a_plugin_name;
74  }
75 
76  public function addButton(string $a_button_name): void
77  {
78  $this->buttons[] = $a_button_name;
79  }
80 
81  public function removePlugin(string $a_plugin_name): void
82  {
83  $key = array_search($a_plugin_name, $this->plugins, true);
84  if ($key !== false) {
85  unset($this->plugins[$key]);
86  }
87  }
88 
89  public function removeAllPlugins(): void
90  {
91  foreach ($this->plugins as $plugin) {
92  $this->removePlugin($plugin);
93  }
94  }
95 
96  public function removeButton(string $a_button_name): void
97  {
98  $key = array_search($a_button_name, $this->buttons, true);
99  if ($key !== false) {
100  unset($this->buttons[$key]);
101  }
102  }
103 
104  public function addRTESupport(
105  int $obj_id,
106  string $obj_type,
107  string $a_module = '',
108  bool $allowFormElements = false,
109  ?string $cfg_template = null,
110  bool $hide_switch = false
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  {
131  if (strtolower($editor) === 'tinymce') {
132  return ilTinyMCE::class;
133  }
134 
135  return self::class;
136  }
137 
144  public static function _cleanupMediaObjectUsage(string $a_text, string $a_usage_type, int $a_usage_id): void
145  {
146  $mobs = ilObjMediaObject::_getMobsOfObject($a_usage_type, $a_usage_id);
147  while (preg_match("/data\/" . CLIENT_ID . "\/mobs\/mm_([0-9]+)/i", $a_text, $found)) {
148  $a_text = str_replace($found[0], '', $a_text);
149  $found_mob_id = (int) $found[1];
150 
151  if (!in_array($found_mob_id, $mobs, true)) {
152  // save usage if missing
153  ilObjMediaObject::_saveUsage($found_mob_id, $a_usage_type, $a_usage_id);
154  } else {
155  // if already saved everything ok -> take mob out of mobs array
156  unset($mobs[$found_mob_id]);
157  }
158  }
159  // remaining usages are not in text anymore -> delete them
160  // and media objects (note: delete method of ilObjMediaObject
161  // checks whether object is used in another context; if yes,
162  // the object is not deleted!)
163  foreach ($mobs as $mob) {
164  ilObjMediaObject::_removeUsage($mob, $a_usage_type, $a_usage_id);
165  $mob_obj = new ilObjMediaObject($mob);
166  $mob_obj->delete();
167  }
168  }
169 
177  public static function _replaceMediaObjectImageSrc(
178  string $a_text,
179  int $a_direction = 0,
180  string $nic = ''
181  ): string {
182  if ($a_text === '') {
183  return '';
184  }
185 
186  if ($nic === '' && defined('IL_INST_ID')) {
187  $nic = (string) IL_INST_ID;
188  }
189 
190  if ($a_direction === 0) {
191  $a_text = preg_replace(
192  '/src="([^"]*?\/mobs\/mm_([0-9]+)\/.*?)\"/',
193  'src="il_' . $nic . '_mob_\\2"',
194  $a_text
195  );
196  } else {
197  $resulttext = $a_text;
198  if (preg_match_all('/src="il_([0-9]+)_mob_([0-9]+)"/', $a_text, $matches)) {
199  foreach ($matches[2] as $idx => $mob) {
200  if (ilObject::_lookupType((int) $mob) === 'mob') {
201  $mob_obj = new ilObjMediaObject((int) $mob);
202  $replace = 'il_' . $matches[1][$idx] . '_mob_' . $mob;
203  $path_to_file = ilWACSignedPath::signFile(
204  ILIAS_HTTP_PATH . '/data/' . CLIENT_ID . '/mobs/mm_' . $mob . '/' . $mob_obj->getTitle()
205  );
206  $resulttext = str_replace("src=\"$replace\"", "src=\"" . $path_to_file . "\"", $resulttext);
207  }
208  }
209  }
210  $a_text = $resulttext;
211  }
212 
213  return $a_text;
214  }
215 
222  public static function _getMediaObjects(string $a_text, int $a_direction = 0): array
223  {
224  if ($a_text === '') {
225  return [];
226  }
227 
228  $mediaObjects = [];
229  if ($a_direction === 0) {
230  $is_matching = preg_match_all('/src="([^"]*?\/mobs\/mm_([0-9]+)\/.*?)\"/', $a_text, $matches);
231  } else {
232  $is_matching = preg_match_all('/src="il_([0-9]+)_mob_([0-9]+)"/', $a_text, $matches);
233  }
234 
235  if ($is_matching) {
236  foreach ($matches[2] as $idx => $mob) {
237  $mob = (int) $mob;
238 
239  if (ilObjMediaObject::_exists($mob) && !in_array($mob, $mediaObjects, true)) {
240  $mediaObjects[] = $mob;
241  }
242  }
243  }
244 
245  return $mediaObjects;
246  }
247 
248  public function setRTERootBlockElement(?string $a_root_block_element): self
249  {
250  $this->root_block_element = $a_root_block_element;
251  return $this;
252  }
253 
254  public function getRTERootBlockElement(): ?string
255  {
257  }
258 
264  public function disableButtons($a_button): self
265  {
266  if (is_array($a_button)) {
267  $this->disabled_buttons = array_unique(array_merge($this->disabled_buttons, $a_button));
268  } else {
269  $this->disabled_buttons = array_unique(array_merge($this->disabled_buttons, [$a_button]));
270  }
271 
272  return $this;
273  }
274 
280  public function getDisabledButtons(bool $as_list = true)
281  {
282  if (!$as_list) {
283  return implode(',', $this->disabled_buttons);
284  }
285 
287  }
288 
289  public function getInitialWidth(): ?int
290  {
291  return $this->initialWidth;
292  }
293 
294  public function setInitialWidth(?int $initialWidth): void
295  {
296  $this->initialWidth = $initialWidth;
297  }
298 }
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...
static _getMediaObjects(string $a_text, int $a_direction=0)
Returns all media objects found in the passed string.
static _getRTEClassname()
__construct()
Definition: class.ilRTE.php:59
const IL_INST_ID
Definition: constants.php:40
This library is borrowed from the phpGroupWare API http://www.phpgroupware.org/api Modifications made...
$mobs
Definition: imgupload.php:70
static _getRichTextEditor()
Returns the identifier for the Rich Text Editor.
addPlugin(string $a_plugin_name)
Definition: class.ilRTE.php:71
int $initialWidth
Definition: class.ilRTE.php:39
array $disabled_buttons
Definition: class.ilRTE.php:57
disableButtons($a_button)
Sets buttons which should be disabled in the RTE.
removePlugin(string $a_plugin_name)
Definition: class.ilRTE.php:81
removeAllPlugins()
Definition: class.ilRTE.php:89
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.
AgentDetermination $browser
Definition: class.ilRTE.php:37
addCustomRTESupport(int $obj_id, string $obj_type, array $tags)
Adds custom support for an RTE in an ILIAS form.
ilLanguage $lng
Definition: class.ilRTE.php:36
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.
array $buttons
Definition: class.ilRTE.php:51
getInitialWidth()
global $DIC
Definition: feed.php:28
ilIniFile $client_init
Definition: class.ilRTE.php:38
string $root_block_element
Definition: class.ilRTE.php:45
addUserTextEditor(string $editor_selector)
Rich Text Editor base class This class provides access methods to a Rich Text Editor (RTE) integrated...
Definition: class.ilRTE.php:29
ilObjUser $user
Definition: class.ilRTE.php:35
const ILIAS_IMG_MANAGER_PLUGIN
Definition: class.ilRTE.php:31
const CLIENT_ID
Definition: constants.php:41
string $key
Consumer key/client ID value.
Definition: System.php:193
static _exists(int $id, bool $reference=false, ?string $type=null)
setInitialWidth(?int $initialWidth)
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
ilCtrlInterface $ctrl
Definition: class.ilRTE.php:34
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.
getDisabledButtons(bool $as_list=true)
Returns the disabled RTE buttons.
addRTESupport(int $obj_id, string $obj_type, string $a_module='', bool $allowFormElements=false, ?string $cfg_template=null, bool $hide_switch=false)
getRTERootBlockElement()
setRTERootBlockElement(?string $a_root_block_element)
static signFile(string $path_to_file)
addButton(string $a_button_name)
Definition: class.ilRTE.php:76
removeButton(string $a_button_name)
Definition: class.ilRTE.php:96
ilGlobalTemplateInterface $tpl
Definition: class.ilRTE.php:33
static _lookupType(int $id, bool $reference=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
array $plugins
Definition: class.ilRTE.php:48