HeadStyle.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  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$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_View_Helper_Placeholder_Container_Standalone */
  23. require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
  24. /**
  25. * Helper for setting and retrieving stylesheets
  26. *
  27. * @uses Zend_View_Helper_Placeholder_Container_Standalone
  28. * @package Zend_View
  29. * @subpackage Helper
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_View_Helper_HeadStyle extends Zend_View_Helper_Placeholder_Container_Standalone
  34. {
  35. /**
  36. * Registry key for placeholder
  37. * @var string
  38. */
  39. protected $_regKey = 'Zend_View_Helper_HeadStyle';
  40. /**
  41. * Allowed optional attributes
  42. * @var array
  43. */
  44. protected $_optionalAttributes = array('lang', 'title', 'media', 'dir');
  45. /**
  46. * Allowed media types
  47. * @var array
  48. */
  49. protected $_mediaTypes = array(
  50. 'all', 'aural', 'braille', 'handheld', 'print',
  51. 'projection', 'screen', 'tty', 'tv'
  52. );
  53. /**
  54. * Capture type and/or attributes (used for hinting during capture)
  55. * @var string
  56. */
  57. protected $_captureAttrs = null;
  58. /**
  59. * Capture lock
  60. * @var bool
  61. */
  62. protected $_captureLock;
  63. /**
  64. * Capture type (append, prepend, set)
  65. * @var string
  66. */
  67. protected $_captureType;
  68. /**
  69. * Constructor
  70. *
  71. * Set separator to PHP_EOL.
  72. *
  73. * @return void
  74. */
  75. public function __construct()
  76. {
  77. parent::__construct();
  78. $this->setSeparator(PHP_EOL);
  79. }
  80. /**
  81. * Return headStyle object
  82. *
  83. * Returns headStyle helper object; optionally, allows specifying
  84. *
  85. * @param string $content Stylesheet contents
  86. * @param string $placement Append, prepend, or set
  87. * @param string|array $attributes Optional attributes to utilize
  88. * @return Zend_View_Helper_HeadStyle
  89. */
  90. public function headStyle($content = null, $placement = 'APPEND', $attributes = array())
  91. {
  92. if ((null !== $content) && is_string($content)) {
  93. switch (strtoupper($placement)) {
  94. case 'SET':
  95. $action = 'setStyle';
  96. break;
  97. case 'PREPEND':
  98. $action = 'prependStyle';
  99. break;
  100. case 'APPEND':
  101. default:
  102. $action = 'appendStyle';
  103. break;
  104. }
  105. $this->$action($content, $attributes);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Overload method calls
  111. *
  112. * Allows the following method calls:
  113. * - appendStyle($content, $attributes = array())
  114. * - offsetSetStyle($index, $content, $attributes = array())
  115. * - prependStyle($content, $attributes = array())
  116. * - setStyle($content, $attributes = array())
  117. *
  118. * @param string $method
  119. * @param array $args
  120. * @return void
  121. * @throws Zend_View_Exception When no $content provided or invalid method
  122. */
  123. public function __call($method, $args)
  124. {
  125. if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(Style)$/', $method, $matches)) {
  126. $index = null;
  127. $argc = count($args);
  128. $action = $matches['action'];
  129. if ('offsetSet' == $action) {
  130. if (0 < $argc) {
  131. $index = array_shift($args);
  132. --$argc;
  133. }
  134. }
  135. if (1 > $argc) {
  136. require_once 'Zend/View/Exception.php';
  137. throw new Zend_View_Exception(sprintf('Method "%s" requires minimally content for the stylesheet', $method));
  138. }
  139. $content = $args[0];
  140. $attrs = array();
  141. if (isset($args[1])) {
  142. $attrs = (array) $args[1];
  143. }
  144. $item = $this->createData($content, $attrs);
  145. if ('offsetSet' == $action) {
  146. $this->offsetSet($index, $item);
  147. } else {
  148. $this->$action($item);
  149. }
  150. return $this;
  151. }
  152. return parent::__call($method, $args);
  153. }
  154. /**
  155. * Determine if a value is a valid style tag
  156. *
  157. * @param mixed $value
  158. * @param string $method
  159. * @return boolean
  160. */
  161. protected function _isValid($value)
  162. {
  163. if ((!$value instanceof stdClass)
  164. || !isset($value->content)
  165. || !isset($value->attributes))
  166. {
  167. return false;
  168. }
  169. return true;
  170. }
  171. /**
  172. * Override append to enforce style creation
  173. *
  174. * @param mixed $value
  175. * @return void
  176. */
  177. public function append($value)
  178. {
  179. if (!$this->_isValid($value)) {
  180. require_once 'Zend/View/Exception.php';
  181. throw new Zend_View_Exception('Invalid value passed to append; please use appendStyle()');
  182. }
  183. return $this->getContainer()->append($value);
  184. }
  185. /**
  186. * Override offsetSet to enforce style creation
  187. *
  188. * @param string|int $index
  189. * @param mixed $value
  190. * @return void
  191. */
  192. public function offsetSet($index, $value)
  193. {
  194. if (!$this->_isValid($value)) {
  195. require_once 'Zend/View/Exception.php';
  196. throw new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetStyle()');
  197. }
  198. return $this->getContainer()->offsetSet($index, $value);
  199. }
  200. /**
  201. * Override prepend to enforce style creation
  202. *
  203. * @param mixed $value
  204. * @return void
  205. */
  206. public function prepend($value)
  207. {
  208. if (!$this->_isValid($value)) {
  209. require_once 'Zend/View/Exception.php';
  210. throw new Zend_View_Exception('Invalid value passed to prepend; please use prependStyle()');
  211. }
  212. return $this->getContainer()->prepend($value);
  213. }
  214. /**
  215. * Override set to enforce style creation
  216. *
  217. * @param mixed $value
  218. * @return void
  219. */
  220. public function set($value)
  221. {
  222. if (!$this->_isValid($value)) {
  223. require_once 'Zend/View/Exception.php';
  224. throw new Zend_View_Exception('Invalid value passed to set; please use setStyle()');
  225. }
  226. return $this->getContainer()->set($value);
  227. }
  228. /**
  229. * Start capture action
  230. *
  231. * @param mixed $captureType
  232. * @param string $typeOrAttrs
  233. * @return void
  234. */
  235. public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $attrs = null)
  236. {
  237. if ($this->_captureLock) {
  238. require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
  239. throw new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest headStyle captures');
  240. }
  241. $this->_captureLock = true;
  242. $this->_captureAttrs = $attrs;
  243. $this->_captureType = $type;
  244. ob_start();
  245. }
  246. /**
  247. * End capture action and store
  248. *
  249. * @return void
  250. */
  251. public function captureEnd()
  252. {
  253. $content = ob_get_clean();
  254. $attrs = $this->_captureAttrs;
  255. $this->_captureAttrs = null;
  256. $this->_captureLock = false;
  257. switch ($this->_captureType) {
  258. case Zend_View_Helper_Placeholder_Container_Abstract::SET:
  259. $this->setStyle($content, $attrs);
  260. break;
  261. case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
  262. $this->prependStyle($content, $attrs);
  263. break;
  264. case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
  265. default:
  266. $this->appendStyle($content, $attrs);
  267. break;
  268. }
  269. }
  270. /**
  271. * Convert content and attributes into valid style tag
  272. *
  273. * @param stdClass $item Item to render
  274. * @param string $indent Indentation to use
  275. * @return string
  276. */
  277. public function itemToString(stdClass $item, $indent)
  278. {
  279. $attrString = '';
  280. if (!empty($item->attributes)) {
  281. foreach ($item->attributes as $key => $value) {
  282. if (!in_array($key, $this->_optionalAttributes)) {
  283. continue;
  284. }
  285. if ('media' == $key) {
  286. if(false === strpos($value, ',')) {
  287. if (!in_array($value, $this->_mediaTypes)) {
  288. continue;
  289. }
  290. } else {
  291. $media_types = explode(',', $value);
  292. $value = '';
  293. foreach($media_types as $type) {
  294. if (!in_array($type, $this->_mediaTypes)) {
  295. continue;
  296. }
  297. $value .= $type .',';
  298. }
  299. $value = substr($value, 0, -1);
  300. }
  301. }
  302. $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value));
  303. }
  304. }
  305. $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
  306. . $indent . '<!--' . PHP_EOL . $indent . $item->content . PHP_EOL . $indent . '-->' . PHP_EOL
  307. . '</style>';
  308. if (isset($item->attributes['conditional'])
  309. && !empty($item->attributes['conditional'])
  310. && is_string($item->attributes['conditional']))
  311. {
  312. $html = '<!--[if ' . $item->attributes['conditional'] . ']> ' . $html . '<![endif]-->';
  313. }
  314. return $html;
  315. }
  316. /**
  317. * Create string representation of placeholder
  318. *
  319. * @param string|int $indent
  320. * @return string
  321. */
  322. public function toString($indent = null)
  323. {
  324. $indent = (null !== $indent)
  325. ? $this->getWhitespace($indent)
  326. : $this->getIndent();
  327. $items = array();
  328. $this->getContainer()->ksort();
  329. foreach ($this as $item) {
  330. if (!$this->_isValid($item)) {
  331. continue;
  332. }
  333. $items[] = $this->itemToString($item, $indent);
  334. }
  335. $return = $indent . implode($this->getSeparator() . $indent, $items);
  336. $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
  337. return $return;
  338. }
  339. /**
  340. * Create data item for use in stack
  341. *
  342. * @param string $content
  343. * @param array $attributes
  344. * @return stdClass
  345. */
  346. public function createData($content, array $attributes)
  347. {
  348. if (!isset($attributes['media'])) {
  349. $attributes['media'] = 'screen';
  350. } else if(is_array($attributes['media'])) {
  351. $attributes['media'] = implode(',', $attributes['media']);
  352. }
  353. $data = new stdClass();
  354. $data->content = $content;
  355. $data->attributes = $attributes;
  356. return $data;
  357. }
  358. }