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