Editor.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2015 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. /**
  555. * Add a single editor extra plugin.
  556. *
  557. * @param string $plugin
  558. * @return Zend_Dojo_Form_Element_Editor
  559. */
  560. public function addExtraPlugin($plugin)
  561. {
  562. $plugin = (string) $plugin;
  563. $extraPlugins = $this->getExtraPlugins();
  564. if (in_array($plugin, $extraPlugins)) {
  565. return $this;
  566. }
  567. $extraPlugins[] = (string) $plugin;
  568. $this->setDijitParam('extraPlugins', $extraPlugins);
  569. return $this;
  570. }
  571. /**
  572. * Add multiple extra plugins.
  573. *
  574. * @param array $extraPlugins
  575. * @return Zend_Dojo_Form_Element_Editor
  576. */
  577. public function addExtraPlugins(array $plugins)
  578. {
  579. foreach ($plugins as $plugin) {
  580. $this->addExtraPlugin($plugin);
  581. }
  582. return $this;
  583. }
  584. /**
  585. * Overwrite many extra plugins at once.
  586. *
  587. * @param array $plugins
  588. * @return Zend_Dojo_Form_Element_Editor
  589. */
  590. public function setExtraPlugins(array $plugins)
  591. {
  592. $this->clearExtraPlugins();
  593. $this->addExtraPlugins($plugins);
  594. return $this;
  595. }
  596. /**
  597. * Get all extra plugins.
  598. *
  599. * @return array
  600. */
  601. public function getExtraPlugins()
  602. {
  603. if (!$this->hasDijitParam('extraPlugins')) {
  604. return array();
  605. }
  606. return $this->getDijitParam('extraPlugins');
  607. }
  608. /**
  609. * Is a given extra plugin registered?
  610. *
  611. * @param string $plugin
  612. * @return bool
  613. */
  614. public function hasExtraPlugin($plugin)
  615. {
  616. $extraPlugins = $this->getExtraPlugins();
  617. return in_array((string) $plugin, $extraPlugins);
  618. }
  619. /**
  620. * Remove a given extra plugin.
  621. *
  622. * @param string $plugin
  623. * @return Zend_Dojo_Form_Element_Editor
  624. */
  625. public function removeExtraPlugin($plugin)
  626. {
  627. $extraPlugins = $this->getExtraPlugins();
  628. if (false === ($index = array_search($plugin, $extraPlugins))) {
  629. return $this;
  630. }
  631. unset($extraPlugins[$index]);
  632. $this->setDijitParam('extraPlugins', $extraPlugins);
  633. return $this;
  634. }
  635. /**
  636. * Clear all extra plugins.
  637. *
  638. * @return Zend_Dojo_Form_Element_Editor
  639. */
  640. public function clearExtraPlugins()
  641. {
  642. return $this->removeDijitParam('extraPlugins');
  643. }
  644. }