[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Web/UI/WebControls/ -> THtmlArea.php (source)

   1  <?php
   2  /**
   3   * THtmlArea class file.
   4   *
   5   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2006 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: THtmlArea.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Web.UI
  11   */
  12  
  13  /**
  14   * Includes TTextBox class
  15   */
  16  Prado::using('System.Web.UI.WebControls.TTextBox');
  17  
  18  /**
  19   * THtmlArea class
  20   *
  21   * THtmlArea wraps the visual editting functionalities provided by the
  22   * TinyMCE project {@link http://tinymce.moxiecode.com/}.
  23   *
  24   * THtmlArea displays a WYSIWYG text area on the Web page for user input
  25   * in the HTML format. The text displayed in the THtmlArea component is
  26   * specified or determined by using the <b>Text</b> property.
  27   *
  28   * To enable the visual editting on the client side, set the property
  29   * <b>EnableVisualEdit</b> to true (which is default value).
  30   * To set the size of the editor when the visual editting is enabled,
  31   * set the <b>Width</b> and <b>Height</b> properties instead of
  32   * <b>Columns</b> and <b>Rows</b> because the latter has no meaning
  33   * under the situation.
  34   *
  35   * The default editor gives only the basic tool bar. To change or add
  36   * additional tool bars, use the {@link setOptions Options} property to add additional
  37   * editor options with each options on a new line.
  38   * See http://tinymce.moxiecode.com/tinymce/docs/index.html
  39   * for a list of options. The options can be change/added as shown in the
  40   * following example.
  41   * <code>
  42   * <com:THtmlArea>
  43   *      <prop:Options>
  44   *           plugins : "contextmenu,paste"
  45   *           language : "zh_cn"
  46   *      </prop:Options>
  47   * </com:THtmlArea>
  48   * </code>
  49   *
  50   * Compatibility
  51   * The client-side visual editting capability is supported by
  52   * Internet Explorer 5.0+ for Windows and Gecko-based browser.
  53   * If the browser does not support the visual editting,
  54   * a traditional textarea will be displayed.
  55   *
  56   * Browser support
  57   *
  58   * <code>
  59   *                    Windows XP        MacOS X 10.4
  60   * ----------------------------------------------------
  61   * MSIE 6                  OK
  62   * MSIE 5.5 SP2            OK
  63   * MSIE 5.0                OK
  64   * Mozilla 1.7.x           OK              OK
  65   * Firefox 1.0.x           OK              OK
  66   * Firefox 1.5b2           OK              OK
  67   * Safari 2.0 (412)                        OK(1)
  68   * Opera 9 Preview 1       OK(1)           OK(1)
  69   * ----------------------------------------------------
  70   *    * (1) - Partialy working
  71   * ----------------------------------------------------
  72   * </code>
  73   *
  74   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  75   * @version $Id: THtmlArea.php 1397 2006-09-07 07:55:53Z wei $
  76   * @package System.Web.UI.WebControls
  77   * @since 3.0
  78   */
  79  class THtmlArea extends TTextBox
  80  {
  81      /**
  82       * @var array list of locale => language file pairs.
  83       */
  84      private static $_langs = array(
  85              'ar' => 'ar',
  86              'ca' => 'ca',
  87              'cs' => 'cs',
  88              'da' => 'da',
  89              'de' => 'de',
  90              'el' => 'el',
  91              'en' => 'en',
  92              'es' => 'es',
  93              'fa' => 'fa',
  94              'fi' => 'fi',
  95              'fr' => 'fr',
  96              'fr_CA' => 'fr_ca',
  97              'he' => 'he',
  98              'hu' => 'hu',
  99              'is' => 'is',
 100              'it' => 'it',
 101              'ja' => 'ja',
 102              'ko' => 'ko',
 103              'nb' => 'nb',
 104              'nl' => 'nl',
 105              'pl' => 'pl',
 106              'pt' => 'pt',
 107              'pt_BR' => 'pt_br',
 108              'ru' => 'ru_UTF-8',
 109              'si' => 'si',
 110              'sk' => 'sk',
 111              'sv' => 'sv',
 112              'th' => 'th',
 113              'tr' => 'tr',
 114              'vi' => 'vi',
 115              'zh' => 'zh_cn_utf8',
 116              'zh_CN' => 'zh_cn_utf8',
 117              'zh_HK' => 'zh_tw_utf8',
 118              'zh_TW' => 'zh_tw_utf8'
 119          );
 120  
 121      /**
 122       * Constructor.
 123       * Sets default width and height.
 124       */
 125  	public function __construct()
 126      {
 127          $this->setWidth('450px');
 128          $this->setHeight('250px');
 129      }
 130  
 131      /**
 132       * Overrides the parent implementation.
 133       * TextMode for THtmlArea control is always 'MultiLine'
 134       * @return string the behavior mode of the THtmlArea component.
 135       */
 136  	public function getTextMode()
 137      {
 138          return 'MultiLine';
 139      }
 140  
 141      /**
 142       * Overrides the parent implementation.
 143       * TextMode for THtmlArea is always 'MultiLine' and cannot be changed to others.
 144       * @param string the text mode
 145       */
 146  	public function setTextMode($value)
 147      {
 148          throw new TInvalidOperationException("htmlarea_textmode_readonly");
 149      }
 150  
 151      /**
 152       * @return boolean whether change of the content should cause postback. Return false if EnableVisualEdit is true.
 153       */
 154  	public function getAutoPostBack()
 155      {
 156          return $this->getEnableVisualEdit() ? false : parent::getAutoPostBack();
 157      }
 158  
 159      /**
 160       * @return boolean whether to show WYSIWYG text editor. Defaults to true.
 161       */
 162  	public function getEnableVisualEdit()
 163      {
 164          return $this->getViewState('EnableVisualEdit',true);
 165      }
 166  
 167      /**
 168       * Sets whether to show WYSIWYG text editor.
 169       * @param boolean whether to show WYSIWYG text editor
 170       */
 171  	public function setEnableVisualEdit($value)
 172      {
 173          $this->setViewState('EnableVisualEdit',TPropertyValue::ensureBoolean($value),true);
 174      }
 175  
 176      /**
 177       * Gets the current culture.
 178       * @return string current culture, e.g. en_AU.
 179       */
 180  	public function getCulture()
 181      {
 182          return $this->getViewState('Culture', '');
 183      }
 184  
 185      /**
 186       * Sets the culture/language for the html area
 187       * @param string a culture string, e.g. en_AU.
 188       */
 189  	public function setCulture($value)
 190      {
 191          $this->setViewState('Culture', $value, '');
 192      }
 193  
 194      /**
 195       * Gets the list of options for the WYSIWYG (TinyMCE) editor
 196       * @see http://tinymce.moxiecode.com/tinymce/docs/index.html
 197       * @return string options
 198       */
 199  	public function getOptions()
 200      {
 201          return $this->getViewState('Options', '');
 202      }
 203  
 204      /**
 205       * Sets the list of options for the WYSIWYG (TinyMCE) editor
 206       * @see http://tinymce.moxiecode.com/tinymce/docs/index.html
 207       * @param string options
 208       */
 209  	public function setOptions($value)
 210      {
 211          $this->setViewState('Options', $value, '');
 212      }
 213  
 214      /**
 215       * Adds attribute name-value pairs to renderer.
 216       * This method overrides the parent implementation by registering
 217       * additional javacript code.
 218       * @param THtmlWriter the writer used for the rendering purpose
 219       */
 220  	protected function addAttributesToRender($writer)
 221      {
 222          if($this->getEnableVisualEdit() && $this->getEnabled(true))
 223          {
 224              $writer->addAttribute('id',$this->getClientID());
 225              $this->registerEditorClientScript($writer);
 226          }
 227          parent::addAttributesToRender($writer);
 228      }
 229  
 230      /**
 231       * Registers the editor javascript file and code to initialize the editor.
 232       */
 233  	protected function registerEditorClientScript($writer)
 234      {
 235          $scripts = $this->getPage()->getClientScript();
 236          if(!$scripts->isScriptFileRegistered('prado:THtmlArea'))
 237              $scripts->registerScriptFile('prado:THtmlArea', $this->getScriptUrl());
 238          $options = TJavaScript::encode($this->getEditorOptions());
 239          $script = "if(tinyMCE){ tinyMCE.init($options); }";
 240          $scripts->registerEndScript('prado:THtmlArea'.$this->ClientID,$script);
 241      }
 242  
 243      /**
 244       * @return string editor script URL.
 245       */
 246  	protected function getScriptUrl()
 247      {
 248          return $this->getScriptDeploymentPath().'/tiny_mce/tiny_mce_gzip.php';
 249      }
 250  
 251      /**
 252       * Gets the editor script base URL by publishing the tarred source via TTarAssetManager.
 253       * @return string URL base path to the published editor script
 254       */
 255  	protected function getScriptDeploymentPath()
 256      {
 257          $tarfile = Prado::getPathOfNamespace('System.3rdParty.TinyMCE.tiny_mce', '.tar');
 258          $md5sum = Prado::getPathOfNamespace('System.3rdParty.TinyMCE.tiny_mce', '.md5');
 259          if($tarfile===null || $md5sum===null)
 260              throw new TConfigurationException('htmlarea_tarfile_invalid');
 261          return $this->getApplication()->getAssetManager()->publishTarFile($tarfile, $md5sum);
 262      }
 263  
 264      /**
 265       * Default editor options gives basic tool bar only.
 266       * @return array editor initialization options.
 267       */
 268  	protected function getEditorOptions()
 269      {
 270          $options['mode'] = 'exact';
 271          $options['elements'] = $this->getClientID();
 272          $options['language'] = $this->getLanguageSuffix($this->getCulture());
 273          $options['theme'] = 'advanced';
 274  
 275          //make it basic advanced to fit into 1 line of buttons.
 276          //$options['theme_advanced_buttons1'] = 'bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright, justifyfull,separator,bullist,numlist,separator,undo,redo,separator,link,unlink,separator,charmap,separator,code,help';
 277          //$options['theme_advanced_buttons2'] = ' ';
 278          $options['theme_advanced_buttons1'] = 'formatselect,fontselect,fontsizeselect,separator,bold,italic,underline,strikethrough,sub,sup';
 279          $options['theme_advanced_buttons2'] = 'justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,separator,outdent,indent,separator,forecolor,backcolor,separator,hr,link,unlink,image,charmap,separator,removeformat,code,help';
 280          $options['theme_advanced_buttons3'] = ' ';
 281  
 282          $options['theme_advanced_toolbar_location'] = 'top';
 283          $options['theme_advanced_toolbar_align'] = 'left';
 284          $options['theme_advanced_path_location'] = 'bottom';
 285          $options['extended_valid_elements'] = 'a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]';
 286  
 287          $options = array_merge($options, $this->parseEditorOptions($this->getOptions()));
 288          return $options;
 289      }
 290  
 291      /**
 292       * Parse additional options set in the Options property.
 293       * @return array additional custom options
 294       */
 295  	protected function parseEditorOptions($string)
 296      {
 297          $options = array();
 298          $substrings = preg_split('/\n|,\n/', trim($string));
 299          foreach($substrings as $bits)
 300          {
 301              $option = explode(":",$bits);
 302              if(count($option) == 2)
 303                  $options[trim($option[0])] = trim(preg_replace('/\'|"/','',  $option[1]));
 304          }
 305          return $options;
 306      }
 307  
 308      /**
 309       * @return string localized editor interface language extension.
 310       */
 311  	protected function getLanguageSuffix($culture)
 312      {
 313          $app = $this->getApplication()->getGlobalization();
 314          if(empty($culture) && !is_null($app))
 315              $culture = $app->getCulture();
 316          $variants = array();
 317          if(!is_null($app))
 318              $variants = $app->getCultureVariants($culture);
 319  
 320          foreach($variants as $variant)
 321          {
 322              if(isset(self::$_langs[$variant]))
 323                  return self::$_langs[$variant];
 324          }
 325  
 326          return 'en';
 327      }
 328  }
 329  
 330  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7