ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilSystemStyleLessGUI.php
Go to the documentation of this file.
1<?php
2include_once "Services/Form/classes/class.ilPropertyFormGUI.php";
3include_once("Services/Style/System/classes/Utilities/class.ilSystemStyleSkinContainer.php");
4include_once("Services/Style/System/classes/Less/class.ilSystemStyleLessFile.php");
5include_once("Services/Style/System/classes/Utilities/class.ilSystemStyleMessageStack.php");
6
7
14{
18 protected $ctrl;
19
23 protected $lng;
24
28 protected $tpl;
29
34
38 protected $less_file;
39
43 protected $message_stack;
44
45
51 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
79 }
80
84 function executeCommand()
85 {
86 $cmd = $this->ctrl->getCmd();
87
88 switch ($cmd)
89 {
90 case "save":
91 case "edit":
92 case "reset":
93 case "update":
94 $this->$cmd();
95 break;
96 default:
97 $this->edit();
98 break;
99 }
100 }
101
102
106 protected function checkRequirements(){
107 $style_id = $_GET['style_id'];
108 $less_path = $this->getStyleContainer()->getLessFilePath($style_id);
109
110 $pass = $this->checkLessInstallation();
111
112 if(file_exists($less_path)){
113 $less_variables_name = $this->getStyleContainer()->getLessVariablesName($style_id);
114 $content = "";
115 try{
116 $content = file_get_contents($less_path);
117 }catch(Exception $e){
118 $this->getMessageStack()->addMessage(
119 new ilSystemStyleMessage($this->lng->txt("can_not_read_less_file")." ".$less_path,ilSystemStyleMessage::TYPE_ERROR)
120 );
121 $pass = false;
122 }
123 if($content){
124 $reg_exp = "/".preg_quote ($less_variables_name,"/")."/";
125
126 if(!preg_match($reg_exp,$content)){
127 $this->getMessageStack()->addMessage(
128 new ilSystemStyleMessage($this->lng->txt("less_variables_file_not_included")." ".$less_variables_name
129 ." ".$this->lng->txt("in_main_less_file")." ".$less_path,ilSystemStyleMessage::TYPE_ERROR)
130 );
131 $pass = false;
132 }
133 }
134
135
136 }else{
137 $this->getMessageStack()->addMessage(
138 new ilSystemStyleMessage($this->lng->txt("less_file_does_not_exist").$less_path,ilSystemStyleMessage::TYPE_ERROR)
139 );
140 $pass = false;
141 }
142 return $pass;
143 }
144
148 protected function checkLessInstallation(){
149 $pass = true;
150
151 if(!PATH_TO_LESSC){
152 $this->getMessageStack()->addMessage(
153 new ilSystemStyleMessage($this->lng->txt("no_less_path_set"),ilSystemStyleMessage::TYPE_ERROR)
154 );
155 $pass = false;
156 }else if(!shell_exec(PATH_TO_LESSC)){
157 $this->getMessageStack()->addMessage(
158 new ilSystemStyleMessage($this->lng->txt("invalid_less_path"),ilSystemStyleMessage::TYPE_ERROR)
159 );
160 $this->getMessageStack()->addMessage(
161 new ilSystemStyleMessage($this->lng->txt("provided_less_path")." ".PATH_TO_LESSC,ilSystemStyleMessage::TYPE_ERROR)
162 );
163 $pass = false;
164 }
165
166 if(!$pass && shell_exec("which lessc")){
167 $this->getMessageStack()->addMessage(
168 new ilSystemStyleMessage($this->lng->txt("less_less_installation_detected").shell_exec("which lessc"),ilSystemStyleMessage::TYPE_ERROR)
169 );
170 }
171
172 return $pass;
173 }
174
175
176 protected function edit(){
177
178 $modify = true;
179
180 if(!$this->checkRequirements()){
181 $this->getMessageStack()->prependMessage(
182 new ilSystemStyleMessage($this->lng->txt("less_can_not_be_modified"), ilSystemStyleMessage::TYPE_ERROR));
183 $modify = false;
184 }
185
186 if($this->getLessFile()){
187 $form = $this->initSystemStyleLessForm($modify);
188 $this->getVariablesValues($form);
189 $this->tpl->setContent($form->getHTML());
190 }
191
192 $this->getMessageStack()->sendMessages(true);
193
194
195 }
196
201 public function initSystemStyleLessForm($modify = true)
202 {
203 $form = new ilPropertyFormGUI();
204
205 $form->setTitle($this->lng->txt("adapt_less"));
206 $form->setDescription($this->lng->txt("adapt_less_description"));
207 $focus_variable = $_GET['id_less_variable'];
208 if($focus_variable){
209 $this->tpl->addOnLoadCode("setTimeout(function() { $('#".$focus_variable."').focus();}, 100);");
210 }
211
212 foreach($this->getLessFile()->getCategories() as $category){
214 $section->setTitle($category->getName());
215 $section->setInfo($category->getComment());
216 //$section->setSectionAnchor($category->getName());
217 $form->addItem($section);
218 foreach($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable){
219 $input = new ilTextInputGUI($variable->getName(), $variable->getName());
220 $input->setRequired(true);
221 $input->setDisabled(!$modify);
222
223 $references = $this->getLessFile()->getReferencesToVariableAsString($variable->getName());
224
225 if($references != ""){
226 if($variable->getComment()){
227 $info = $variable->getComment()."</br>".$this->lng->txt("usages")." ".$references;
228 }else{
229 $info = $this->lng->txt("usages")." ".$references;
230 }
231 }else{
232 $info = $variable->getComment();
233 }
234 $input->setInfo($info);
235
236 $form->addItem($input);
237
238 }
239 }
240
241 if($modify){
242 $form->addCommandButton("update", $this->lng->txt("update_variables"));
243 $form->addCommandButton("reset", $this->lng->txt("reset_variables"));
244 $form->addCommandButton("cancel", $this->lng->txt("cancel"));
245 }
246
247
248 $form->setFormAction($this->ctrl->getFormAction($this));
249
250 return $form;
251 }
252
253
258 {
259 $values = [];
260 foreach($this->getLessFile()->getCategories() as $category){
261 foreach($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable){
262 $values[$variable->getName()] = $variable->getValue();
263 }
264 }
265
266 $form->setValuesByArray($values);
267 }
268
272 public function reset()
273 {
274 $style = $this->getStyleContainer()->getSkin()->getStyle($_GET["style_id"]);
275 $this->setLessFile($this->getStyleContainer()->copyVariablesFromDefault($style));
276 try{
277 $this->getStyleContainer()->compileLess($style->getId());
278 }catch(ilSystemStyleException $e){
279 ilUtil::sendFailure($this->lng->txt($e->getMessage()),true);
280 }
281
282 $this->edit();
283 }
284
285 public function update()
286 {
287 $form = $this->initSystemStyleLessForm();
288 if ($form->checkInput())
289 {
290 foreach($this->getLessFile()->getCategories() as $category){
291 foreach($this->getLessFile()->getVariablesPerCategory($category->getName()) as $variable){
292 $variable->setValue($form->getInput($variable->getName()));
293 }
294 }
295 try{
296 $this->getLessFile()->write();
297 $this->getStyleContainer()->compileLess($_GET["style_id"]);
298 $skin = $this->getStyleContainer()->getSkin();
299 $skin->getVersionStep($skin->getVersion());
300 $this->getStyleContainer()->updateSkin($skin);
301 ilUtil::sendSuccess($this->lng->txt("less_file_updated"));
302 }catch(Exception $e){
303 ilUtil::sendFailure($this->lng->txt($e->getMessage()),true);
304 }
305 }
306
307 $form->setValuesByPost();
308 $this->tpl->setContent($form->getHTML());
309
310 }
311
315 public function getStyleContainer()
316 {
318 }
319
324 {
325 $this->style_container = $style_container;
326 }
327
331 public function getLessFile()
332 {
333 return $this->less_file;
334 }
335
339 public function setLessFile($less_file)
340 {
341 $this->less_file = $less_file;
342 }
343
347 public function getMessageStack()
348 {
350 }
351
356 {
357 $this->message_stack = $message_stack;
358 }
359
360}
$section
Definition: Utf8Test.php:83
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
This class represents a section header in a property form.
This class represents a property form user interface.
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
Class for advanced editing exception handling in ILIAS.
__construct($skin_id="", $style_id="")
ilSystemStyleLessGUI constructor.
getVariablesValues(ilPropertyFormGUI $form)
Used to stack messages to be shown to the user.
static generateFromId($skin_id, ilSystemStyleMessageStack $message_stack=null, ilSystemStyleConfig $system_styles_conf=null)
Generate the container class by parsing the corresponding XML.
This class represents a text property in a property form.
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$style
Definition: example_012.php:70
$info
Definition: example_052.php:80
$cmd
Definition: sahs_server.php:35
global $DIC