PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Dojo/Form/Element/Editor.php

https://github.com/Shreef/zf2
PHP | 601 lines | 293 code | 46 blank | 262 comment | 26 complexity | 12f0e9b04ff318835b5af893a8ca512f MD5 | raw file
  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 Form_Element
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Dojo\Form\Element;
  25. use Zend\Form\ElementException;
  26. /**
  27. * Editor dijit
  28. *
  29. * @uses \Zend\Dojo\Form\Element\Dijit
  30. * @uses \Zend\Form\ElementException
  31. * @category Zend
  32. * @package Zend_Dojo
  33. * @subpackage Form_Element
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Editor extends Dijit
  38. {
  39. /**
  40. * @var string View helper
  41. */
  42. public $helper = 'Editor';
  43. /**
  44. * Add a single event to connect to the editing area
  45. *
  46. * @param string $event
  47. * @return \Zend\Dojo\Form\Element\Editor
  48. */
  49. public function addCaptureEvent($event)
  50. {
  51. $event = (string) $event;
  52. $captureEvents = $this->getCaptureEvents();
  53. if (in_array($event, $captureEvents)) {
  54. return $this;
  55. }
  56. $captureEvents[] = (string) $event;
  57. $this->setDijitParam('captureEvents', $captureEvents);
  58. return $this;
  59. }
  60. /**
  61. * Add multiple capture events
  62. *
  63. * @param array $events
  64. * @return \Zend\Dojo\Form\Element\Editor
  65. */
  66. public function addCaptureEvents(array $events)
  67. {
  68. foreach ($events as $event) {
  69. $this->addCaptureEvent($event);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Overwrite many capture events at once
  75. *
  76. * @param array $events
  77. * @return \Zend\Dojo\Form\Element\Editor
  78. */
  79. public function setCaptureEvents(array $events)
  80. {
  81. $this->clearCaptureEvents();
  82. $this->addCaptureEvents($events);
  83. return $this;
  84. }
  85. /**
  86. * Get all capture events
  87. *
  88. * @return array
  89. */
  90. public function getCaptureEvents()
  91. {
  92. if (!$this->hasDijitParam('captureEvents')) {
  93. return array();
  94. }
  95. return $this->getDijitParam('captureEvents');
  96. }
  97. /**
  98. * Is a given capture event registered?
  99. *
  100. * @param string $event
  101. * @return bool
  102. */
  103. public function hasCaptureEvent($event)
  104. {
  105. $captureEvents = $this->getCaptureEvents();
  106. return in_array((string) $event, $captureEvents);
  107. }
  108. /**
  109. * Remove a given capture event
  110. *
  111. * @param string $event
  112. * @return \Zend\Dojo\Form\Element\Editor
  113. */
  114. public function removeCaptureEvent($event)
  115. {
  116. $event = (string) $event;
  117. $captureEvents = $this->getCaptureEvents();
  118. if (false === ($index = array_search($event, $captureEvents))) {
  119. return $this;
  120. }
  121. unset($captureEvents[$index]);
  122. $this->setDijitParam('captureEvents', $captureEvents);
  123. return $this;
  124. }
  125. /**
  126. * Clear all capture events
  127. *
  128. * @return \Zend\Dojo\Form\Element\Editor
  129. */
  130. public function clearCaptureEvents()
  131. {
  132. return $this->removeDijitParam('captureEvents');
  133. }
  134. /**
  135. * Add a single event to the dijit
  136. *
  137. * @param string $event
  138. * @return \Zend\Dojo\Form\Element\Editor
  139. */
  140. public function addEvent($event)
  141. {
  142. $event = (string) $event;
  143. $events = $this->getEvents();
  144. if (in_array($event, $events)) {
  145. return $this;
  146. }
  147. $events[] = (string) $event;
  148. $this->setDijitParam('events', $events);
  149. return $this;
  150. }
  151. /**
  152. * Add multiple events
  153. *
  154. * @param array $events
  155. * @return \Zend\Dojo\Form\Element\Editor
  156. */
  157. public function addEvents(array $events)
  158. {
  159. foreach ($events as $event) {
  160. $this->addEvent($event);
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Overwrite many events at once
  166. *
  167. * @param array $events
  168. * @return \Zend\Dojo\Form\Element\Editor
  169. */
  170. public function setEvents(array $events)
  171. {
  172. $this->clearEvents();
  173. $this->addEvents($events);
  174. return $this;
  175. }
  176. /**
  177. * Get all events
  178. *
  179. * @return array
  180. */
  181. public function getEvents()
  182. {
  183. if (!$this->hasDijitParam('events')) {
  184. return array();
  185. }
  186. return $this->getDijitParam('events');
  187. }
  188. /**
  189. * Is a given event registered?
  190. *
  191. * @param string $event
  192. * @return bool
  193. */
  194. public function hasEvent($event)
  195. {
  196. $events = $this->getEvents();
  197. return in_array((string) $event, $events);
  198. }
  199. /**
  200. * Remove a given event
  201. *
  202. * @param string $event
  203. * @return \Zend\Dojo\Form\Element\Editor
  204. */
  205. public function removeEvent($event)
  206. {
  207. $events = $this->getEvents();
  208. if (false === ($index = array_search($event, $events))) {
  209. return $this;
  210. }
  211. unset($events[$index]);
  212. $this->setDijitParam('events', $events);
  213. return $this;
  214. }
  215. /**
  216. * Clear all events
  217. *
  218. * @return \Zend\Dojo\Form\Element\Editor
  219. */
  220. public function clearEvents()
  221. {
  222. return $this->removeDijitParam('events');
  223. }
  224. /**
  225. * Add a single editor plugin
  226. *
  227. * @param string $plugin
  228. * @return \Zend\Dojo\Form\Element\Editor
  229. */
  230. public function addPlugin($plugin)
  231. {
  232. $plugin = (string) $plugin;
  233. $plugins = $this->getPlugins();
  234. if (in_array($plugin, $plugins)) {
  235. return $this;
  236. }
  237. $plugins[] = (string) $plugin;
  238. $this->setDijitParam('plugins', $plugins);
  239. return $this;
  240. }
  241. /**
  242. * Add multiple plugins
  243. *
  244. * @param array $plugins
  245. * @return \Zend\Dojo\Form\Element\Editor
  246. */
  247. public function addPlugins(array $plugins)
  248. {
  249. foreach ($plugins as $plugin) {
  250. $this->addPlugin($plugin);
  251. }
  252. return $this;
  253. }
  254. /**
  255. * Overwrite many plugins at once
  256. *
  257. * @param array $plugins
  258. * @return \Zend\Dojo\Form\Element\Editor
  259. */
  260. public function setPlugins(array $plugins)
  261. {
  262. $this->clearPlugins();
  263. $this->addPlugins($plugins);
  264. return $this;
  265. }
  266. /**
  267. * Get all plugins
  268. *
  269. * @return array
  270. */
  271. public function getPlugins()
  272. {
  273. if (!$this->hasDijitParam('plugins')) {
  274. return array();
  275. }
  276. return $this->getDijitParam('plugins');
  277. }
  278. /**
  279. * Is a given plugin registered?
  280. *
  281. * @param string $plugin
  282. * @return bool
  283. */
  284. public function hasPlugin($plugin)
  285. {
  286. $plugins = $this->getPlugins();
  287. return in_array((string) $plugin, $plugins);
  288. }
  289. /**
  290. * Remove a given plugin
  291. *
  292. * @param string $plugin
  293. * @return \Zend\Dojo\Form\Element\Editor
  294. */
  295. public function removePlugin($plugin)
  296. {
  297. $plugins = $this->getPlugins();
  298. if (false === ($index = array_search($plugin, $plugins))) {
  299. return $this;
  300. }
  301. unset($plugins[$index]);
  302. $this->setDijitParam('plugins', $plugins);
  303. return $this;
  304. }
  305. /**
  306. * Clear all plugins
  307. *
  308. * @return \Zend\Dojo\Form\Element\Editor
  309. */
  310. public function clearPlugins()
  311. {
  312. return $this->removeDijitParam('plugins');
  313. }
  314. /**
  315. * Set edit action interval
  316. *
  317. * @param int $interval
  318. * @return \Zend\Dojo\Form\Element\Editor
  319. */
  320. public function setEditActionInterval($interval)
  321. {
  322. return $this->setDijitParam('editActionInterval', (int) $interval);
  323. }
  324. /**
  325. * Get edit action interval; defaults to 3
  326. *
  327. * @return int
  328. */
  329. public function getEditActionInterval()
  330. {
  331. if (!$this->hasDijitParam('editActionInterval')) {
  332. $this->setEditActionInterval(3);
  333. }
  334. return $this->getDijitParam('editActionInterval');
  335. }
  336. /**
  337. * Set focus on load flag
  338. *
  339. * @param bool $flag
  340. * @return \Zend\Dojo\Form\Element\Editor
  341. */
  342. public function setFocusOnLoad($flag)
  343. {
  344. return $this->setDijitParam('focusOnLoad', (bool) $flag);
  345. }
  346. /**
  347. * Retrieve focus on load flag
  348. *
  349. * @return bool
  350. */
  351. public function getFocusOnLoad()
  352. {
  353. if (!$this->hasDijitParam('focusOnLoad')) {
  354. return false;
  355. }
  356. return $this->getDijitParam('focusOnLoad');
  357. }
  358. /**
  359. * Set editor height
  360. *
  361. * @param string|int $height
  362. * @return \Zend\Dojo\Form\Element\Editor
  363. */
  364. public function setHeight($height)
  365. {
  366. if (!preg_match('/^\d+(em|px|%)?$/i', $height)) {
  367. throw new ElementException('Invalid height provided; must be integer or CSS measurement');
  368. }
  369. if (!preg_match('/(em|px|%)$/', $height)) {
  370. $height .= 'px';
  371. }
  372. return $this->setDijitParam('height', $height);
  373. }
  374. /**
  375. * Retrieve height
  376. *
  377. * @return string
  378. */
  379. public function getHeight()
  380. {
  381. if (!$this->hasDijitParam('height')) {
  382. return '300px';
  383. }
  384. return $this->getDijitParam('height');
  385. }
  386. /**
  387. * Set whether or not to inherit parent's width
  388. *
  389. * @param bool $flag
  390. * @return \Zend\Dojo\Form\Element\Editor
  391. */
  392. public function setInheritWidth($flag)
  393. {
  394. return $this->setDijitParam('inheritWidth', (bool) $flag);
  395. }
  396. /**
  397. * Whether or not to inherit the parent's width
  398. *
  399. * @return bool
  400. */
  401. public function getInheritWidth()
  402. {
  403. if (!$this->hasDijitParam('inheritWidth')) {
  404. return false;
  405. }
  406. return $this->getDijitParam('inheritWidth');
  407. }
  408. /**
  409. * Set minimum height of editor
  410. *
  411. * @param string|int $minHeight
  412. * @return \Zend\Dojo\Form\Element\Editor
  413. */
  414. public function setMinHeight($minHeight)
  415. {
  416. if (!preg_match('/^\d+(em)?$/i', $minHeight)) {
  417. throw new ElementException('Invalid minHeight provided; must be integer or CSS measurement');
  418. }
  419. if ('em' != substr($minHeight, -2)) {
  420. $minHeight .= 'em';
  421. }
  422. return $this->setDijitParam('minHeight', $minHeight);
  423. }
  424. /**
  425. * Get minimum height of editor
  426. *
  427. * @return string
  428. */
  429. public function getMinHeight()
  430. {
  431. if (!$this->hasDijitParam('minHeight')) {
  432. return '1em';
  433. }
  434. return $this->getDijitParam('minHeight');
  435. }
  436. /**
  437. * Add a custom stylesheet
  438. *
  439. * @param string $styleSheet
  440. * @return \Zend\Dojo\Form\Element\Editor
  441. */
  442. public function addStyleSheet($styleSheet)
  443. {
  444. $stylesheets = $this->getStyleSheets();
  445. if (strstr($stylesheets, ';')) {
  446. $stylesheets = explode(';', $stylesheets);
  447. } elseif (!empty($stylesheets)) {
  448. $stylesheets = (array) $stylesheets;
  449. } else {
  450. $stylesheets = array();
  451. }
  452. if (!in_array($styleSheet, $stylesheets)) {
  453. $stylesheets[] = (string) $styleSheet;
  454. }
  455. return $this->setDijitParam('styleSheets', implode(';', $stylesheets));
  456. }
  457. /**
  458. * Add multiple custom stylesheets
  459. *
  460. * @param array $styleSheets
  461. * @return \Zend\Dojo\Form\Element\Editor
  462. */
  463. public function addStyleSheets(array $styleSheets)
  464. {
  465. foreach ($styleSheets as $styleSheet) {
  466. $this->addStyleSheet($styleSheet);
  467. }
  468. return $this;
  469. }
  470. /**
  471. * Overwrite all stylesheets
  472. *
  473. * @param array $styleSheets
  474. * @return \Zend\Dojo\Form\Element\Editor
  475. */
  476. public function setStyleSheets(array $styleSheets)
  477. {
  478. $this->clearStyleSheets();
  479. return $this->addStyleSheets($styleSheets);
  480. }
  481. /**
  482. * Get all stylesheets
  483. *
  484. * @return string
  485. */
  486. public function getStyleSheets()
  487. {
  488. if (!$this->hasDijitParam('styleSheets')) {
  489. return '';
  490. }
  491. return $this->getDijitParam('styleSheets');
  492. }
  493. /**
  494. * Is a given stylesheet registered?
  495. *
  496. * @param string $styleSheet
  497. * @return bool
  498. */
  499. public function hasStyleSheet($styleSheet)
  500. {
  501. $styleSheets = $this->getStyleSheets();
  502. $styleSheets = explode(';', $styleSheets);
  503. return in_array($styleSheet, $styleSheets);
  504. }
  505. /**
  506. * Remove a single stylesheet
  507. *
  508. * @param string $styleSheet
  509. * @return \Zend\Dojo\Form\Element\Editor
  510. */
  511. public function removeStyleSheet($styleSheet)
  512. {
  513. $styleSheets = $this->getStyleSheets();
  514. $styleSheets = explode(';', $styleSheets);
  515. if (false !== ($index = array_search($styleSheet, $styleSheets))) {
  516. unset($styleSheets[$index]);
  517. $this->setDijitParam('styleSheets', implode(';', $styleSheets));
  518. }
  519. return $this;
  520. }
  521. /**
  522. * Clear all stylesheets
  523. *
  524. * @return \Zend\Dojo\Form\Element\Editor
  525. */
  526. public function clearStyleSheets()
  527. {
  528. if ($this->hasDijitParam('styleSheets')) {
  529. $this->removeDijitParam('styleSheets');
  530. }
  531. return $this;
  532. }
  533. /**
  534. * Set update interval
  535. *
  536. * @param int $interval
  537. * @return \Zend\Dojo\Form\Element\Editor
  538. */
  539. public function setUpdateInterval($interval)
  540. {
  541. return $this->setDijitParam('updateInterval', (int) $interval);
  542. }
  543. /**
  544. * Get update interval
  545. *
  546. * @return int
  547. */
  548. public function getUpdateInterval()
  549. {
  550. if (!$this->hasDijitParam('updateInterval')) {
  551. return 200;
  552. }
  553. return $this->getDijitParam('updateInterval');
  554. }
  555. }