Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
setStyle.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 $usage = new ApiUsage();
8 $usage->setDefinitionText("apply given style - if no style is set then update current style");
9 $styFilePath = $usage->addOptionalParameter("style", "path to style file");
10 if (!$styFilePath) {
11  /*
12  * @var Action $action
13  */
14  $defautStyle = $action->getParam("STYLE");
15  $styFilePath = sprintf("STYLE/%s/%s.sty", $defautStyle, $defautStyle);
16 }
17 $verbose = ('yes' === $usage->addOptionalParameter('verbose', 'verbose', array(
18  'yes',
19  'no'
20 ) , 'no'));
21 $usage->verify();
22 
23 chdir(DEFAULT_PUBDIR);
24 
26 {
27 
28  const CUSTOM_RULES_DIR_NAME = "rules.d";
29  const DEFAULT_CSS_PARSER_DEPLOY_CLASS = '\Dcp\Style\dcpCssConcatParser';
31  const GLOBAL_RULES_DIR_NAME = "global-rules.d";
32 
33  protected $verbose = false;
34  protected $logIndent = 0;
35  protected $styleConfig = array();
36  /* @var Action $action */
37  protected $action = null;
38 
39  protected function log($msg)
40  {
41  if ($this->verbose) {
42  print str_repeat("\t", $this->logIndent) . " -- " . $msg . PHP_EOL;
43  }
44  }
45 
46  public function __construct(Action $action)
47  {
48  $this->action = $action;
49  }
50 
51  public function loadStyle($styFilePath)
52  {
53 
54  $styleDefinition = $this->loadStyleDefinition($styFilePath);
55  $this->computeStyleColors($styleDefinition);
56  }
57 
58  protected function loadStyleDefinition($styFilePath)
59  {
60  $styFilePath = DEFAULT_PUBDIR . DIRECTORY_SEPARATOR . $styFilePath;
61  $this->log("load style definition from $styFilePath");
62  if (!is_readable($styFilePath)) {
63  if (!file_exists($styFilePath)) {
64  $msg = "sty file [$styFilePath] does not exists";
65  } else {
66  $msg = "sty file [$styFilePath] is not readable";
67  }
68  throw new \Dcp\Style\Exception("STY0001", $msg);
69  }
70  /** @noinspection PhpIncludeInspection */
71  require $styFilePath;
72 
73  $styleDefinition = array(
74  "sty_desc" => empty($sty_desc) ? array() : $sty_desc,
75  "sty_const" => empty($sty_const) ? array() : $sty_const,
76  "sty_colors" => empty($sty_colors) ? array() : $sty_colors,
77  "sty_local" => empty($sty_local) ? array() : $sty_local,
78  "sty_rules" => empty($sty_rules) ? array() : $sty_rules
79  );
80 
81  if (!isset($styleDefinition['sty_desc']) || !isset($styleDefinition['sty_desc']['name'])) {
82  throw new \Dcp\Style\Exception("STY0002", "Style definition does not contains style name");
83  }
84  // init with parent style
85  if (!empty($sty_inherit)) {
86  $this->log("using parent style file: $sty_inherit");
87  $this->logIndent+= 1;
88  $parentStyleDefinition = $this->loadStyleDefinition($sty_inherit);
89  $styleDefinition = array_replace_recursive($parentStyleDefinition, $styleDefinition);
90  $this->logIndent-= 1;
91  }
92  //load rules (rules.d)
93  $customRulesDirPath = dirname($styFilePath) . DIRECTORY_SEPARATOR . self::CUSTOM_RULES_DIR_NAME;
94  if (is_dir($customRulesDirPath) && is_readable($customRulesDirPath)) {
95  $customRules = & $styleDefinition['sty_rules'];
96  $customRulesFiles = scandir($customRulesDirPath);
97  if (false !== $customRulesFiles) {
98  foreach ($customRulesFiles as $customRulesFile) {
99  if ($customRulesFile == '.' || $customRulesFile == '..') continue;
100  $customRules = array_replace_recursive($customRules, $this->loadCustomRulesFromFile($customRulesDirPath . DIRECTORY_SEPARATOR . $customRulesFile));
101  }
102  }
103  }
104  //load global rules (rules.d)
105  $globalRulesDirPath = dirname(dirname($styFilePath)) . DIRECTORY_SEPARATOR . self::GLOBAL_RULES_DIR_NAME;
106  if (is_dir($globalRulesDirPath) && is_readable($globalRulesDirPath)) {
107  $globalRules = & $styleDefinition['sty_rules'];
108  $globalRulesFiles = scandir($globalRulesDirPath);
109  if (false !== $globalRulesFiles) {
110  foreach ($globalRulesFiles as $globalRulesFile) {
111  if ($globalRulesFile == '.' || $globalRulesFile == '..') continue;
112  $globalRules = array_replace_recursive($globalRules, $this->loadCustomRulesFromFile($globalRulesDirPath . DIRECTORY_SEPARATOR . $globalRulesFile));
113  }
114  }
115  }
116 
117  return $styleDefinition;
118  }
119 
120  protected function loadCustomRulesFromFile($customRulesFilePath)
121  {
122 
123  $this->log("load custom rules from $customRulesFilePath");
124  if (!is_readable($customRulesFilePath)) {
125  if (!file_exists($customRulesFilePath)) {
126  $msg = "$customRulesFilePath does not exists";
127  } else {
128  $msg = "$customRulesFilePath is not readable";
129  }
130  throw new \Dcp\ApiUsage\Exception("FILE0011", $msg);
131  }
132  /** @noinspection PhpIncludeInspection */
133  require $customRulesFilePath;
134  return empty($sty_rules) ? array() : $sty_rules;
135  }
136 
137  protected function computeStyleColors($styleDefinition)
138  {
139  $styleConfig = $styleDefinition;
140  // compute colors
141 
142  /** @noinspection PhpIncludeInspection */
143  require_once "Lib.Color.php";
144 
145  $computedColors = array();
146 
147  if (empty($styleConfig['sty_colors'])) {
148  throw new \Dcp\Style\Exception("STY0002", "Style definition does not contains sty_colors");
149  }
150 
151  $styleBaseColors = $styleConfig['sty_colors'];
152 
153  $darkStyle = false;
154  $whiteHsl = array();
155  if (isset($sty_const["COLOR_WHITE"])) {
156  $whiteHsl = srgb2hsl($sty_const["COLOR_WHITE"]);
157  }
158  if (!empty($whiteHsl)) {
159  $darkStyle = ($whiteHsl[2] < 0.5);
160  }
161  foreach ($styleBaseColors as $colorKey => $baseColor) {
162  $baseColorHsl = srgb2hsl($baseColor);
163  $baseHue = $baseColorHsl[0];
164  $baseSaturation = $baseColorHsl[1];
165  $baseLuminance = $baseColorHsl[2];
166  if ($darkStyle) {
167  $luminanceStep = (0 - $baseLuminance) / 10;
168  } else {
169  $luminanceStep = (1 - $baseLuminance) / 10;
170  }
171  $computedColors[$colorKey] = array();
172  $currentStepLuminance = $baseColorHsl[2];
173  for ($i = 0; $i < 10; $i++) {
174  $currentStepColor = HSL2RGB($baseHue, $baseSaturation, $currentStepLuminance);
175  $computedColors[$colorKey][$i] = $currentStepColor;
176  $currentStepLuminance+= $luminanceStep;
177  }
178  }
179 
180  $styleConfig['computed_colors'] = $computedColors;
181 
182  $this->styleConfig = $styleConfig;
183  }
184 
185  public function applyStyle()
186  {
188 
189  if (empty($styleConfig['sty_desc']) || empty($styleConfig['sty_desc']['name'])) {
190  throw new \Dcp\Style\Exception("STY0002", "Style definition does not contains name");
191  }
192 
193  $param = new Param();
194 
195  $styleName = $styleConfig['sty_desc']['name'];
196  $style = new Style('', $styleName);
197  $style->description = empty($styleConfig['sty_desc']['description']) ? '' : $styleConfig['sty_desc']['description'];
198  if (isset($styleConfig['sty_desc']['parsable'])) {
199  print "\n[WARNING] use of parsable property on style is deprecated\n\n";
200  $style->parsable = (('Y' === $styleConfig['sty_desc']['parsable']) ? 'Y' : 'N');
201  }
202 
203  if (!$style->isAffected()) {
204  $style->name = $styleName;
205  $err = $style->Add();
206  if ($err) {
207  throw new \Dcp\Style\Exception("STY0003", "error when registering style");
208  }
209  }
210  $err = $style->modify();
211  if ($err) {
212  throw new \Dcp\Style\Exception("STY0003", "error when modifying style");
213  }
214  // delete previous style parameters
215  $this->log("delete previous style parameters");
216  $query = new QueryDb("", "Param");
217  $query->AddQuery(sprintf("type ~ '^%s'", Param::PARAM_STYLE)); //all of them, regardless of the style they come from
218  $oldParamList = $query->Query();
219  if (!empty($oldParamList)) {
220  foreach ($oldParamList as $oldParam) {
221  /** @var $oldParam Param */
222  $oldParam->delete();
223  }
224  }
225 
226  $paramType = Param::PARAM_STYLE . $styleName;
227  // register color params ($styleConfig['sty_computed_colors'])
228  $this->log("register color params");
229  $this->logIndent+= 1;
230  foreach ($styleConfig['computed_colors'] as $colorClass => $colorList) {
231  foreach ($colorList as $colorIndex => $color) {
232  $paramName = "COLOR_{$colorClass}{$colorIndex}";
233  // if value is a reference to another parameter
234  $dynamicColorValue = ApplicationParameterManager::getScopedParameterValue($color);
235  if (!empty($dynamicColorValue)) {
236  $this->log("dynamic value " . var_export($dynamicColorValue, true) . " set for $paramName ($color)");
237  $color = $dynamicColorValue;
238  } else {
239  $this->log("static value " . var_export($color, true) . " set for $paramName ($color)");
240  }
241  $param->Set($paramName, $color, $paramType, 1);
242  $this->action->parent->SetVolatileParam($paramName, $color); //add parameter in session cache
243 
244  }
245  }
246  $this->logIndent-= 1;
247  // register other params ($styleConfig['sty_const'])
248  $this->log("register other params");
249  $this->logIndent+= 1;
250  foreach ($styleConfig['sty_const'] as $paramName => $paramValue) {
251  // if value is a reference to another parameter
252  $dynamicParamValue = ApplicationParameterManager::getScopedParameterValue($paramValue);
253  if (!empty($dynamicParamValue)) {
254  $this->log("dynamic value " . var_export($dynamicParamValue, true) . " set for $paramName ($paramValue)");
255  $paramValue = $dynamicParamValue;
256  } else {
257  $this->log("static value " . var_export($paramValue, true) . " set for $paramName ($paramValue)");
258  }
259  $param->Set($paramName, $paramValue, $paramType, 1);
260  $this->action->parent->SetVolatileParam($paramName, $paramValue); //add parameter in session cache
261 
262  }
263  $this->logIndent-= 1;
264  // volatile register parsing params ($styleConfig['sty_local'])
265  $this->log("declare volatile params");
266  $this->logIndent+= 1;
267  foreach ($styleConfig['sty_local'] as $paramName => $paramValue) {
268  // if value is a reference to another parameter
269  $dynamicParamValue = ApplicationParameterManager::getScopedParameterValue($paramValue);
270  if (!empty($dynamicParamValue)) {
271  $this->log("dynamic value " . var_export($dynamicParamValue, true) . " used for $paramName ($paramValue)");
272  $paramValue = $dynamicParamValue;
273  } else {
274  $this->log("static value " . var_export($paramValue, true) . " used for $paramName ($paramValue)");
275  }
276  $this->action->parent->SetVolatileParam($paramName, $paramValue); //add parameter in session cache
277 
278  }
279  $this->logIndent-= 1;
280  // apply sty_rules
281  $this->deployStyleFiles($styleConfig['sty_rules']);
282 
283  $style->setRules($styleConfig['sty_rules']);
284  $err = $style->modify();
285  if ($err) {
286  throw new \Dcp\Style\Exception("STY0003", "error when modifying style");
287  }
288  ApplicationParameterManager::setCommonParameterValue("CORE", "STYLE", $styleName);
289  }
290 
291  protected function deployStyleFiles(Array $rules)
292  {
293  $this->log("deploy style files");
294  $this->logIndent+= 1;
295 
296  $filesDefinition = array();
297  $cssRules = empty($rules['css']) ? array() : $rules['css'];
298  $filesDefinition['css'] = $this->deployStyleCssFiles($cssRules, 'css');
299 
300  $this->logIndent-= 1;
301 
302  return $filesDefinition;
303  }
304 
305  protected function deployStyleCssFiles(Array $cssRules, $targetDirName)
306  {
307  $this->log("deploy css files");
308  $filesDefinition = array();
309 
310  $pubDir = DEFAULT_PUBDIR;
311  $targetDir = $pubDir . DIRECTORY_SEPARATOR . $targetDirName;
312  // clean previous files
313  $this->deleteDirectory($targetDir);
314  mkdir($targetDir);
315  // deploy new css files
316  $this->logIndent+= 1;
317  foreach ($cssRules as $targetFile => $rule) {
318  $this->log("processing rules for $targetFile");
319  // check src file
320  if (empty($rule['src'])) {
321  throw new \Dcp\Style\Exception("STY0002", "rule for $targetFile does not contains src");
322  }
323 
324  $src = $rule['src'];
325  $destFile = $targetDirName . DIRECTORY_SEPARATOR . $targetFile;
326 
327  $deployParserClass = self::DEFAULT_CSS_PARSER_DEPLOY_CLASS;
328  $deployParserOptions = array();
329  if (!empty($rule['deploy_parser'])) {
330  if (!empty($rule['deploy_parser']['className'])) {
331  $deployParserClass = $rule['deploy_parser']['className'];
332  }
333  if (!empty($rule['deploy_parser']['options'])) {
334  $deployParserOptions = $rule['deploy_parser']['options'];
335  }
336  }
337  $this->log("parsing $targetFile using $deployParserClass");
338  $classInterfaces = class_implements($deployParserClass, true);
339  if (!isset($classInterfaces['Dcp\Style\ICssParser'])) {
340  throw new \Dcp\Style\Exception("STY0006", "class $deployParserClass does not implements \\Dcp\\Style\\ICssParser");
341  }
342  $this->log(print_r($src, true));
343  /** @var $parser \Dcp\Style\ICssParser */
344  $parser = new $deployParserClass($src, $deployParserOptions, $this->styleConfig);
345  $parser->gen($destFile);
346  }
347  $this->logIndent-= 1;
348 
349  return $filesDefinition;
350  }
351 
352  protected function deleteDirectory($dir)
353  {
354  if (!file_exists($dir)) return true;
355  if (!is_dir($dir) || is_link($dir)) return unlink($dir);
356  foreach (scandir($dir) as $item) {
357  if ($item == '.' || $item == '..') continue;
358  if (!$this->deleteDirectory($dir . "/" . $item)) {
359  chmod($dir . "/" . $item, 0777);
360  if (!$this->deleteDirectory($dir . "/" . $item)) return false;
361  };
362  }
363 
364  return rmdir($dir);
365  }
366 
367  public function setVerbose($verbose)
368  {
369  $this->verbose = $verbose;
370  }
371 }
372 /** @global $action Action */
374 $sm->setVerbose($verbose);
375 $sm->loadStyle($styFilePath);
376 $sm->applyStyle();
377 
$usage
Definition: setStyle.php:7
loadStyle($styFilePath)
Definition: setStyle.php:51
global $action
const DEFAULT_CSS_PARSER_RUNTIME_CLASS
Definition: setStyle.php:30
const DEFAULT_CSS_PARSER_DEPLOY_CLASS
Definition: setStyle.php:29
setVerbose($verbose)
Definition: setStyle.php:367
computeStyleColors($styleDefinition)
Definition: setStyle.php:137
deployStyleFiles(Array $rules)
Definition: setStyle.php:291
log($msg)
Definition: setStyle.php:39
$sm
Definition: setStyle.php:373
$styFilePath
Definition: setStyle.php:9
HSL2RGB($h, $s, $l)
Definition: Lib.Color.php:92
const GLOBAL_RULES_DIR_NAME
Definition: setStyle.php:31
if(!$styFilePath) $verbose
Definition: setStyle.php:17
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
loadStyleDefinition($styFilePath)
Definition: setStyle.php:58
static setCommonParameterValue($application, $parameterName, $value)
__construct(Action $action)
Definition: setStyle.php:46
deleteDirectory($dir)
Definition: setStyle.php:352
const CUSTOM_RULES_DIR_NAME
Definition: setStyle.php:28
const PARAM_STYLE
Definition: Class.Param.php:35
srgb2hsl($rgb)
Definition: Lib.Color.php:27
print
Definition: checklist.php:49
$dir
Definition: resizeimg.php:144
if(($docid!==0)&&(!is_numeric($docid))) $query
if($file) if($subject==""&&$file) if($subject=="") $err
loadCustomRulesFromFile($customRulesFilePath)
Definition: setStyle.php:120
$param
Definition: import_size.php:31
Verify arguments for wsh programs.
← centre documentaire © anakeen