XOOPS  2.6.0
DhtmlTextArea.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Form;
13 
27 class DhtmlTextArea extends TextArea
28 {
55  public $htmlEditor = array();
56 
62  private $hiddenText;
63 
67  public $skipPreview = false;
68 
72  public $doHtml = false;
73 
77  public $js = '';
78 
82  public $configs = array();
83 
95  public function __construct(
96  $caption,
97  $name,
98  $value = "",
99  $rows = 5,
100  $cols = 50,
101  $hiddentext = "xoopsHiddenText",
102  $options = array()
103  ) {
104  static $inLoop = 0;
105 
106  ++$inLoop;
107  // Second loop, invalid, return directly
108  if ($inLoop > 2) {
109  return;
110  }
111 
112  // Else, initialize
113  parent::__construct($caption, $name, $value, $rows, $cols);
114  $this->hiddenText = $hiddentext;
115 
116  if ($inLoop > 1) {
117  return;
118  }
119 
121  if (!isset($options['editor'])) {
122  if ($editor = $xoops->getConfig('editor')) {
123  $options['editor'] = $editor;
124  }
125  }
126 
127  if (!empty($this->htmlEditor) || !empty($options['editor'])) {
128  $options['name'] = $this->getName();
129  $options['value'] = $this->getValue();
130  if (!empty($options['editor'])) {
131  $this->htmlEditor = is_array($options['editor']) ? $options['editor'] : array($options['editor']);
132  }
133 
134  if (count($this->htmlEditor) == 1) {
135  $editor_handler = \XoopsEditorHandler::getInstance();
136  $this->htmlEditor = $editor_handler->get($this->htmlEditor[0], $options);
137  if ($inLoop > 1) {
138  $this->htmlEditor = null;
139  }
140  } else {
141  list ($class, $path) = $this->htmlEditor;
142  include_once \XoopsBaseConfig::get('root-path') . $path;
143  if (class_exists($class)) {
144  $this->htmlEditor = new $class($options);
145  }
146  if ($inLoop > 1) {
147  $this->htmlEditor = null;
148  }
149  }
150  }
151 
152  $inLoop = 0;
153  }
154 
160  public function render()
161  {
162  if ($this->htmlEditor && is_object($this->htmlEditor)) {
163  if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
164  return $this->htmlEditor->render();
165  }
166  }
167  static $js_loaded;
168 
170  if ($this->getCols() > $this->getMaxcols()) {
171  $maxcols = 5;
172  } else {
173  $maxcols = $this->getCols();
174  }
175  $class = ($this->getClass() != '' ? " class='span" . $maxcols . " " . $this->getClass() . "'" : " class='span" . $maxcols . "'");
176  $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
177  $required = ($this->isRequired() ? ' required' : '');
178  $ret = "";
179  // actions
180  $ret .= $this->codeIcon() . "<br />\n";
181  // fonts
182  $ret .= $this->fontArray();
183  // length checker
184  $ret .= "<input type='button' class='btn' onclick=\"XoopsCheckLength('" . $this->getName() . "', '" . @$this->configs['maxlength'] . "', '" . \XoopsLocale::F_CURRENT_TEXT_LENGTH . "', '" . \XoopsLocale::MAXIMUM_LENGTH . "');\" value=' ? ' title='" . \XoopsLocale::CHECK_TEXT_LENGTH . "' />";
185  $ret .= "<br />\n";
186  // the textarea box
187  $ret .= "<textarea" . $class . " id='" . $this->getName() . "' name='" . $this->getName() . "' title='" . $this->getTitle() . "' onselect=\"xoopsSavePosition('" . $this->getName() . "');\" onclick=\"xoopsSavePosition('" . $this->getName() . "');\" onkeyup=\"xoopsSavePosition('" . $this->getName() . "');\" rows='" . $this->getRows() . "'" . $extra . $required . ">" . $this->getValue() . "</textarea><br />\n";
188 
189  if (empty($this->skipPreview)) {
190  if (!$xoops->theme()) {
191  $this->js .= implode("", file(\XoopsBaseConfig::get('root-path') . "/class/textsanitizer/image/image.js"));
192  } else {
193  $xoops->theme()->addScript('/class/textsanitizer/image/image.js', array('type' => 'text/javascript'));
194  }
195  $button = "<input id='" . $this->getName() . "_preview_button' " . "type='button' " . "class='btn' value='" . \XoopsLocale::A_PREVIEW . "' " . "onclick=\"form_instantPreview('" . XOOPS_URL . "', '" . $this->getName() . "','" . XOOPS_URL . "/images', " . intval($this->doHtml) . ", '" . $xoops->security()->createToken() . "')\"" . " />";
196  $ret .= "<br />" . "<div id='" . $this->getName() . "_hidden' style='display: block;'> " . "<fieldset>" . "<legend>" . $button . "</legend>" . "<div id='" . $this->getName() . "_hidden_data'>" . \XoopsLocale::CLICK_PREVIEW_TO_SEE_CONTENT . "</div>" . "</fieldset>" . "</div>";
197  }
198  // Load javascript
199  if (empty($js_loaded)) {
201  ? '<script type="text/javascript">' . $this->js . '</script>'
202  : '') . '<script type="text/javascript" src="' . \XoopsBaseConfig::get('url') . '/include/formdhtmltextarea.js"></script>';
203  $ret = $javascript . $ret;
204  $js_loaded = true;
205  }
206  return $ret;
207  }
208 
214  public function codeIcon()
215  {
216  $textarea_id = $this->getName();
217  $code = "<a name='moresmiley'></a>";
218  $code .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/url.gif' alt='" . \XoopsLocale::URL . "' title='" . \XoopsLocale::URL . "' onclick='xoopsCodeUrl(\"{$textarea_id}\", \"" . htmlspecialchars(\XoopsLocale::ENTER_LINK_URL, ENT_QUOTES) . "\", \"" . htmlspecialchars(\XoopsLocale::ENTER_WEBSITE_TITLE, ENT_QUOTES) . "\");' onmouseover='style.cursor=\"hand\"'/>&nbsp;";
219  $code .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/email.gif' alt='" . \XoopsLocale::EMAIL . "' title='" . \XoopsLocale::EMAIL . "' onclick='xoopsCodeEmail(\"{$textarea_id}\", \"" . htmlspecialchars(\XoopsLocale::ENTER_EMAIL, ENT_QUOTES) . "\");' onmouseover='style.cursor=\"hand\"'/>&nbsp;";
220  $code .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/imgsrc.gif' alt='" . \XoopsLocale::IMAGES . "' title='" . \XoopsLocale::IMAGES . "' onclick='xoopsCodeImg(\"{$textarea_id}\", \"" . htmlspecialchars(\XoopsLocale::ENTER_IMAGE_URL, ENT_QUOTES) . "\", \"" . htmlspecialchars(\XoopsLocale::ENTER_IMAGE_POSITION, ENT_QUOTES) . "\", \"" . htmlspecialchars(\XoopsLocale::IMAGE_POSITION_DESCRIPTION, ENT_QUOTES) . "\", \"" . htmlspecialchars(\XoopsLocale::E_ENTER_IMAGE_POSITION, ENT_QUOTES) . "\", \"" . htmlspecialchars(\XoopsLocale::WIDTH, ENT_QUOTES) . "\");' onmouseover='style.cursor=\"hand\"'/>&nbsp;";
221 
223  $extensions = array_filter($myts->config['extensions']);
224  foreach (array_keys($extensions) as $key) {
225  $extension = $myts->loadExtension($key);
226  @list ($encode, $js) = $extension->encode($textarea_id);
227  if (empty($encode)) {
228  continue;
229  }
230  $code .= $encode;
231  if (!empty($js)) {
232  $this->js .= $js;
233  }
234  }
235  $code .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/code.gif' alt='" . \XoopsLocale::SOURCE_CODE . "' title='" . \XoopsLocale::SOURCE_CODE . "' onclick='xoopsCodeCode(\"{$textarea_id}\", \"" . htmlspecialchars(\XoopsLocale::ENTER_CODE, ENT_QUOTES) . "\");' onmouseover='style.cursor=\"hand\"'/>&nbsp;";
236  $code .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/quote.gif' alt='" . \XoopsLocale::QUOTE . "' title='" . \XoopsLocale::QUOTE . "' onclick='xoopsCodeQuote(\"{$textarea_id}\", \"" . htmlspecialchars(\XoopsLocale::ENTER_QUOTE, ENT_QUOTES) . "\");' onmouseover='style.cursor=\"hand\"'/>&nbsp;";
237 
238  \Xoops::getInstance()->events()->triggerEvent('core.class.xoopsform.formdhtmltextarea.codeicon', array(&$code, $this));
239  return $code;
240  }
241 
247  public function fontArray()
248  {
249  $textarea_id = $this->getName();
250  $hiddentext = $this->hiddenText;
251 
252  $fontStr = "<script type=\"text/javascript\" language=\"JavaScript\">";
253  $fontStr .= "var _editor_dialog = ''" . "+ '<select class=\"span2\" id=\'{$textarea_id}Size\' onchange=\'xoopsSetElementAttribute(\"size\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
254  $fontStr .= "+ '<option value=\'SIZE\'>" . \XoopsLocale::SIZE . "</option>'";
255  $localeFontSizes = \XoopsLocale::getFontSizes();
256  foreach ($localeFontSizes as $_val => $_name) {
257  $fontStr .= " + '<option value=\'{$_val}\'>{$_name}</option>'";
258  }
259  $fontStr .= " + '</select> '";
260  $fontStr .= "+ '<select class=\"span2\" id=\'{$textarea_id}Font\' onchange=\'xoopsSetElementAttribute(\"font\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
261  $fontStr .= "+ '<option value=\'FONT\'>" . \XoopsLocale::FONT . "</option>'";
262  $localeFonts = \XoopsLocale::getFonts();
263  $fontarray = !empty($localeFonts) ? $localeFonts :
264  array("Arial", "Courier", "Georgia", "Helvetica", "Impact", "Verdana", "Haettenschweiler");
265  foreach ($fontarray as $font) {
266  $fontStr .= " + '<option value=\'{$font}\'>{$font}</option>'";
267  }
268  $fontStr .= " + '</select> '";
269  $fontStr .= "+ '<select class=\"span2\" id=\'{$textarea_id}Color\' onchange=\'xoopsSetElementAttribute(\"color\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
270  $fontStr .= "+ '<option value=\'COLOR\'>" . \XoopsLocale::COLOR . "</option>';";
271  $fontStr .= "var _color_array = new Array('00', '33', '66', '99', 'CC', 'FF');
272  for(var i = 0; i < _color_array.length; i ++) {
273  for(var j = 0; j < _color_array.length; j ++) {
274  for(var k = 0; k < _color_array.length; k ++) {
275  var _color_ele = _color_array[i] + _color_array[j] + _color_array[k];
276  _editor_dialog += '<option value=\''+_color_ele+'\' style=\'background-color:#'+_color_ele+';color:#'+_color_ele+';\'>#'+_color_ele+'</option>';
277  }
278  }
279  }
280  _editor_dialog += '</select>';";
281 
282  $fontStr .= "document.write(_editor_dialog); </script>";
283 
284  $styleStr = "<img src='" . \XoopsBaseConfig::get('url') . "/images/bold.gif' alt='" . \XoopsLocale::BOLD . "' title='" . \XoopsLocale::BOLD . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeBold(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
285  $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/italic.gif' alt='" . \XoopsLocale::ITALIC . "' title='" . \XoopsLocale::ITALIC . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeItalic(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
286  $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/underline.gif' alt='" . \XoopsLocale::UNDERLINE . "' title='" . \XoopsLocale::UNDERLINE . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeUnderline(\"{$hiddentext}\", \"{$textarea_id}\");'/>&nbsp;";
287  $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/linethrough.gif' alt='" . \XoopsLocale::LINE_THROUGH . "' title='" . \XoopsLocale::LINE_THROUGH . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeLineThrough(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
288 
289  $alignStr = "<img src='" . \XoopsBaseConfig::get('url') . "/images/alignleft.gif' alt='" . \XoopsLocale::LEFT . "' title='" . \XoopsLocale::LEFT . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeLeft(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
290  $alignStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/aligncenter.gif' alt='" . \XoopsLocale::CENTER . "' title='" . \XoopsLocale::CENTER . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeCenter(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
291  $alignStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/alignright.gif' alt='" . \XoopsLocale::RIGHT . "' title='" . \XoopsLocale::RIGHT . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeRight(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
292  $fontStr = $fontStr . "<br />\n{$styleStr}&nbsp;{$alignStr}&nbsp;\n";
293  return $fontStr;
294  }
295 
301  public function renderValidationJS()
302  {
303  if ($this->htmlEditor && is_object($this->htmlEditor) && method_exists($this->htmlEditor, 'renderValidationJS')) {
304  if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
305  return $this->htmlEditor->renderValidationJS();
306  }
307  }
308  return parent::renderValidationJS();
309  }
310 }
$path
Definition: execute.php:31
const ENTER_LINK_URL
Definition: en_US.php:293
const F_CURRENT_TEXT_LENGTH
Definition: en_US.php:404
const ITALIC
Definition: en_US.php:487
const ENTER_CODE
Definition: en_US.php:289
const ENTER_QUOTE
Definition: en_US.php:294
const CHECK_TEXT_LENGTH
Definition: en_US.php:158
static getInstance()
Definition: Xoops.php:160
getValue($encode=false)
Definition: Element.php:180
const ENTER_WEBSITE_TITLE
Definition: en_US.php:297
$options['editor']
const E_ENTER_IMAGE_POSITION
Definition: en_US.php:324
const ENTER_IMAGE_URL
Definition: en_US.php:292
const IMAGES
Definition: en_US.php:471
$button
Definition: xoops_code.php:29
$xoops
Definition: admin.php:25
const SOURCE_CODE
Definition: en_US.php:1014
getExtra($encode=false)
Definition: Element.php:539
const ENTER_EMAIL
Definition: en_US.php:290
static get($name)
__construct($caption, $name, $value="", $rows=5, $cols=50, $hiddentext="xoopsHiddenText", $options=array())
const A_PREVIEW
Definition: en_US.php:109
const MAXIMUM_LENGTH
Definition: en_US.php:827
const ENTER_IMAGE_POSITION
Definition: en_US.php:291
const LINE_THROUGH
Definition: en_US.php:511
const UNDERLINE
Definition: en_US.php:1070
const IMAGE_POSITION_DESCRIPTION
Definition: en_US.php:473
const CENTER
Definition: en_US.php:150
$myts
Definition: edituser.php:38
$editor
$code
Definition: lostpass.php:48
const CLICK_PREVIEW_TO_SEE_CONTENT
Definition: en_US.php:162