nyroFwk  0.2
html.class.php
Go to the documentation of this file.
1 <?php
12 
18  protected $incFiles;
19 
25  protected $blocks = array();
26 
32  protected $blocksJquery = array();
33 
34  protected function afterInit() {
35  parent::afterInit();
36  $this->initIncFiles();
37  }
38 
44  public function initIncFiles($addDefaults = true) {
45  $this->incFiles = array(
46  'js'=>array('nyro'=>array(), 'web'=>array(), 'nyroLast'=>array()),
47  'css'=>array('nyro'=>array(), 'web'=>array(), 'nyroLast'=>array())
48  );
49  if ($addDefaults && !empty($this->cfg->incFiles) && is_array($this->cfg->incFiles)) {
50  foreach($this->cfg->incFiles as $ic)
51  $this->add($ic);
52  }
53  }
54 
60  public function getTitle() {
61  return $this->getMeta('title');
62  }
63 
69  public function setTitle($title) {
70  $this->setMeta('title', $title);
71  }
72 
79  public function addTitleBefore($title, $sep = ', ') {
80  $this->setMetaBefore('title', $title, $sep);
81  }
82 
89  public function addTitleAfter($title, $sep = ', ') {
90  $this->setMetaAfter('title', $title, $sep);
91  }
92 
98  public function setTitleInDes($titleInDes) {
99  $this->cfg->titleInDes = $titleInDes;
100  }
101 
108  public function getMeta($name) {
109  return $this->cfg->getInArray('meta', $name);
110  }
111 
118  public function setMeta($name, $value) {
119  $this->cfg->setInArray('meta', $name, $value);
120  }
121 
128  public function getMetaProperty($name) {
129  return $this->cfg->getInArray('metaProperty', $name);
130  }
131 
138  public function setMetaProperty($name, $value) {
139  $this->cfg->setInArray('metaProperty', $name, $value);
140  }
141 
148  public function getLink($rel) {
149  return $this->cfg->getInArray('link', $rel);
150  }
151 
158  public function setLink($rel, array $attributes) {
159  $this->cfg->setInArray('link', $rel, $attributes);
160  }
161 
169  public function setMetaBefore($name, $value, $sep = ', ') {
170  $old = $this->getMeta($name);
171  $value.= $old? $sep.$old : null;
172  $this->setMeta($name, $value);
173  }
174 
182  public function setMetaAfter($name, $value, $sep = ', ') {
183  $old = $this->getMeta($name);
184  $value = $old? $old.$sep.$value : $value;
185  $this->setMeta($name, $value);
186  }
187 
195  public function setMetaPropertyBefore($name, $value, $sep = ', ') {
196  $old = $this->getMetaProperty($name);
197  $value.= $old? $sep.$old : null;
198  $this->setMetaProperty($name, $value);
199  }
200 
208  public function setMetaPropertyAfter($name, $value, $sep = ', ') {
209  $old = $this->getMetaProperty($name);
210  $value = $old? $old.$sep.$value : $value;
211  $this->setMetaProperty($name, $value);
212  }
213 
219  protected function initBlocks($type) {
220  if (!array_key_exists($type, $this->blocks))
221  $this->blocks[$type] = array();
222  }
223 
233  public function add(array $prm) {
234  if (config::initTab($prm, array(
235  'type'=>null,
236  'file'=>null,
237  'dir'=>'nyro',
238  'media'=>'screen',
239  'condIE'=>false,
240  'verifExists'=>true
241  ))) {
242  $ret = false;
243  $firstFile = $prm['file'];
244 
245  if (strpos($firstFile, 'jquery') === 0 && $firstFile != 'jquery')
246  $this->addJS('jquery');
247 
248  foreach($this->getDepend($prm['file'], $prm['type']) as $d) {
249  if (is_array($d))
250  $this->add(array_merge($prm, $d));
251  else
252  $this->add(array_merge($prm, array('file'=>$d)));
253  }
254 
255  foreach($this->cfg->getInArray($prm['type'], 'alias') as $k=>$a) {
256  if (strtolower($prm['file']) == strtolower($k))
257  $prm['file'] = $a;
258  }
259 
260  $prmType = $this->cfg->get($prm['type']);
261 
262  $locDir = $prm['dir'];
263  if (!array_key_exists($locDir, $this->incFiles[$prm['type']]))
264  $locDir = 'nyro';
265 
266  $fileExt = $prm['file'].'.'.$prm['type'];
267 
268  if ($prm['verifExists'])
269  $fileExists = $locDir == 'web'
270  ? file::webExists($prmType['dirWeb'].DS.$fileExt)
271  : file::nyroExists(array(
272  'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$prm['type'].'_'.$prm['file'],
273  'type'=>'tpl',
274  'tplExt'=>$prm['type']
275  ));
276  else
277  $fileExists = true;
278 
279  if ($fileExists) {
280  if (!isset($this->incFiles[$prm['type']][$locDir][$prm['media']]))
281  $this->incFiles[$prm['type']][$locDir][$prm['media']] = array();
282  $this->incFiles[$prm['type']][$locDir][$prm['media']][$prm['file']] = $prm;
283  if ($prm['type'] == 'css') {
284  $c = file::read($fileExists);
285  preg_match_all('`@import (url\()?"(.+).css"\)?;`', $c, $matches);
286  if (!empty($matches[2])) {
287  $prefix = substr($prm['file'], 0, strpos($prm['file'], '_')+1);
288  foreach($matches[2 ] as $m)
289  $this->add(array_merge($prm, array('file'=>$prefix.$m)));
290  }
291  }
292  $ret = true;
293  }
294 
295  foreach($this->getDepend($firstFile, $prm['type'], true) as $d) {
296  if (is_array($d))
297  $this->add(array_merge($prm, $d));
298  else
299  $this->add(array_merge($prm, array('file'=>$d)));
300  }
301 
302  return $ret;
303  } else
304  throw new nException('reponse::add: parameters file and/or type not provied');
305  }
306 
312  public function getIncFiles() {
313  return $this->incFiles;
314  }
315 
323  public function getDepend($file, $type = 'js', $after = false) {
324  $ret = array();
325 
326  $depend = $this->cfg->getInArray($type, 'depend'.($after?'After' : null));
327  if (is_array($depend) && array_key_exists($file, $depend)) {
328  if (is_array($depend[$file]))
329  $ret = $depend[$file];
330  else
331  $ret = array($depend[$file]);
332  }
333 
334  return $ret;
335  }
336 
347  public function addJs($prm) {
348  if (is_array($prm))
349  return $this->add(array_merge($prm, array('type'=>'js')));
350  else
351  return $this->add(array('file'=>$prm,'type'=>'js'));
352  }
353 
366  public function addCss($prm) {
367  if (is_array($prm))
368  return $this->add(array_merge($prm, array('type'=>'css')));
369  else
370  return $this->add(array('file'=>$prm,'type'=>'css'));
371  }
372 
381  public function block($block, $type = 'js', $first = false) {
382  $this->initBlocks($type);
383 
384  if ($first)
385  array_unshift($this->blocks[$type], $block);
386  else
387  $this->blocks[$type][] = $block;
388 
389  return true;
390  }
391 
400  public function blockJs($block, $first = false) {
401  return $this->block($block, 'js', $first);
402  }
403 
411  public function blockjQuery($block, $addjQuery = true) {
412  if ($addjQuery)
413  $this->addJs('jquery');
414  $this->blocksJquery[] = $block;
415  }
416 
425  public function blockCss($block, $first = false) {
426  return $this->block($block, 'css', $first);
427  }
428 
438  public function getHtmlElt($prm = 'all', $ln = "\n") {
439  $ret = null;
440  switch($prm) {
441  case 'title':
442  $ret.= '[{[TITLE]}]';
443  break;
444  case 'meta':
445  $ret.= '[{[META]}]';
446  break;
447  case 'css':
448  $ret.= '[{[CSS]}]';
449  break;
450  case 'js':
451  $ret.= '[{[JS]}]';
452  break;
453  default:
454  $ret.= $this->getHtmlElt('title').$ln
455  .$this->getHtmlElt('meta').$ln
456  .$this->getHtmlElt('css', $ln);
457  }
458  return $ret.$ln;
459  }
460 
468  protected function setHtmlEltIntern($content) {
469  $ln = "\n";
470  $jsBlocks = $this->getHtmlBlocks('js', $ln);
471  $addJsBlocks = request::isAjax() && strpos($content, '[{[JS]}]') === false;
472  return str_replace(
473  array('[{[TITLE]}]', '[{[META]}]', '[{[CSS]}]', '[{[JS]}]'),
474  array(
475  '<title>'.utils::htmlOut($this->getMeta('title')).'</title>',
476  $this->getHtmlMeta(),
477  $this->getHtmlIncFiles('css', $ln).$ln.$this->getHtmlBlocks('css', $ln),
478  $this->getHtmlIncFiles('js', $ln).$ln.$jsBlocks
479  ),
480  $content).($addJsBlocks ? $jsBlocks : null);
481 
482  }
483 
490  protected function getHtmlMeta($ln = "\n") {
491  $ret = null;
492 
493  if (array_key_exists('Content-Type', $this->headers))
494  $ret.= '<meta http-equiv="Content-Type" content="'.$this->headers['Content-Type'].'" />'.$ln;
495 
496  if ($this->cfg->titleInDes)
497  $this->cfg->setInArray('meta', 'description',
498  $this->cfg->getInarray('meta', 'title').
499  $this->cfg->titleInDes.
500  $this->cfg->getInarray('meta', 'description'));
501  foreach($this->cfg->meta as $k=>$v) {
502  if ($k != 'title' || $this->cfg->useTitleInMeta)
503  $ret.= '<meta name="'.$k.'" content="'.utils::htmlOut($v).'" />'.$ln;
504  }
505  foreach($this->cfg->metaProperty as $k=>$v)
506  $ret.= '<meta property="'.$k.'" content="'.utils::htmlOut($v).'" />'.$ln;
507  foreach($this->cfg->link as $k=>$v)
508  $ret.= utils::htmlTag('link', array_merge(array('rel'=>$k), utils::htmlOut($v))).$ln;
509  return $ret;
510  }
511 
519  protected function getHtmlIncFiles($type, $ln = "\n") {
520  $ret = null;
521  if (array_key_exists($type, $this->incFiles)) {
522  $all = array_filter($this->incFiles[$type]);
523  $prm = $this->cfg->get($type);
524 
525  foreach($all as $dir=>$medias) {
526  foreach($medias as $media=>$files) {
527  $tmp = array();
528  foreach($files as $f) {
529  $tmp[$f['condIE']][] = $f['file'];
530  }
531  foreach($tmp as $ie=>$t) {
532  if ($ie)
533  $ret.= '<!--[if '.$ie.']>'.$ln;
534  $ret.= $this->getIncludeTagFile($type,
535  $t,
536  $dir,
537  $media
538  ).$ln;
539  if ($ie)
540  $ret.= '<![endif]-->'.$ln;
541  }
542  }
543  }
544  }
545  return $ret;
546  }
547 
557  public function getIncludeTagFile($type, $files, $dir = 'nyro', $media = 'screen') {
558  $prm = $this->cfg->get($type);
559  $url = $this->getUrlFile($type, $files, $dir);
560  return sprintf($prm['include'], $url, $media);
561  }
562 
571  public function getUrlFile($type, $files, $dir = 'nyro') {
572  $prm = $this->cfg->get($type);
573  $url = $dir == 'web'
574  ? request::get('path').$prm['dirWeb']
575  : request::getPathControllerUri().$prm['dirUriNyro'];
577  $url = request::get('domain').$url;
578  $url.= '/'.(is_array($files)? implode(request::getCfg('sepParam'), $files) : $files);
579  $url.= '.'.$prm['ext'];
580  return $url;
581  }
582 
590  public function getHtmlBlocks($type, $ln = "\n") {
591  $ret = null;
592 
593  if ($type == 'js' && !empty($this->blocksJquery))
594  $this->blockJs('jQuery(function($) {'.$ln.implode($ln, $this->blocksJquery).$ln.'});');
595 
596  if (array_key_exists($type, $this->blocks)) {
597  $prm = $this->cfg->get($type);
598  $ret.= sprintf($prm['block'], implode($ln, $this->blocks[$type]));
599  }
600 
601  return $ret;
602  }
603 
604  public function send($headerOnly = false) {
605  $ret = parent::send($headerOnly);
606  $ret = $this->setHtmlEltIntern(DEV ? str_replace('</body>', debug::debugger().'</body>', $ret) : $ret);
607  //if ($ret) $this->addHeader('Content-Length', strlen($ret), true);
608  return $ret;
609  }
610 
611 }
getHtmlBlocks($type, $ln="\)
Definition: html.class.php:590
addTitleBefore($title, $sep=', ')
Definition: html.class.php:79
static get($get=null)
static htmlOut($val, $key=false)
getIncludeTagFile($type, $files, $dir='nyro', $media='screen')
Definition: html.class.php:557
addTitleAfter($title, $sep=', ')
Definition: html.class.php:89
getDepend($file, $type='js', $after=false)
Definition: html.class.php:323
blockJs($block, $first=false)
Definition: html.class.php:400
static read($file)
Definition: file.class.php:242
static htmlTag($tag, array $attributes, $content=null)
Definition: utils.class.php:46
setMetaProperty($name, $value)
Definition: html.class.php:138
static getCfg()
Definition: nyro.class.php:133
if(!defined('NYROROOT')) define('NYROROOT' ROOT DS
Definition: start.inc.php:56
setMetaPropertyBefore($name, $value, $sep=', ')
Definition: html.class.php:195
getUrlFile($type, $files, $dir='nyro')
Definition: html.class.php:571
blockCss($block, $first=false)
Definition: html.class.php:425
static webExists($file)
Definition: file.class.php:232
static getCfg($key=null)
add(array $prm)
Definition: html.class.php:233
static initTab(array &$vars, array $init)
static getPathControllerUri($forceController=false)
setMetaAfter($name, $value, $sep=', ')
Definition: html.class.php:182
static debugger(array $elts=null)
Definition: debug.class.php:88
setMeta($name, $value)
Definition: html.class.php:118
setHtmlEltIntern($content)
Definition: html.class.php:468
setLink($rel, array $attributes)
Definition: html.class.php:158
static isAjax()
getHtmlElt($prm='all', $ln="\)
Definition: html.class.php:438
static nyroExists($prm)
Definition: file.class.php:137
setMetaPropertyAfter($name, $value, $sep=', ')
Definition: html.class.php:208
getHtmlIncFiles($type, $ln="\)
Definition: html.class.php:519
send($headerOnly=false)
Definition: html.class.php:604
static isAbsolutizeAllUris()
initIncFiles($addDefaults=true)
Definition: html.class.php:44
blockjQuery($block, $addjQuery=true)
Definition: html.class.php:411
setTitleInDes($titleInDes)
Definition: html.class.php:98
$f
Definition: list.php:6
setMetaBefore($name, $value, $sep=', ')
Definition: html.class.php:169
block($block, $type='js', $first=false)
Definition: html.class.php:381
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13