Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / View / Helper / HeadScript.php
blobf2bea24a3f3e6dd1dc15b813758716054bd443e1
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\View\Helper;
12 use stdClass;
13 use Zend\View;
14 use Zend\View\Exception;
16 /**
17 * Helper for setting and retrieving script elements for HTML head section
19 * Allows the following method calls:
20 * @method HeadScript appendFile($src, $type = 'text/javascript', $attrs = array())
21 * @method HeadScript offsetSetFile($index, $src, $type = 'text/javascript', $attrs = array())
22 * @method HeadScript prependFile($src, $type = 'text/javascript', $attrs = array())
23 * @method HeadScript setFile($src, $type = 'text/javascript', $attrs = array())
24 * @method HeadScript appendScript($script, $type = 'text/javascript', $attrs = array())
25 * @method HeadScript offsetSetScript($index, $src, $type = 'text/javascript', $attrs = array())
26 * @method HeadScript prependScript($script, $type = 'text/javascript', $attrs = array())
27 * @method HeadScript setScript($script, $type = 'text/javascript', $attrs = array())
29 class HeadScript extends Placeholder\Container\AbstractStandalone
31 /**
32 * Script type constants
34 * @const string
36 const FILE = 'FILE';
37 const SCRIPT = 'SCRIPT';
39 /**
40 * Registry key for placeholder
42 * @var string
44 protected $regKey = 'Zend_View_Helper_HeadScript';
46 /**
47 * Are arbitrary attributes allowed?
49 * @var bool
51 protected $arbitraryAttributes = false;
53 /**
54 * Is capture lock?
56 * @var bool
58 protected $captureLock;
60 /**
61 * Capture type
63 * @var string
65 protected $captureScriptType;
67 /**
68 * Capture attributes
70 * @var null|array
72 protected $captureScriptAttrs = null;
74 /**
75 * Capture type (append, prepend, set)
77 * @var string
79 protected $captureType;
81 /**
82 * Optional allowed attributes for script tag
84 * @var array
86 protected $optionalAttributes = array('charset', 'defer', 'language', 'src');
88 /**
89 * Required attributes for script tag
91 * @var string
93 protected $requiredAttributes = array('type');
95 /**
96 * Whether or not to format scripts using CDATA; used only if doctype
97 * helper is not accessible
99 * @var bool
101 public $useCdata = false;
104 * Constructor
106 * Set separator to PHP_EOL.
108 public function __construct()
110 parent::__construct();
112 $this->setSeparator(PHP_EOL);
116 * Return headScript object
118 * Returns headScript helper object; optionally, allows specifying a script
119 * or script file to include.
121 * @param string $mode Script or file
122 * @param string $spec Script/url
123 * @param string $placement Append, prepend, or set
124 * @param array $attrs Array of script attributes
125 * @param string $type Script type and/or array of script attributes
126 * @return HeadScript
128 public function __invoke($mode = self::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
130 if ((null !== $spec) && is_string($spec)) {
131 $action = ucfirst(strtolower($mode));
132 $placement = strtolower($placement);
133 switch ($placement) {
134 case 'set':
135 case 'prepend':
136 case 'append':
137 $action = $placement . $action;
138 break;
139 default:
140 $action = 'append' . $action;
141 break;
143 $this->$action($spec, $type, $attrs);
146 return $this;
150 * Overload method access
152 * @param string $method Method to call
153 * @param array $args Arguments of method
154 * @throws Exception\BadMethodCallException if too few arguments or invalid method
155 * @return HeadScript
157 public function __call($method, $args)
159 if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(?P<mode>File|Script)$/', $method, $matches)) {
160 if (1 > count($args)) {
161 throw new Exception\BadMethodCallException(sprintf(
162 'Method "%s" requires at least one argument',
163 $method
167 $action = $matches['action'];
168 $mode = strtolower($matches['mode']);
169 $type = 'text/javascript';
170 $attrs = array();
172 if ('offsetSet' == $action) {
173 $index = array_shift($args);
174 if (1 > count($args)) {
175 throw new Exception\BadMethodCallException(sprintf(
176 'Method "%s" requires at least two arguments, an index and source',
177 $method
182 $content = $args[0];
184 if (isset($args[1])) {
185 $type = (string) $args[1];
187 if (isset($args[2])) {
188 $attrs = (array) $args[2];
191 switch ($mode) {
192 case 'script':
193 $item = $this->createData($type, $attrs, $content);
194 if ('offsetSet' == $action) {
195 $this->offsetSet($index, $item);
196 } else {
197 $this->$action($item);
199 break;
200 case 'file':
201 default:
202 if (!$this->isDuplicate($content)) {
203 $attrs['src'] = $content;
204 $item = $this->createData($type, $attrs);
205 if ('offsetSet' == $action) {
206 $this->offsetSet($index, $item);
207 } else {
208 $this->$action($item);
211 break;
214 return $this;
217 return parent::__call($method, $args);
221 * Retrieve string representation
223 * @param string|int $indent Amount of whitespaces or string to use for indention
224 * @return string
226 public function toString($indent = null)
228 $indent = (null !== $indent)
229 ? $this->getWhitespace($indent)
230 : $this->getIndent();
232 if ($this->view) {
233 $useCdata = $this->view->plugin('doctype')->isXhtml() ? true : false;
234 } else {
235 $useCdata = $this->useCdata ? true : false;
238 $escapeStart = ($useCdata) ? '//<![CDATA[' : '//<!--';
239 $escapeEnd = ($useCdata) ? '//]]>' : '//-->';
241 $items = array();
242 $this->getContainer()->ksort();
243 foreach ($this as $item) {
244 if (!$this->isValid($item)) {
245 continue;
248 $items[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
251 return implode($this->getSeparator(), $items);
255 * Start capture action
257 * @param mixed $captureType Type of capture
258 * @param string $type Type of script
259 * @param array $attrs Attributes of capture
260 * @throws Exception\RuntimeException
261 * @return void
263 public function captureStart($captureType = Placeholder\Container\AbstractContainer::APPEND, $type = 'text/javascript', $attrs = array())
265 if ($this->captureLock) {
266 throw new Exception\RuntimeException('Cannot nest headScript captures');
269 $this->captureLock = true;
270 $this->captureType = $captureType;
271 $this->captureScriptType = $type;
272 $this->captureScriptAttrs = $attrs;
273 ob_start();
277 * End capture action and store
279 * @return void
281 public function captureEnd()
283 $content = ob_get_clean();
284 $type = $this->captureScriptType;
285 $attrs = $this->captureScriptAttrs;
286 $this->captureScriptType = null;
287 $this->captureScriptAttrs = null;
288 $this->captureLock = false;
290 switch ($this->captureType) {
291 case Placeholder\Container\AbstractContainer::SET:
292 case Placeholder\Container\AbstractContainer::PREPEND:
293 case Placeholder\Container\AbstractContainer::APPEND:
294 $action = strtolower($this->captureType) . 'Script';
295 break;
296 default:
297 $action = 'appendScript';
298 break;
301 $this->$action($content, $type, $attrs);
305 * Create data item containing all necessary components of script
307 * @param string $type Type of data
308 * @param array $attributes Attributes of data
309 * @param string $content Content of data
310 * @return stdClass
312 public function createData($type, array $attributes, $content = null)
314 $data = new stdClass();
315 $data->type = $type;
316 $data->attributes = $attributes;
317 $data->source = $content;
319 return $data;
323 * Is the file specified a duplicate?
325 * @param string $file Name of file to check
326 * @return bool
328 protected function isDuplicate($file)
330 foreach ($this->getContainer() as $item) {
331 if (($item->source === null)
332 && array_key_exists('src', $item->attributes)
333 && ($file == $item->attributes['src']))
335 return true;
339 return false;
343 * Is the script provided valid?
345 * @param mixed $value Is the given script valid?
346 * @return bool
348 protected function isValid($value)
350 if ((!$value instanceof stdClass)
351 || !isset($value->type)
352 || (!isset($value->source) && !isset($value->attributes)))
354 return false;
357 return true;
361 * Create script HTML
363 * @param mixed $item Item to convert
364 * @param string $indent String to add before the item
365 * @param string $escapeStart Starting sequence
366 * @param string $escapeEnd Ending sequence
367 * @return string
369 public function itemToString($item, $indent, $escapeStart, $escapeEnd)
371 $attrString = '';
372 if (!empty($item->attributes)) {
373 foreach ($item->attributes as $key => $value) {
374 if ((!$this->arbitraryAttributesAllowed() && !in_array($key, $this->optionalAttributes))
375 || in_array($key, array('conditional', 'noescape')))
377 continue;
379 if ('defer' == $key) {
380 $value = 'defer';
382 $attrString .= sprintf(' %s="%s"', $key, ($this->autoEscape) ? $this->escape($value) : $value);
386 $addScriptEscape = !(isset($item->attributes['noescape']) && filter_var($item->attributes['noescape'], FILTER_VALIDATE_BOOLEAN));
388 $type = ($this->autoEscape) ? $this->escape($item->type) : $item->type;
389 $html = '<script type="' . $type . '"' . $attrString . '>';
390 if (!empty($item->source)) {
391 $html .= PHP_EOL;
393 if ($addScriptEscape) {
394 $html .= $indent . ' ' . $escapeStart . PHP_EOL;
397 $html .= $indent . ' ' . $item->source;
399 if ($addScriptEscape) {
400 $html .= PHP_EOL . $indent . ' ' . $escapeEnd;
403 $html .= PHP_EOL . $indent;
405 $html .= '</script>';
407 if (isset($item->attributes['conditional'])
408 && !empty($item->attributes['conditional'])
409 && is_string($item->attributes['conditional']))
411 $html = $indent . '<!--[if ' . $item->attributes['conditional'] . ']>' . $html . '<![endif]-->';
412 } else {
413 $html = $indent . $html;
416 return $html;
420 * Override append
422 * @param string $value Append script or file
423 * @throws Exception\InvalidArgumentException
424 * @return void
426 public function append($value)
428 if (!$this->isValid($value)) {
429 throw new Exception\InvalidArgumentException(
430 'Invalid argument passed to append(); please use one of the helper methods, appendScript() or appendFile()'
434 return $this->getContainer()->append($value);
438 * Override prepend
440 * @param string $value Prepend script or file
441 * @throws Exception\InvalidArgumentException
442 * @return void
444 public function prepend($value)
446 if (!$this->isValid($value)) {
447 throw new Exception\InvalidArgumentException(
448 'Invalid argument passed to prepend(); please use one of the helper methods, prependScript() or prependFile()'
452 return $this->getContainer()->prepend($value);
456 * Override set
458 * @param string $value Set script or file
459 * @throws Exception\InvalidArgumentException
460 * @return void
462 public function set($value)
464 if (!$this->isValid($value)) {
465 throw new Exception\InvalidArgumentException(
466 'Invalid argument passed to set(); please use one of the helper methods, setScript() or setFile()'
470 return $this->getContainer()->set($value);
474 * Override offsetSet
476 * @param string|int $index Set script of file offset
477 * @param mixed $value
478 * @throws Exception\InvalidArgumentException
479 * @return void
481 public function offsetSet($index, $value)
483 if (!$this->isValid($value)) {
484 throw new Exception\InvalidArgumentException(
485 'Invalid argument passed to offsetSet(); please use one of the helper methods, offsetSetScript() or offsetSetFile()'
489 return $this->getContainer()->offsetSet($index, $value);
493 * Set flag indicating if arbitrary attributes are allowed
495 * @param bool $flag Set flag
496 * @return HeadScript
498 public function setAllowArbitraryAttributes($flag)
500 $this->arbitraryAttributes = (bool) $flag;
501 return $this;
505 * Are arbitrary attributes allowed?
507 * @return bool
509 public function arbitraryAttributesAllowed()
511 return $this->arbitraryAttributes;