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

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