Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Style.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 namespace {
7  /**
8  * Generated Header (not documented yet)
9  *
10  * @author Anakeen
11  * @version $Id: Class.Style.php,v 1.5 2003/08/18 15:46:42 eric Exp $
12  * @package FDL
13  * @subpackage CORE
14  */
15 
16  class Style extends DbObj
17  {
19 
20  var $fields = array(
21  "name",
22  "description",
23  "parsable",
24  "rules"
25  );
26 
27  var $id_fields = array(
28  "name"
29  );
30  public $name;
31  public $description;
32  public $parsable;
33  protected $rules;
34  /**
35  * @var Application
36  */
37  public $parent;
38 
39  var $sqlcreate = "
40  create table style (
41  name text not null,
42  primary key (name),
43  description text,
44  parsable char default 'N',
45  rules text default '{}'
46  );
47  create sequence SEQ_ID_STYLE start 10000;
48 ";
49 
50  var $dbtable = "style";
51 
52  protected $_expanded_rules = array();
53 
54  public function __construct($dbaccess = '', $id = '', $res = '', $dbid = 0)
55  {
56  parent::__construct($dbaccess, $id, $res, $dbid);
57  if (!empty($this->rules)) {
58  $this->_expanded_rules = json_decode($this->rules, true);
59  }
60  }
61 
62  public function preupdate()
63  {
64  $this->encodeRules();
65  }
66 
67  public function preInsert()
68  {
69  $this->encodeRules();
70  }
71 
72  protected function encodeRules()
73  {
74  $this->rules = json_encode($this->_expanded_rules);
75  }
76 
77  function set(&$parent)
78  {
79  $this->parent = & $parent;
80  }
81 
82  function getImageUrl($img, $default)
83  {
84  $root = DEFAULT_PUBDIR;
85 
86  $socStyle = $this->parent->Getparam("CORE_SOCSTYLE");
87  // first see if i have an society style
88  if (($socStyle != "") && file_exists($root . "/STYLE/" . $socStyle . "/Images/" . $img)) {
89  return ("STYLE/" . $socStyle . "/Images/" . $img);
90  }
91 
92  if (file_exists($root . "/STYLE/" . $this->name . "/Images/" . $img)) {
93  return ("STYLE/" . $this->name . "/Images/" . $img);
94  } else {
95  return ($default);
96  }
97  }
98 
99  function getLayoutFile($layname, $default = "")
100  {
101  $root = DEFAULT_PUBDIR;
102 
103  $socStyle = $this->parent->Getparam("CORE_SOCSTYLE");
104  // first see if i have an society style
105  if ($socStyle != "") {
106  $file = $root . "/STYLE/" . $socStyle . "/Layout/" . $layname;
107  if (file_exists($file)) return ($file);
108  }
109 
110  $file = $root . "/STYLE/" . $this->name . "/Layout/" . $layname;
111  if (file_exists($file)) return ($file);
112 
113  return ($default);
114  }
115 
116  public function setRules(Array $filesDefinition)
117  {
118  $this->_expanded_rules = $filesDefinition;
119  }
120 
121  public function setRule($fileType, $file, $definition)
122  {
123  $this->_expanded_rules[$fileType][$file] = $definition;
124  }
125 
126  public function getRules()
127  {
128  return $this->_expanded_rules;
129  }
130 
131  public function getRule($fileType, $file)
132  {
133  $file = substr($file, 4); // delete "css/"
134  if (!isset($this->_expanded_rules[$fileType])) {
135  return null;
136  }
137 
138  if (!isset($this->_expanded_rules[$fileType][$file])) {
139  return null;
140  }
141  return $this->_expanded_rules[$fileType][$file];
142  }
143  }
144 
146  {
147  /**
148  * @errorCode sty file not reachable
149  */
150  const STY0001 = 'sty file not reachable: %s';
151  /**
152  * @errorCode malformed sty file
153  */
154  const STY0002 = 'malformed sty file: %s';
155  /**
156  * @errorCode style registration error
157  */
158  const STY0003 = 'style registration error: %s';
159  /**
160  * @errorCode source file not readable for sty target
161  */
162  const STY0004 = 'source file not readable for sty target: %s';
163  /**
164  * @errorCode file creation error
165  */
166  const STY0005 = 'file creation error: %s';
167  /**
168  * @errorCode parser does not implements required interfaces
169  */
170  const STY0006 = 'parser does not implements required interfaces: %s';
171  /**
172  * @errorCode unimplemented feature
173  */
174  const STY0007 = 'unimplemented feature: %s';
175  /**
176  * @errorCode when try create target directory
177  * @see dcpCssCopyDirectory
178  */
179  const STY0008 = 'cannot create target directory: %s';
180  /**
181  * @errorCode when try create view target directory
182  * @see dcpCssCopyDirectory
183  */
184  const STY0009 = 'origin "%s" is not a directory';
185  /**
186  * @errorCode copy error when copy directory for css
187  * @see dcpCssCopyDirectory
188  */
189  const STY0010 = 'cannot copy from origin "%s" to "%s"';
190  }
191 }
192 
193 namespace Dcp\Style
194 {
195  class Exception extends \Dcp\Exception
196  {
197  /**
198  * for beautifier
199  */
200  private function _bo()
201  {
202  if (true) $a = 1;
203  }
204  }
205 
206  interface IParser
207  {
208  /**
209  * @param string|string[] $srcFiles path or array of path of source file(s) relative to server root
210  * @param array $options
211  * @param array $styleConfig full style configuration
212  */
213  public function __construct($srcFiles, Array $options, Array $styleConfig);
214  /**
215  * @param string $destFile destination file path relative to server root (if null, parsed result is returned)
216  * @throws Exception
217  * @return mixed
218  */
219  public function gen($destFile = null);
220  }
221 
222  interface ICssParser extends IParser
223  {
224  }
225 
226  class dcpCssConcatParser implements ICssParser
227  {
228 
229  protected $_srcFiles = null;
230  /**
231  * @param string|string[] $srcFiles path or array of path of source file(s) relative to server root
232  * @param array $options
233  * @param array $styleConfig full style configuration
234  */
235  public function __construct($srcFiles, Array $options, Array $styleConfig)
236  {
237  if (is_array($srcFiles)) {
238  $this->_srcFiles = $srcFiles;
239  } else {
240  $this->_srcFiles = array(
241  $srcFiles
242  );
243  }
244  }
245  /**
246  * @param string $destFile destination file path relative to server root (if null, parsed result is returned)
247  * @throws Exception
248  * @return mixed
249  */
250  public function gen($destFile = null)
251  {
252  $pubDir = DEFAULT_PUBDIR;
253  // prepare target dir
254  $fullTargetPath = $pubDir . DIRECTORY_SEPARATOR . $destFile;
255  $fullTargetDirname = dirname($fullTargetPath);
256  if (!is_dir($fullTargetDirname) && (false === mkdir($fullTargetDirname, 0777, true))) {
257  throw new Exception("STY0005", "$fullTargetDirname dir could not be created for file $destFile");
258  }
259 
260  $targetHandler = fopen($fullTargetPath, 'w');
261  if (false === $targetHandler) {
262  throw new Exception("STY0005", "$destFile dir could not be created");
263  }
264 
265  foreach ($this->_srcFiles as $srcPath) {
266  $srcFullPath = $pubDir . DIRECTORY_SEPARATOR . $srcPath;
267  if (!is_readable($srcFullPath)) {
268  if (!file_exists($srcFullPath)) {
269  $msg = "source file $srcFullPath does not exists for file $destFile";
270  } else {
271  $msg = "source file $srcFullPath is not readable for file $destFile";
272  }
273  throw new Exception("STY0004", $msg);
274  }
275  if (false === fwrite($targetHandler, file_get_contents($srcFullPath))) {
276  throw new Exception("STY0005", "data from $srcFullPath could not be written to file $fullTargetPath");
277  }
278  }
279  if (false === fclose($targetHandler)) {
280  throw new Exception("STY0005", "$fullTargetPath could not be closed");
281  }
282  }
283  }
284 
285  class dcpCssTemplateParser implements ICssParser
286  {
287 
288  protected $_srcFiles = null;
289  protected $_styleConfig = array();
290  /**
291  * @param string|string[] $srcFiles path or array of path of source file(s) relative to server root
292  * @param array $options array of options
293  * @param array $styleConfig full style configuration
294  */
295  public function __construct($srcFiles, Array $options, Array $styleConfig)
296  {
297  if (is_array($srcFiles)) {
298  $this->_srcFiles = $srcFiles;
299  } else {
300  $this->_srcFiles = array(
301  $srcFiles
302  );
303  }
304  $this->_styleConfig = $styleConfig;
305  }
306  /**
307  * @param string $destFile destination file path relative to server root (if null, parsed result is returned)
308  * @throws Exception
309  * @return mixed
310  */
311  public function gen($destFile = null)
312  {
313  $template = '';
314  $pubDir = DEFAULT_PUBDIR;
315  global $action;
316  foreach ($this->_srcFiles as $srcPath) {
317  $srcFullPath = $pubDir . DIRECTORY_SEPARATOR . $srcPath;
318  if (!is_readable($srcFullPath)) {
319  if (!file_exists($srcFullPath)) {
320  $msg = "source file $srcFullPath does not exists for file $destFile";
321  } else {
322  $msg = "source file $srcFullPath is not readable for file $destFile";
323  }
324  throw new Exception("STY0004", $msg);
325  }
326  $template.= file_get_contents($srcFullPath);
327  }
328  // prepare target dir
329  $fullTargetPath = $pubDir . DIRECTORY_SEPARATOR . $destFile;
330  $fullTargetDirname = dirname($fullTargetPath);
331  if (!is_dir($fullTargetDirname) && (false === mkdir($fullTargetDirname, 0777, true))) {
332  throw new Exception("STY0005", "$fullTargetDirname dir could not be created for file $destFile");
333  }
334  $explorerP = getExplorerParamtersName();
335  foreach ($explorerP as $ep) {
336  $action->parent->SetVolatileParam($ep, null);
337  }
338  $lay = new \Layout("", $action, $template);
339  $template = $lay->gen();
340  $keyForStyle = preg_replace("/css\//", "", $destFile);
341  if (!isset($this->_styleConfig["sty_rules"]["css"][$keyForStyle]["flags"]) || $this->_styleConfig["sty_rules"]["css"][$keyForStyle]["flags"] !== \Style::RULE_FLAG_PARSE_ON_RUNTIME) {
342  $subRepositoryLevel = substr_count($destFile, "/");
343  $levelToGo = "";
344  for ($i = 0; $i < $subRepositoryLevel; $i++) {
345  $levelToGo.= "../";
346  }
347  $template = preg_replace('/(url\()\s*([\'"]?)\s*(.*?)\s*(\2\s*\))/', "$1$2" . $levelToGo . "$3$4", $template);
348  }
349  if (false === file_put_contents($fullTargetPath, $template)) {
350  throw new Exception("STY0005", "$fullTargetPath could not be written for file $destFile");
351  }
352  }
353  }
354 
355  class dcpCssCopyDirectory implements ICssParser
356  {
357 
358  protected $_srcFiles = null;
359  /**
360  * @param string|string[] $srcFiles path or array of path of source file(s) relative to server root
361  * @param array $options array of options
362  * @param array $styleConfig full style configuration
363  */
364  public function __construct($srcFiles, Array $options, Array $styleConfig)
365  {
366  if (is_array($srcFiles)) {
367  $this->_srcFiles = $srcFiles;
368  } else {
369  $this->_srcFiles = array(
370  $srcFiles
371  );
372  }
373  }
374  /**
375  * @param string $destFile destination file path relative to server root (if null, parsed result is returned)
376  * @throws Exception
377  * @return mixed
378  */
379  public function gen($destFile = null)
380  {
381  $template = '';
382  $pubDir = DEFAULT_PUBDIR;
383  global $action;
384  foreach ($this->_srcFiles as $srcPath) {
385  $srcFullPath = $pubDir . DIRECTORY_SEPARATOR . $srcPath;
386  if (!is_dir($srcFullPath)) {
387 
388  throw new Exception("STY0009", $srcFullPath);
389  }
390  if (!is_dir($pubDir . DIRECTORY_SEPARATOR . $destFile)) {
391  $r = mkdir($pubDir . DIRECTORY_SEPARATOR . $destFile);
392  if ($r === false) {
393  throw new Exception("STY0008", $pubDir . DIRECTORY_SEPARATOR . $destFile);
394  }
395  }
396  $cpCmd = sprintf("cp -r %s/* %s", escapeshellarg($srcFullPath) , escapeshellarg($pubDir . DIRECTORY_SEPARATOR . $destFile));
397  $r = shell_exec("$cpCmd 2>&1 && echo 1");
398  if ($r === null) {
399  throw new Exception("STY0010", $srcFullPath, $pubDir . DIRECTORY_SEPARATOR . $destFile);
400  }
401  }
402  }
403  }
404 }
405 
getRule($fileType, $file)
setRule($fileType, $file, $definition)
global $action
__construct($srcFiles, Array $options, Array $styleConfig)
$_expanded_rules
Definition: Class.Style.php:52
getImageUrl($img, $default)
Definition: Class.Style.php:82
print< H1 > Check Database< i > $dbaccess</i ></H1 > $a
Definition: checklist.php:45
print $fam getTitle() $fam name
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
encodeRules()
Definition: Class.Style.php:72
__construct($srcFiles, Array $options, Array $styleConfig)
$file
getLayoutFile($layname, $default="")
Definition: Class.Style.php:99
__construct($dbaccess= '', $id= '', $res= '', $dbid=0)
Definition: Class.Style.php:54
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
$description
Definition: Class.Style.php:31
setRules(Array $filesDefinition)
set(&$parent)
Definition: Class.Style.php:77
const RULE_FLAG_PARSE_ON_RUNTIME
Definition: Class.Style.php:18
gen($destFile=null)
getRules()
preupdate()
Definition: Class.Style.php:62
__construct($srcFiles, Array $options, Array $styleConfig)
getExplorerParamtersName()
Definition: Lib.Main.php:153
preInsert()
Definition: Class.Style.php:67
__construct($srcFiles, Array $options, Array $styleConfig)
← centre documentaire © anakeen