Container.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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-2010 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) 2005-2010 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.3.2';
  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. }
  242. }
  243. return $this;
  244. }
  245. /**
  246. * Specify one or multiple modules to require
  247. *
  248. * @param string|array $modules
  249. * @return Zend_Dojo_View_Helper_Dojo_Container
  250. */
  251. public function requireModule($modules)
  252. {
  253. if (!is_string($modules) && !is_array($modules)) {
  254. require_once 'Zend/Dojo/View/Exception.php';
  255. throw new Zend_Dojo_View_Exception('Invalid module name specified; must be a string or an array of strings');
  256. }
  257. $modules = (array) $modules;
  258. foreach ($modules as $mod) {
  259. if (!preg_match('/^[a-z][a-z0-9._-]+$/i', $mod)) {
  260. require_once 'Zend/Dojo/View/Exception.php';
  261. throw new Zend_Dojo_View_Exception(sprintf('Module name specified, "%s", contains invalid characters', (string) $mod));
  262. }
  263. if (!in_array($mod, $this->_modules)) {
  264. $this->_modules[] = $mod;
  265. }
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Retrieve list of modules to require
  271. *
  272. * @return array
  273. */
  274. public function getModules()
  275. {
  276. return $this->_modules;
  277. }
  278. /**
  279. * Register a module path
  280. *
  281. * @param string $module The module to register a path for
  282. * @param string $path The path to register for the module
  283. * @return Zend_Dojo_View_Helper_Dojo_Container
  284. */
  285. public function registerModulePath($module, $path)
  286. {
  287. $path = (string) $path;
  288. if (!in_array($module, $this->_modulePaths)) {
  289. $this->_modulePaths[$module] = $path;
  290. }
  291. return $this;
  292. }
  293. /**
  294. * List registered module paths
  295. *
  296. * @return array
  297. */
  298. public function getModulePaths()
  299. {
  300. return $this->_modulePaths;
  301. }
  302. /**
  303. * Add layer (custom build) path
  304. *
  305. * @param string $path
  306. * @return Zend_Dojo_View_Helper_Dojo_Container
  307. */
  308. public function addLayer($path)
  309. {
  310. $path = (string) $path;
  311. if (!in_array($path, $this->_layers)) {
  312. $this->_layers[] = $path;
  313. }
  314. return $this;
  315. }
  316. /**
  317. * Get registered layers
  318. *
  319. * @return array
  320. */
  321. public function getLayers()
  322. {
  323. return $this->_layers;
  324. }
  325. /**
  326. * Remove a registered layer
  327. *
  328. * @param string $path
  329. * @return Zend_Dojo_View_Helper_Dojo_Container
  330. */
  331. public function removeLayer($path)
  332. {
  333. $path = (string) $path;
  334. $layers = array_flip($this->_layers);
  335. if (array_key_exists($path, $layers)) {
  336. unset($layers[$path]);
  337. $this->_layers = array_keys($layers);
  338. }
  339. return $this;
  340. }
  341. /**
  342. * Clear all registered layers
  343. *
  344. * @return Zend_Dojo_View_Helper_Dojo_Container
  345. */
  346. public function clearLayers()
  347. {
  348. $this->_layers = array();
  349. return $this;
  350. }
  351. /**
  352. * Set CDN base path
  353. *
  354. * @param string $url
  355. * @return Zend_Dojo_View_Helper_Dojo_Container
  356. */
  357. public function setCdnBase($url)
  358. {
  359. $this->_cdnBase = (string) $url;
  360. return $this;
  361. }
  362. /**
  363. * Return CDN base URL
  364. *
  365. * @return string
  366. */
  367. public function getCdnBase()
  368. {
  369. return $this->_cdnBase;
  370. }
  371. /**
  372. * Use CDN, using version specified
  373. *
  374. * @param string $version
  375. * @return Zend_Dojo_View_Helper_Dojo_Container
  376. */
  377. public function setCdnVersion($version = null)
  378. {
  379. $this->enable();
  380. if (preg_match('/^[1-9]\.[0-9](\.[0-9])?$/', $version)) {
  381. $this->_cdnVersion = $version;
  382. }
  383. return $this;
  384. }
  385. /**
  386. * Get CDN version
  387. *
  388. * @return string
  389. */
  390. public function getCdnVersion()
  391. {
  392. return $this->_cdnVersion;
  393. }
  394. /**
  395. * Set CDN path to dojo (relative to CDN base + version)
  396. *
  397. * @param string $path
  398. * @return Zend_Dojo_View_Helper_Dojo_Container
  399. */
  400. public function setCdnDojoPath($path)
  401. {
  402. $this->_cdnDojoPath = (string) $path;
  403. return $this;
  404. }
  405. /**
  406. * Get CDN path to dojo (relative to CDN base + version)
  407. *
  408. * @return string
  409. */
  410. public function getCdnDojoPath()
  411. {
  412. return $this->_cdnDojoPath;
  413. }
  414. /**
  415. * Are we using the CDN?
  416. *
  417. * @return bool
  418. */
  419. public function useCdn()
  420. {
  421. return !$this->useLocalPath();
  422. }
  423. /**
  424. * Set path to local dojo
  425. *
  426. * @param string $path
  427. * @return Zend_Dojo_View_Helper_Dojo_Container
  428. */
  429. public function setLocalPath($path)
  430. {
  431. $this->enable();
  432. $this->_localPath = (string) $path;
  433. return $this;
  434. }
  435. /**
  436. * Get local path to dojo
  437. *
  438. * @return string
  439. */
  440. public function getLocalPath()
  441. {
  442. return $this->_localPath;
  443. }
  444. /**
  445. * Are we using a local path?
  446. *
  447. * @return bool
  448. */
  449. public function useLocalPath()
  450. {
  451. return (null === $this->_localPath) ? false : true;
  452. }
  453. /**
  454. * Set Dojo configuration
  455. *
  456. * @param string $option
  457. * @param mixed $value
  458. * @return Zend_Dojo_View_Helper_Dojo_Container
  459. */
  460. public function setDjConfig(array $config)
  461. {
  462. $this->_djConfig = $config;
  463. return $this;
  464. }
  465. /**
  466. * Set Dojo configuration option
  467. *
  468. * @param string $option
  469. * @param mixed $value
  470. * @return Zend_Dojo_View_Helper_Dojo_Container
  471. */
  472. public function setDjConfigOption($option, $value)
  473. {
  474. $option = (string) $option;
  475. $this->_djConfig[$option] = $value;
  476. return $this;
  477. }
  478. /**
  479. * Retrieve dojo configuration values
  480. *
  481. * @return array
  482. */
  483. public function getDjConfig()
  484. {
  485. return $this->_djConfig;
  486. }
  487. /**
  488. * Get dojo configuration value
  489. *
  490. * @param string $option
  491. * @param mixed $default
  492. * @return mixed
  493. */
  494. public function getDjConfigOption($option, $default = null)
  495. {
  496. $option = (string) $option;
  497. if (array_key_exists($option, $this->_djConfig)) {
  498. return $this->_djConfig[$option];
  499. }
  500. return $default;
  501. }
  502. /**
  503. * Add a stylesheet by module name
  504. *
  505. * @param string $module
  506. * @return Zend_Dojo_View_Helper_Dojo_Container
  507. */
  508. public function addStylesheetModule($module)
  509. {
  510. if (!preg_match('/^[a-z0-9]+\.[a-z0-9_-]+(\.[a-z0-9_-]+)*$/i', $module)) {
  511. require_once 'Zend/Dojo/View/Exception.php';
  512. throw new Zend_Dojo_View_Exception('Invalid stylesheet module specified');
  513. }
  514. if (!in_array($module, $this->_stylesheetModules)) {
  515. $this->_stylesheetModules[] = $module;
  516. }
  517. return $this;
  518. }
  519. /**
  520. * Get all stylesheet modules currently registered
  521. *
  522. * @return array
  523. */
  524. public function getStylesheetModules()
  525. {
  526. return $this->_stylesheetModules;
  527. }
  528. /**
  529. * Add a stylesheet
  530. *
  531. * @param string $path
  532. * @return Zend_Dojo_View_Helper_Dojo_Container
  533. */
  534. public function addStylesheet($path)
  535. {
  536. $path = (string) $path;
  537. if (!in_array($path, $this->_stylesheets)) {
  538. $this->_stylesheets[] = (string) $path;
  539. }
  540. return $this;
  541. }
  542. /**
  543. * Register the dojo.css stylesheet?
  544. *
  545. * With no arguments, returns the status of the flag; with arguments, sets
  546. * the flag and returns the object.
  547. *
  548. * @param null|bool $flag
  549. * @return Zend_Dojo_View_Helper_Dojo_Container|bool
  550. */
  551. public function registerDojoStylesheet($flag = null)
  552. {
  553. if (null === $flag) {
  554. return $this->_registerDojoStylesheet;
  555. }
  556. $this->_registerDojoStylesheet = (bool) $flag;
  557. return $this;
  558. }
  559. /**
  560. * Retrieve registered stylesheets
  561. *
  562. * @return array
  563. */
  564. public function getStylesheets()
  565. {
  566. return $this->_stylesheets;
  567. }
  568. /**
  569. * Add a script to execute onLoad
  570. *
  571. * dojo.addOnLoad accepts:
  572. * - function name
  573. * - lambda
  574. *
  575. * @param string $callback Lambda
  576. * @return Zend_Dojo_View_Helper_Dojo_Container
  577. */
  578. public function addOnLoad($callback)
  579. {
  580. if (!in_array($callback, $this->_onLoadActions, true)) {
  581. $this->_onLoadActions[] = $callback;
  582. }
  583. return $this;
  584. }
  585. /**
  586. * Prepend an onLoad event to the list of onLoad actions
  587. *
  588. * @param string $callback Lambda
  589. * @return Zend_Dojo_View_Helper_Dojo_Container
  590. */
  591. public function prependOnLoad($callback)
  592. {
  593. if (!in_array($callback, $this->_onLoadActions, true)) {
  594. array_unshift($this->_onLoadActions, $callback);
  595. }
  596. return $this;
  597. }
  598. /**
  599. * Retrieve all registered onLoad actions
  600. *
  601. * @return array
  602. */
  603. public function getOnLoadActions()
  604. {
  605. return $this->_onLoadActions;
  606. }
  607. /**
  608. * Start capturing routines to run onLoad
  609. *
  610. * @return bool
  611. */
  612. public function onLoadCaptureStart()
  613. {
  614. if ($this->_captureLock) {
  615. require_once 'Zend/Dojo/View/Exception.php';
  616. throw new Zend_Dojo_View_Exception('Cannot nest onLoad captures');
  617. }
  618. $this->_captureLock = true;
  619. ob_start();
  620. return;
  621. }
  622. /**
  623. * Stop capturing routines to run onLoad
  624. *
  625. * @return bool
  626. */
  627. public function onLoadCaptureEnd()
  628. {
  629. $data = ob_get_clean();
  630. $this->_captureLock = false;
  631. $this->addOnLoad($data);
  632. return true;
  633. }
  634. /**
  635. * Add a programmatic dijit
  636. *
  637. * @param string $id
  638. * @param array $params
  639. * @return Zend_Dojo_View_Helper_Dojo_Container
  640. */
  641. public function addDijit($id, array $params)
  642. {
  643. if (array_key_exists($id, $this->_dijits)) {
  644. require_once 'Zend/Dojo/View/Exception.php';
  645. throw new Zend_Dojo_View_Exception(sprintf('Duplicate dijit with id "%s" already registered', $id));
  646. }
  647. $this->_dijits[$id] = array(
  648. 'id' => $id,
  649. 'params' => $params,
  650. );
  651. return $this;
  652. }
  653. /**
  654. * Set a programmatic dijit (overwrites)
  655. *
  656. * @param string $id
  657. * @param array $params
  658. * @return Zend_Dojo_View_Helper_Dojo_Container
  659. */
  660. public function setDijit($id, array $params)
  661. {
  662. $this->removeDijit($id);
  663. return $this->addDijit($id, $params);
  664. }
  665. /**
  666. * Add multiple dijits at once
  667. *
  668. * Expects an array of id => array $params pairs
  669. *
  670. * @param array $dijits
  671. * @return Zend_Dojo_View_Helper_Dojo_Container
  672. */
  673. public function addDijits(array $dijits)
  674. {
  675. foreach ($dijits as $id => $params) {
  676. $this->addDijit($id, $params);
  677. }
  678. return $this;
  679. }
  680. /**
  681. * Set multiple dijits at once (overwrites)
  682. *
  683. * Expects an array of id => array $params pairs
  684. *
  685. * @param array $dijits
  686. * @return Zend_Dojo_View_Helper_Dojo_Container
  687. */
  688. public function setDijits(array $dijits)
  689. {
  690. $this->clearDijits();
  691. return $this->addDijits($dijits);
  692. }
  693. /**
  694. * Is the given programmatic dijit already registered?
  695. *
  696. * @param string $id
  697. * @return bool
  698. */
  699. public function hasDijit($id)
  700. {
  701. return array_key_exists($id, $this->_dijits);
  702. }
  703. /**
  704. * Retrieve a dijit by id
  705. *
  706. * @param string $id
  707. * @return array|null
  708. */
  709. public function getDijit($id)
  710. {
  711. if ($this->hasDijit($id)) {
  712. return $this->_dijits[$id]['params'];
  713. }
  714. return null;
  715. }
  716. /**
  717. * Retrieve all dijits
  718. *
  719. * Returns dijits as an array of assoc arrays
  720. *
  721. * @return array
  722. */
  723. public function getDijits()
  724. {
  725. return array_values($this->_dijits);
  726. }
  727. /**
  728. * Remove a programmatic dijit if it exists
  729. *
  730. * @param string $id
  731. * @return Zend_Dojo_View_Helper_Dojo_Container
  732. */
  733. public function removeDijit($id)
  734. {
  735. if (array_key_exists($id, $this->_dijits)) {
  736. unset($this->_dijits[$id]);
  737. }
  738. return $this;
  739. }
  740. /**
  741. * Clear all dijits
  742. *
  743. * @return Zend_Dojo_View_Helper_Dojo_Container
  744. */
  745. public function clearDijits()
  746. {
  747. $this->_dijits = array();
  748. return $this;
  749. }
  750. /**
  751. * Render dijits as JSON structure
  752. *
  753. * @return string
  754. */
  755. public function dijitsToJson()
  756. {
  757. require_once 'Zend/Json.php';
  758. return Zend_Json::encode($this->getDijits());
  759. }
  760. /**
  761. * Create dijit loader functionality
  762. *
  763. * @return void
  764. */
  765. public function registerDijitLoader()
  766. {
  767. if (!$this->_dijitLoaderRegistered) {
  768. $js =<<<EOJ
  769. function() {
  770. dojo.forEach(zendDijits, function(info) {
  771. var n = dojo.byId(info.id);
  772. if (null != n) {
  773. dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
  774. }
  775. });
  776. dojo.parser.parse();
  777. }
  778. EOJ;
  779. $this->requireModule('dojo.parser');
  780. $this->_addZendLoad($js);
  781. $this->addJavascript('var zendDijits = ' . $this->dijitsToJson() . ';');
  782. $this->_dijitLoaderRegistered = true;
  783. }
  784. }
  785. /**
  786. * Add arbitrary javascript to execute in dojo JS container
  787. *
  788. * @param string $js
  789. * @return Zend_Dojo_View_Helper_Dojo_Container
  790. */
  791. public function addJavascript($js)
  792. {
  793. $js = preg_replace('/^\s*(.*?)\s*$/s', '$1', $js);
  794. if (!in_array(substr($js, -1), array(';', '}'))) {
  795. $js .= ';';
  796. }
  797. if (in_array($js, $this->_javascriptStatements)) {
  798. return $this;
  799. }
  800. $this->_javascriptStatements[] = $js;
  801. return $this;
  802. }
  803. /**
  804. * Return all registered javascript statements
  805. *
  806. * @return array
  807. */
  808. public function getJavascript()
  809. {
  810. return $this->_javascriptStatements;
  811. }
  812. /**
  813. * Clear arbitrary javascript stack
  814. *
  815. * @return Zend_Dojo_View_Helper_Dojo_Container
  816. */
  817. public function clearJavascript()
  818. {
  819. $this->_javascriptStatements = array();
  820. return $this;
  821. }
  822. /**
  823. * Capture arbitrary javascript to include in dojo script
  824. *
  825. * @return void
  826. */
  827. public function javascriptCaptureStart()
  828. {
  829. if ($this->_captureLock) {
  830. require_once 'Zend/Dojo/View/Exception.php';
  831. throw new Zend_Dojo_View_Exception('Cannot nest captures');
  832. }
  833. $this->_captureLock = true;
  834. ob_start();
  835. return;
  836. }
  837. /**
  838. * Finish capturing arbitrary javascript to include in dojo script
  839. *
  840. * @return true
  841. */
  842. public function javascriptCaptureEnd()
  843. {
  844. $data = ob_get_clean();
  845. $this->_captureLock = false;
  846. $this->addJavascript($data);
  847. return true;
  848. }
  849. /**
  850. * String representation of dojo environment
  851. *
  852. * @return string
  853. */
  854. public function __toString()
  855. {
  856. if (!$this->isEnabled()) {
  857. return '';
  858. }
  859. $this->_isXhtml = $this->view->doctype()->isXhtml();
  860. if (Zend_Dojo_View_Helper_Dojo::useDeclarative()) {
  861. if (null === $this->getDjConfigOption('parseOnLoad')) {
  862. $this->setDjConfigOption('parseOnLoad', true);
  863. }
  864. }
  865. if (!empty($this->_dijits)) {
  866. $this->registerDijitLoader();
  867. }
  868. $html = $this->_renderStylesheets() . PHP_EOL
  869. . $this->_renderDjConfig() . PHP_EOL
  870. . $this->_renderDojoScriptTag() . PHP_EOL
  871. . $this->_renderLayers() . PHP_EOL
  872. . $this->_renderExtras();
  873. return $html;
  874. }
  875. /**
  876. * Retrieve local path to dojo resources for building relative paths
  877. *
  878. * @return string
  879. */
  880. protected function _getLocalRelativePath()
  881. {
  882. if (null === $this->_localRelativePath) {
  883. $localPath = $this->getLocalPath();
  884. $localPath = preg_replace('|[/\\\\]dojo[/\\\\]dojo.js[^/\\\\]*$|i', '', $localPath);
  885. $this->_localRelativePath = $localPath;
  886. }
  887. return $this->_localRelativePath;
  888. }
  889. /**
  890. * Render dojo stylesheets
  891. *
  892. * @return string
  893. */
  894. protected function _renderStylesheets()
  895. {
  896. if ($this->useCdn()) {
  897. $base = $this->getCdnBase()
  898. . $this->getCdnVersion();
  899. } else {
  900. $base = $this->_getLocalRelativePath();
  901. }
  902. $registeredStylesheets = $this->getStylesheetModules();
  903. foreach ($registeredStylesheets as $stylesheet) {
  904. $themeName = substr($stylesheet, strrpos($stylesheet, '.') + 1);
  905. $stylesheet = str_replace('.', '/', $stylesheet);
  906. $stylesheets[] = $base . '/' . $stylesheet . '/' . $themeName . '.css';
  907. }
  908. foreach ($this->getStylesheets() as $stylesheet) {
  909. $stylesheets[] = $stylesheet;
  910. }
  911. if ($this->_registerDojoStylesheet) {
  912. $stylesheets[] = $base . '/dojo/resources/dojo.css';
  913. }
  914. if (empty($stylesheets)) {
  915. return '';
  916. }
  917. array_reverse($stylesheets);
  918. $style = '<style type="text/css">' . PHP_EOL
  919. . (($this->_isXhtml) ? '<!--' : '<!--') . PHP_EOL;
  920. foreach ($stylesheets as $stylesheet) {
  921. $style .= ' @import "' . $stylesheet . '";' . PHP_EOL;
  922. }
  923. $style .= (($this->_isXhtml) ? '-->' : '-->') . PHP_EOL
  924. . '</style>';
  925. return $style;
  926. }
  927. /**
  928. * Render DjConfig values
  929. *
  930. * @return string
  931. */
  932. protected function _renderDjConfig()
  933. {
  934. $djConfigValues = $this->getDjConfig();
  935. if (empty($djConfigValues)) {
  936. return '';
  937. }
  938. require_once 'Zend/Json.php';
  939. $scriptTag = '<script type="text/javascript">' . PHP_EOL
  940. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  941. . ' var djConfig = ' . Zend_Json::encode($djConfigValues) . ';' . PHP_EOL
  942. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  943. . '</script>';
  944. return $scriptTag;
  945. }
  946. /**
  947. * Render dojo script tag
  948. *
  949. * Renders Dojo script tag by utilizing either local path provided or the
  950. * CDN. If any djConfig values were set, they will be serialized and passed
  951. * with that attribute.
  952. *
  953. * @return string
  954. */
  955. protected function _renderDojoScriptTag()
  956. {
  957. if ($this->useCdn()) {
  958. $source = $this->getCdnBase()
  959. . $this->getCdnVersion()
  960. . $this->getCdnDojoPath();
  961. } else {
  962. $source = $this->getLocalPath();
  963. }
  964. $scriptTag = '<script type="text/javascript" src="' . $source . '"></script>';
  965. return $scriptTag;
  966. }
  967. /**
  968. * Render layers (custom builds) as script tags
  969. *
  970. * @return string
  971. */
  972. protected function _renderLayers()
  973. {
  974. $layers = $this->getLayers();
  975. if (empty($layers)) {
  976. return '';
  977. }
  978. $enc = 'UTF-8';
  979. if ($this->view instanceof Zend_View_Interface
  980. && method_exists($this->view, 'getEncoding')
  981. ) {
  982. $enc = $this->view->getEncoding();
  983. }
  984. $html = array();
  985. foreach ($layers as $path) {
  986. $html[] = sprintf(
  987. '<script type="text/javascript" src="%s"></script>',
  988. htmlspecialchars($path, ENT_QUOTES, $enc)
  989. );
  990. }
  991. return implode("\n", $html);
  992. }
  993. /**
  994. * Render dojo module paths and requires
  995. *
  996. * @return string
  997. */
  998. protected function _renderExtras()
  999. {
  1000. $js = array();
  1001. $modulePaths = $this->getModulePaths();
  1002. if (!empty($modulePaths)) {
  1003. foreach ($modulePaths as $module => $path) {
  1004. $js[] = 'dojo.registerModulePath("' . $this->view->escape($module) . '", "' . $this->view->escape($path) . '");';
  1005. }
  1006. }
  1007. $modules = $this->getModules();
  1008. if (!empty($modules)) {
  1009. foreach ($modules as $module) {
  1010. $js[] = 'dojo.require("' . $this->view->escape($module) . '");';
  1011. }
  1012. }
  1013. $onLoadActions = array();
  1014. // Get Zend specific onLoad actions; these will always be first to
  1015. // ensure that dijits are created in the correct order
  1016. foreach ($this->_getZendLoadActions() as $callback) {
  1017. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  1018. }
  1019. // Get all other onLoad actions
  1020. foreach ($this->getOnLoadActions() as $callback) {
  1021. $onLoadActions[] = 'dojo.addOnLoad(' . $callback . ');';
  1022. }
  1023. $javascript = implode("\n ", $this->getJavascript());
  1024. $content = '';
  1025. if (!empty($js)) {
  1026. $content .= implode("\n ", $js) . "\n";
  1027. }
  1028. if (!empty($onLoadActions)) {
  1029. $content .= implode("\n ", $onLoadActions) . "\n";
  1030. }
  1031. if (!empty($javascript)) {
  1032. $content .= $javascript . "\n";
  1033. }
  1034. if (preg_match('/^\s*$/s', $content)) {
  1035. return '';
  1036. }
  1037. $html = '<script type="text/javascript">' . PHP_EOL
  1038. . (($this->_isXhtml) ? '//<![CDATA[' : '//<!--') . PHP_EOL
  1039. . $content
  1040. . (($this->_isXhtml) ? '//]]>' : '//-->') . PHP_EOL
  1041. . PHP_EOL . '</script>';
  1042. return $html;
  1043. }
  1044. /**
  1045. * Add an onLoad action related to ZF dijit creation
  1046. *
  1047. * This method is public, but prefixed with an underscore to indicate that
  1048. * it should not normally be called by userland code. It is pertinent to
  1049. * ensuring that the correct order of operations occurs during dijit
  1050. * creation.
  1051. *
  1052. * @param string $callback
  1053. * @return Zend_Dojo_View_Helper_Dojo_Container
  1054. */
  1055. public function _addZendLoad($callback)
  1056. {
  1057. if (!in_array($callback, $this->_zendLoadActions, true)) {
  1058. $this->_zendLoadActions[] = $callback;
  1059. }
  1060. return $this;
  1061. }
  1062. /**
  1063. * Retrieve all ZF dijit callbacks
  1064. *
  1065. * @return array
  1066. */
  1067. public function _getZendLoadActions()
  1068. {
  1069. return $this->_zendLoadActions;
  1070. }
  1071. }