ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSystemStyleLessGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
28 {
29  protected ilCtrl $ctrl;
30  protected ilLanguage $lng;
35  protected Factory $ui_factory;
36  protected Renderer $renderer;
37  protected ServerRequestInterface $request;
39  protected Refinery $refinery;
41  protected string $style_id;
42 
43  public function __construct(
44  ilCtrl $ctrl,
45  ilLanguage $lng,
47  Factory $ui_factory,
48  Renderer $renderer,
49  ServerRequestInterface $request,
50  ilToolbarGUI $toolbar,
51  Refinery $refinery,
53  string $skin_id,
54  string $style_id
55  ) {
56  $this->ctrl = $ctrl;
57  $this->lng = $lng;
58  $this->tpl = $tpl;
59  $this->ui_factory = $ui_factory;
60  $this->renderer = $renderer;
61  $this->request = $request;
62  $this->toolbar = $toolbar;
63  $this->refinery = $refinery;
64  $this->style_id = $style_id;
65 
66  $this->message_stack = new ilSystemStyleMessageStack($this->tpl);
67 
68  $this->style_container = $factory->skinStyleContainerFromId($skin_id, $this->message_stack);
69  $this->less_file = new ilSystemStyleLessFile($this->style_container->getLessVariablesFilePath($style_id));
70  }
71 
75  public function executeCommand(): void
76  {
77  $this->addResetToolbar();
78  $form = null;
79 
80  $cmd = $this->ctrl->getCmd();
81 
82  switch ($cmd) {
83  case 'update':
84  $form = $this->update();
85  break;
86  case 'reset':
87  $this->reset();
88  $form = $this->edit();
89  break;
90  case 'edit':
91  case '':
92  $form = $this->edit();
93  break;
94  }
95  $components = $this->message_stack->getUIComponentsMessages($this->ui_factory);
96  if ($form) {
97  $components[] = $form;
98  }
99 
100  $this->tpl->setContent($this->renderer->render($components));
101  }
102 
103  protected function addResetToolbar(): void
104  {
105  $this->toolbar->addComponent($this->ui_factory->button()->standard(
106  $this->lng->txt('reset_variables'),
107  $this->ctrl->getLinkTarget($this, 'reset')
108  ));
109  }
110 
111  protected function reset(): void
112  {
113  $style = $this->style_container->getSkin()->getStyle($this->style_id);
114  $this->less_file = $this->style_container->copyVariablesFromDefault($style);
115  try {
116  $this->message_stack->addMessage(new ilSystemStyleMessage($this->lng->txt('less_file_reset')));
117  $this->style_container->compileLess($style->getId());
118  } catch (ilSystemStyleException $e) {
119  $this->message_stack->addMessage(new ilSystemStyleMessage(
120  $this->lng->txt($e->getMessage()),
122  ));
123  }
124  }
125 
126  protected function checkRequirements(): bool
127  {
128  $less_path = $this->style_container->getLessFilePath($this->style_id);
129 
130  $pass = $this->checkLessInstallation();
131 
132  if (file_exists($less_path)) {
133  $less_variables_name = $this->style_container->getLessVariablesName($this->style_id);
134  $content = '';
135  try {
136  $content = file_get_contents($less_path);
137  } catch (Exception $e) {
138  $this->message_stack->addMessage(
140  $this->lng->txt('can_not_read_less_file') . ' ' . $less_path,
142  )
143  );
144  $pass = false;
145  }
146  if ($content) {
147  $reg_exp = '/' . preg_quote($less_variables_name, '/') . '/';
148 
149  if (!preg_match($reg_exp, $content)) {
150  $this->message_stack->addMessage(
152  $this->lng->txt('less_variables_file_not_included') . ' ' . $less_variables_name
153  . ' ' . $this->lng->txt('in_main_less_file') . ' ' . $less_path,
155  )
156  );
157  $pass = false;
158  }
159  }
160  } else {
161  $this->message_stack->addMessage(
163  $this->lng->txt('less_file_does_not_exist') . $less_path,
165  )
166  );
167  $pass = false;
168  }
169  return $pass;
170  }
171 
172  protected function checkLessInstallation(): bool
173  {
174  $pass = true;
175 
176  if (!PATH_TO_LESSC) {
177  $this->message_stack->addMessage(
178  new ilSystemStyleMessage($this->lng->txt('no_less_path_set'), ilSystemStyleMessage::TYPE_ERROR)
179  );
180  $pass = false;
181  } elseif (!shell_exec(PATH_TO_LESSC)) {
182  $this->message_stack->addMessage(
183  new ilSystemStyleMessage($this->lng->txt('invalid_less_path'), ilSystemStyleMessage::TYPE_ERROR)
184  );
185  $this->message_stack->addMessage(
187  $this->lng->txt('provided_less_path') . ' ' . PATH_TO_LESSC,
189  )
190  );
191  $pass = false;
192  }
193 
194  if (!$pass && shell_exec('which lessc')) {
195  $this->message_stack->addMessage(
197  $this->lng->txt('less_less_installation_detected') . shell_exec('which lessc'),
199  )
200  );
201  }
202 
203  return $pass;
204  }
205 
206  protected function edit(): Form
207  {
208  $modify = true;
209 
210  if (!$this->checkRequirements()) {
211  $this->message_stack->prependMessage(
212  new ilSystemStyleMessage($this->lng->txt('less_can_not_be_modified'), ilSystemStyleMessage::TYPE_ERROR)
213  );
214  $modify = false;
215  }
216 
217  return $this->initSystemStyleLessForm($modify);
218  }
219 
220  public function initSystemStyleLessForm(bool $modify = true): Form
221  {
222  $f = $this->ui_factory->input();
223  $category_section = [];
224  foreach ($this->less_file->getCategories() as $category) {
225  $variables_inptus = [];
226  foreach ($this->less_file->getVariablesPerCategory($category->getName()) as $variable) {
227  $info = $this->less_file->getRefAndCommentAsString($variable->getName(), $this->lng->txt('usages'));
228  $save_closure = function ($v) use ($variable) {
229  $variable->setValue($v);
230  };
231  $variables_inptus[] = $f->field()->text($variable->getName(), $info)
232  //->withRequired(true)
233  ->withDisabled(!$modify)
234  ->withValue($variable->getValue())
235  ->withAdditionalTransformation($this->refinery->custom()->transformation($save_closure));
236  }
237 
238  $category_section[] = $f->field()->section(
239  $variables_inptus,
240  $category->getName(),
241  $category->getComment()
242  );
243  }
244 
245  $form_section = $f->field()->section(
246  $category_section,
247  $this->lng->txt('adapt_less'),
248  $this->lng->txt('adapt_less_description')
249  );
250 
251  return $f->container()->form()->standard(
252  $this->ctrl->getFormAction($this, 'update'),
253  [$form_section]
254  )->withSubmitCaption($this->lng->txt('update_variables'));
255  }
256 
257  public function update(): Form
258  {
259  $form = $this->initSystemStyleLessForm();
260  $form = $form->withRequest($this->request);
261 
262  if (!$form->getData()) {
263  $this->message_stack->addMessage(new ilSystemStyleMessage(
264  $this->lng->txt('less_variables_empty_might_have_changed'),
266  ));
267  return $form;
268  }
269 
270  try {
271  $this->less_file->write();
272  $this->style_container->compileLess($this->style_id);
273  $skin = $this->style_container->getSkin();
274  $skin->getVersionStep($skin->getVersion());
275  $this->style_container->updateSkin($skin);
276  $this->message_stack->addMessage(new ilSystemStyleMessage($this->lng->txt('less_file_updated')));
277  } catch (Exception $e) {
278  $this->message_stack->addMessage(new ilSystemStyleMessage(
279  $this->lng->txt($e->getMessage()),
281  ));
282  }
283 
284  return $form;
285  }
286 }
An entity that renders components to a string output.
Definition: Renderer.php:30
ilSystemStyleMessageStack $message_stack
ilGlobalTemplateInterface $tpl
Factory to create Skin classes holds an manages the basic data of a skin as provide by the template o...
ilSystemStyleLessFile $less_file
initSystemStyleLessForm(bool $modify=true)
ilSkinStyleContainer $style_container
This class is responsible for all file system related actions related actions of a skin such as copyi...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
skinStyleContainerFromId(string $skin_id, ilSystemStyleMessageStack $message_stack)
Get container class is responsible for all file system related actions related actions of a skin such...
ServerRequestInterface $request
ilSystemStyleConfig wraps all &#39;constants&#39; to ensure the testability of all classes using those &#39;const...
__construct(ilCtrl $ctrl, ilLanguage $lng, ilGlobalTemplateInterface $tpl, Factory $ui_factory, Renderer $renderer, ServerRequestInterface $request, ilToolbarGUI $toolbar, Refinery $refinery, ilSkinFactory $factory, string $skin_id, string $style_id)
Used to stack messages to be shown to the user.
$factory
Definition: metadata.php:75