ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ParagraphCommandActionHandler.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */
4
6
9
14{
18 protected $ui;
19
23 protected $lng;
24
28 protected $page_gui;
29
33 protected $user;
34
39
43 protected $ui_wrapper;
44
46 {
47 global $DIC;
48
49 $this->ui = $DIC->ui();
50 $this->lng = $DIC->language();
51 $this->page_gui = $page_gui;
52 $this->user = $DIC->user();
53
54 $this->response_factory = new ParagraphResponseFactory();
55
56 $this->ui_wrapper = new Server\UIWrapper($this->ui, $this->lng);
57 }
58
64 public function handle($query, $body) : Server\Response
65 {
66 switch ($body["action"]) {
67 case "insert":
68 return $this->insertCommand($body);
69 break;
70
71 case "update":
72 return $this->updateCommand($body);
73 break;
74
75 case "update.auto":
76 return $this->autoUpdateCommand($body);
77 break;
78
79 case "insert.auto":
80 return $this->autoInsertCommand($body);
81 break;
82
83 case "split":
84 return $this->split($body);
85 break;
86
87 case "cmd.sec.class":
88 return $this->sectionClassCommand($body);
89 break;
90
91 case "cmd.merge.previous":
92 return $this->mergePrevious($body);
93 break;
94
95 case "cmd.cancel":
96 return $this->cancelCommand($body);
97 break;
98
99 case "delete":
100 return $this->deleteCommand($body);
101 break;
102
103 default:
104 throw new Exception("Unknown action " . $body["action"]);
105 break;
106 }
107 }
108
114 protected function insertCommand($body, $auto = false) : Server\Response
115 {
116 $updated = $this->insertParagraph($body["data"]["pcid"], $body["data"]["after_pcid"], $body["data"]["content"], $body["data"]["characteristic"], $body["data"]["fromPlaceholder"]);
117
118 return $this->response_factory->getResponseObject($this->page_gui, $updated, $body["data"]["pcid"]);
119 }
120
126 protected function insertParagraph($pcid, $after_pcid, $content, $characteristic, bool $from_placeholder = false)
127 {
128 $page = $this->page_gui->getPageObject();
129
130 $pcid = ":" . $pcid;
131 $insert_id = $this->getFullIdForPCId($page, $after_pcid);
132 $content = $this->getContentForSaving($pcid, $content, $characteristic);
133
134 $this->content_obj = new \ilPCParagraph($page);
135 return $this->content_obj->saveJS(
136 $page,
137 $content,
138 \ilUtil::stripSlashes($characteristic),
139 \ilUtil::stripSlashes($pcid),
140 $insert_id,
141 $from_placeholder
142 );
143 }
144
150 protected function autoInsertCommand($body) : Server\Response
151 {
152 return $this->insertCommand($body, true);
153 }
154
160 protected function updateCommand($body, $auto = false) : Server\Response
161 {
162 $updated = $this->updateParagraph($body["data"]["pcid"], $body["data"]["content"], $body["data"]["characteristic"]);
163
164 // note: the initial_section_class is only sent from
165 // a cancel command to reset the class, if changed while editing
166
167 if ($body["data"]["initial_section_class"] != "") {
168 $page = $this->page_gui->getPageObject();
169 $page->addHierIDs();
170 $parent = $page->getParentContentObjectForPcId($body["data"]["pcid"]);
171 // case 1: parent section exists and new characteristic is not empty
172 if (!is_null($parent) && $parent->getType() == "sec") {
173 $parent->setCharacteristic($body["data"]["initial_section_class"]);
174 $updated = $page->update();
175 }
176 }
177
178 return $this->response_factory->getResponseObject($this->page_gui, $updated, $body["data"]["pcid"]);
179 }
180
186 protected function updateParagraph($pcid, $content, $characteristic)
187 {
188 $page = $this->page_gui->getPageObject();
189
190 $pcid = $this->getFullIdForPCId($page, $pcid);
191 $content = $this->getContentForSaving($pcid, $content, $characteristic);
192 $this->content_obj = new \ilPCParagraph($page);
193 return $this->content_obj->saveJS(
194 $page,
195 $content,
196 \ilUtil::stripSlashes($characteristic),
198 );
199 }
200
206 protected function autoUpdateCommand($body) : Server\Response
207 {
208 return $this->updateCommand($body, true);
209 }
210
216 protected function split($body, $auto = false) : Server\Response
217 {
218 $page = $this->page_gui->getPageObject();
219
220 $pcid = ":" . $body["data"]["pcid"];
221 $insert_id = null;
222 if ($body["data"]["insert_mode"]) {
223 $insert_id = $this->getFullIdForPCId($page, $body["data"]["after_pcid"]);
224 }
225
226 $content = $this->getContentForSaving($pcid, $body["data"]["text"], $body["data"]["characteristic"]);
227
228 $content_obj = new \ilPCParagraph($page);
229 $updated = $content_obj->saveJS(
230 $page,
231 $content,
232 \ilUtil::stripSlashes($body["data"]["characteristic"]),
233 \ilUtil::stripSlashes($pcid),
234 $insert_id,
235 $body["data"]["fromPlaceholder"]
236 );
237 $current_after_id = $body["data"]["pcid"];
238 $all_pc_ids[] = $current_after_id;
239
240 foreach ($body["data"]["new_paragraphs"] as $p) {
241 if ($updated === true) {
242 $page->addHierIDs();
243 $insert_id = $this->getFullIdForPCId($page, $current_after_id);
244 $content = $this->getContentForSaving($p["pcid"], $p["model"]["text"], $p["model"]["characteristic"]);
245 $content_obj = new \ilPCParagraph($page);
246 $updated = $content_obj->saveJS(
247 $page,
248 $content,
249 \ilUtil::stripSlashes($p["model"]["characteristic"]),
250 ":" . \ilUtil::stripSlashes($p["pcid"]),
251 $insert_id
252 );
253 $all_pc_ids[] = $p["pcid"];
254 $current_after_id = $p["pcid"];
255 }
256 }
257
258 return $this->response_factory->getResponseObjectMulti($this->page_gui, $updated, $all_pc_ids);
259 }
260
267 protected function getFullIdForPCId($page, $pc_id)
268 {
269 $id = "pg:";
270 if (!in_array($pc_id, ["", "pg"])) {
271 $hier_ids = $page->getHierIdsForPCIds([$pc_id]);
272 $id = $hier_ids[$pc_id] . ":" . $pc_id;
273 }
274 return $id;
275 }
276
282 protected function getContentForSaving($pcid, $content, $characteristic)
283 {
284 $content = str_replace("&nbsp;", " ", $content);
285 return "<div id='" .
286 $pcid . "' class='ilc_text_block_" .
287 $characteristic . "'>" . $content . "</div>";
288 }
289
295 protected function sectionClassCommand($body)
296 {
297 $insert_mode = $body["data"]["insert_mode"];
298 $after_pcid = $body["data"]["after_pcid"];
299 $pcid = $body["data"]["pcid"];
300 $content = $body["data"]["text"];
301 $characteristic = $body["data"]["characteristic"];
302 $old_section_characteristic = $body["data"]["old_section_characteristic"];
303 $new_section_characteristic = $body["data"]["new_section_characteristic"];
304
305 // first insert/update the current paragraph
306 if (!$insert_mode) {
307 $updated = $this->updateParagraph($pcid, $content, $characteristic);
308 } else {
309 $updated = $this->insertParagraph($pcid, $after_pcid, $content, $characteristic);
310 }
311
312
314 if ($updated) {
315 $page = $this->page_gui->getPageObject();
316 $page->addHierIDs();
317 $parent = $page->getParentContentObjectForPcId($pcid);
318
319 // case 1: parent section exists and new characteristic is not empty
320 if (!is_null($parent) && $parent->getType() == "sec" && $new_section_characteristic != "") {
321 $parent->setCharacteristic($new_section_characteristic);
322 $updated = $page->update();
323 }
324 // case 2: move from none to section
325 elseif ((is_null($parent) || $parent->getType() != "sec") && $old_section_characteristic == "" && $new_section_characteristic != "") {
326 $sec = new \ilPCSection($page);
327 $hier_ids = $page->getHierIdsForPCIds([$pcid]);
328 $sec->create($page, $hier_ids[$pcid], $pcid);
329 $sec->setCharacteristic($new_section_characteristic);
330 $sec_pcid = $page->generatePcId();
331 $sec->writePCId($sec_pcid);
332 $updated = $page->update();
333 $page->addHierIDs();
334 $par = $page->getContentObjectForPcId($pcid);
335 $sec = $page->getContentObjectForPcId($sec_pcid);
336 // note: we want the pcid of the Section itself here
337 $sec_node_pc_id = $sec->getNode()->first_child()->get_attribute("PCID");
338 $hier_ids = $page->getHierIdsForPCIds([$sec_node_pc_id]);
339 $node = $par->getNode();
340 $node->unlink_node();
341 $page->insertContentNode($node, $hier_ids[$sec_node_pc_id], IL_INSERT_CHILD, $sec_node_pc_id);
342 $updated = $page->update();
343 } // case 3: move from section to none
344 elseif ((!is_null($parent) && $parent->getType() == "sec") && $old_section_characteristic != "" && $new_section_characteristic == "") {
345 // note: we want the pcid of the PageContent element of the Section here
346 $sec_node_pc_id = $parent->getNode()->get_attribute("PCID");
347 $sec_node_hier_id = $page->getHierIdForPCId($sec_node_pc_id);
348 // all kids of the section
349 $childs_reverse = array_reverse($parent->getNode()->first_child()->child_nodes());
350 foreach ($childs_reverse as $child) {
351 // unlink kid
352 $child->unlink_node();
353 // insert after section
354 $page->insertContentNode($child, $sec_node_hier_id, IL_INSERT_AFTER, $sec_node_pc_id, true);
355 }
356 // unlink section
357 $node = $parent->getNode();
358 $node->unlink_node();
359 $updated = $page->update();
360 }
361 }
362 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
363 }
364
370 protected function mergePrevious($body) : Server\Response
371 {
372 $page = $this->page_gui->getPageObject();
373
374 $updated = $this->updateParagraph(
375 $body["data"]["previousPcid"],
376 $body["data"]["newPreviousContent"],
377 $body["data"]["previousCharacteristic"]
378 );
379
380 $page->addHierIDs();
381 $hier_id = $page->getHierIdForPcId($body["data"]["pcid"]);
382 $updated = $page->deleteContents(
383 [$hier_id],
384 true,
385 $this->page_gui->getPageConfig()->getEnableSelfAssessment()
386 );
387 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
388 }
389
396 protected function cancelCommand($body)
397 {
398 $remove_section_for_pcid = $body["data"]["removeSectionFromPcid"];
399 $par_text = $body["data"]["paragraphText"];
400 $par_characteristic = $body["data"]["paragraphCharacteristic"];
401
402 $page = $this->page_gui->getPageObject();
403 $page->addHierIDs();
404 $paragraph = $page->getContentObjectForPcId($remove_section_for_pcid);
405 $parent = $page->getParentContentObjectForPcId($remove_section_for_pcid);
406 $parent_pc_id = $parent->getPCId();
407
408 $updated = true;
409
410 // case 1: parent section exists and new characteristic is not empty
411 if (!is_null($parent) && $parent->getType() == "sec") {
412 $updated = $this->updateParagraph($remove_section_for_pcid, $par_text, $par_characteristic);
413
414 if ($updated) {
415 $page->addHierIDs();
416 $page->moveContentAfter($paragraph->getHierId(), $parent->getHierId());
417 $updated = $page->update();
418 }
419
420 if ($updated) {
421 $page->addHierIDs();
422 $hid = $page->getHierIdForPcId($parent_pc_id);
423 $updated = $page->deleteContents(
424 [$hid],
425 true,
426 $this->page_gui->getPageConfig()->getEnableSelfAssessment()
427 );
428 }
429
430 if ($updated) {
431 $updated = $page->update();
432 }
433 }
434 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
435 }
436
441 protected function deleteCommand($body)
442 {
443 $pcids = [$body["data"]["pcid"]];
444
445 $page = $this->page_gui->getPageObject();
446
447 $hids = array_map(
448 function ($pcid) {
449 return $this->getIdForPCId($pcid);
450 },
451 $pcids
452 );
453
454 $updated = $page->deleteContents(
455 $hids,
456 true,
457 $this->page_gui->getPageConfig()->getEnableSelfAssessment()
458 );
459
460 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
461 }
462
468 protected function getIdForPCId($pcid)
469 {
470 $page = $this->page_gui->getPageObject();
471 $id = "pg:";
472 if (!in_array($pcid, ["", "pg"])) {
473 $hier_ids = $page->getHierIdsForPCIds([$pcid]);
474 $id = $hier_ids[$pcid] . ":" . $pcid;
475 }
476 return $id;
477 }
478}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
insertParagraph($pcid, $after_pcid, $content, $characteristic, bool $from_placeholder=false)
Insert paragraph.
Page editor json server.
const IL_INSERT_CHILD
const IL_INSERT_AFTER
Class ilPageObjectGUI.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
global $DIC
Definition: goto.php:24
$query
ui()
Definition: ui.php:5