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->incFiles = array(
00037 'js'=>array('nyro'=>array(), 'web'=>array(), 'nyroLast'=>array()),
00038 'css'=>array('nyro'=>array(), 'web'=>array(), 'nyroLast'=>array())
00039 );
00040 if (!empty($this->cfg->incFiles) && is_array($this->cfg->incFiles)) {
00041 foreach($this->cfg->incFiles as $ic)
00042 $this->add($ic);
00043 }
00044 }
00045
00051 public function getTitle() {
00052 return $this->getMeta('title');
00053 }
00054
00060 public function setTitle($title) {
00061 $this->setMeta('title', $title);
00062 }
00063
00070 public function addTitleBefore($title, $sep=', ') {
00071 $this->setMetaBefore('title', $title, $sep);
00072 }
00073
00080 public function addTitleAfter($title, $sep=', ') {
00081 $this->setMetaAfter('title', $title, $sep);
00082 }
00083
00090 public function getMeta($name) {
00091 return $this->cfg->getInArray('meta', $name);
00092 }
00093
00100 public function setMeta($name, $value) {
00101 $this->cfg->setInArray('meta', $name, $value);
00102 }
00103
00110 public function getLink($rel) {
00111 return $this->cfg->getInArray('link', $rel);
00112 }
00113
00120 public function setLink($rel, array $attributes) {
00121 $this->cfg->setInArray('link', $rel, $attributes);
00122 }
00123
00131 public function setMetaBefore($name, $value, $sep=', ') {
00132 $old = $this->getMeta($name);
00133 $value.= $old? $sep.$old : null;
00134 $this->setMeta($name, $value);
00135 }
00136
00144 public function setMetaAfter($name, $value, $sep=', ') {
00145 $old = $this->getMeta($name);
00146 $value = $old? $old.$sep.$value : $value;
00147 $this->setMeta($name, $value);
00148 }
00149
00155 protected function initBlocks($type) {
00156 if (!array_key_exists($type, $this->blocks))
00157 $this->blocks[$type] = array();
00158 }
00159
00169 public function add(array $prm) {
00170 if (config::initTab($prm, array(
00171 'type'=>null,
00172 'file'=>null,
00173 'dir'=>'nyro',
00174 'media'=>'screen',
00175 'condIE'=>false,
00176 'verifExists'=>true
00177 ))) {
00178 $ret = false;
00179 $firstFile = $prm['file'];
00180
00181 if (strpos($firstFile, 'jquery') === 0 && $firstFile != 'jquery')
00182 $this->addJS('jquery');
00183
00184 foreach($this->getDepend($prm['file'], $prm['type']) as $d) {
00185 if (is_array($d))
00186 $this->add(array_merge($prm, $d));
00187 else
00188 $this->add(array_merge($prm, array('file'=>$d)));
00189 }
00190
00191 foreach($this->cfg->getInArray($prm['type'], 'alias') as $k=>$a) {
00192 if (strtolower($prm['file']) == strtolower($k))
00193 $prm['file'] = $a;
00194 }
00195
00196 $prmType = $this->cfg->get($prm['type']);
00197
00198 $locDir = $prm['dir'];
00199 if (!array_key_exists($locDir, $this->incFiles[$prm['type']]))
00200 $locDir = 'nyro';
00201
00202 $fileExt = $prm['file'].'.'.$prm['type'];
00203
00204 if ($prm['verifExists'])
00205 $fileExists = $locDir == 'web'
00206 ?file::webExists($prmType['dirWeb'].DS.$fileExt)
00207 :file::nyroExists(array(
00208 'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$prm['type'].'_'.$prm['file'],
00209 'type'=>'tpl',
00210 'tplExt'=>$prm['type']
00211 ));
00212 else
00213 $fileExists = true;
00214
00215 if ($fileExists) {
00216 $this->incFiles[$prm['type']][$locDir][$prm['file']] = $prm;
00217 if ($prm['type'] == 'css') {
00218 $c = file::read($fileExists);
00219 preg_match_all('`@import (url\()?"(.+).css"\)?;`', $c, $matches);
00220 if (!empty($matches[2])) {
00221 $prefix = substr($prm['file'], 0, strpos($prm['file'], '_')+1);
00222 foreach($matches[2 ] as $m)
00223 $this->add(array_merge($prm, array('file'=>$prefix.$m)));
00224 }
00225 }
00226 $ret = true;
00227 }
00228
00229 foreach($this->getDepend($firstFile, $prm['type'], true) as $d) {
00230 if (is_array($d))
00231 $this->add(array_merge($prm, $d));
00232 else
00233 $this->add(array_merge($prm, array('file'=>$d)));
00234 }
00235
00236 return $ret;
00237 } else
00238 throw new nException('reponse::add: parameters file and/or type not provied');
00239 }
00240
00246 public function getIncFiles() {
00247 return $this->incFiles;
00248 }
00249
00257 public function getDepend($file, $type='js', $after=false) {
00258 $ret = array();
00259
00260 $depend = $this->cfg->getInArray($type, 'depend'.($after?'After' : null));
00261 if (is_array($depend) && array_key_exists($file, $depend)) {
00262 if (is_array($depend[$file]))
00263 $ret = $depend[$file];
00264 else
00265 $ret = array($depend[$file]);
00266 }
00267
00268 return $ret;
00269 }
00270
00281 public function addJs($prm) {
00282 if (is_array($prm))
00283 return $this->add(array_merge($prm, array('type'=>'js')));
00284 else
00285 return $this->add(array('file'=>$prm,'type'=>'js'));
00286 }
00287
00300 public function addCss($prm) {
00301 if (is_array($prm))
00302 return $this->add(array_merge($prm, array('type'=>'css')));
00303 else
00304 return $this->add(array('file'=>$prm,'type'=>'css'));
00305 }
00306
00315 public function block($block, $type='js', $first=false) {
00316 $this->initBlocks($type);
00317
00318 if ($first)
00319 array_unshift($this->blocks[$type], $block);
00320 else
00321 $this->blocks[$type][] = $block;
00322
00323 return true;
00324 }
00325
00334 public function blockJs($block, $first=false) {
00335 return $this->block($block, 'js', $first);
00336 }
00337
00344 public function blockjQuery($block) {
00345 $this->addJs('jquery');
00346 $this->blocksJquery[] = $block;
00347 }
00348
00357 public function blockCss($block, $first=false) {
00358 return $this->block($block, 'css', $first);
00359 }
00360
00370 public function getHtmlElt($prm='all', $ln="\n") {
00371 $ret = null;
00372 switch($prm) {
00373 case 'title':
00374 $ret.= '[{[TITLE]}]';
00375 break;
00376 case 'meta':
00377 $ret.= '[{[META]}]';
00378 break;
00379 case 'css':
00380 $ret.= '[{[CSS]}]';
00381 break;
00382 case 'js':
00383 $ret.= '[{[JS]}]';
00384 break;
00385 default:
00386 $ret.= $this->getHtmlElt('title').$ln
00387 .$this->getHtmlElt('meta').$ln
00388 .$this->getHtmlElt('css', $ln);
00389 }
00390 return $ret.$ln;
00391 }
00392
00400 protected function setHtmlEltIntern($content) {
00401 $ln = "\n";
00402 return str_replace(
00403 array('[{[TITLE]}]', '[{[META]}]', '[{[CSS]}]', '[{[JS]}]'),
00404 array(
00405 '<title>'.utils::htmlOut($this->getMeta('title')).'</title>',
00406 $this->getHtmlMeta(),
00407 $this->getHtmlIncFiles('css', $ln).$ln.$this->getHtmlBlocks('css', $ln),
00408 $this->getHtmlIncFiles('js', $ln).$ln.$this->getHtmlBlocks('js', $ln)
00409 ),
00410 $content);
00411 }
00412
00419 protected function getHtmlMeta($ln="\n") {
00420 $ret = null;
00421
00422 if (array_key_exists('Content-Type', $this->headers))
00423 $ret.= '<meta http-equiv="Content-Type" content="'.$this->headers['Content-Type'].'" />'.$ln;
00424
00425 if ($this->cfg->titleInDes)
00426 $this->cfg->setInArray('meta', 'description',
00427 $this->cfg->getInarray('meta', 'title').
00428 $this->cfg->titleInDes.
00429 $this->cfg->getInarray('meta', 'description'));
00430 foreach($this->cfg->meta as $k=>$v)
00431 $ret.= '<meta name="'.$k.'" content="'.utils::htmlOut($v).'" />'.$ln;
00432 foreach($this->cfg->link as $k=>$v)
00433 $ret.= utils::htmlTag('link', array_merge(array('rel'=>$k), utils::htmlOut($v))).$ln;
00434 return $ret;
00435 }
00436
00444 protected function getHtmlIncFiles($type, $ln="\n") {
00445 $ret = null;
00446 if (array_key_exists($type, $this->incFiles)) {
00447 $files = array_filter($this->incFiles[$type]);
00448 $prm = $this->cfg->get($type);
00449
00450 foreach($files as $dir=>$file) {
00451 if ($type == 'css') {
00452 $tmp = array();
00453 foreach($file as $f) {
00454 $tmp[$f['condIE']][$f['media']][] = $f['file'];
00455 }
00456 foreach($tmp as $ie=>$t) {
00457 if ($ie)
00458 $ret.= '<!--[if '.$ie.']>'.$ln;
00459 foreach($t as $media=>$f)
00460 $ret.= $this->getIncludeTagFile($type,
00461 $f,
00462 $dir,
00463 $media
00464 ).$ln;
00465 if ($ie)
00466 $ret.= '<![endif]-->'.$ln;
00467 }
00468 } else {
00469 $tmp = array();
00470 foreach($file as $f) {
00471 $tmp[$f['condIE']][] = $f['file'];
00472 }
00473 foreach($tmp as $ie=>$f) {
00474 if ($ie)
00475 $ret.= '<!--[if '.$ie.']>'.$ln;
00476 $ret.= $this->getIncludeTagFile($type,
00477 $f,
00478 $dir
00479 ).$ln;
00480 if ($ie)
00481 $ret.= '<![endif]-->'.$ln;
00482 }
00483 }
00484 }
00485 }
00486 return $ret;
00487 }
00488
00498 public function getIncludeTagFile($type, $files, $dir='nyro', $media='screen') {
00499 $prm = $this->cfg->get($type);
00500 $url = $this->getUrlFile($type, $files, $dir);
00501 return sprintf($prm['include'], $url, $media);
00502 }
00503
00512 public function getUrlFile($type, $files, $dir='nyro') {
00513 $prm = $this->cfg->get($type);
00514 $url = $dir == 'web'
00515 ? request::get('path').$prm['dirWeb']
00516 : request::getPathControllerUri().$prm['dirUriNyro'];
00517 $url.= '/'.(is_array($files)? implode(request::getCfg('sepParam'), $files) : $files);
00518 $url.= '.'.$prm['ext'];
00519 return $url;
00520 }
00521
00529 public function getHtmlBlocks($type, $ln="\n") {
00530 $ret = null;
00531
00532 if ($type == 'js' && !empty($this->blocksJquery))
00533 $this->blockJs('jQuery(function($) {'.$ln.implode($ln, $this->blocksJquery).$ln.'});');
00534
00535 if (array_key_exists($type, $this->blocks)) {
00536 $prm = $this->cfg->get($type);
00537 $ret.= sprintf($prm['block'], implode($ln, $this->blocks[$type]));
00538 }
00539
00540 return $ret;
00541 }
00542
00543 public function send($headerOnly=false) {
00544 $ret = parent::send($headerOnly);
00545 return $this->setHtmlEltIntern(DEV ? str_replace('</body>', debug::debugger().'</body>', $ret) : $ret);
00546 }
00547
00548 }