ILIAS  release_8 Revision v8.24
class.ilPCPlugged.php
Go to the documentation of this file.
1<?php
2
26{
27 protected ilLanguage $lng;
31
32 public function init(): void
33 {
34 global $DIC;
35
36 $this->lng = $DIC->language();
37 $this->setType("plug");
38 $this->component_repository = $DIC["component.repository"];
39 $this->component_factory = $DIC["component.factory"];
40 }
41
42 public function setNode(php4DOMElement $a_node): void
43 {
44 parent::setNode($a_node); // this is the PageContent node
45 $this->plug_node = $a_node->first_child(); // this is the Plugged node
46 }
47
51 public function create(
52 ilPageObject $a_pg_obj,
53 string $a_hier_id,
54 string $a_pc_id,
55 string $a_plugin_name,
56 string $a_plugin_version
57 ): void {
58 $this->node = $this->createPageContentNode();
59 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
60 $this->plug_node = $this->dom->create_element("Plugged");
61 $this->plug_node = $this->node->append_child($this->plug_node);
62 $this->plug_node->set_attribute("PluginName", $a_plugin_name);
63 $this->plug_node->set_attribute("PluginVersion", $a_plugin_version);
64 }
65
69 public function setProperties(array $a_properties): void
70 {
71 if (!is_object($this->plug_node)) {
72 return;
73 }
74
75 // delete properties
76 $children = $this->plug_node->child_nodes();
77 for ($i = 0; $i < count($children); $i++) {
78 $this->plug_node->remove_child($children[$i]);
79 }
80 // set properties
81 foreach ($a_properties as $key => $value) {
82 $prop_node = $this->dom->create_element("PluggedProperty");
83 $prop_node = $this->plug_node->append_child($prop_node);
84 $prop_node->set_attribute("Name", $key);
85 if ($value != "") {
86 $prop_node->set_content($value);
87 }
88 }
89 }
90
94 public function getProperties(): array
95 {
96 $properties = array();
97
98 if (is_object($this->plug_node)) {
99 // delete properties
100 $children = $this->plug_node->child_nodes();
101 for ($i = 0; $i < count($children); $i++) {
102 if ($children[$i]->node_name() == "PluggedProperty") {
103 $properties[$children[$i]->get_attribute("Name")] =
104 $children[$i]->get_content();
105 }
106 }
107 }
108
109 return $properties;
110 }
111
112 public function setPluginVersion(string $a_version): void
113 {
114 if (!empty($a_version)) {
115 $this->plug_node->set_attribute("PluginVersion", $a_version);
116 } else {
117 if ($this->plug_node->has_attribute("PluginVersion")) {
118 $this->plug_node->remove_attribute("PluginVersion");
119 }
120 }
121 }
122
123 public function getPluginVersion(): string
124 {
125 if (is_object($this->plug_node)) {
126 return $this->plug_node->get_attribute("PluginVersion");
127 }
128 return "";
129 }
130
131 public function setPluginName(string $a_name): void
132 {
133 if (!empty($a_name)) {
134 $this->plug_node->set_attribute("PluginName", $a_name);
135 } else {
136 if ($this->plug_node->has_attribute("PluginName")) {
137 $this->plug_node->remove_attribute("PluginName");
138 }
139 }
140 }
141
142 public function getPluginName(): string
143 {
144 if (is_object($this->plug_node)) {
145 return $this->plug_node->get_attribute("PluginName");
146 }
147 return "";
148 }
149
154 public static function handleCopiedPluggedContent(
155 ilPageObject $a_page,
156 DOMDocument $a_domdoc
157 ): void {
158 global $DIC;
159 $component_repository = $DIC['component.repository'];
160 $component_factory = $DIC['component.factory'];
161
162 $xpath = new DOMXPath($a_domdoc);
163 $nodes = $xpath->query("//Plugged");
164
166 foreach ($nodes as $node) {
167 $plugin_name = $node->getAttribute('PluginName');
168 $plugin_version = $node->getAttribute('PluginVersion');
169
170 $plugin_info = null;
171 try {
172 $plugin_info = $component_repository->getPluginByName($plugin_name);
173 } catch (Exception $e) {
174 }
175
176 if (is_object($plugin_info) && $plugin_info->isActive()) {
178 $plugin_obj = $component_factory->getPlugin($plugin_info->getId());
179 $plugin_obj->setPageObj($a_page);
180
181 $properties = array();
183 foreach ($node->childNodes as $child) {
184 $properties[$child->getAttribute('Name')] = $child->nodeValue;
185 }
186
187 // let the plugin copy additional content
188 // and allow it to modify the saved parameters
189 $plugin_obj->onClone($properties, $plugin_version);
190
191 foreach ($node->childNodes as $child) {
192 $node->removeChild($child);
193 }
194 foreach ($properties as $name => $value) {
195 $child = new DOMElement(
196 'PluggedProperty',
197 str_replace("&", "&amp;", $value)
198 );
199 $node->appendChild($child);
200 $child->setAttribute('Name', $name);
201 }
202 }
203 }
204 }
205
209 public static function afterRepositoryCopy(ilPageObject $page, array $mapping, int $source_ref_id): void
210 {
211 global $DIC;
212 $ilPluginAdmin = $DIC['ilPluginAdmin'];
213
215 $component_factory = $DIC["component.factory"];
216
218 $component_repository = $DIC["component.repository"];
219
220 $xpath = new DOMXPath($page->getDomDoc());
221 $nodes = $xpath->query("//Plugged");
222
224 foreach ($nodes as $node) {
225 $plugin_name = $node->getAttribute('PluginName');
226 $plugin_version = $node->getAttribute('PluginVersion');
227
228 $info = null;
229 try {
230 $info = $component_repository->getPluginByName($plugin_name);
231 } catch (InvalidArgumentException $e) {
232 }
233 if (!is_null($info) && $info->isActive()) {
235 $plugin_obj = $component_factory->getPlugin($info->getId());
236 $plugin_obj->setPageObj($page);
237
238 $properties = array();
240 foreach ($node->childNodes as $child) {
241 $properties[$child->getAttribute('Name')] = $child->nodeValue;
242 }
243
244 // let the plugin copy additional content
245 // and allow it to modify the saved parameters
246 $plugin_obj->afterRepositoryCopy($properties, $mapping, $source_ref_id, $plugin_version);
247
248 foreach ($node->childNodes as $child) {
249 $node->removeChild($child);
250 }
251 foreach ($properties as $name => $value) {
252 $child = new DOMElement(
253 'PluggedProperty',
254 str_replace("&", "&amp;", $value)
255 );
256 $node->appendChild($child);
257 $child->setAttribute('Name', $name);
258 }
259 }
260 }
261 }
262
267 public static function handleDeletedPluggedNode(
268 ilPageObject $a_page,
269 DOMNode $a_node,
270 bool $move_operation = false
271 ): void {
272 global $DIC;
273 $component_repository = $DIC['component.repository'];
274 $component_factory = $DIC['component.factory'];
275
276 $plugin_name = $a_node->getAttribute('PluginName');
277 $plugin_version = $a_node->getAttribute('PluginVersion');
278
279 $plugin_info = $component_repository->getPluginByName($plugin_name);
280 if ($plugin_info->isActive()) {
282 $plugin_obj = $component_factory->getPlugin($plugin_info->getId());
283 $plugin_obj->setPageObj($a_page);
284
285 $properties = array();
287 foreach ($a_node->childNodes as $child) {
288 $properties[$child->getAttribute('Name')] = $child->nodeValue;
289 }
290
291 // let the plugin delete additional content
292 $plugin_obj->onDelete($properties, $plugin_version, $move_operation);
293 }
294 }
295
297 string $a_output,
298 string $a_mode,
299 bool $a_abstract_only = false
300 ): string {
301 $lng = $this->lng;
302
303 $end = 0;
304 $start = strpos($a_output, "{{{{{Plugged<pl");
305 //echo htmlentities($a_html)."-";
306 if (is_int($start)) {
307 $end = strpos($a_output, "}}}}}", $start);
308 }
309
310 while ($end > 0) {
311 $param = substr($a_output, $start + 5, $end - $start - 5);
312 $param = str_replace(' xmlns:xhtml="http://www.w3.org/1999/xhtml"', "", $param);
313 $param = explode("<pl/>", $param);
314 //var_dump($param); exit;
315 $plugin_name = $param[1];
316 $plugin_version = $param[2];
317 $properties = array();
318
319 for ($i = 3; $i < count($param); $i += 2) {
320 $properties[$param[$i]] = $param[$i + 1];
321 }
322
323 // get html from plugin
324 if ($a_mode == "edit") {
325 $plugin_html = '<div class="ilBox">' . $lng->txt("content_plugin_not_activated") . " (" . $plugin_name . ")</div>";
326 }
327
328 $plugin_info = $this->component_repository->getPluginByName($plugin_name);
329 $plugin_html = '';
330 if ($plugin_info->isActive()) {
331 $plugin_obj = $this->component_factory->getPlugin($plugin_info->getId());
332 $plugin_obj->setPageObj($this->getPage());
333 $gui_obj = $plugin_obj->getUIClassInstance();
334 $plugin_html = $gui_obj->getElementHTML($a_mode, $properties, $plugin_version);
335 } else {
336 if ($a_mode === "edit") {
337 $plugin_html = sprintf($this->lng->txt("copg_plugin_not_avail"), $plugin_name);
338 } else {
339 $plugin_html = " ";
340 }
341 }
342
343 $a_output = substr($a_output, 0, $start) .
344 $plugin_html .
345 substr($a_output, $end + 5);
346
347 if (strlen($a_output) > $start + 5) {
348 $start = strpos($a_output, "{{{{{Plugged<pl", $start + 5);
349 } else {
350 $start = false;
351 }
352 $end = 0;
353 if (is_int($start)) {
354 $end = strpos($a_output, "}}}}}", $start);
355 }
356 }
357
358 return $a_output;
359 }
360
361 public function getJavascriptFiles(string $a_mode): array
362 {
363 $js_files = array();
364
365 foreach ($this->component_factory->getActivePluginsInSlot("pgcp") as $plugin) {
366 $plugin->setPageObj($this->getPage());
367 $pl_dir = $plugin->getDirectory();
368
369 $pl_js_files = $plugin->getJavascriptFiles($a_mode);
370 foreach ($pl_js_files as $pl_js_file) {
371 if (!is_int(strpos($pl_js_file, "//"))) {
372 $pl_js_file = $pl_dir . "/" . $pl_js_file;
373 }
374 if (!in_array($pl_js_file, $js_files)) {
375 $js_files[] = $pl_js_file;
376 }
377 }
378 }
379 //var_dump($js_files);
380 return $js_files;
381 }
382
383 public function getCssFiles(string $a_mode): array
384 {
385 $css_files = array();
386
387 foreach ($this->component_factory->getActivePluginsInSlot("pgcp") as $plugin) {
388 $plugin->setPageObj($this->getPage());
389 $pl_dir = $plugin->getDirectory();
390
391 $pl_css_files = $plugin->getCssFiles($a_mode);
392 foreach ($pl_css_files as $pl_css_file) {
393 if (!is_int(strpos($pl_css_file, "//"))) {
394 $pl_css_file = $pl_dir . "/" . $pl_css_file;
395 }
396 if (!in_array($pl_css_file, $css_files)) {
397 $css_files[] = $pl_css_file;
398 }
399 }
400 }
401
402 return $css_files;
403 }
404}
const IL_INSERT_AFTER
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
php4DOMElement $plug_node
modifyPageContentPostXsl(string $a_output, string $a_mode, bool $a_abstract_only=false)
Modify page content after xsl.
create(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id, string $a_plugin_name, string $a_plugin_version)
Create plugged node in xml.
setPluginVersion(string $a_version)
init()
Init object.
setPluginName(string $a_name)
setNode(php4DOMElement $a_node)
Set xml node of page content.
ilComponentFactory $component_factory
ilLanguage $lng
ilComponentRepository $component_repository
getProperties()
Get properties of plugged component.
getCssFiles(string $a_mode)
setProperties(array $a_properties)
Set properties of plugged component.
getJavascriptFiles(string $a_mode)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createPageContentNode(bool $a_set_this_node=true)
Create page content node (always use this method first when adding a new element)
setType(string $a_type)
Set Type.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
insertContent(ilPageContent $a_cont_obj, string $a_pos, int $a_mode=IL_INSERT_AFTER, string $a_pcid="", bool $remove_placeholder=true)
insert a content node before/after a sibling or as first child of a parent
global $DIC
Definition: feed.php:28
global $ilPluginAdmin
Definition: goto.php:23
getPlugin(string $id)
Get the plugin for the given id.
Readable part of repository interface to ilComponentDataDB.
getPluginByName(string $name)
Get a plugin by name.
if($format !==null) $name
Definition: metadata.php:247
$i
Definition: metadata.php:41
string $key
Consumer key/client ID value.
Definition: System.php:193
$lng
$param
Definition: xapitoken.php:46