ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSystemStyleLessGUI.php
Go to the documentation of this file.
1 <?php
2 include_once "Services/Form/classes/class.ilPropertyFormGUI.php";
3 include_once("Services/Style/System/classes/Utilities/class.ilSystemStyleSkinContainer.php");
4 include_once("Services/Style/System/classes/Less/class.ilSystemStyleLessFile.php");
5 include_once("Services/Style/System/classes/Utilities/class.ilSystemStyleMessageStack.php");
6 
7 
14 {
18  protected $ctrl;
19 
23  protected $lng;
24 
28  protected $tpl;
29 
33  protected $style_container;
34 
38  protected $less_file;
39 
43  protected $message_stack;
44 
45 
51  public function __construct($skin_id = "", $style_id = "")
52  {
53  global $DIC;
54 
55  $this->ctrl = $DIC->ctrl();
56  $this->lng = $DIC->language();
57  $this->tpl = $DIC["tpl"];
58 
60 
61  if ($skin_id == "") {
62  $skin_id = $_GET["skin_id"];
63  }
64  if ($style_id == "") {
65  $style_id = $_GET["style_id"];
66  }
67 
68  try {
70  $less_file = new ilSystemStyleLessFile($this->getStyleContainer()->getLessVariablesFilePath($style_id));
71  $this->setLessFile($less_file);
72  } catch (ilSystemStyleException $e) {
73  $this->getMessageStack()->addMessage(
75  );
76  }
77  }
78 
82  public function executeCommand()
83  {
84  $cmd = $this->ctrl->getCmd();
85 
86  switch ($cmd) {
87  case "save":
88  case "edit":
89  case "reset":
90  case "update":
91  $this->$cmd();
92  break;
93  default:
94  $this->edit();
95  break;
96  }
97  }
98 
99 
103  protected function checkRequirements()
104  {
105  $style_id = $_GET['style_id'];
106  $less_path = $this->getStyleContainer()->getLessFilePath($style_id);
107 
108  $pass = $this->checkLessInstallation();
109 
110  if (file_exists($less_path)) {
111  $less_variables_name = $this->getStyleContainer()->getLessVariablesName($style_id);
112  $content = "";
113  try {
114  $content = file_get_contents($less_path);
115  } catch (Exception $e) {
116  $this->getMessageStack()->addMessage(
117  new ilSystemStyleMessage($this->lng->txt("can_not_read_less_file") . " " . $less_path, ilSystemStyleMessage::TYPE_ERROR)
118  );
119  $pass = false;
120  }
121  if ($content) {
122  $reg_exp = "/" . preg_quote($less_variables_name, "/") . "/";
123 
124  if (!preg_match($reg_exp, $content)) {
125  $this->getMessageStack()->addMessage(
126  new ilSystemStyleMessage($this->lng->txt("less_variables_file_not_included") . " " . $less_variables_name
127  . " " . $this->lng->txt("in_main_less_file") . " " . $less_path, ilSystemStyleMessage::TYPE_ERROR)
128  );
129  $pass = false;
130  }
131  }
132  } else {
133  $this->getMessageStack()->addMessage(
134  new ilSystemStyleMessage($this->lng->txt("less_file_does_not_exist") . $less_path, ilSystemStyleMessage::TYPE_ERROR)
135  );
136  $pass = false;
137  }
138  return $pass;
139  }
140 
144  protected function checkLessInstallation()
145  {
146  $pass = true;
147 
148  if (!PATH_TO_LESSC) {
149  $this->getMessageStack()->addMessage(
150  new ilSystemStyleMessage($this->lng->txt("no_less_path_set"), ilSystemStyleMessage::TYPE_ERROR)
151  );
152  $pass = false;
153  } elseif (!shell_exec(PATH_TO_LESSC)) {
154  $this->getMessageStack()->addMessage(
155  new ilSystemStyleMessage($this->lng->txt("invalid_less_path"), ilSystemStyleMessage::TYPE_ERROR)
156  );
157  $this->getMessageStack()->addMessage(
158  new ilSystemStyleMessage($this->lng->txt("provided_less_path") . " " . PATH_TO_LESSC, ilSystemStyleMessage::TYPE_ERROR)
159  );
160  $pass = false;
161  }
162 
163  if (!$pass && shell_exec("which lessc")) {
164  $this->getMessageStack()->addMessage(
165  new ilSystemStyleMessage($this->lng->txt("less_less_installation_detected") . shell_exec("which lessc"), ilSystemStyleMessage::TYPE_ERROR)
166  );
167  }
168 
169  return $pass;
170  }
171 
172 
173  protected function edit()
174  {
175  $modify = true;
176 
177  if (!$this->checkRequirements()) {
178  $this->getMessageStack()->prependMessage(
179  new ilSystemStyleMessage($this->lng->txt("less_can_not_be_modified"), ilSystemStyleMessage::TYPE_ERROR)
180  );
181  $modify = false;
182  }
183 
184  if ($this->getLessFile()) {
185  $form = $this->initSystemStyleLessForm($modify);
186  $this->getVariablesValues($form);
187  $this->tpl->setContent($form->getHTML());
188  }
189 
190  $this->getMessageStack()->sendMessages(true);
191  }
192 
197  public function initSystemStyleLessForm($modify = true)
198  {
199  $form = new ilPropertyFormGUI();
200 
201  $form->setTitle($this->lng->txt("adapt_less"));
202  $form->setDescription($this->lng->txt("adapt_less_description"));
203  $focus_variable = $_GET['id_less_variable'];
204  if ($focus_variable) {
205  $this->tpl->addOnLoadCode("setTimeout(function() { $('#" . $focus_variable . "').focus();}, 100);");
206  }
207 
208  foreach ($this->getLessFile()->getCategories() as $category) {
210  $section->setTitle($category->getName());
211  $section->setInfo($category->getComment());
212  //$section->setSectionAnchor($category->getName());
213  $form->addItem($section);
214  foreach ($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable) {
215  $input = new ilTextInputGUI($variable->getName(), $variable->getName());
216  $input->setRequired(true);
217  $input->setDisabled(!$modify);
218 
219  $references = $this->getLessFile()->getReferencesToVariableAsString($variable->getName());
220 
221  if ($references != "") {
222  if ($variable->getComment()) {
223  $info = $variable->getComment() . "</br>" . $this->lng->txt("usages") . " " . $references;
224  } else {
225  $info = $this->lng->txt("usages") . " " . $references;
226  }
227  } else {
228  $info = $variable->getComment();
229  }
230  $input->setInfo($info);
231 
232  $form->addItem($input);
233  }
234  }
235 
236  if ($modify) {
237  $form->addCommandButton("update", $this->lng->txt("update_variables"));
238  $form->addCommandButton("reset", $this->lng->txt("reset_variables"));
239  $form->addCommandButton("cancel", $this->lng->txt("cancel"));
240  }
241 
242 
243  $form->setFormAction($this->ctrl->getFormAction($this));
244 
245  return $form;
246  }
247 
248 
253  {
254  $values = [];
255  foreach ($this->getLessFile()->getCategories() as $category) {
256  foreach ($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable) {
257  $values[$variable->getName()] = $variable->getValue();
258  }
259  }
260 
261  $form->setValuesByArray($values);
262  }
263 
267  public function reset()
268  {
269  $style = $this->getStyleContainer()->getSkin()->getStyle($_GET["style_id"]);
270  $this->setLessFile($this->getStyleContainer()->copyVariablesFromDefault($style));
271  try {
272  ilUtil::sendSuccess($this->lng->txt("less_file_reset"));
273  $this->getStyleContainer()->compileLess($style->getId());
274  } catch (ilSystemStyleException $e) {
275  ilUtil::sendFailure($this->lng->txt($e->getMessage()), true);
276  }
277 
278  $this->edit();
279  }
280 
281  public function update()
282  {
283  $form = $this->initSystemStyleLessForm();
284  if (!$form->checkInput()) {
285  $empty_fields = [];
286  foreach ($this->getLessFile()->getCategories() as $category) {
287  foreach ($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable) {
288  if ($form->getInput($variable->getName()) == "") {
289  $empty_fields[$variable->getName()] = $this->getLessFile()->getVariableByName($variable->getName())->getValue();
290  $item = $form->getItemByPostVar($variable->getName());
291  $item->setAlert($this->lng->txt("less_variable_empty"));
292  }
293  }
294  }
295  if (!empty($empty_fields)) {
296  $form->setValuesByPost();
297  $form->setValuesByArray($empty_fields, true);
298  ilUtil::sendFailure($this->lng->txt("less_variables_empty_might_have_changed"), true);
299  $this->tpl->setContent($form->getHTML());
300  return;
301  }
302  } else {
303  foreach ($this->getLessFile()->getCategories() as $category) {
304  foreach ($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable) {
305  $variable->setValue($form->getInput($variable->getName()));
306  }
307  }
308  try {
309  $this->getLessFile()->write();
310  $this->getStyleContainer()->compileLess($_GET["style_id"]);
311  $skin = $this->getStyleContainer()->getSkin();
312  $skin->getVersionStep($skin->getVersion());
313  $this->getStyleContainer()->updateSkin($skin);
314  ilUtil::sendSuccess($this->lng->txt("less_file_updated"));
315  } catch (Exception $e) {
316  ilUtil::sendFailure($this->lng->txt($e->getMessage()), true);
317  }
318  }
319 
320  $form->setValuesByPost();
321  $this->tpl->setContent($form->getHTML());
322  }
323 
327  public function getStyleContainer()
328  {
329  return $this->style_container;
330  }
331 
336  {
337  $this->style_container = $style_container;
338  }
339 
343  public function getLessFile()
344  {
345  return $this->less_file;
346  }
347 
351  public function setLessFile($less_file)
352  {
353  $this->less_file = $less_file;
354  }
355 
359  public function getMessageStack()
360  {
361  return $this->message_stack;
362  }
363 
368  {
369  $this->message_stack = $message_stack;
370  }
371 }
$style
Definition: example_012.php:70
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
$_GET["client_id"]
This class represents a section header in a property form.
$section
Definition: Utf8Test.php:83
__construct($skin_id="", $style_id="")
ilSystemStyleLessGUI constructor.
if(isset($_POST['submit'])) $form
$values
This class represents a text property in a property form.
getVariablesValues(ilPropertyFormGUI $form)
Class for advanced editing exception handling in ILIAS.
static generateFromId($skin_id, ilSystemStyleMessageStack $message_stack=null, ilSystemStyleConfig $system_styles_conf=null)
Generate the container class by parsing the corresponding XML.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
Used to stack messages to be shown to the user.
$info
Definition: index.php:5
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.