PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Dojo/View/Helper/Dojo/Container.php

https://bitbucket.org/ksekar/campus
PHP | 1205 lines | 592 code | 128 blank | 485 comment | 50 complexity | e4ecf52f8411c2569f83fe48d1ec0cba 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_Dojo
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Container.php 24594 2012-01-05 21:27:01Z matthew $
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Dojo */
  23. require_once 'Zend/Dojo.php';
  24. /**
  25. * Container for Dojo View Helper
  26. *
  27. *
  28. * @package Zend_Dojo
  29. * @subpackage View
  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_Dojo_View_Helper_Dojo_Container
  34. {
  35. /**
  36. * @var Zend_View_Interface
  37. */
  38. public $view;
  39. /**
  40. * addOnLoad capture lock
  41. * @var bool
  42. */
  43. protected $_captureLock = false;
  44. /**
  45. * addOnLoad object on which to apply lambda
  46. * @var string
  47. */
  48. protected $_captureObj;
  49. /**
  50. * Base CDN url to utilize
  51. * @var string
  52. */
  53. protected $_cdnBase = Zend_Dojo::CDN_BASE_GOOGLE;
  54. /**
  55. * Path segment following version string of CDN path
  56. * @var string
  57. */
  58. protected $_cdnDojoPath = Zend_Dojo::CDN_DOJO_PATH_GOOGLE;
  59. /**
  60. * Dojo version to use from CDN
  61. * @var string
  62. */
  63. protected $_cdnVersion = '1.5.0';
  64. /**
  65. * Has the dijit loader been registered?
  66. * @var bool
  67. */
  68. protected $_dijitLoaderRegistered = false;
  69. /**
  70. * Registered programmatic dijits
  71. * @var array
  72. */
  73. protected $_dijits = array();
  74. /**
  75. * Dojo configuration
  76. * @var array
  77. */
  78. protected $_djConfig = array();
  79. /**
  80. * Whether or not dojo is enabled
  81. * @var bool
  82. */
  83. protected $_enabled = false;
  84. /**
  85. * Are we rendering as XHTML?
  86. * @var bool
  87. */
  88. protected $_isXhtml = false;
  89. /**
  90. * Arbitrary javascript to include in dojo script
  91. * @var array
  92. */
  93. protected $_javascriptStatements = array();
  94. /**
  95. * Dojo layers (custom builds) to use
  96. * @var array
  97. */
  98. protected $_layers = array();
  99. /**
  100. * Relative path to dojo
  101. * @var string
  102. */
  103. protected $_localPath = null;
  104. /**
  105. * Root of dojo where all dojo files are installed
  106. * @var string
  107. */
  108. protected $_localRelativePath = null;
  109. /**
  110. * Modules to require
  111. * @var array
  112. */
  113. protected $_modules = array();
  114. /**
  115. * Registered module paths
  116. * @var array
  117. */
  118. protected $_modulePaths = array();
  119. /**
  120. * Actions to perform on window load
  121. * @var array
  122. */
  123. protected $_onLoadActions = array();
  124. /**
  125. * Register the Dojo stylesheet?
  126. * @var bool
  127. */
  128. protected $_registerDojoStylesheet = false;
  129. /**
  130. * Style sheet modules to load
  131. * @var array
  132. */
  133. protected $_stylesheetModules = array();
  134. /**
  135. * Local stylesheets
  136. * @var array
  137. */
  138. protected $_stylesheets = array();
  139. /**
  140. * Array of onLoad events specific to Zend_Dojo integration operations
  141. * @var array
  142. */
  143. protected $_zendLoadActions = array();
  144. /**
  145. * Set view object
  146. *
  147. * @param Zend_Dojo_View_Interface $view
  148. * @return void
  149. */
  150. public function setView(Zend_View_Interface $view)
  151. {
  152. $this->view = $view;
  153. }
  154. /**
  155. * Enable dojo
  156. *
  157. * @return Zend_Dojo_View_Helper_Dojo_Container
  158. */
  159. public function enable()
  160. {
  161. $this->_enabled = true;
  162. return $this;
  163. }
  164. /**
  165. * Disable dojo
  166. *
  167. * @return Zend_Dojo_View_Helper_Dojo_Container
  168. */
  169. public function disable()
  170. {
  171. $this->_enabled = false;
  172. return $this;
  173. }
  174. /**
  175. * Is dojo enabled?
  176. *
  177. * @return bool
  178. */
  179. public function isEnabled()
  180. {
  181. return $this->_enabled;
  182. }
  183. /**
  184. * Add options for the Dojo Container to use
  185. *
  186. * @param array|Zend_Config Array or Zend_Config object with options to use
  187. * @return Zend_Dojo_View_Helper_Dojo_Container
  188. */
  189. public function setOptions($options)
  190. {
  191. if($options instanceof Zend_Config) {
  192. $options = $options->toArray();
  193. }
  194. foreach($options as $key => $value) {
  195. $key = strtolower($key);
  196. switch($key) {
  197. case 'requiremodules':
  198. $this->requireModule($value);
  199. break;
  200. case 'modulepaths':
  201. foreach($value as $module => $path) {
  202. $this->registerModulePath($module, $path);
  203. }
  204. break;
  205. case 'layers':
  206. $value = (array) $value;
  207. foreach($value as $layer) {
  208. $this->addLayer($layer);
  209. }
  210. break;
  211. case 'cdnbase':
  212. $this->setCdnBase($value);
  213. break;
  214. case 'cdnversion':
  215. $this->setCdnVersion($value);
  216. break;
  217. case 'cdndojopath':
  218. $this->setCdnDojoPath($value);
  219. break;
  220. case 'localpath':
  221. $this->setLocalPath($value);
  222. break;
  223. case 'djconfig':
  224. $this->setDjConfig($value);
  225. break;
  226. case 'stylesheetmodules':
  227. $value = (array) $value;
  228. foreach($value as $module) {
  229. $this->addStylesheetModule($module);
  230. }
  231. break;
  232. case 'stylesheets':
  233. $value = (array) $value;
  234. foreach($value as $stylesheet) {
  235. $this->addStylesheet($stylesheet);
  236. }
  237. break;
  238. case 'registerdojostylesheet':
  239. $this->registerDojoStylesheet($value);
  240. break;
  241. case 'enable':
  242. if($value) {
  243. $this->enable();
  244. } else {
  245. $this->disable();
  246. }
  247. }
  248. }
  249. return $this;
  250. }
  251. /**
  252. * Specify one or multiple modules to require
  253. *
  254. * @param string|array $modules
  255. * @return Zend_Dojo_View_Helper_Dojo_Container
  256. */
  257. public function requireModule($modules)
  258. {
  259. if (!is_string($modules) && !is_array($modules)) {
  260. require_once 'Zend/Dojo/View/Exception.php';
  261. throw new Zend_Dojo_View_Exception('Invalid module name specified; must be a string or an array of strings');
  262. }
  263. $modules = (array) $modules;
  264. foreach ($modules as $mod) {
  265. if (!preg_match('/^[a-z][a-z0-9._-]+$/i', $mod)) {
  266. require_once 'Zend/Dojo/View/Exception.php';
  267. throw new Zend_Dojo_View_Exception(sprintf('Module name specified, "%s", contains invalid characters', (string) $mod));
  268. }
  269. if (!in_array($mod, $this->_modules)) {
  270. $this->_modules[] = $mod;
  271. }
  272. }
  273. return $this;
  274. }
  275. /**
  276. * Retrieve list of modules to require
  277. *
  278. * @return array
  279. */
  280. public function getModules()
  281. {
  282. return $this->_modules;
  283. }
  284. /**
  285. * Register a module path
  286. *
  287. * @param string $module The module to register a path for
  288. * @param string $path The path to register for the module
  289. * @return Zend_Dojo_View_Helper_Dojo_Container
  290. */
  291. public function registerModulePath($module, $path)
  292. {
  293. $path = (string) $path;
  294. if (!in_array($module, $this->_modulePaths)) {
  295. $this->_modulePaths[$module] = $path;
  296. }
  297. return $this;
  298. }
  299. /**
  300. * List registered module paths
  301. *
  302. * @return array
  303. */
  304. public function getModulePaths()
  305. {
  306. return $this->_modulePaths;
  307. }
  308. /**
  309. * Add layer (custom build) path
  310. *
  311. * @param string $path
  312. * @return Zend_Dojo_View_Helper_Dojo_Container
  313. */
  314. public function addLayer($path)
  315. {
  316. $path = (string) $path;
  317. if (!in_array($path, $this->_layers)) {
  318. $this->_layers[] = $path;
  319. }
  320. return $this;
  321. }
  322. /**
  323. * Get registered layers
  324. *
  325. * @return array
  326. */
  327. public function getLayers()
  328. {
  329. return $this->_layers;
  330. }
  331. /**
  332. * Remove a registered layer
  333. *
  334. * @param string $path
  335. * @return Zend_Dojo_View_Helper_Dojo_Container
  336. */
  337. public function removeLayer($path)
  338. {
  339. $path = (string) $path;
  340. $layers = array_flip($this->_layers);
  341. if (array_key_exists($path, $layers)) {
  342. unset($layers[$path]);
  343. $this->_layers = array_keys($layers);
  344. }
  345. return $this;
  346. }
  347. /**
  348. * Clear all registered layers
  349. *
  350. * @return Zend_Dojo_View_Helper_Dojo_Container
  351. */
  352. public function clearLayers()
  353. {
  354. $this->_layers = array();
  355. return $this;
  356. }
  357. /**
  358. * Set CDN base path
  359. *
  360. * @param string $url
  361. * @return Zend_Dojo_View_Helper_Dojo_Container
  362. */
  363. public function setCdnBase($url)
  364. {
  365. $this->_cdnBase = (string) $url;
  366. return $this;
  367. }
  368. /**
  369. * Return CDN base URL
  370. *
  371. * @return string
  372. */
  373. public function getCdnBase()
  374. {
  375. return $this->_cdnBase;
  376. }
  377. /**
  378. * Use CDN, using version specified
  379. *
  380. * @param string $version
  381. * @return Zend_Dojo_View_Helper_Dojo_Container
  382. */
  383. public function setCdnVersion($version = null)
  384. {
  385. $this->enable();
  386. if (preg_match('/^[1-9]\.[0-9](\.[0-9])?$/', $version)) {
  387. $this->_cdnVersion = $version;
  388. }
  389. return $this;
  390. }
  391. /**
  392. * Get CDN version
  393. *
  394. * @return string
  395. */
  396. public function getCdnVersion()
  397. {
  398. return $this->_cdnVersion;
  399. }
  400. /**
  401. * Set CDN path to dojo (relative to CDN base + version)
  402. *
  403. * @param string $path
  404. * @return Zend_Dojo_View_Helper_Dojo_Container
  405. */
  406. public function setCdnDojoPath($path)
  407. {
  408. $this->_cdnDojoPath = (string) $path;
  409. return $this;
  410. }
  411. /**
  412. * Get CDN path to dojo (relative to CDN base + version)
  413. *
  414. * @return string
  415. */
  416. public function getCdnDojoPath()
  417. {
  418. return $this->_cdnDojoPath;
  419. }
  420. /**
  421. * Are we using the CDN?
  422. *
  423. * @return bool
  424. */
  425. public function useCdn()
  426. {
  427. return !$this->useLocalPath();
  428. }
  429. /**
  430. * Set path to local dojo
  431. *
  432. * @param string $path
  433. * @return Zend_Dojo_View_Helper_Dojo_Container
  434. */
  435. public function setLocalPath($path)
  436. {
  437. $this->enable();
  438. $this->_localPath = (string) $path;
  439. return $this;
  440. }
  441. /**
  442. * Get local path to dojo
  443. *
  444. * @return string
  445. */
  446. public function getLocalPath()
  447. {
  448. return $this->_localPath;
  449. }
  450. /**
  451. * Are we using a local path?
  452. *
  453. * @return bool
  454. */
  455. public function useLocalPath()
  456. {
  457. return (null === $this->_localPath) ? false : true;
  458. }
  459. /**
  460. * Set Dojo configuration
  461. *
  462. * @param string $option
  463. * @param mixed $value
  464. * @return Zend_Dojo_View_Helper_Dojo_Container
  465. */
  466. public function setDjConfig(array $config)
  467. {
  468. $this->_djConfig = $config;
  469. return $this;
  470. }
  471. /**
  472. * Set Dojo configuration option
  473. *
  474. * @param string $option
  475. * @param mixed $value
  476. * @return Zend_Dojo_View_Helper_Dojo_Container
  477. */
  478. public function setDjConfigOption($option, $value)
  479. {
  480. $option = (string) $option;
  481. $this->_djConfig[$option] = $value;
  482. return $this;
  483. }
  484. /**
  485. * Retrieve dojo configuration values
  486. *
  487. * @return array
  488. */
  489. public function getDjConfig()
  490. {
  491. return $this->_djConfig;
  492. }
  493. /**
  494. * Get dojo configuration value
  495. *
  496. * @param string $option
  497. * @param mixed $default
  498. * @return mixed
  499. */
  500. public function getDjConfigOption($option, $default = null)
  501. {
  502. $option = (string) $option;
  503. if (array_key_exists($option, $this->_djConfig)) {
  504. return $this->_djConfig[$option];
  505. }
  506. return $default;
  507. }
  508. /**
  509. * Add a stylesheet by module name
  510. *
  511. * @param string $module
  512. * @return Zend_Dojo_View_Helper_Dojo_Container
  513. */
  514. public function addStylesheetModule($module)
  515. {
  516. if (!preg_match('/^[a-z0-9]+\.[a-z0-9_-]+(\.[a-z0-9_-]+)*$/i', $module)) {
  517. require_once 'Zend/Dojo/View/Exception.php';
  518. throw new Zend_Dojo_View_Exception('Invalid stylesheet module specified');
  519. }
  520. if (!in_array($module, $this->_stylesheetModules)) {
  521. $this->_stylesheetModules[] = $module;
  522. }
  523. return $this;
  524. }
  525. /**
  526. * Get all stylesheet modules currently registered
  527. *
  528. * @return array
  529. */
  530. public function getStylesheetModules()
  531. {
  532. return $this->_stylesheetModules;
  533. }
  534. /**
  535. * Add a stylesheet
  536. *
  537. * @param string $path
  538. * @return Zend_Dojo_View_Helper_Dojo_Container
  539. */
  540. public function addStylesheet($path)
  541. {
  542. $path = (string) $path;
  543. if (!in_array($path, $this->_stylesheets)) {
  544. $this->_stylesheets[] = (string) $path;
  545. }
  546. return $this;
  547. }
  548. /**
  549. * Register the dojo.css stylesheet?
  550. *
  551. * With no arguments, returns the status of the flag; with arguments, sets
  552. * the flag and returns the object.
  553. *
  554. * @param null|bool $flag
  555. * @return Zend_Dojo_View_Helper_Dojo_Container|bool
  556. */
  557. public function registerDojoStylesheet($flag = null)
  558. {
  559. if (null === $flag) {
  560. return $this->_registerDojoStylesheet;
  561. }
  562. $this->_registerDojoStylesheet = (bool) $flag;
  563. return $this;
  564. }
  565. /**
  566. * Retrieve registered stylesheets
  567. *
  568. * @return array
  569. */
  570. public function getStylesheets()
  571. {
  572. return $this->_stylesheets;
  573. }
  574. /**
  575. * Add a script to execute onLoad
  576. *
  577. * dojo.addOnLoad accepts:
  578. * - function name
  579. * - lambda
  580. *
  581. * @param string $callback Lambda
  582. * @return Zend_Dojo_View_Helper_Dojo_Container
  583. */
  584. public function addOnLoad($callback)
  585. {
  586. if (!in_array($callback, $this->_onLoadActions, true)) {
  587. $this->_onLoadActions[] = $callback;
  588. }
  589. return $this;
  590. }
  591. /**
  592. * Prepend an onLoad event to the list of onLoad actions
  593. *
  594. * @param string $callback Lambda
  595. * @return Zend_Dojo_View_Helper_Dojo_Container
  596. */
  597. public function prependOnLoad($callback)
  598. {
  599. if (!in_array($callback, $this->_onLoadActions, true)) {
  600. array_unshift($this->_onLoadActions, $callback);
  601. }
  602. return $this;
  603. }
  604. /**
  605. * Retrieve all registered onLoad actions
  606. *
  607. * @return array
  608. */
  609. public function getOnLoadActions()
  610. {
  611. return $this->_onLoadActions;
  612. }
  613. /**
  614. * Start capturing routines to run onLoad
  615. *
  616. * @return bool
  617. */
  618. public function onLoadCaptureStart()
  619. {
  620. if ($this->_captureLock) {
  621. require_once 'Zend/Dojo/View/Exception.php';
  622. throw new Zend_Dojo_View_Exception('Cannot nest onLoad captures');
  623. }
  624. $this->_captureLock = true;
  625. ob_start();
  626. return;
  627. }
  628. /**
  629. * Stop capturing routines to run onLoad
  630. *
  631. * @return bool
  632. */
  633. public function onLoadCaptureEnd()
  634. {
  635. $data = ob_get_clean();
  636. $this->_captureLock = false;
  637. $this->addOnLoad($data);
  638. return true;
  639. }
  640. /**
  641. * Add a programmatic dijit
  642. *
  643. * @param string $id
  644. * @param array $params
  645. * @return Zend_Dojo_View_Helper_Dojo_Container
  646. */
  647. public function addDijit($id, array $params)
  648. {
  649. if (array_key_exists($id, $this->_dijits)) {
  650. require_once 'Zend/Dojo/View/Exception.php';
  651. throw new Zend_Dojo_View_Exception(sprintf('Duplicate dijit with id "%s" already registered', $id));
  652. }
  653. $this->_dijits[$id] = array(
  654. 'id' => $id,
  655. 'params' => $params,
  656. );
  657. return $this;
  658. }
  659. /**
  660. * Set a programmatic dijit (overwrites)
  661. *
  662. * @param string $id
  663. * @param array $params
  664. * @return Zend_Dojo_View_Helper_Dojo_Container
  665. */
  666. public function setDijit($id, array $params)
  667. {
  668. $this->removeDijit($id);
  669. return $this->addDijit($id, $params);
  670. }
  671. /**
  672. * Add multiple dijits at once
  673. *
  674. * Expects an array of id => array $params pairs
  675. *
  676. * @param array $dijits
  677. * @return Zend_Dojo_View_Helper_Dojo_Container
  678. */
  679. public function addDijits(array $dijits)
  680. {
  681. foreach ($dijits as $id => $params) {
  682. $this->addDijit($id, $params);
  683. }
  684. return $this;
  685. }
  686. /**
  687. * Set multiple dijits at once (overwrites)
  688. *
  689. * Expects an array of id => array $params pairs
  690. *
  691. * @param array $dijits
  692. * @return Zend_Dojo_View_Helper_Dojo_Container
  693. */
  694. public function setDijits(array $dijits)
  695. {
  696. $this->clearDijits();
  697. return $this->addDijits($dijits);
  698. }
  699. /**
  700. * Is the given programmatic dijit already registered?
  701. *
  702. * @param string $id
  703. * @return bool
  704. */
  705. public function hasDijit($id)
  706. {
  707. return array_key_exists($id, $this->_dijits);
  708. }
  709. /**
  710. * Retrieve a dijit by id
  711. *
  712. * @param string $id
  713. * @return array|null
  714. */
  715. public function getDijit($id)
  716. {
  717. if ($this->hasDijit($id)) {
  718. return $this->_dijits[$id]['params'];
  719. }
  720. return null;
  721. }
  722. /**
  723. * Retrieve all dijits
  724. *
  725. * Returns dijits as an array of assoc arrays
  726. *
  727. * @return array
  728. */
  729. public function getDijits()
  730. {
  731. return array_values($this->_dijits);
  732. }
  733. /**
  734. * Remove a programmatic dijit if it exists
  735. *
  736. * @param string $id
  737. * @return Zend_Dojo_View_Helper_Dojo_Container
  738. */
  739. public function removeDijit($id)
  740. {
  741. if (array_key_exists($id, $this->_dijits)) {
  742. unset($this->_dijits[$id]);
  743. }
  744. return $this;
  745. }
  746. /**
  747. * Clear all dijits
  748. *
  749. * @return Zend_Dojo_View_Helper_Dojo_Container
  750. */
  751. public function clearDijits()
  752. {
  753. $this->_dijits = array();
  754. return $this;
  755. }
  756. /**
  757. * Render dijits as JSON structure
  758. *
  759. * @return string
  760. */
  761. public function dijitsToJson()
  762. {
  763. require_once 'Zend/Json.php';
  764. return Zend_Json::encode($this->getDijits(), false, array('enableJsonExprFinder' => true));
  765. }
  766. /**
  767. * Create dijit loader functionality
  768. *
  769. * @return void
  770. */
  771. public function registerDijitLoader()
  772. {
  773. if (!$this->_dijitLoaderRegistered) {
  774. $js =<<<EOJ
  775. function() {
  776. dojo.forEach(zendDijits, function(info) {
  777. var n = dojo.byId(info.id);
  778. if (null != n) {
  779. dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
  780. }
  781. });
  782. dojo.parser.parse();
  783. }
  784. EOJ;
  785. $this->requireModule('dojo.parser');
  786. $this->_addZendLoad($js);
  787. $this->addJavascript('var zendDijits = ' . $this->dijitsToJson() . ';');
  788. $this->_dijitLoaderRegistered = true;
  789. }
  790. }
  791. /**
  792. * Add arbitrary javascript to execute in dojo JS container
  793. *
  794. * @param string $js
  795. * @return Zend_Dojo_View_Helper_Dojo_Container
  796. */
  797. public function addJavascript($js)
  798. {
  799. $js = trim($js);
  800. if (!in_array(substr($js, -1), array(';', '}'))) {
  801. $js .= ';';
  802. }
  803. if (in_array($js, $this->_javascriptStatements)) {
  804. return $this;
  805. }
  806. $this->_javascriptStatements[] = $js;
  807. return $this;
  808. }
  809. /**
  810. * Return all registered javascript statements
  811. *
  812. * @return array
  813. */
  814. public function getJavascript()
  815. {
  816. return $this->_javascriptStatements;
  817. }
  818. /**
  819. * Clear arbitrary javascript stack
  820. *
  821. * @return Zend_Dojo_View_Helper_Dojo_Container
  822. */
  823. public function clearJavascript()
  824. {
  825. $this->_javascriptStatements = array();
  826. return $this;
  827. }
  828. /**
  829. * Capture arbitrary javascript to include in dojo script
  830. *
  831. * @return void
  832. */
  833. public function javascriptCaptureStart()
  834. {
  835. if ($this->_captureLock) {
  836. require_once 'Zend/Dojo/View/Exception.php';
  837. throw new Zend_Dojo_View_Exception('Cannot nest captures');
  838. }
  839. $this->_captureLock = true;
  840. ob_start();
  841. return;
  842. }
  843. /**
  844. * Finish capturing arbitrary javascript to include in dojo script
  845. *
  846. * @return true
  847. */
  848. public function javascriptCaptureEnd()
  849. {
  850. $data = ob_get_clean();
  851. $this->_captureLock = false;
  852. $this->addJavascript($data);
  853. return true;
  854. }
  855. /**
  856. * String representation of dojo environment
  857. *
  858. * @return string
  859. */
  860. public function __toString()
  861. {
  862. if (!$this->isEnabled()) {
  863. return '';
  864. }
  865. $this->_isXhtml = $this->view->doctype()->isXhtml();
  866. if (Zend_Dojo_View_Helper_Dojo::useDeclarative()) {
  867. if (null === $this->getDjConfigOption('parseOnLoad')) {
  868. $this->setDjConfigOption('parseOnLoad', true);
  869. }
  870. }
  871. if (!empty($this->_dijits)) {
  872. $this->registerDijitLoader();
  873. }
  874. $html = $this->_renderStylesheets() . PHP_EOL
  875. . $this->_renderDjConfig() . PHP_EOL
  876. . $this->_renderDojoScriptTag() . PHP_EOL
  877. . $this->_renderLayers() . PHP_EOL
  878. . $this->_renderExtras();
  879. return $html;
  880. }
  881. /**
  882. * Retrieve local path to dojo resources for building relative paths
  883. *
  884. * @return string
  885. */
  886. protected function _getLocalRelativePath()
  887. {
  888. if (null === $this->_localRelativePath) {
  889. $localPath = $this->getLocalPath();
  890. $localPath = preg_replace('|[/\\\\]dojo[/\\\\]dojo.js[^/\\\\]*$|i', '', $localPath);
  891. $this->_localRelativePath = $localPath;
  892. }
  893. return $this->_localRelativePath;
  894. }
  895. /**
  896. * Render dojo stylesheets
  897. *
  898. * @return string
  899. */
  900. protected function _renderStylesheets()
  901. {
  902. if ($this->useCdn()) {
  903. $base = $this->getCdnBase()
  904. . $this->getCdnVersion();
  905. } else {
  906. $base = $this->_getLocalRelativePath();
  907. }
  908. $registeredStylesheets = $this->getStylesheetModules();
  909. foreach ($registeredStylesheets as $stylesheet) {
  910. $themeName = substr($stylesheet, strrpos($stylesheet, '.') + 1);
  911. $stylesheet = str_replace('.', '/', $stylesheet);
  912. $stylesheets[] = $base . '/' . $stylesheet . '/' . $themeName . '.css';
  913. }
  914. foreach ($this->getStylesheets() as $stylesheet) {
  915. $stylesheets[] = $stylesheet;
  916. }
  917. if ($this->_registerDojoStylesheet) {
  918. $stylesheets[] = $base . '/dojo/resources/dojo.css';
  919. }
  920. if (empty($stylesheets)) {
  921. return '';
  922. }
  923. array_reverse($stylesheets);
  924. $style = '<style type="text/css">' . PHP_EOL
  925. . (($this->_isXhtml) ? '<!--' : '<!--') . PHP_EOL;
  926. foreach ($stylesheets as $stylesheet) {
  927. $style .= ' @import "' . $stylesheet . '";' . PHP_EOL;
  928. }
  929. $style .= (($this->_isXhtml) ? '-->' : '-->') . PHP_EOL
  930. . '</style>';
  931. return $style;
  932. }
  933. /**
  934. * Render DjConfig values
  935. *
  936. * @return string
  937. */
  938. protected function _renderDjConfig()
  939. {
  940. $djConfigValues = $this->getDjConfig();
  941. if (empty($djConfigValues)) {
  942. return '';
  943. }
  944. require_once 'Zend/Json.php';
  945. $scriptTag = '<script type="text/javascript">' . PHP_EOL
  946. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  947. . ' var djConfig = ' . Zend_Json::encode($djConfigValues) . ';' . PHP_EOL
  948. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  949. . '</script>';
  950. return $scriptTag;
  951. }
  952. /**
  953. * Render dojo script tag
  954. *
  955. * Renders Dojo script tag by utilizing either local path provided or the
  956. * CDN. If any djConfig values were set, they will be serialized and passed
  957. * with that attribute.
  958. *
  959. * @return string
  960. */
  961. protected function _renderDojoScriptTag()
  962. {
  963. if ($this->useCdn()) {
  964. $source = $this->getCdnBase()
  965. . $this->getCdnVersion()
  966. . $this->getCdnDojoPath();
  967. } else {
  968. $source = $this->getLocalPath();
  969. }
  970. $scriptTag = '<script type="text/javascript" src="' . $source . '"></script>';
  971. return $scriptTag;
  972. }
  973. /**
  974. * Render layers (custom builds) as script tags
  975. *
  976. * @return string
  977. */
  978. protected function _renderLayers()
  979. {
  980. $layers = $this->getLayers();
  981. if (empty($layers)) {
  982. return '';
  983. }
  984. $enc = 'UTF-8';
  985. if ($this->view instanceof Zend_View_Interface
  986. && method_exists($this->view, 'getEncoding')
  987. ) {
  988. $enc = $this->view->getEncoding();
  989. }
  990. $html = array();
  991. foreach ($layers as $path) {
  992. $html[] = sprintf(
  993. '<script type="text/javascript" src="%s"></script>',
  994. htmlspecialchars($path, ENT_QUOTES, $enc)
  995. );
  996. }
  997. return implode("\n", $html);
  998. }
  999. /**
  1000. * Render dojo module paths and requires
  1001. *
  1002. * @return string
  1003. */
  1004. protected function _renderExtras()
  1005. {
  1006. $js = array();
  1007. $modulePaths = $this->getModulePaths();
  1008. if (!empty($modulePaths)) {
  1009. foreach ($modulePaths as $module => $path) {
  1010. $js[] = 'dojo.registerModulePath("' . $this->view->escape($module) . '", "' . $this->view->escape($path) . '");';
  1011. }
  1012. }
  1013. $modules = $this->getModules();
  1014. if (!empty($modules)) {
  1015. foreach ($modules as $module) {
  1016. $js[] = 'dojo.require("' . $this->view->escape($module) . '");';
  1017. }
  1018. }
  1019. $onLoadActions = array();
  1020. // Get Zend specific onLoad actions; these will always be first to
  1021. // ensure that dijits are created in the correct order
  1022. foreach ($this->_getZendLoadActions() as $callback) {
  1023. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  1024. }
  1025. // Get all other onLoad actions
  1026. foreach ($this->getOnLoadActions() as $callback) {
  1027. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  1028. }
  1029. $javascript = implode("\n ", $this->getJavascript());
  1030. $content = '';
  1031. if (!empty($js)) {
  1032. $content .= implode("\n ", $js) . "\n";
  1033. }
  1034. if (!empty($onLoadActions)) {
  1035. $content .= implode("\n ", $onLoadActions) . "\n";
  1036. }
  1037. if (!empty($javascript)) {
  1038. $content .= $javascript . "\n";
  1039. }
  1040. if (preg_match('/^\s*$/s', $content)) {
  1041. return '';
  1042. }
  1043. $html = '<script type="text/javascript">' . PHP_EOL
  1044. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  1045. . $content
  1046. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  1047. . PHP_EOL . '</script>';
  1048. return $html;
  1049. }
  1050. /**
  1051. * Add an onLoad action related to ZF dijit creation
  1052. *
  1053. * This method is public, but prefixed with an underscore to indicate that
  1054. * it should not normally be called by userland code. It is pertinent to
  1055. * ensuring that the correct order of operations occurs during dijit
  1056. * creation.
  1057. *
  1058. * @param string $callback
  1059. * @return Zend_Dojo_View_Helper_Dojo_Container
  1060. */
  1061. public function _addZendLoad($callback)
  1062. {
  1063. if (!in_array($callback, $this->_zendLoadActions, true)) {
  1064. $this->_zendLoadActions[] = $callback;
  1065. }
  1066. return $this;
  1067. }
  1068. /**
  1069. * Retrieve all ZF dijit callbacks
  1070. *
  1071. * @return array
  1072. */
  1073. public function _getZendLoadActions()
  1074. {
  1075. return $this->_zendLoadActions;
  1076. }
  1077. }