1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Form;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class DhtmlTextArea extends \XoopsEditor
27: {
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54:
55: 56: 57:
58: public $htmlEditor;
59:
60: 61: 62: 63: 64:
65: private $hiddenText;
66:
67: 68: 69:
70: public $skipPreview = false;
71:
72: 73: 74:
75: public $doHtml = false;
76:
77: 78: 79:
80: public $js = '';
81:
82: 83: 84:
85: public $configs = array();
86:
87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97:
98: public function __construct(
99: $caption,
100: $name,
101: $value = "",
102: $rows = 5,
103: $cols = 50,
104: $hiddentext = "xoopsHiddenText",
105: $options = array()
106: ) {
107: static $inLoop = 0;
108:
109: ++$inLoop;
110:
111: if ($inLoop > 2) {
112: return;
113: }
114:
115:
116: parent::__construct($caption, $name, $value, $rows, $cols);
117: $this->hiddenText = $hiddentext;
118:
119: if ($inLoop > 1) {
120: return;
121: }
122:
123: $xoops = \Xoops::getInstance();
124: if (!isset($options['editor'])) {
125: if ($editor = $xoops->getConfig('editor')) {
126: $options['editor'] = $editor;
127: }
128: }
129:
130: if (!empty($this->htmlEditor) || !empty($options['editor'])) {
131: $options['name'] = $this->getName();
132: $options['value'] = $this->getValue();
133: if (!empty($options['editor'])) {
134: $this->htmlEditor = is_array($options['editor']) ? $options['editor'] : array($options['editor']);
135: }
136:
137: if (count($this->htmlEditor) == 1) {
138: $editor_handler = \XoopsEditorHandler::getInstance();
139: $this->htmlEditor = $editor_handler->get($this->htmlEditor[0], $options);
140: if ($inLoop > 1) {
141: $this->htmlEditor = null;
142: }
143: } else {
144: list ($class, $path) = $this->htmlEditor;
145: include_once \XoopsBaseConfig::get('root-path') . $path;
146: if (class_exists($class)) {
147: $this->htmlEditor = new $class($options);
148: }
149: if ($inLoop > 1) {
150: $this->htmlEditor = null;
151: }
152: }
153: }
154:
155: $inLoop = 0;
156: }
157:
158: 159: 160: 161: 162:
163: public function render()
164: {
165: if ($this->htmlEditor && is_object($this->htmlEditor)) {
166: if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
167: return $this->htmlEditor->render();
168: }
169: }
170: static $js_loaded;
171:
172: $xoops = \Xoops::getInstance();
173:
174: $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
175: $ret = "";
176:
177: $ret .= $this->codeIcon() . "<br />\n";
178:
179: $ret .= $this->fontArray();
180:
181: $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 . "' />";
182: $ret .= "<br />\n";
183:
184:
185: $this->suppressRender(['value']);
186: $this->themeDecorateElement();
187: $attributes = $this->renderAttributeString();
188:
189: $ret .= '<textarea ' . $attributes . $extra . '>' . $this->getValue() . "</textarea><br />\n";
190:
191: if (empty($this->skipPreview)) {
192: if (!$xoops->theme()) {
193: $this->js .= implode("", file($xoops->path('media/xoops/image.js')));
194: } else {
195: $xoops->theme()->addScript('media/xoops/image.js', array('type' => 'text/javascript'));
196: }
197: $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', " . (int)($this->doHtml) . ", '" . $xoops->security()->createToken() . "')\"" . " />";
198: $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>";
199: }
200:
201: if (empty($js_loaded)) {
202: $javascript = (($this->js)
203: ? '<script type="text/javascript">' . $this->js . '</script>'
204: : '') . '<script type="text/javascript" src="' . \XoopsBaseConfig::get('url') . '/include/formdhtmltextarea.js"></script>';
205: $ret = $javascript . $ret;
206: $js_loaded = true;
207: }
208: return $ret;
209: }
210:
211: 212: 213: 214: 215:
216: public function codeIcon()
217: {
218: $textarea_id = $this->getName();
219: $xoops = \Xoops::getInstance();
220: $myts = \Xoops\Core\Text\Sanitizer::getInstance();
221:
222: $code = '';
223: $code .= '<img src="' . $xoops->url('images/form/url.gif') . '" alt="' . \XoopsLocale::URL
224: . '" title="' . \XoopsLocale::URL . '" onclick="xoopsCodeUrl(\'' . $textarea_id . '\', \''
225: . $myts->escapeForJavascript(\XoopsLocale::ENTER_LINK_URL) . '\', \''
226: . $myts->escapeForJavascript(\XoopsLocale::ENTER_WEBSITE_TITLE)
227: . '\')" onmouseover="style.cursor=\'hand\'" /> ';
228: $code .= '<img src="' . $xoops->url('images/form/email.gif') . '" alt="' . \XoopsLocale::EMAIL
229: . '" title="' . \XoopsLocale::EMAIL . '" onclick="xoopsCodeEmail(\'' . $textarea_id . '\', \''
230: . $myts->escapeForJavascript(\XoopsLocale::ENTER_EMAIL)
231: . '\');" onmouseover="style.cursor=\'hand\'" /> ';
232: $code .= '<img src="' . $xoops->url('images/form/imgsrc.gif') . '" alt="' . \XoopsLocale::IMAGES
233: . '" title="' . \XoopsLocale::IMAGES . '" onclick="xoopsCodeImg(\'' . $textarea_id . '\', \''
234: . $myts->escapeForJavascript(\XoopsLocale::ENTER_IMAGE_URL) . '\', \''
235: . $myts->escapeForJavascript(\XoopsLocale::ENTER_IMAGE_POSITION) . '\', \''
236: . $myts->escapeForJavascript(\XoopsLocale::IMAGE_POSITION_DESCRIPTION) . '\', \''
237: . $myts->escapeForJavascript(\XoopsLocale::E_ENTER_IMAGE_POSITION) . '\', \''
238: . $myts->escapeForJavascript(\XoopsLocale::WIDTH) . '\');" onmouseover="style.cursor=\'hand\'" /> ';
239:
240: $extensions = array_filter($myts->listExtensions());
241: foreach ($extensions as $extension) {
242: list ($button, $js) = $myts->getDhtmlEditorSupport($extension, $textarea_id);
243: if (!empty($button)) {
244: $code .= $button;
245: }
246: if (!empty($js)) {
247: $this->js .= $js;
248: }
249: }
250: $code .= '<img src="' . $xoops->url('images/form/code.gif') .'" alt="' . \XoopsLocale::SOURCE_CODE . '" title="'
251: . \XoopsLocale::SOURCE_CODE . '" onclick="xoopsCodeCode(\'' . $textarea_id . '\', \''
252: . $myts->escapeForJavascript(\XoopsLocale::ENTER_CODE) . '\');" onmouseover="style.cursor=\'hand\'" /> ';
253:
254: $code .= '<img src="' . $xoops->url('images/form/quote.gif') .'" alt="' . \XoopsLocale::QUOTE . '" title="'
255: . \XoopsLocale::QUOTE . '" onclick="xoopsCodeQuote(\'' . $textarea_id . '\', \''
256: . $myts->escapeForJavascript(\XoopsLocale::ENTER_QUOTE) . '\');" onmouseover="style.cursor=\'hand\'" /> ';
257:
258: $response = \Xoops::getInstance()->service('emoji')->renderEmojiSelector($this->getName());
259: if ($response->isSuccess()) {
260: $emojiSelector = $response->getValue();
261: $code .= $emojiSelector;
262: }
263:
264: return $code;
265: }
266:
267: 268: 269: 270: 271:
272: public function fontArray()
273: {
274: $textarea_id = $this->getName();
275: $hiddentext = $this->hiddenText;
276:
277: $fontStr = "<script type=\"text/javascript\" language=\"JavaScript\">";
278: $fontStr .= "var _editor_dialog = ''" . "+ '<select class=\"span2\" id=\'{$textarea_id}Size\' onchange=\'xoopsSetElementAttribute(\"size\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
279: $fontStr .= "+ '<option value=\'SIZE\'>" . \XoopsLocale::SIZE . "</option>'";
280: $localeFontSizes = \XoopsLocale::getFontSizes();
281: foreach ($localeFontSizes as $_val => $_name) {
282: $fontStr .= " + '<option value=\'{$_val}\'>{$_name}</option>'";
283: }
284: $fontStr .= " + '</select> '";
285: $fontStr .= "+ '<select class=\"span2\" id=\'{$textarea_id}Font\' onchange=\'xoopsSetElementAttribute(\"font\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
286: $fontStr .= "+ '<option value=\'FONT\'>" . \XoopsLocale::FONT . "</option>'";
287: $localeFonts = \XoopsLocale::getFonts();
288: $fontarray = !empty($localeFonts) ? $localeFonts :
289: array("Arial", "Courier", "Georgia", "Helvetica", "Impact", "Verdana", "Haettenschweiler");
290: foreach ($fontarray as $font) {
291: $fontStr .= " + '<option value=\'{$font}\'>{$font}</option>'";
292: }
293: $fontStr .= " + '</select> '";
294: $fontStr .= "+ '<select class=\"span2\" id=\'{$textarea_id}Color\' onchange=\'xoopsSetElementAttribute(\"color\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
295: $fontStr .= "+ '<option value=\'COLOR\'>" . \XoopsLocale::COLOR . "</option>';";
296: $fontStr .= "var _color_array = new Array('00', '33', '66', '99', 'CC', 'FF');
297: for(var i = 0; i < _color_array.length; i ++) {
298: for(var j = 0; j < _color_array.length; j ++) {
299: for(var k = 0; k < _color_array.length; k ++) {
300: var _color_ele = _color_array[i] + _color_array[j] + _color_array[k];
301: _editor_dialog += '<option value=\''+_color_ele+'\' style=\'background-color:#'+_color_ele+';color:#'+_color_ele+';\'>#'+_color_ele+'</option>';
302: }
303: }
304: }
305: _editor_dialog += '</select>';";
306:
307: $fontStr .= "document.write(_editor_dialog); </script>";
308:
309: $styleStr = "<img src='" . \XoopsBaseConfig::get('url') . "/images/bold.gif' alt='" . \XoopsLocale::BOLD . "' title='" . \XoopsLocale::BOLD . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeBold(\"{$hiddentext}\", \"{$textarea_id}\");' /> ";
310: $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/italic.gif' alt='" . \XoopsLocale::ITALIC . "' title='" . \XoopsLocale::ITALIC . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeItalic(\"{$hiddentext}\", \"{$textarea_id}\");' /> ";
311: $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/underline.gif' alt='" . \XoopsLocale::UNDERLINE . "' title='" . \XoopsLocale::UNDERLINE . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeUnderline(\"{$hiddentext}\", \"{$textarea_id}\");'/> ";
312: $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}\");' /> ";
313:
314: $alignStr = "<img src='" . \XoopsBaseConfig::get('url') . "/images/alignleft.gif' alt='" . \XoopsLocale::LEFT . "' title='" . \XoopsLocale::LEFT . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeLeft(\"{$hiddentext}\", \"{$textarea_id}\");' /> ";
315: $alignStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/aligncenter.gif' alt='" . \XoopsLocale::CENTER . "' title='" . \XoopsLocale::CENTER . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeCenter(\"{$hiddentext}\", \"{$textarea_id}\");' /> ";
316: $alignStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/alignright.gif' alt='" . \XoopsLocale::RIGHT . "' title='" . \XoopsLocale::RIGHT . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeRight(\"{$hiddentext}\", \"{$textarea_id}\");' /> ";
317: $fontStr = $fontStr . "<br />\n{$styleStr} {$alignStr} \n";
318: return $fontStr;
319: }
320:
321: 322: 323: 324: 325:
326: public function renderValidationJS()
327: {
328: if ($this->htmlEditor && is_object($this->htmlEditor) && method_exists($this->htmlEditor, 'renderValidationJS')) {
329: if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
330: return $this->htmlEditor->renderValidationJS();
331: }
332: }
333: return parent::renderValidationJS();
334: }
335: }
336: