ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSystemStyleScssGUI.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,
52  ilSkinFactory $factory,
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->scss_folder = new ilSystemStyleScssSettings($this->style_container->getScssSettingsPath($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->scss_folder = $this->style_container->copySettingsFromDefault($style);
115  try {
116  $this->message_stack->addMessage(new ilSystemStyleMessage($this->lng->txt('scss_folder_reset')));
117  $this->style_container->compileScss($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  $scss_path = $this->style_container->getScssFilePath($this->style_id);
129 
130  $pass = $this->checkScssInstallation();
131 
132  if (file_exists($scss_path)) {
133  $scss_file_path = $this->style_container->getScssFilePath($this->style_id);
134  $content = '';
135  try {
136  $content = file_get_contents($scss_file_path);
137  } catch (Exception $e) {
138  $this->message_stack->addMessage(
140  $this->lng->txt('can_not_read_scss_folder') . ' ' . $scss_file_path,
142  )
143  );
144  $pass = false;
145  }
146  if ($content) {
147  $reg_exp = '/' . preg_quote($this->style_container->getScssSettingsFolderName(), '/') . '/';
148 
149  if (!preg_match($reg_exp, $content)) {
150  $this->message_stack->addMessage(
152  $this->lng->txt('scss_variables_file_not_included') . ' ' . $this->style_container->getScssSettingsFolderName()
153  . ' ' . $this->lng->txt('in_main_scss_folder') . ' ' . $scss_path,
155  )
156  );
157  $pass = false;
158  }
159  }
160  } else {
161  $this->message_stack->addMessage(
163  $this->lng->txt('scss_folder_does_not_exist') . $scss_path,
165  )
166  );
167  $pass = false;
168  }
169  return $pass;
170  }
171 
172  protected function checkScssInstallation(): bool
173  {
174  $pass = true;
175 
176  if (!PATH_TO_SCSS) {
177  $this->message_stack->addMessage(
178  new ilSystemStyleMessage($this->lng->txt('no_scss_path_set'), ilSystemStyleMessage::TYPE_ERROR)
179  );
180  $pass = false;
181  } elseif (!shell_exec(PATH_TO_SCSS . " -h")) {
182  $this->message_stack->addMessage(
183  new ilSystemStyleMessage($this->lng->txt('invalid_scss_path'), ilSystemStyleMessage::TYPE_ERROR)
184  );
185  $this->message_stack->addMessage(
187  $this->lng->txt('provided_scss_path') . ' ' . PATH_TO_SCSS,
189  )
190  );
191  $pass = false;
192  }
193 
194  if (!$pass && shell_exec('which sass')) {
195  $this->message_stack->addMessage(
197  $this->lng->txt('scss_scss_installation_detected') . shell_exec('which sass'),
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('scss_can_not_be_modified'), ilSystemStyleMessage::TYPE_ERROR)
213  );
214  $modify = false;
215  }
216 
217  return $this->initSystemStyleScssForm($modify);
218  }
219 
220  public function initSystemStyleScssForm(bool $modify = true): Form
221  {
222  $f = $this->ui_factory->input();
223  $category_sections = [];
224  foreach ($this->scss_folder->getCategories() as $category) {
225  $variables_inptus = [];
226  foreach ($this->scss_folder->getVariablesPerCategory($category->getName()) as $variable) {
227  $info = $this->scss_folder->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  if(count($variables_inptus) > 0) {
239  $category_sections[] = $f->field()->section(
240  $variables_inptus,
241  $category->getName(),
242  $category->getComment()
243  );
244  }
245  }
246 
247  $form_section = $f->field()->section(
248  $category_sections,
249  $this->lng->txt('adapt_scss'),
250  $this->lng->txt('adapt_scss_description')
251  );
252 
253  return $f->container()->form()->standard(
254  $this->ctrl->getFormAction($this, 'update'),
255  [$form_section]
256  )->withSubmitLabel($this->lng->txt('update_variables'));
257  }
258 
259  public function update(): Form
260  {
261  $form = $this->initSystemStyleScssForm();
262  $form = $form->withRequest($this->request);
263 
264  if (!$form->getData()) {
265  $this->message_stack->addMessage(new ilSystemStyleMessage(
266  $this->lng->txt('scss_variables_empty_might_have_changed'),
268  ));
269  return $form;
270  }
271 
272  try {
273  $this->scss_folder->write();
274  $this->style_container->compileScss($this->style_id);
275  $skin = $this->style_container->getSkin();
276  $skin->getVersionStep($skin->getVersion());
277  $this->style_container->updateSkin($skin);
278  $this->message_stack->addMessage(new ilSystemStyleMessage($this->lng->txt('scss_folder_updated')));
279  } catch (Exception $e) {
280  $this->message_stack->addMessage(new ilSystemStyleMessage(
281  $this->lng->txt($e->getMessage()),
283  ));
284  }
285 
286  return $form;
287  }
288 }
An entity that renders components to a string output.
Definition: Renderer.php:30
Factory to create Skin classes holds an manages the basic data of a skin as provide by the template o...
initSystemStyleScssForm(bool $modify=true)
This class is responsible for all file system related actions related actions of a skin such as copyi...
ilGlobalTemplateInterface $tpl
skinStyleContainerFromId(string $skin_id, ilSystemStyleMessageStack $message_stack)
Get container class is responsible for all file system related actions related actions of a skin such...
ilSkinStyleContainer $style_container
ilSystemStyleConfig wraps all &#39;constants&#39; to ensure the testability of all classes using those &#39;const...
Used to stack messages to be shown to the user.
ServerRequestInterface $request
ilSystemStyleScssSettings $scss_folder
__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)
ilSystemStyleMessageStack $message_stack