File indexing completed on 2025-03-02 05:29:19

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Dojo
0017  * @subpackage Form_Element
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Dojo_Form_Element_Dijit */
0024 // require_once 'Zend/Dojo/Form/Element/Dijit.php';
0025 
0026 /**
0027  * Editor dijit
0028  *
0029  * @uses       Zend_Dojo_Form_Element_Dijit
0030  * @category   Zend
0031  * @package    Zend_Dojo
0032  * @subpackage Form_Element
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Dojo_Form_Element_Editor extends Zend_Dojo_Form_Element_Dijit
0037 {
0038     /**
0039      * @var string View helper
0040      */
0041     public $helper = 'Editor';
0042 
0043     /**
0044      * Add a single event to connect to the editing area
0045      *
0046      * @param  string $event
0047      * @return Zend_Dojo_Form_Element_Editor
0048      */
0049     public function addCaptureEvent($event)
0050     {
0051         $event = (string) $event;
0052         $captureEvents = $this->getCaptureEvents();
0053         if (in_array($event, $captureEvents)) {
0054             return $this;
0055         }
0056 
0057         $captureEvents[] = (string) $event;
0058         $this->setDijitParam('captureEvents', $captureEvents);
0059         return $this;
0060     }
0061 
0062     /**
0063      * Add multiple capture events
0064      *
0065      * @param  array $events
0066      * @return Zend_Dojo_Form_Element_Editor
0067      */
0068     public function addCaptureEvents(array $events)
0069     {
0070         foreach ($events as $event) {
0071             $this->addCaptureEvent($event);
0072         }
0073         return $this;
0074     }
0075 
0076     /**
0077      * Overwrite many capture events at once
0078      *
0079      * @param  array $events
0080      * @return Zend_Dojo_Form_Element_Editor
0081      */
0082     public function setCaptureEvents(array $events)
0083     {
0084         $this->clearCaptureEvents();
0085         $this->addCaptureEvents($events);
0086         return $this;
0087     }
0088 
0089     /**
0090      * Get all capture events
0091      *
0092      * @return array
0093      */
0094     public function getCaptureEvents()
0095     {
0096         if (!$this->hasDijitParam('captureEvents')) {
0097             return array();
0098         }
0099         return $this->getDijitParam('captureEvents');
0100     }
0101 
0102     /**
0103      * Is a given capture event registered?
0104      *
0105      * @param  string $event
0106      * @return bool
0107      */
0108     public function hasCaptureEvent($event)
0109     {
0110         $captureEvents = $this->getCaptureEvents();
0111         return in_array((string) $event, $captureEvents);
0112     }
0113 
0114     /**
0115      * Remove a given capture event
0116      *
0117      * @param  string $event
0118      * @return Zend_Dojo_Form_Element_Editor
0119      */
0120     public function removeCaptureEvent($event)
0121     {
0122         $event = (string) $event;
0123         $captureEvents = $this->getCaptureEvents();
0124         if (false === ($index = array_search($event, $captureEvents))) {
0125             return $this;
0126         }
0127         unset($captureEvents[$index]);
0128         $this->setDijitParam('captureEvents', $captureEvents);
0129         return $this;
0130     }
0131 
0132     /**
0133      * Clear all capture events
0134      *
0135      * @return Zend_Dojo_Form_Element_Editor
0136      */
0137     public function clearCaptureEvents()
0138     {
0139         return $this->removeDijitParam('captureEvents');
0140     }
0141 
0142     /**
0143      * Add a single event to the dijit
0144      *
0145      * @param  string $event
0146      * @return Zend_Dojo_Form_Element_Editor
0147      */
0148     public function addEvent($event)
0149     {
0150         $event = (string) $event;
0151         $events = $this->getEvents();
0152         if (in_array($event, $events)) {
0153             return $this;
0154         }
0155 
0156         $events[] = (string) $event;
0157         $this->setDijitParam('events', $events);
0158         return $this;
0159     }
0160 
0161     /**
0162      * Add multiple events
0163      *
0164      * @param  array $events
0165      * @return Zend_Dojo_Form_Element_Editor
0166      */
0167     public function addEvents(array $events)
0168     {
0169         foreach ($events as $event) {
0170             $this->addEvent($event);
0171         }
0172         return $this;
0173     }
0174 
0175     /**
0176      * Overwrite many events at once
0177      *
0178      * @param  array $events
0179      * @return Zend_Dojo_Form_Element_Editor
0180      */
0181     public function setEvents(array $events)
0182     {
0183         $this->clearEvents();
0184         $this->addEvents($events);
0185         return $this;
0186     }
0187 
0188     /**
0189      * Get all events
0190      *
0191      * @return array
0192      */
0193     public function getEvents()
0194     {
0195         if (!$this->hasDijitParam('events')) {
0196             return array();
0197         }
0198         return $this->getDijitParam('events');
0199     }
0200 
0201     /**
0202      * Is a given event registered?
0203      *
0204      * @param  string $event
0205      * @return bool
0206      */
0207     public function hasEvent($event)
0208     {
0209         $events = $this->getEvents();
0210         return in_array((string) $event, $events);
0211     }
0212 
0213     /**
0214      * Remove a given event
0215      *
0216      * @param  string $event
0217      * @return Zend_Dojo_Form_Element_Editor
0218      */
0219     public function removeEvent($event)
0220     {
0221         $events = $this->getEvents();
0222         if (false === ($index = array_search($event, $events))) {
0223             return $this;
0224         }
0225         unset($events[$index]);
0226         $this->setDijitParam('events', $events);
0227         return $this;
0228     }
0229 
0230     /**
0231      * Clear all events
0232      *
0233      * @return Zend_Dojo_Form_Element_Editor
0234      */
0235     public function clearEvents()
0236     {
0237         return $this->removeDijitParam('events');
0238     }
0239 
0240     /**
0241      * Add a single editor plugin
0242      *
0243      * @param  string $plugin
0244      * @return Zend_Dojo_Form_Element_Editor
0245      */
0246     public function addPlugin($plugin)
0247     {
0248         $plugin = (string) $plugin;
0249         $plugins = $this->getPlugins();
0250         if (in_array($plugin, $plugins) && $plugin !== '|') {
0251             return $this;
0252         }
0253 
0254         $plugins[] = (string) $plugin;
0255         $this->setDijitParam('plugins', $plugins);
0256         return $this;
0257     }
0258 
0259     /**
0260      * Add multiple plugins
0261      *
0262      * @param  array $plugins
0263      * @return Zend_Dojo_Form_Element_Editor
0264      */
0265     public function addPlugins(array $plugins)
0266     {
0267         foreach ($plugins as $plugin) {
0268             $this->addPlugin($plugin);
0269         }
0270         return $this;
0271     }
0272 
0273     /**
0274      * Overwrite many plugins at once
0275      *
0276      * @param  array $plugins
0277      * @return Zend_Dojo_Form_Element_Editor
0278      */
0279     public function setPlugins(array $plugins)
0280     {
0281         $this->clearPlugins();
0282         $this->addPlugins($plugins);
0283         return $this;
0284     }
0285 
0286     /**
0287      * Get all plugins
0288      *
0289      * @return array
0290      */
0291     public function getPlugins()
0292     {
0293         if (!$this->hasDijitParam('plugins')) {
0294             return array();
0295         }
0296         return $this->getDijitParam('plugins');
0297     }
0298 
0299     /**
0300      * Is a given plugin registered?
0301      *
0302      * @param  string $plugin
0303      * @return bool
0304      */
0305     public function hasPlugin($plugin)
0306     {
0307         $plugins = $this->getPlugins();
0308         return in_array((string) $plugin, $plugins);
0309     }
0310 
0311     /**
0312      * Remove a given plugin
0313      *
0314      * @param  string $plugin
0315      * @return Zend_Dojo_Form_Element_Editor
0316      */
0317     public function removePlugin($plugin)
0318     {
0319         $plugins = $this->getPlugins();
0320         if (false === ($index = array_search($plugin, $plugins))) {
0321             return $this;
0322         }
0323         unset($plugins[$index]);
0324         $this->setDijitParam('plugins', $plugins);
0325         return $this;
0326     }
0327 
0328     /**
0329      * Clear all plugins
0330      *
0331      * @return Zend_Dojo_Form_Element_Editor
0332      */
0333     public function clearPlugins()
0334     {
0335         return $this->removeDijitParam('plugins');
0336     }
0337 
0338     /**
0339      * Set edit action interval
0340      *
0341      * @param  int $interval
0342      * @return Zend_Dojo_Form_Element_Editor
0343      */
0344     public function setEditActionInterval($interval)
0345     {
0346         return $this->setDijitParam('editActionInterval', (int) $interval);
0347     }
0348 
0349     /**
0350      * Get edit action interval; defaults to 3
0351      *
0352      * @return int
0353      */
0354     public function getEditActionInterval()
0355     {
0356         if (!$this->hasDijitParam('editActionInterval')) {
0357             $this->setEditActionInterval(3);
0358         }
0359         return $this->getDijitParam('editActionInterval');
0360     }
0361 
0362     /**
0363      * Set focus on load flag
0364      *
0365      * @param  bool $flag
0366      * @return Zend_Dojo_Form_Element_Editor
0367      */
0368     public function setFocusOnLoad($flag)
0369     {
0370         return $this->setDijitParam('focusOnLoad', (bool) $flag);
0371     }
0372 
0373     /**
0374      * Retrieve focus on load flag
0375      *
0376      * @return bool
0377      */
0378     public function getFocusOnLoad()
0379     {
0380         if (!$this->hasDijitParam('focusOnLoad')) {
0381              return false;
0382         }
0383         return $this->getDijitParam('focusOnLoad');
0384     }
0385 
0386     /**
0387      * Set editor height
0388      *
0389      * @param  string|int $height
0390      * @return Zend_Dojo_Form_Element_Editor
0391      */
0392     public function setHeight($height)
0393     {
0394         if (!preg_match('/^\d+(em|px|%)?$/i', $height)) {
0395             // require_once 'Zend/Form/Element/Exception.php';
0396             throw new Zend_Form_Element_Exception('Invalid height provided; must be integer or CSS measurement');
0397         }
0398         if (!preg_match('/(em|px|%)$/', $height)) {
0399             $height .= 'px';
0400         }
0401         return $this->setDijitParam('height', $height);
0402     }
0403 
0404     /**
0405      * Retrieve height
0406      *
0407      * @return string
0408      */
0409     public function getHeight()
0410     {
0411         if (!$this->hasDijitParam('height')) {
0412             return '300px';
0413         }
0414         return $this->getDijitParam('height');
0415     }
0416 
0417     /**
0418      * Set whether or not to inherit parent's width
0419      *
0420      * @param  bool $flag
0421      * @return Zend_Dojo_Form_Element_Editor
0422      */
0423     public function setInheritWidth($flag)
0424     {
0425         return $this->setDijitParam('inheritWidth', (bool) $flag);
0426     }
0427 
0428     /**
0429      * Whether or not to inherit the parent's width
0430      *
0431      * @return bool
0432      */
0433     public function getInheritWidth()
0434     {
0435         if (!$this->hasDijitParam('inheritWidth')) {
0436             return false;
0437         }
0438         return $this->getDijitParam('inheritWidth');
0439     }
0440 
0441     /**
0442      * Set minimum height of editor
0443      *
0444      * @param  string|int $minHeight
0445      * @return Zend_Dojo_Form_Element_Editor
0446      */
0447     public function setMinHeight($minHeight)
0448     {
0449         if (!preg_match('/^\d+(em|px|%)?$/i', $minHeight)) {
0450             // require_once 'Zend/Form/Element/Exception.php';
0451             throw new Zend_Form_Element_Exception('Invalid minHeight provided; must be integer or CSS measurement');
0452         }
0453         if (!preg_match('/(em|px|%)$/', $minHeight)) {
0454             $minHeight .= 'em';
0455         }
0456         return $this->setDijitParam('minHeight', $minHeight);
0457     }
0458 
0459     /**
0460      * Get minimum height of editor
0461      *
0462      * @return string
0463      */
0464     public function getMinHeight()
0465     {
0466         if (!$this->hasDijitParam('minHeight')) {
0467             return '1em';
0468         }
0469         return $this->getDijitParam('minHeight');
0470     }
0471 
0472     /**
0473      * Add a custom stylesheet
0474      *
0475      * @param  string $styleSheet
0476      * @return Zend_Dojo_Form_Element_Editor
0477      */
0478     public function addStyleSheet($styleSheet)
0479     {
0480         $stylesheets = $this->getStyleSheets();
0481         if (strstr($stylesheets, ';')) {
0482             $stylesheets = explode(';', $stylesheets);
0483         } elseif (!empty($stylesheets)) {
0484             $stylesheets = (array) $stylesheets;
0485         } else {
0486             $stylesheets = array();
0487         }
0488         if (!in_array($styleSheet, $stylesheets)) {
0489             $stylesheets[] = (string) $styleSheet;
0490         }
0491         return $this->setDijitParam('styleSheets', implode(';', $stylesheets));
0492     }
0493 
0494     /**
0495      * Add multiple custom stylesheets
0496      *
0497      * @param  array $styleSheets
0498      * @return Zend_Dojo_Form_Element_Editor
0499      */
0500     public function addStyleSheets(array $styleSheets)
0501     {
0502         foreach ($styleSheets as $styleSheet) {
0503             $this->addStyleSheet($styleSheet);
0504         }
0505         return $this;
0506     }
0507 
0508     /**
0509      * Overwrite all stylesheets
0510      *
0511      * @param  array $styleSheets
0512      * @return Zend_Dojo_Form_Element_Editor
0513      */
0514     public function setStyleSheets(array $styleSheets)
0515     {
0516         $this->clearStyleSheets();
0517         return $this->addStyleSheets($styleSheets);
0518     }
0519 
0520     /**
0521      * Get all stylesheets
0522      *
0523      * @return string
0524      */
0525     public function getStyleSheets()
0526     {
0527         if (!$this->hasDijitParam('styleSheets')) {
0528             return '';
0529         }
0530         return $this->getDijitParam('styleSheets');
0531     }
0532 
0533     /**
0534      * Is a given stylesheet registered?
0535      *
0536      * @param  string $styleSheet
0537      * @return bool
0538      */
0539     public function hasStyleSheet($styleSheet)
0540     {
0541         $styleSheets = $this->getStyleSheets();
0542         $styleSheets = explode(';', $styleSheets);
0543         return in_array($styleSheet, $styleSheets);
0544     }
0545 
0546     /**
0547      * Remove a single stylesheet
0548      *
0549      * @param  string $styleSheet
0550      * @return Zend_Dojo_Form_Element_Editor
0551      */
0552     public function removeStyleSheet($styleSheet)
0553     {
0554         $styleSheets = $this->getStyleSheets();
0555         $styleSheets = explode(';', $styleSheets);
0556         if (false !== ($index = array_search($styleSheet, $styleSheets))) {
0557             unset($styleSheets[$index]);
0558             $this->setDijitParam('styleSheets', implode(';', $styleSheets));
0559         }
0560         return $this;
0561     }
0562 
0563     /**
0564      * Clear all stylesheets
0565      *
0566      * @return Zend_Dojo_Form_Element_Editor
0567      */
0568     public function clearStyleSheets()
0569     {
0570         if ($this->hasDijitParam('styleSheets')) {
0571             $this->removeDijitParam('styleSheets');
0572         }
0573         return $this;
0574     }
0575 
0576     /**
0577      * Set update interval
0578      *
0579      * @param  int $interval
0580      * @return Zend_Dojo_Form_Element_Editor
0581      */
0582     public function setUpdateInterval($interval)
0583     {
0584         return $this->setDijitParam('updateInterval', (int) $interval);
0585     }
0586 
0587     /**
0588      * Get update interval
0589      *
0590      * @return int
0591      */
0592     public function getUpdateInterval()
0593     {
0594         if (!$this->hasDijitParam('updateInterval')) {
0595              return 200;
0596         }
0597         return $this->getDijitParam('updateInterval');
0598     }
0599 
0600     /**
0601      * Add a single editor extra plugin.
0602      *
0603      * @param  string $plugin
0604      * @return Zend_Dojo_Form_Element_Editor
0605      */
0606     public function addExtraPlugin($plugin)
0607     {
0608         $plugin = (string) $plugin;
0609         $extraPlugins = $this->getExtraPlugins();
0610         if (in_array($plugin, $extraPlugins)) {
0611             return $this;
0612         }
0613     
0614         $extraPlugins[] = (string) $plugin;
0615         $this->setDijitParam('extraPlugins', $extraPlugins);
0616         return $this;
0617     }
0618     
0619     /**
0620      * Add multiple extra plugins.
0621      *
0622      * @param  array $extraPlugins
0623      * @return Zend_Dojo_Form_Element_Editor
0624      */
0625     public function addExtraPlugins(array $plugins)
0626     {
0627         foreach ($plugins as $plugin) {
0628             $this->addExtraPlugin($plugin);
0629         }
0630         return $this;
0631     }
0632     
0633     /**
0634      * Overwrite many extra plugins at once.
0635      *
0636      * @param  array $plugins
0637      * @return Zend_Dojo_Form_Element_Editor
0638      */
0639     public function setExtraPlugins(array $plugins)
0640     {
0641         $this->clearExtraPlugins();
0642         $this->addExtraPlugins($plugins);
0643         return $this;
0644     }
0645     
0646     /**
0647      * Get all extra plugins.
0648      *
0649      * @return array
0650      */
0651     public function getExtraPlugins()
0652     {
0653         if (!$this->hasDijitParam('extraPlugins')) {
0654             return array();
0655         }
0656         return $this->getDijitParam('extraPlugins');
0657     }
0658     
0659     /**
0660      * Is a given extra plugin registered?
0661      *
0662      * @param  string $plugin
0663      * @return bool
0664      */
0665     public function hasExtraPlugin($plugin)
0666     {
0667         $extraPlugins = $this->getExtraPlugins();
0668         return in_array((string) $plugin, $extraPlugins);
0669     }
0670     
0671     /**
0672      * Remove a given extra plugin.
0673      *
0674      * @param  string $plugin
0675      * @return Zend_Dojo_Form_Element_Editor
0676      */
0677     public function removeExtraPlugin($plugin)
0678     {
0679         $extraPlugins = $this->getExtraPlugins();
0680         if (false === ($index = array_search($plugin, $extraPlugins))) {
0681             return $this;
0682         }
0683         unset($extraPlugins[$index]);
0684         $this->setDijitParam('extraPlugins', $extraPlugins);
0685         return $this;
0686     }
0687     
0688     /**
0689      * Clear all extra plugins.
0690      *
0691      * @return Zend_Dojo_Form_Element_Editor
0692      */
0693     public function clearExtraPlugins()
0694     {
0695         return $this->removeDijitParam('extraPlugins');
0696     }
0697 }