HosipLan

Untitled

Jan 4th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This file is part of the Kdyby (http://www.kdyby.org)
  5.  *
  6.  * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
  7.  *
  8.  * @license http://www.kdyby.org/license
  9.  */
  10.  
  11. namespace Kdyby\Components;
  12.  
  13. use Kdyby;
  14. use Kdyby\Assets\FormulaeManager;
  15. use Nette;
  16. use Nette\Application\Application;
  17. use Nette\Utils\Html;
  18.  
  19.  
  20.  
  21. /**
  22.  * @author Filip Procházka <[email protected]>
  23.  */
  24. class HeaderControl extends Kdyby\Application\UI\Control
  25. {
  26.  
  27.     /** @var \Nette\Utils\Html */
  28.     private $headEl;
  29.  
  30.     /** @var \Nette\Utils\Html */
  31.     private $titleEl;
  32.  
  33.     /** @var \Kdyby\Assets\FormulaeManager */
  34.     private $formulaeManager;
  35.  
  36.     /** @var array */
  37.     private $meta = array(
  38.         'content-type' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8'),
  39.         'robots' => array('name' => 'robots', 'content' => 'noindex')
  40.     );
  41.  
  42.  
  43.  
  44.     /**
  45.      * @param \Nette\Application\Application $application
  46.      * @param \Kdyby\Assets\FormulaeManager $formulaeManager
  47.      */
  48.     public function __construct(Application $application, FormulaeManager $formulaeManager)
  49.     {
  50.         parent::__construct();
  51.  
  52.         $this->formulaeManager = $formulaeManager;
  53.         $application->onShutdown[] = function () use ($formulaeManager) {
  54.             $formulaeManager->publish();
  55.         };
  56.  
  57.         $this->headEl = Html::el('head');
  58.         $this->titleEl = Html::el('title');
  59.     }
  60.  
  61.  
  62.  
  63.     /**
  64.      * @return \Nette\Utils\Html
  65.      */
  66.     public function getHeadPrototype()
  67.     {
  68.         return $this->headEl;
  69.     }
  70.  
  71.  
  72.  
  73.     /**
  74.      * @param array|string $title
  75.      * @param bool $reverse
  76.      * @param string $separator
  77.      */
  78.     public function setTitle($title, $reverse = FALSE, $separator = '-')
  79.     {
  80.         if (is_array($title)) {
  81.             $title = $reverse ? array_reverse($title) : $title;
  82.             $title = implode(' ' . $separator . ' ', $title);
  83.         }
  84.  
  85.         $this->titleEl->setText($title);
  86.     }
  87.  
  88.  
  89.  
  90.     /**
  91.      * @param array $meta
  92.      */
  93.     public function addMeta(array $meta)
  94.     {
  95.         if (!isset($meta['name']) && !isset($meta['http-equiv'])) {
  96.             throw new Kdyby\InvalidArgumentException('Meta must contain either "name" or "http-equiv".');
  97.         }
  98.  
  99.         $key = isset($meta['name']) ? $meta['name'] : $meta['http-equiv'];
  100.         $this->meta[strtolower($key)] = $meta;
  101.     }
  102.  
  103.  
  104.  
  105.     /**
  106.      */
  107.     public function render()
  108.     {
  109.         $head = clone $this->headEl;
  110.  
  111.         // meta
  112.         foreach ($this->meta as $meta) {
  113.             $head->add(Html::el('meta')->addAttributes($meta));
  114.         }
  115.  
  116.         // title
  117.         $head->add(clone $this->titleEl);
  118.  
  119.         // styles
  120.         $fm = $this->formulaeManager;
  121.         foreach ($fm->getAssets(FormulaeManager::TYPE_STYLESHEET) as $style) {
  122.             $el = Html::el('link')->href($style['src'])->type('text/css');
  123.             $el->rel(isset($style['rel']) ? $style['rel'] : 'stylesheet');
  124.             $el->media(isset($style['media']) ? $style['media'] : 'screen,projection,tv');
  125.             $head->add($el);
  126.         }
  127.  
  128.         // scripts
  129.         foreach ($fm->getAssets(FormulaeManager::TYPE_JAVASCRIPT) as $script) {
  130.             $head->add(Html::el('script')->src($script['src'])->type('text/javascript'));
  131.         }
  132.  
  133.         echo $head;
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment