*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / View / Helper / HeadStyle.php
blobee1242f709f974189044d0c8be0daef2b0b24eff
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_View
17 * @subpackage Helper
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @version $Id: HeadStyle.php 16971 2009-07-22 18:05:45Z mikaelkael $
20 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_View_Helper_Placeholder_Container_Standalone */
24 require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
26 /**
27 * Helper for setting and retrieving stylesheets
29 * @uses Zend_View_Helper_Placeholder_Container_Standalone
30 * @package Zend_View
31 * @subpackage Helper
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_View_Helper_HeadStyle extends Zend_View_Helper_Placeholder_Container_Standalone
37 /**
38 * Registry key for placeholder
39 * @var string
41 protected $_regKey = 'Zend_View_Helper_HeadStyle';
43 /**
44 * Allowed optional attributes
45 * @var array
47 protected $_optionalAttributes = array('lang', 'title', 'media', 'dir');
49 /**
50 * Allowed media types
51 * @var array
53 protected $_mediaTypes = array(
54 'all', 'aural', 'braille', 'handheld', 'print',
55 'projection', 'screen', 'tty', 'tv'
58 /**
59 * Capture type and/or attributes (used for hinting during capture)
60 * @var string
62 protected $_captureAttrs = null;
64 /**
65 * Capture lock
66 * @var bool
68 protected $_captureLock;
70 /**
71 * Capture type (append, prepend, set)
72 * @var string
74 protected $_captureType;
76 /**
77 * Constructor
79 * Set separator to PHP_EOL.
81 * @return void
83 public function __construct()
85 parent::__construct();
86 $this->setSeparator(PHP_EOL);
89 /**
90 * Return headStyle object
92 * Returns headStyle helper object; optionally, allows specifying
94 * @param string $content Stylesheet contents
95 * @param string $placement Append, prepend, or set
96 * @param string|array $attributes Optional attributes to utilize
97 * @return Zend_View_Helper_HeadStyle
99 public function headStyle($content = null, $placement = 'APPEND', $attributes = array())
101 if ((null !== $content) && is_string($content)) {
102 switch (strtoupper($placement)) {
103 case 'SET':
104 $action = 'setStyle';
105 break;
106 case 'PREPEND':
107 $action = 'prependStyle';
108 break;
109 case 'APPEND':
110 default:
111 $action = 'appendStyle';
112 break;
114 $this->$action($content, $attributes);
117 return $this;
121 * Overload method calls
123 * Allows the following method calls:
124 * - appendStyle($content, $attributes = array())
125 * - offsetSetStyle($index, $content, $attributes = array())
126 * - prependStyle($content, $attributes = array())
127 * - setStyle($content, $attributes = array())
129 * @param string $method
130 * @param array $args
131 * @return void
132 * @throws Zend_View_Exception When no $content provided or invalid method
134 public function __call($method, $args)
136 if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(Style)$/', $method, $matches)) {
137 $index = null;
138 $argc = count($args);
139 $action = $matches['action'];
141 if ('offsetSet' == $action) {
142 if (0 < $argc) {
143 $index = array_shift($args);
144 --$argc;
148 if (1 > $argc) {
149 require_once 'Zend/View/Exception.php';
150 throw new Zend_View_Exception(sprintf('Method "%s" requires minimally content for the stylesheet', $method));
153 $content = $args[0];
154 $attrs = array();
155 if (isset($args[1])) {
156 $attrs = (array) $args[1];
159 $item = $this->createData($content, $attrs);
161 if ('offsetSet' == $action) {
162 $this->offsetSet($index, $item);
163 } else {
164 $this->$action($item);
167 return $this;
170 return parent::__call($method, $args);
174 * Determine if a value is a valid style tag
176 * @param mixed $value
177 * @param string $method
178 * @return boolean
180 protected function _isValid($value)
182 if ((!$value instanceof stdClass)
183 || !isset($value->content)
184 || !isset($value->attributes))
186 return false;
189 return true;
193 * Override append to enforce style creation
195 * @param mixed $value
196 * @return void
198 public function append($value)
200 if (!$this->_isValid($value)) {
201 require_once 'Zend/View/Exception.php';
202 throw new Zend_View_Exception('Invalid value passed to append; please use appendStyle()');
205 return $this->getContainer()->append($value);
209 * Override offsetSet to enforce style creation
211 * @param string|int $index
212 * @param mixed $value
213 * @return void
215 public function offsetSet($index, $value)
217 if (!$this->_isValid($value)) {
218 require_once 'Zend/View/Exception.php';
219 throw new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetStyle()');
222 return $this->getContainer()->offsetSet($index, $value);
226 * Override prepend to enforce style creation
228 * @param mixed $value
229 * @return void
231 public function prepend($value)
233 if (!$this->_isValid($value)) {
234 require_once 'Zend/View/Exception.php';
235 throw new Zend_View_Exception('Invalid value passed to prepend; please use prependStyle()');
238 return $this->getContainer()->prepend($value);
242 * Override set to enforce style creation
244 * @param mixed $value
245 * @return void
247 public function set($value)
249 if (!$this->_isValid($value)) {
250 require_once 'Zend/View/Exception.php';
251 throw new Zend_View_Exception('Invalid value passed to set; please use setStyle()');
254 return $this->getContainer()->set($value);
258 * Start capture action
260 * @param mixed $captureType
261 * @param string $typeOrAttrs
262 * @return void
264 public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $attrs = null)
266 if ($this->_captureLock) {
267 require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
268 throw new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest headStyle captures');
271 $this->_captureLock = true;
272 $this->_captureAttrs = $attrs;
273 $this->_captureType = $type;
274 ob_start();
278 * End capture action and store
280 * @return void
282 public function captureEnd()
284 $content = ob_get_clean();
285 $attrs = $this->_captureAttrs;
286 $this->_captureAttrs = null;
287 $this->_captureLock = false;
289 switch ($this->_captureType) {
290 case Zend_View_Helper_Placeholder_Container_Abstract::SET:
291 $this->setStyle($content, $attrs);
292 break;
293 case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
294 $this->prependStyle($content, $attrs);
295 break;
296 case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
297 default:
298 $this->appendStyle($content, $attrs);
299 break;
304 * Convert content and attributes into valid style tag
306 * @param stdClass $item Item to render
307 * @param string $indent Indentation to use
308 * @return string
310 public function itemToString(stdClass $item, $indent)
312 $attrString = '';
313 if (!empty($item->attributes)) {
314 foreach ($item->attributes as $key => $value) {
315 if (!in_array($key, $this->_optionalAttributes)) {
316 continue;
318 if ('media' == $key) {
319 if(false === strpos($value, ',')) {
320 if (!in_array($value, $this->_mediaTypes)) {
321 continue;
323 } else {
324 $media_types = explode(',', $value);
325 $value = '';
326 foreach($media_types as $type) {
327 if (!in_array($type, $this->_mediaTypes)) {
328 continue;
330 $value .= $type .',';
332 $value = substr($value, 0, -1);
335 $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value));
339 $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
340 . $indent . '<!--' . PHP_EOL . $indent . $item->content . PHP_EOL . $indent . '-->' . PHP_EOL
341 . '</style>';
343 if (isset($item->attributes['conditional'])
344 && !empty($item->attributes['conditional'])
345 && is_string($item->attributes['conditional']))
347 $html = '<!--[if ' . $item->attributes['conditional'] . ']> ' . $html . '<![endif]-->';
350 return $html;
354 * Create string representation of placeholder
356 * @param string|int $indent
357 * @return string
359 public function toString($indent = null)
361 $indent = (null !== $indent)
362 ? $this->getWhitespace($indent)
363 : $this->getIndent();
365 $items = array();
366 $this->getContainer()->ksort();
367 foreach ($this as $item) {
368 if (!$this->_isValid($item)) {
369 continue;
371 $items[] = $this->itemToString($item, $indent);
374 $return = $indent . implode($this->getSeparator() . $indent, $items);
375 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
376 return $return;
380 * Create data item for use in stack
382 * @param string $content
383 * @param array $attributes
384 * @return stdClass
386 public function createData($content, array $attributes)
388 if (!isset($attributes['media'])) {
389 $attributes['media'] = 'screen';
390 } else if(is_array($attributes['media'])) {
391 $attributes['media'] = implode(',', $attributes['media']);
394 $data = new stdClass();
395 $data->content = $content;
396 $data->attributes = $attributes;
398 return $data;