00001 <?php
00011 class response_http_html extends response_http {
00012
00018 protected $incFiles;
00019
00025 protected $blocks = array();
00026
00032 protected $blocksJquery = array();
00033
00034 protected function afterInit() {
00035 parent::afterInit();
00036 $this->initIncFiles();
00037 }
00038
00044 public function initIncFiles($addDefaults = true) {
00045 $this->incFiles = array(
00046 'js'=>array('nyro'=>array(), 'web'=>array(), 'nyroLast'=>array()),
00047 'css'=>array('nyro'=>array(), 'web'=>array(), 'nyroLast'=>array())
00048 );
00049 if ($addDefaults && !empty($this->cfg->incFiles) && is_array($this->cfg->incFiles)) {
00050 foreach($this->cfg->incFiles as $ic)
00051 $this->add($ic);
00052 }
00053 }
00054
00060 public function getTitle() {
00061 return $this->getMeta('title');
00062 }
00063
00069 public function setTitle($title) {
00070 $this->setMeta('title', $title);
00071 }
00072
00079 public function addTitleBefore($title, $sep = ', ') {
00080 $this->setMetaBefore('title', $title, $sep);
00081 }
00082
00089 public function addTitleAfter($title, $sep = ', ') {
00090 $this->setMetaAfter('title', $title, $sep);
00091 }
00092
00098 public function setTitleInDes($titleInDes) {
00099 $this->cfg->titleInDes = $titleInDes;
00100 }
00101
00108 public function getMeta($name) {
00109 return $this->cfg->getInArray('meta', $name);
00110 }
00111
00118 public function setMeta($name, $value) {
00119 $this->cfg->setInArray('meta', $name, $value);
00120 }
00121
00128 public function getMetaProperty($name) {
00129 return $this->cfg->getInArray('metaProperty', $name);
00130 }
00131
00138 public function setMetaProperty($name, $value) {
00139 $this->cfg->setInArray('metaProperty', $name, $value);
00140 }
00141
00148 public function getLink($rel) {
00149 return $this->cfg->getInArray('link', $rel);
00150 }
00151
00158 public function setLink($rel, array $attributes) {
00159 $this->cfg->setInArray('link', $rel, $attributes);
00160 }
00161
00169 public function setMetaBefore($name, $value, $sep = ', ') {
00170 $old = $this->getMeta($name);
00171 $value.= $old? $sep.$old : null;
00172 $this->setMeta($name, $value);
00173 }
00174
00182 public function setMetaAfter($name, $value, $sep = ', ') {
00183 $old = $this->getMeta($name);
00184 $value = $old? $old.$sep.$value : $value;
00185 $this->setMeta($name, $value);
00186 }
00187
00193 protected function initBlocks($type) {
00194 if (!array_key_exists($type, $this->blocks))
00195 $this->blocks[$type] = array();
00196 }
00197
00207 public function add(array $prm) {
00208 if (config::initTab($prm, array(
00209 'type'=>null,
00210 'file'=>null,
00211 'dir'=>'nyro',
00212 'media'=>'screen',
00213 'condIE'=>false,
00214 'verifExists'=>true
00215 ))) {
00216 $ret = false;
00217 $firstFile = $prm['file'];
00218
00219 if (strpos($firstFile, 'jquery') === 0 && $firstFile != 'jquery')
00220 $this->addJS('jquery');
00221
00222 foreach($this->getDepend($prm['file'], $prm['type']) as $d) {
00223 if (is_array($d))
00224 $this->add(array_merge($prm, $d));
00225 else
00226 $this->add(array_merge($prm, array('file'=>$d)));
00227 }
00228
00229 foreach($this->cfg->getInArray($prm['type'], 'alias') as $k=>$a) {
00230 if (strtolower($prm['file']) == strtolower($k))
00231 $prm['file'] = $a;
00232 }
00233
00234 $prmType = $this->cfg->get($prm['type']);
00235
00236 $locDir = $prm['dir'];
00237 if (!array_key_exists($locDir, $this->incFiles[$prm['type']]))
00238 $locDir = 'nyro';
00239
00240 $fileExt = $prm['file'].'.'.$prm['type'];
00241
00242 if ($prm['verifExists'])
00243 $fileExists = $locDir == 'web'
00244 ?file::webExists($prmType['dirWeb'].DS.$fileExt)
00245 :file::nyroExists(array(
00246 'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$prm['type'].'_'.$prm['file'],
00247 'type'=>'tpl',
00248 'tplExt'=>$prm['type']
00249 ));
00250 else
00251 $fileExists = true;
00252
00253 if ($fileExists) {
00254 if (!isset($this->incFiles[$prm['type']][$locDir][$prm['media']]))
00255 $this->incFiles[$prm['type']][$locDir][$prm['media']] = array();
00256 $this->incFiles[$prm['type']][$locDir][$prm['media']][$prm['file']] = $prm;
00257 if ($prm['type'] == 'css') {
00258 $c = file::read($fileExists);
00259 preg_match_all('`@import (url\()?"(.+).css"\)?;`', $c, $matches);
00260 if (!empty($matches[2])) {
00261 $prefix = substr($prm['file'], 0, strpos($prm['file'], '_')+1);
00262 foreach($matches[2 ] as $m)
00263 $this->add(array_merge($prm, array('file'=>$prefix.$m)));
00264 }
00265 }
00266 $ret = true;
00267 }
00268
00269 foreach($this->getDepend($firstFile, $prm['type'], true) as $d) {
00270 if (is_array($d))
00271 $this->add(array_merge($prm, $d));
00272 else
00273 $this->add(array_merge($prm, array('file'=>$d)));
00274 }
00275
00276 return $ret;
00277 } else
00278 throw new nException('reponse::add: parameters file and/or type not provied');
00279 }
00280
00286 public function getIncFiles() {
00287 return $this->incFiles;
00288 }
00289
00297 public function getDepend($file, $type = 'js', $after = false) {
00298 $ret = array();
00299
00300 $depend = $this->cfg->getInArray($type, 'depend'.($after?'After' : null));
00301 if (is_array($depend) && array_key_exists($file, $depend)) {
00302 if (is_array($depend[$file]))
00303 $ret = $depend[$file];
00304 else
00305 $ret = array($depend[$file]);
00306 }
00307
00308 return $ret;
00309 }
00310
00321 public function addJs($prm) {
00322 if (is_array($prm))
00323 return $this->add(array_merge($prm, array('type'=>'js')));
00324 else
00325 return $this->add(array('file'=>$prm,'type'=>'js'));
00326 }
00327
00340 public function addCss($prm) {
00341 if (is_array($prm))
00342 return $this->add(array_merge($prm, array('type'=>'css')));
00343 else
00344 return $this->add(array('file'=>$prm,'type'=>'css'));
00345 }
00346
00355 public function block($block, $type = 'js', $first = false) {
00356 $this->initBlocks($type);
00357
00358 if ($first)
00359 array_unshift($this->blocks[$type], $block);
00360 else
00361 $this->blocks[$type][] = $block;
00362
00363 return true;
00364 }
00365
00374 public function blockJs($block, $first = false) {
00375 return $this->block($block, 'js', $first);
00376 }
00377
00385 public function blockjQuery($block, $addjQuery = true) {
00386 if ($addjQuery)
00387 $this->addJs('jquery');
00388 $this->blocksJquery[] = $block;
00389 }
00390
00399 public function blockCss($block, $first = false) {
00400 return $this->block($block, 'css', $first);
00401 }
00402
00412 public function getHtmlElt($prm = 'all', $ln = "\n") {
00413 $ret = null;
00414 switch($prm) {
00415 case 'title':
00416 $ret.= '[{[TITLE]}]';
00417 break;
00418 case 'meta':
00419 $ret.= '[{[META]}]';
00420 break;
00421 case 'css':
00422 $ret.= '[{[CSS]}]';
00423 break;
00424 case 'js':
00425 $ret.= '[{[JS]}]';
00426 break;
00427 default:
00428 $ret.= $this->getHtmlElt('title').$ln
00429 .$this->getHtmlElt('meta').$ln
00430 .$this->getHtmlElt('css', $ln);
00431 }
00432 return $ret.$ln;
00433 }
00434
00442 protected function setHtmlEltIntern($content) {
00443 $ln = "\n";
00444 $jsBlocks = $this->getHtmlBlocks('js', $ln);
00445 $addJsBlocks = request::isAjax() && strpos($content, '[{[JS]}]') === false;
00446 return str_replace(
00447 array('[{[TITLE]}]', '[{[META]}]', '[{[CSS]}]', '[{[JS]}]'),
00448 array(
00449 '<title>'.utils::htmlOut($this->getMeta('title')).'</title>',
00450 $this->getHtmlMeta(),
00451 $this->getHtmlIncFiles('css', $ln).$ln.$this->getHtmlBlocks('css', $ln),
00452 $this->getHtmlIncFiles('js', $ln).$ln.$jsBlocks
00453 ),
00454 $content).($addJsBlocks ? $jsBlocks : null);
00455
00456 }
00457
00464 protected function getHtmlMeta($ln = "\n") {
00465 $ret = null;
00466
00467 if (array_key_exists('Content-Type', $this->headers))
00468 $ret.= '<meta http-equiv="Content-Type" content="'.$this->headers['Content-Type'].'" />'.$ln;
00469
00470 if ($this->cfg->titleInDes)
00471 $this->cfg->setInArray('meta', 'description',
00472 $this->cfg->getInarray('meta', 'title').
00473 $this->cfg->titleInDes.
00474 $this->cfg->getInarray('meta', 'description'));
00475 foreach($this->cfg->meta as $k=>$v) {
00476 if ($k != 'title' || $this->cfg->useTitleInMeta)
00477 $ret.= '<meta name="'.$k.'" content="'.utils::htmlOut($v).'" />'.$ln;
00478 }
00479 foreach($this->cfg->metaProperty as $k=>$v) {
00480 $ret.= '<meta property="'.$k.'" content="'.utils::htmlOut($v).'" />'.$ln;
00481 }
00482 foreach($this->cfg->link as $k=>$v)
00483 $ret.= utils::htmlTag('link', array_merge(array('rel'=>$k), utils::htmlOut($v))).$ln;
00484 return $ret;
00485 }
00486
00494 protected function getHtmlIncFiles($type, $ln = "\n") {
00495 $ret = null;
00496 if (array_key_exists($type, $this->incFiles)) {
00497 $all = array_filter($this->incFiles[$type]);
00498 $prm = $this->cfg->get($type);
00499
00500 foreach($all as $dir=>$medias) {
00501 foreach($medias as $media=>$files) {
00502 $tmp = array();
00503 foreach($files as $f) {
00504 $tmp[$f['condIE']][] = $f['file'];
00505 }
00506 foreach($tmp as $ie=>$t) {
00507 if ($ie)
00508 $ret.= '<!--[if '.$ie.']>'.$ln;
00509 $ret.= $this->getIncludeTagFile($type,
00510 $t,
00511 $dir,
00512 $media
00513 ).$ln;
00514 if ($ie)
00515 $ret.= '<![endif]-->'.$ln;
00516 }
00517 }
00518 }
00519 }
00520 return $ret;
00521 }
00522
00532 public function getIncludeTagFile($type, $files, $dir = 'nyro', $media = 'screen') {
00533 $prm = $this->cfg->get($type);
00534 $url = $this->getUrlFile($type, $files, $dir);
00535 return sprintf($prm['include'], $url, $media);
00536 }
00537
00546 public function getUrlFile($type, $files, $dir = 'nyro') {
00547 $prm = $this->cfg->get($type);
00548 $url = $dir == 'web'
00549 ? request::get('path').$prm['dirWeb']
00550 : request::getPathControllerUri().$prm['dirUriNyro'];
00551 if (request::isAbsolutizeAllUris())
00552 $url = request::get('domain').$url;
00553 $url.= '/'.(is_array($files)? implode(request::getCfg('sepParam'), $files) : $files);
00554 $url.= '.'.$prm['ext'];
00555 return $url;
00556 }
00557
00565 public function getHtmlBlocks($type, $ln = "\n") {
00566 $ret = null;
00567
00568 if ($type == 'js' && !empty($this->blocksJquery))
00569 $this->blockJs('jQuery(function($) {'.$ln.implode($ln, $this->blocksJquery).$ln.'});');
00570
00571 if (array_key_exists($type, $this->blocks)) {
00572 $prm = $this->cfg->get($type);
00573 $ret.= sprintf($prm['block'], implode($ln, $this->blocks[$type]));
00574 }
00575
00576 return $ret;
00577 }
00578
00579 public function send($headerOnly = false) {
00580 $ret = parent::send($headerOnly);
00581 return $this->setHtmlEltIntern(DEV ? str_replace('</body>', debug::debugger().'</body>', $ret) : $ret);
00582 }
00583
00584 }