Container.php 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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-2008 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_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) 2008 - Present, Zend Technologies, Inc.
  31. * @license New BSD {@link http://framework.zend.com/license/new-bsd}
  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.2.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. * Set view object
  141. *
  142. * @param Zend_Dojo_View_Interface $view
  143. * @return void
  144. */
  145. public function setView(Zend_View_Interface $view)
  146. {
  147. $this->view = $view;
  148. }
  149. /**
  150. * Enable dojo
  151. *
  152. * @return Zend_Dojo_View_Helper_Dojo_Container
  153. */
  154. public function enable()
  155. {
  156. $this->_enabled = true;
  157. return $this;
  158. }
  159. /**
  160. * Disable dojo
  161. *
  162. * @return Zend_Dojo_View_Helper_Dojo_Container
  163. */
  164. public function disable()
  165. {
  166. $this->_enabled = false;
  167. return $this;
  168. }
  169. /**
  170. * Is dojo enabled?
  171. *
  172. * @return bool
  173. */
  174. public function isEnabled()
  175. {
  176. return $this->_enabled;
  177. }
  178. /**
  179. * Specify a module to require
  180. *
  181. * @param string $module
  182. * @return Zend_Dojo_View_Helper_Dojo_Container
  183. */
  184. public function requireModule($module)
  185. {
  186. if (!is_string($module) && !is_array($module)) {
  187. require_once 'Zend/Dojo/View/Exception.php';
  188. throw new Zend_Dojo_View_Exception('Invalid module name specified; must be a string or an array of strings');
  189. }
  190. $module = (array) $module;
  191. foreach ($module as $mod) {
  192. if (!preg_match('/^[a-z][a-z0-9._-]+$/i', $mod)) {
  193. require_once 'Zend/Dojo/View/Exception.php';
  194. throw new Zend_Dojo_View_Exception(sprintf('Module name specified, "%s", contains invalid characters', (string) $mod));
  195. }
  196. if (!in_array($mod, $this->_modules)) {
  197. $this->_modules[] = $mod;
  198. }
  199. }
  200. return $this;
  201. }
  202. /**
  203. * Retrieve list of modules to require
  204. *
  205. * @return array
  206. */
  207. public function getModules()
  208. {
  209. return $this->_modules;
  210. }
  211. /**
  212. * Register a module path
  213. *
  214. * @param string $path
  215. * @return Zend_Dojo_View_Helper_Dojo_Container
  216. */
  217. public function registerModulePath($module, $path)
  218. {
  219. $path = (string) $path;
  220. if (!in_array($module, $this->_modulePaths)) {
  221. $this->_modulePaths[$module] = $path;
  222. }
  223. return $this;
  224. }
  225. /**
  226. * List registered module paths
  227. *
  228. * @return array
  229. */
  230. public function getModulePaths()
  231. {
  232. return $this->_modulePaths;
  233. }
  234. /**
  235. * Add layer (custom build) path
  236. *
  237. * @param string $path
  238. * @return Zend_Dojo_View_Helper_Dojo_Container
  239. */
  240. public function addLayer($path)
  241. {
  242. $path = (string) $path;
  243. if (!in_array($path, $this->_layers)) {
  244. $this->_layers[] = $path;
  245. }
  246. return $this;
  247. }
  248. /**
  249. * Get registered layers
  250. *
  251. * @return array
  252. */
  253. public function getLayers()
  254. {
  255. return $this->_layers;
  256. }
  257. /**
  258. * Remove a registered layer
  259. *
  260. * @param string $path
  261. * @return Zend_Dojo_View_Helper_Dojo_Container
  262. */
  263. public function removeLayer($path)
  264. {
  265. $path = (string) $path;
  266. $layers = array_flip($this->_layers);
  267. if (array_key_exists($path, $layers)) {
  268. unset($layers[$path]);
  269. $this->_layers = array_keys($layers);
  270. }
  271. return $this;
  272. }
  273. /**
  274. * Clear all registered layers
  275. *
  276. * @return Zend_Dojo_View_Helper_Dojo_Container
  277. */
  278. public function clearLayers()
  279. {
  280. $this->_layers = array();
  281. return $this;
  282. }
  283. /**
  284. * Set CDN base path
  285. *
  286. * @param string $url
  287. * @return Zend_Dojo_View_Helper_Dojo_Container
  288. */
  289. public function setCdnBase($url)
  290. {
  291. $this->_cdnBase = (string) $url;
  292. return $this;
  293. }
  294. /**
  295. * Return CDN base URL
  296. *
  297. * @return string
  298. */
  299. public function getCdnBase()
  300. {
  301. return $this->_cdnBase;
  302. }
  303. /**
  304. * Use CDN, using version specified
  305. *
  306. * @param string $version
  307. * @return Zend_Dojo_View_Helper_Dojo_Container
  308. */
  309. public function setCdnVersion($version = null)
  310. {
  311. $this->enable();
  312. if (preg_match('/^[1-9]\.[0-9](\.[0-9])?$/', $version)) {
  313. $this->_cdnVersion = $version;
  314. }
  315. return $this;
  316. }
  317. /**
  318. * Get CDN version
  319. *
  320. * @return string
  321. */
  322. public function getCdnVersion()
  323. {
  324. return $this->_cdnVersion;
  325. }
  326. /**
  327. * Set CDN path to dojo (relative to CDN base + version)
  328. *
  329. * @param string $path
  330. * @return Zend_Dojo_View_Helper_Dojo_Container
  331. */
  332. public function setCdnDojoPath($path)
  333. {
  334. $this->_cdnDojoPath = (string) $path;
  335. return $this;
  336. }
  337. /**
  338. * Get CDN path to dojo (relative to CDN base + version)
  339. *
  340. * @return string
  341. */
  342. public function getCdnDojoPath()
  343. {
  344. return $this->_cdnDojoPath;
  345. }
  346. /**
  347. * Are we using the CDN?
  348. *
  349. * @return bool
  350. */
  351. public function useCdn()
  352. {
  353. return !$this->useLocalPath();
  354. }
  355. /**
  356. * Set path to local dojo
  357. *
  358. * @param string $path
  359. * @return Zend_Dojo_View_Helper_Dojo_Container
  360. */
  361. public function setLocalPath($path)
  362. {
  363. $this->enable();
  364. $this->_localPath = (string) $path;
  365. return $this;
  366. }
  367. /**
  368. * Get local path to dojo
  369. *
  370. * @return string
  371. */
  372. public function getLocalPath()
  373. {
  374. return $this->_localPath;
  375. }
  376. /**
  377. * Are we using a local path?
  378. *
  379. * @return bool
  380. */
  381. public function useLocalPath()
  382. {
  383. return (null === $this->_localPath) ? false : true;
  384. }
  385. /**
  386. * Set Dojo configuration
  387. *
  388. * @param string $option
  389. * @param mixed $value
  390. * @return Zend_Dojo_View_Helper_Dojo_Container
  391. */
  392. public function setDjConfig(array $config)
  393. {
  394. $this->_djConfig = $config;
  395. return $this;
  396. }
  397. /**
  398. * Set Dojo configuration option
  399. *
  400. * @param string $option
  401. * @param mixed $value
  402. * @return Zend_Dojo_View_Helper_Dojo_Container
  403. */
  404. public function setDjConfigOption($option, $value)
  405. {
  406. $option = (string) $option;
  407. $this->_djConfig[$option] = $value;
  408. return $this;
  409. }
  410. /**
  411. * Retrieve dojo configuration values
  412. *
  413. * @return array
  414. */
  415. public function getDjConfig()
  416. {
  417. return $this->_djConfig;
  418. }
  419. /**
  420. * Get dojo configuration value
  421. *
  422. * @param string $option
  423. * @param mixed $default
  424. * @return mixed
  425. */
  426. public function getDjConfigOption($option, $default = null)
  427. {
  428. $option = (string) $option;
  429. if (array_key_exists($option, $this->_djConfig)) {
  430. return $this->_djConfig[$option];
  431. }
  432. return $default;
  433. }
  434. /**
  435. * Add a stylesheet by module name
  436. *
  437. * @param string $module
  438. * @return Zend_Dojo_View_Helper_Dojo_Container
  439. */
  440. public function addStylesheetModule($module)
  441. {
  442. if (!preg_match('/^[a-z0-9]+\.[a-z0-9_-]+(\.[a-z0-9_-]+)*$/i', $module)) {
  443. require_once 'Zend/Dojo/View/Exception.php';
  444. throw new Zend_Dojo_View_Exception('Invalid stylesheet module specified');
  445. }
  446. if (in_array($module, $this->_stylesheetModules)) {
  447. return $this;
  448. }
  449. $this->_stylesheetModules[] = $module;
  450. return $this;
  451. }
  452. /**
  453. * Get all stylesheet modules currently registered
  454. *
  455. * @return array
  456. */
  457. public function getStylesheetModules()
  458. {
  459. return $this->_stylesheetModules;
  460. }
  461. /**
  462. * Add a stylesheet
  463. *
  464. * @param string $path
  465. * @return Zend_Dojo_View_Helper_Dojo_Container
  466. */
  467. public function addStylesheet($path)
  468. {
  469. $path = (string) $path;
  470. if (!in_array($path, $this->_stylesheets)) {
  471. $this->_stylesheets[] = (string) $path;
  472. }
  473. return $this;
  474. }
  475. /**
  476. * Register the dojo.css stylesheet?
  477. *
  478. * With no arguments, returns the status of the flag; with arguments, sets
  479. * the flag and returns the object.
  480. *
  481. * @param null|bool $flag
  482. * @return Zend_Dojo_View_Helper_Dojo_Container|bool
  483. */
  484. public function registerDojoStylesheet($flag = null)
  485. {
  486. if (null === $flag) {
  487. return $this->_registerDojoStylesheet;
  488. }
  489. $this->_registerDojoStylesheet = (bool) $flag;
  490. return $this;
  491. }
  492. /**
  493. * Retrieve registered stylesheets
  494. *
  495. * @return array
  496. */
  497. public function getStylesheets()
  498. {
  499. return $this->_stylesheets;
  500. }
  501. /**
  502. * Add a script to execute onLoad
  503. *
  504. * dojo.addOnLoad accepts:
  505. * - function name
  506. * - lambda
  507. *
  508. * @param string $callback Lambda
  509. * @return Zend_Dojo_View_Helper_Dojo_Container
  510. */
  511. public function addOnLoad($callback)
  512. {
  513. if (!in_array($callback, $this->_onLoadActions, true)) {
  514. $this->_onLoadActions[] = $callback;
  515. }
  516. return $this;
  517. }
  518. /**
  519. * Prepend an onLoad event to the list of onLoad actions
  520. *
  521. * @param string $callback Lambda
  522. * @return Zend_Dojo_View_Helper_Dojo_Container
  523. */
  524. public function prependOnLoad($callback)
  525. {
  526. if (!in_array($callback, $this->_onLoadActions, true)) {
  527. array_unshift($this->_onLoadActions, $callback);
  528. }
  529. return $this;
  530. }
  531. /**
  532. * Retrieve all registered onLoad actions
  533. *
  534. * @return array
  535. */
  536. public function getOnLoadActions()
  537. {
  538. return $this->_onLoadActions;
  539. }
  540. /**
  541. * Start capturing routines to run onLoad
  542. *
  543. * @return bool
  544. */
  545. public function onLoadCaptureStart()
  546. {
  547. if ($this->_captureLock) {
  548. require_once 'Zend/Dojo/View/Exception.php';
  549. throw new Zend_Dojo_View_Exception('Cannot nest onLoad captures');
  550. }
  551. $this->_captureLock = true;
  552. ob_start();
  553. return;
  554. }
  555. /**
  556. * Stop capturing routines to run onLoad
  557. *
  558. * @return bool
  559. */
  560. public function onLoadCaptureEnd()
  561. {
  562. $data = ob_get_clean();
  563. $this->_captureLock = false;
  564. $this->addOnLoad($data);
  565. return true;
  566. }
  567. /**
  568. * Add a programmatic dijit
  569. *
  570. * @param string $id
  571. * @param array $params
  572. * @return Zend_Dojo_View_Helper_Dojo_Container
  573. */
  574. public function addDijit($id, array $params)
  575. {
  576. if (array_key_exists($id, $this->_dijits)) {
  577. require_once 'Zend/Dojo/View/Exception.php';
  578. throw new Zend_Dojo_View_Exception(sprintf('Duplicate dijit with id "%s" already registered', $id));
  579. }
  580. $this->_dijits[$id] = array(
  581. 'id' => $id,
  582. 'params' => $params,
  583. );
  584. return $this;
  585. }
  586. /**
  587. * Set a programmatic dijit (overwrites)
  588. *
  589. * @param string $id
  590. * @param array $params
  591. * @return Zend_Dojo_View_Helper_Dojo_Container
  592. */
  593. public function setDijit($id, array $params)
  594. {
  595. $this->removeDijit($id);
  596. return $this->addDijit($id, $params);
  597. }
  598. /**
  599. * Add multiple dijits at once
  600. *
  601. * Expects an array of id => array $params pairs
  602. *
  603. * @param array $dijits
  604. * @return Zend_Dojo_View_Helper_Dojo_Container
  605. */
  606. public function addDijits(array $dijits)
  607. {
  608. foreach ($dijits as $id => $params) {
  609. $this->addDijit($id, $params);
  610. }
  611. return $this;
  612. }
  613. /**
  614. * Set multiple dijits at once (overwrites)
  615. *
  616. * Expects an array of id => array $params pairs
  617. *
  618. * @param array $dijits
  619. * @return Zend_Dojo_View_Helper_Dojo_Container
  620. */
  621. public function setDijits(array $dijits)
  622. {
  623. $this->clearDijits();
  624. return $this->addDijits($dijits);
  625. }
  626. /**
  627. * Is the given programmatic dijit already registered?
  628. *
  629. * @param string $id
  630. * @return bool
  631. */
  632. public function hasDijit($id)
  633. {
  634. return array_key_exists($id, $this->_dijits);
  635. }
  636. /**
  637. * Retrieve a dijit by id
  638. *
  639. * @param string $id
  640. * @return array|null
  641. */
  642. public function getDijit($id)
  643. {
  644. if ($this->hasDijit($id)) {
  645. return $this->_dijits[$id]['params'];
  646. }
  647. return null;
  648. }
  649. /**
  650. * Retrieve all dijits
  651. *
  652. * Returns dijits as an array of assoc arrays
  653. *
  654. * @return array
  655. */
  656. public function getDijits()
  657. {
  658. return array_values($this->_dijits);
  659. }
  660. /**
  661. * Remove a programmatic dijit if it exists
  662. *
  663. * @param string $id
  664. * @return Zend_Dojo_View_Helper_Dojo_Container
  665. */
  666. public function removeDijit($id)
  667. {
  668. if (array_key_exists($id, $this->_dijits)) {
  669. unset($this->_dijits[$id]);
  670. }
  671. return $this;
  672. }
  673. /**
  674. * Clear all dijits
  675. *
  676. * @return Zend_Dojo_View_Helper_Dojo_Container
  677. */
  678. public function clearDijits()
  679. {
  680. $this->_dijits = array();
  681. return $this;
  682. }
  683. /**
  684. * Render dijits as JSON structure
  685. *
  686. * @return string
  687. */
  688. public function dijitsToJson()
  689. {
  690. require_once 'Zend/Json.php';
  691. return Zend_Json::encode($this->getDijits());
  692. }
  693. /**
  694. * Create dijit loader functionality
  695. *
  696. * @return void
  697. */
  698. public function registerDijitLoader()
  699. {
  700. if (!$this->_dijitLoaderRegistered) {
  701. $js =<<<EOJ
  702. function() {
  703. dojo.forEach(zendDijits, function(info) {
  704. var n = dojo.byId(info.id);
  705. if (null != n) {
  706. dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
  707. }
  708. });
  709. dojo.parser.parse();
  710. }
  711. EOJ;
  712. $this->requireModule('dojo.parser');
  713. $this->prependOnLoad($js);
  714. $this->addJavascript('var zendDijits = ' . $this->dijitsToJson() . ';');
  715. $this->_dijitLoaderRegistered = true;
  716. }
  717. }
  718. /**
  719. * Add arbitrary javascript to execute in dojo JS container
  720. *
  721. * @param string $js
  722. * @return Zend_Dojo_View_Helper_Dojo_Container
  723. */
  724. public function addJavascript($js)
  725. {
  726. $js = preg_replace('/^\s*(.*?)\s*$/s', '$1', $js);
  727. if (!in_array(substr($js, -1), array(';', '}'))) {
  728. $js .= ';';
  729. }
  730. if (in_array($js, $this->_javascriptStatements)) {
  731. return $this;
  732. }
  733. $this->_javascriptStatements[] = $js;
  734. return $this;
  735. }
  736. /**
  737. * Return all registered javascript statements
  738. *
  739. * @return array
  740. */
  741. public function getJavascript()
  742. {
  743. return $this->_javascriptStatements;
  744. }
  745. /**
  746. * Clear arbitrary javascript stack
  747. *
  748. * @return Zend_Dojo_View_Helper_Dojo_Container
  749. */
  750. public function clearJavascript()
  751. {
  752. $this->_javascriptStatements = array();
  753. return $this;
  754. }
  755. /**
  756. * Capture arbitrary javascript to include in dojo script
  757. *
  758. * @return void
  759. */
  760. public function javascriptCaptureStart()
  761. {
  762. if ($this->_captureLock) {
  763. require_once 'Zend/Dojo/View/Exception.php';
  764. throw new Zend_Dojo_View_Exception('Cannot nest captures');
  765. }
  766. $this->_captureLock = true;
  767. ob_start();
  768. return;
  769. }
  770. /**
  771. * Finish capturing arbitrary javascript to include in dojo script
  772. *
  773. * @return true
  774. */
  775. public function javascriptCaptureEnd()
  776. {
  777. $data = ob_get_clean();
  778. $this->_captureLock = false;
  779. $this->addJavascript($data);
  780. return true;
  781. }
  782. /**
  783. * String representation of dojo environment
  784. *
  785. * @return string
  786. */
  787. public function __toString()
  788. {
  789. if (!$this->isEnabled()) {
  790. return '';
  791. }
  792. $this->_isXhtml = $this->view->doctype()->isXhtml();
  793. if (Zend_Dojo_View_Helper_Dojo::useDeclarative()) {
  794. if (null === $this->getDjConfigOption('parseOnLoad')) {
  795. $this->setDjConfigOption('parseOnLoad', true);
  796. }
  797. }
  798. if (!empty($this->_dijits)) {
  799. $this->registerDijitLoader();
  800. }
  801. $html = $this->_renderStylesheets() . PHP_EOL
  802. . $this->_renderDjConfig() . PHP_EOL
  803. . $this->_renderDojoScriptTag() . PHP_EOL
  804. . $this->_renderLayers() . PHP_EOL
  805. . $this->_renderExtras();
  806. return $html;
  807. }
  808. /**
  809. * Retrieve local path to dojo resources for building relative paths
  810. *
  811. * @return string
  812. */
  813. protected function _getLocalRelativePath()
  814. {
  815. if (null === $this->_localRelativePath) {
  816. $localPath = $this->getLocalPath();
  817. $localPath = preg_replace('|[/\\\\]dojo[/\\\\]dojo.js[^/\\\\]*$|i', '', $localPath);
  818. $this->_localRelativePath = $localPath;
  819. }
  820. return $this->_localRelativePath;
  821. }
  822. /**
  823. * Render dojo stylesheets
  824. *
  825. * @return string
  826. */
  827. protected function _renderStylesheets()
  828. {
  829. if ($this->useCdn()) {
  830. $base = $this->getCdnBase()
  831. . $this->getCdnVersion();
  832. } else {
  833. $base = $this->_getLocalRelativePath();
  834. }
  835. $registeredStylesheets = $this->getStylesheetModules();
  836. foreach ($registeredStylesheets as $stylesheet) {
  837. $themeName = substr($stylesheet, strrpos($stylesheet, '.') + 1);
  838. $stylesheet = str_replace('.', '/', $stylesheet);
  839. $stylesheets[] = $base . '/' . $stylesheet . '/' . $themeName . '.css';
  840. }
  841. foreach ($this->getStylesheets() as $stylesheet) {
  842. $stylesheets[] = $stylesheet;
  843. }
  844. if ($this->_registerDojoStylesheet) {
  845. $stylesheets[] = $base . '/dojo/resources/dojo.css';
  846. }
  847. if (empty($stylesheets)) {
  848. return '';
  849. }
  850. array_reverse($stylesheets);
  851. $style = '<style type="text/css">' . PHP_EOL
  852. . (($this->_isXhtml) ? '<!--' : '<!--') . PHP_EOL;
  853. foreach ($stylesheets as $stylesheet) {
  854. $style .= ' @import "' . $stylesheet . '";' . PHP_EOL;
  855. }
  856. $style .= (($this->_isXhtml) ? '-->' : '-->') . PHP_EOL
  857. . '</style>';
  858. return $style;
  859. }
  860. /**
  861. * Render DjConfig values
  862. *
  863. * @return string
  864. */
  865. protected function _renderDjConfig()
  866. {
  867. $djConfigValues = $this->getDjConfig();
  868. if (empty($djConfigValues)) {
  869. return '';
  870. }
  871. require_once 'Zend/Json.php';
  872. $scriptTag = '<script type="text/javascript">' . PHP_EOL
  873. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  874. . ' var djConfig = ' . Zend_Json::encode($djConfigValues) . ';' . PHP_EOL
  875. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  876. . '</script>';
  877. return $scriptTag;
  878. }
  879. /**
  880. * Render dojo script tag
  881. *
  882. * Renders Dojo script tag by utilizing either local path provided or the
  883. * CDN. If any djConfig values were set, they will be serialized and passed
  884. * with that attribute.
  885. *
  886. * @return string
  887. */
  888. protected function _renderDojoScriptTag()
  889. {
  890. if ($this->useCdn()) {
  891. $source = $this->getCdnBase()
  892. . $this->getCdnVersion()
  893. . $this->getCdnDojoPath();
  894. } else {
  895. $source = $this->getLocalPath();
  896. }
  897. $scriptTag = '<script type="text/javascript" src="' . $source . '"></script>';
  898. return $scriptTag;
  899. }
  900. /**
  901. * Render layers (custom builds) as script tags
  902. *
  903. * @return string
  904. */
  905. protected function _renderLayers()
  906. {
  907. $layers = $this->getLayers();
  908. if (empty($layers)) {
  909. return '';
  910. }
  911. $html = array();
  912. foreach ($layers as $path) {
  913. $html[] = sprintf(
  914. '<script type="text/javascript" src="%s"></script>',
  915. htmlentities($path, ENT_QUOTES)
  916. );
  917. }
  918. return implode("\n", $html);
  919. }
  920. /**
  921. * Render dojo module paths and requires
  922. *
  923. * @return string
  924. */
  925. protected function _renderExtras()
  926. {
  927. $js = array();
  928. $modulePaths = $this->getModulePaths();
  929. if (!empty($modulePaths)) {
  930. foreach ($modulePaths as $module => $path) {
  931. $js[] = 'dojo.registerModulePath("' . $this->view->escape($module) . '", "' . $this->view->escape($path) . '");';
  932. }
  933. }
  934. $modules = $this->getModules();
  935. if (!empty($modules)) {
  936. foreach ($modules as $module) {
  937. $js[] = 'dojo.require("' . $this->view->escape($module) . '");';
  938. }
  939. }
  940. $onLoadActions = array();
  941. foreach ($this->getOnLoadActions() as $callback) {
  942. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  943. }
  944. $javascript = implode("\n ", $this->getJavascript());
  945. $content = '';
  946. if (!empty($js)) {
  947. $content .= implode("\n ", $js) . "\n";
  948. }
  949. if (!empty($onLoadActions)) {
  950. $content .= implode("\n ", $onLoadActions) . "\n";
  951. }
  952. if (!empty($javascript)) {
  953. $content .= $javascript . "\n";
  954. }
  955. if (preg_match('/^\s*$/s', $content)) {
  956. return '';
  957. }
  958. $html = '<script type="text/javascript">' . PHP_EOL
  959. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  960. . $content
  961. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  962. . PHP_EOL . '</script>';
  963. return $html;
  964. }
  965. }