ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.SourceCodeCommandActionHandler.php
Go to the documentation of this file.
1 <?php
2 
20 
23 
27 class SourceCodeCommandActionHandler implements Server\CommandActionHandler
28 {
29  protected \ILIAS\DI\UIServices $ui;
30  protected \ilLanguage $lng;
31  protected \ilPageObjectGUI $page_gui;
32  protected \ilObjUser $user;
33  protected Server\UIWrapper $ui_wrapper;
34 
35  public function __construct(\ilPageObjectGUI $page_gui)
36  {
37  global $DIC;
38 
39  $this->ui = $DIC->ui();
40  $this->lng = $DIC->language();
41  $this->page_gui = $page_gui;
42  $this->user = $DIC->user();
43 
44  $this->ui_wrapper = new Server\UIWrapper($this->ui, $this->lng);
45  }
46 
47  public function handle(array $query, array $body): Server\Response
48  {
49  switch ($body["action"]) {
50  case "insert":
51  return $this->insertCommand($body);
52 
53  case "update":
54  return $this->updateCommand($body);
55 
56  default:
57  throw new Exception("Unknown action " . $body["action"]);
58  }
59  }
60 
61  protected function insertCommand(array $body): Server\Response
62  {
63  $page = $this->page_gui->getPageObject();
64 
65  $hier_id = "pg";
66  $pc_id = "";
67  if (!in_array($body["after_pcid"], ["", "pg"])) {
68  $hier_ids = $page->getHierIdsForPCIds([$body["after_pcid"]]);
69  $hier_id = $hier_ids[$body["after_pcid"]];
70  $pc_id = $body["after_pcid"];
71  }
72  $manual = ((string) ($body["form/input_0"] ?? "") === "manual");
73 
74  $src = new \ilPCSourceCode($page);
75  $src->create($page, $hier_id, $pc_id);
76  if (($body["pcid"] ?? "") !== "") {
77  $src->writePCId($body["pcid"]);
78  }
79  $src_gui = new \ilPCSourceCodeGUI($page, $src, "", "");
80  $src_gui->setPageConfig($page->getPageConfig());
81  $src->setLanguage($this->user->getLanguage());
82  $src->setCharacteristic('Code');
83 
84  $updated = true;
85 
86  if ($manual) {
87  $form = $src_gui->getManualFormAdapter();
88  if ($form->isValid()) {
89  $src->setDownloadTitle(str_replace('"', '', $form->getData("title")));
90  $src->setSubCharacteristic($form->getData("subchar"));
91  $src->setShowLineNumbers($form->getData("linenumbers") ? "y" : "n");
92  $updated = $page->update();
93  }
94  } else {
95  $form = $src_gui->getImportFormAdapter();
96  if ($form->isValid()) {
97  $src->importFile((string) $form->getData("input_file"));
98  $src->setDownloadTitle(str_replace('"', '', $form->getData("title")));
99  $src->setSubCharacteristic($form->getData("subchar"));
100  $src->setShowLineNumbers($form->getData("linenumbers") ? "y" : "n");
101  $updated = $page->update();
102  }
103  }
104 
105  return $this->ui_wrapper->sendPage($this->page_gui, $updated);
106  }
107 
108  protected function updateCommand(array $body): Server\Response
109  {
110  $page = $this->page_gui->getPageObject();
111 
113  $pc_src = $page->getContentObjectForPcId($body["pcid"]);
114  $src_gui = new \ilPCSourceCodeGUI($page, $pc_src, "", $body["pcid"]);
115  $form = $src_gui->getEditingFormAdapter();
116  if ($form->isValid()) {
117  $pc_src->setDownloadTitle(str_replace('"', '', $form->getData("title")));
118  $pc_src->setSubCharacteristic($form->getData("subchar"));
119  $pc_src->setShowLineNumbers($form->getData("linenumbers") ? "y" : "n");
120  $pc_src->setText(
121  $pc_src->input2xml($body["code"], 0, false)
122  );
123  $updated = $page->update();
124  }
125  return $this->ui_wrapper->sendPage($this->page_gui, $updated);
126  }
127 }
Class ilPageObjectGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Page editor json server.
global $DIC
Definition: shib_login.php:26