Editor.php 14 KB

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