PageRenderTime 109ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/View/Helper/HeadStyle.php

https://bitbucket.org/ksekar/campus
PHP | 426 lines | 234 code | 43 blank | 149 comment | 42 complexity | a37bc0b281778d682081270d8e0a875d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: HeadStyle.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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. $e = new Zend_View_Exception(sprintf('Method "%s" requires minimally content for the stylesheet', $method));
  138. $e->setView($this->view);
  139. throw $e;
  140. }
  141. $content = $args[0];
  142. $attrs = array();
  143. if (isset($args[1])) {
  144. $attrs = (array) $args[1];
  145. }
  146. $item = $this->createData($content, $attrs);
  147. if ('offsetSet' == $action) {
  148. $this->offsetSet($index, $item);
  149. } else {
  150. $this->$action($item);
  151. }
  152. return $this;
  153. }
  154. return parent::__call($method, $args);
  155. }
  156. /**
  157. * Determine if a value is a valid style tag
  158. *
  159. * @param mixed $value
  160. * @param string $method
  161. * @return boolean
  162. */
  163. protected function _isValid($value)
  164. {
  165. if ((!$value instanceof stdClass)
  166. || !isset($value->content)
  167. || !isset($value->attributes))
  168. {
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * Override append to enforce style creation
  175. *
  176. * @param mixed $value
  177. * @return void
  178. */
  179. public function append($value)
  180. {
  181. if (!$this->_isValid($value)) {
  182. require_once 'Zend/View/Exception.php';
  183. $e = new Zend_View_Exception('Invalid value passed to append; please use appendStyle()');
  184. $e->setView($this->view);
  185. throw $e;
  186. }
  187. return $this->getContainer()->append($value);
  188. }
  189. /**
  190. * Override offsetSet to enforce style creation
  191. *
  192. * @param string|int $index
  193. * @param mixed $value
  194. * @return void
  195. */
  196. public function offsetSet($index, $value)
  197. {
  198. if (!$this->_isValid($value)) {
  199. require_once 'Zend/View/Exception.php';
  200. $e = new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetStyle()');
  201. $e->setView($this->view);
  202. throw $e;
  203. }
  204. return $this->getContainer()->offsetSet($index, $value);
  205. }
  206. /**
  207. * Override prepend to enforce style creation
  208. *
  209. * @param mixed $value
  210. * @return void
  211. */
  212. public function prepend($value)
  213. {
  214. if (!$this->_isValid($value)) {
  215. require_once 'Zend/View/Exception.php';
  216. $e = new Zend_View_Exception('Invalid value passed to prepend; please use prependStyle()');
  217. $e->setView($this->view);
  218. throw $e;
  219. }
  220. return $this->getContainer()->prepend($value);
  221. }
  222. /**
  223. * Override set to enforce style creation
  224. *
  225. * @param mixed $value
  226. * @return void
  227. */
  228. public function set($value)
  229. {
  230. if (!$this->_isValid($value)) {
  231. require_once 'Zend/View/Exception.php';
  232. $e = new Zend_View_Exception('Invalid value passed to set; please use setStyle()');
  233. $e->setView($this->view);
  234. throw $e;
  235. }
  236. return $this->getContainer()->set($value);
  237. }
  238. /**
  239. * Start capture action
  240. *
  241. * @param mixed $captureType
  242. * @param string $typeOrAttrs
  243. * @return void
  244. */
  245. public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $attrs = null)
  246. {
  247. if ($this->_captureLock) {
  248. require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
  249. $e = new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest headStyle captures');
  250. $e->setView($this->view);
  251. throw $e;
  252. }
  253. $this->_captureLock = true;
  254. $this->_captureAttrs = $attrs;
  255. $this->_captureType = $type;
  256. ob_start();
  257. }
  258. /**
  259. * End capture action and store
  260. *
  261. * @return void
  262. */
  263. public function captureEnd()
  264. {
  265. $content = ob_get_clean();
  266. $attrs = $this->_captureAttrs;
  267. $this->_captureAttrs = null;
  268. $this->_captureLock = false;
  269. switch ($this->_captureType) {
  270. case Zend_View_Helper_Placeholder_Container_Abstract::SET:
  271. $this->setStyle($content, $attrs);
  272. break;
  273. case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
  274. $this->prependStyle($content, $attrs);
  275. break;
  276. case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
  277. default:
  278. $this->appendStyle($content, $attrs);
  279. break;
  280. }
  281. }
  282. /**
  283. * Convert content and attributes into valid style tag
  284. *
  285. * @param stdClass $item Item to render
  286. * @param string $indent Indentation to use
  287. * @return string
  288. */
  289. public function itemToString(stdClass $item, $indent)
  290. {
  291. $attrString = '';
  292. if (!empty($item->attributes)) {
  293. $enc = 'UTF-8';
  294. if ($this->view instanceof Zend_View_Interface
  295. && method_exists($this->view, 'getEncoding')
  296. ) {
  297. $enc = $this->view->getEncoding();
  298. }
  299. foreach ($item->attributes as $key => $value) {
  300. if (!in_array($key, $this->_optionalAttributes)) {
  301. continue;
  302. }
  303. if ('media' == $key) {
  304. if(false === strpos($value, ',')) {
  305. if (!in_array($value, $this->_mediaTypes)) {
  306. continue;
  307. }
  308. } else {
  309. $media_types = explode(',', $value);
  310. $value = '';
  311. foreach($media_types as $type) {
  312. $type = trim($type);
  313. if (!in_array($type, $this->_mediaTypes)) {
  314. continue;
  315. }
  316. $value .= $type .',';
  317. }
  318. $value = substr($value, 0, -1);
  319. }
  320. }
  321. $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value, ENT_COMPAT, $enc));
  322. }
  323. }
  324. $escapeStart = $indent . '<!--'. PHP_EOL;
  325. $escapeEnd = $indent . '-->'. PHP_EOL;
  326. if (isset($item->attributes['conditional'])
  327. && !empty($item->attributes['conditional'])
  328. && is_string($item->attributes['conditional'])
  329. ) {
  330. $escapeStart = null;
  331. $escapeEnd = null;
  332. }
  333. $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
  334. . $escapeStart . $indent . $item->content . PHP_EOL . $escapeEnd
  335. . '</style>';
  336. if (null == $escapeStart && null == $escapeEnd) {
  337. $html = '<!--[if ' . $item->attributes['conditional'] . ']> ' . $html . '<![endif]-->';
  338. }
  339. return $html;
  340. }
  341. /**
  342. * Create string representation of placeholder
  343. *
  344. * @param string|int $indent
  345. * @return string
  346. */
  347. public function toString($indent = null)
  348. {
  349. $indent = (null !== $indent)
  350. ? $this->getWhitespace($indent)
  351. : $this->getIndent();
  352. $items = array();
  353. $this->getContainer()->ksort();
  354. foreach ($this as $item) {
  355. if (!$this->_isValid($item)) {
  356. continue;
  357. }
  358. $items[] = $this->itemToString($item, $indent);
  359. }
  360. $return = $indent . implode($this->getSeparator() . $indent, $items);
  361. $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
  362. return $return;
  363. }
  364. /**
  365. * Create data item for use in stack
  366. *
  367. * @param string $content
  368. * @param array $attributes
  369. * @return stdClass
  370. */
  371. public function createData($content, array $attributes)
  372. {
  373. if (!isset($attributes['media'])) {
  374. $attributes['media'] = 'screen';
  375. } else if(is_array($attributes['media'])) {
  376. $attributes['media'] = implode(',', $attributes['media']);
  377. }
  378. $data = new stdClass();
  379. $data->content = $content;
  380. $data->attributes = $attributes;
  381. return $data;
  382. }
  383. }