HeadStyle.php 12 KB

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