|
From: <luc...@us...> - 2013-09-26 12:53:23
|
Revision: 12096
http://sourceforge.net/p/xoops/svn/12096
Author: luciorota
Date: 2013-09-26 12:53:19 +0000 (Thu, 26 Sep 2013)
Log Message:
-----------
+ added renderGetContentJS method to form objects (Renders the Javascript function needed to get contents)
+ added "mode" option to editor...
Modified Paths:
--------------
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/sampleform.inc.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/formtinymce.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/tinymce.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/xoopseditor.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/form.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcheckbox.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcolorpicker.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formdhtmltextarea.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formeditor.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formelement.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselect.php
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselecteditor.php
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/sampleform.inc.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/sampleform.inc.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/sampleform.inc.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -34,8 +34,11 @@
$editor = @ $xoopsUser->getVar('editor'); // Need set through user profile
}
// Add the editor selection box
- // If dohtml is disabled, set $noHtml = true
- $sample_form->addElement(new XoopsFormSelectEditor($sample_form, 'editor', $editor, $noHtml = false));
+ // If dohtml is disabled, set $noHtml = true; $mode not considered if $noHtml is false
+ xoops_load('XoopsCache');
+ XoopsCache::delete('editorlist');
+ $mode = 'html'; // extension or mimetype
+ $sample_form->addElement(new XoopsFormSelectEditor($sample_form, 'editor', $editor, $noHtml = false, $mode));
// options for the editor
// required configs
$options['editor'] = $editor;
@@ -46,8 +49,8 @@
$options['cols'] = 60; // default value = 50
$options['width'] = '100%'; // default value = 100%
$options['height'] = '400px'; // default value = 400px
+ $options['mode'] = $mode; // for editors that support mode option
-
// "textarea": if the selected editor with name of $editor can not be created, the editor "textarea" will be used
// if no $onFailure is set, then the first available editor will be used
// If dohtml is disabled, set $noHtml to true
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/formtinymce.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/formtinymce.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/formtinymce.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -86,6 +86,23 @@
}
/**
+ * Renders the Javascript function needed for client-side for get content
+ *
+ * I'VE USED THIS EXAMPLE TO WRITE VALIDATION CODE
+ * http://tinymce.moxiecode.com/punbb/viewtopic.php?id=12616
+ *
+ * @return string
+ */
+ function renderGetContentJS()
+ {
+ if ($eltname = $this->getName()) {
+ $ret = "tinyMCE.get('{$eltname}').getContent()";
+ return $ret;
+ }
+ return '';
+ }
+
+ /**
* get language
*
* @return string
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/tinymce.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/tinymce.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/tinymce/tinymce.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -269,11 +269,10 @@
$this->init();
if (self::$LastOfElementsTinymce!=$this->setting["elements"]) {
$ret = "\n<!-- 'tiny_mce.js' SCRIPT NOT YET ".$this->setting["elements"]." -->\n"; //debug
-
return $ret;
- } else {
+ } else {
$this->setting["elements"]=implode(",",self::$ListOfElementsTinymce);
- }
+ }
if ( !empty($this->setting["callback"]) ) {
$callback = $this->setting["callback"];
unset($this->setting["callback"]);
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/xoopseditor.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/xoopseditor.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopseditor/xoopseditor.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -90,6 +90,7 @@
var $root_path = "";
var $nohtml = false;
var $allowed_editors = array();
+ var $modes = null;
/**
* Enter description here...
*
@@ -125,15 +126,16 @@
*
* @param string $name Editor name which is actually the folder name
* @param array $options editor options: $key => $val
+ * @param bool $noHtml dohtml disabled
* @param string $OnFailure a pre-validated editor that will be used if the required editor is failed to create
- * @param bool $noHtml dohtml disabled
+ * @param mixed $modes Requested mode (or array of them)
*/
- function get($name = '', $options = null, $noHtml = false, $OnFailure = '')
+ function get($name = '', $options = null, $noHtml = false, $OnFailure = '', $modes = null)
{
- if (array_key_exists($name, $this->getList($noHtml)) && $editor = $this->_loadEditor($name, $options)) {
+ if (array_key_exists($name, $this->getList($noHtml, $modes)) && $editor = $this->_loadEditor($name, $options)) {
return $editor;
}
- $list = array_keys($this->getList($noHtml));
+ $list = array_keys($this->getList($noHtml, $modes));
if (empty($OnFailure) || !in_array($OnFailure, $list)) {
$OnFailure = $list[0];
}
@@ -145,9 +147,10 @@
* XoopsEditorHandler::getList()
*
* @param mixed $noHtml
+ * @param mixed $modes Requested mode (or array of them)
* @return
*/
- function getList($noHtml = false)
+ function getList($noHtml = false, $modes = null)
{
/*
Do NOT use this method statically, please use
@@ -177,7 +180,17 @@
if (empty($config['order']))
continue;
$order[] = $config['order'];
- $list[$item] = array('title' => $config['title'] , 'nohtml' => $config['nohtml']);
+ if (!isset($config['modes'])) {
+ $config['modes'] = null;
+ }
+ if ($config['nohtml'] == false) {
+ $config['modes'] = array('htm', 'html', 'htmlmixed', 'text/html');
+ }
+ $list[$item] = array(
+ 'title' => $config['title'] ,
+ 'nohtml' => $config['nohtml'],
+ 'modes' => $config['modes']
+ );
}
}
array_multisort($order, $list);
@@ -192,6 +205,10 @@
foreach ($editors as $name) {
if (!empty($noHtml) && empty($list[$name]['nohtml']))
continue;
+ if (!empty($modes) && is_string($modes) && !empty($list[$name]['modes']) && !in_array(strtolower($modes), $list[$name]['modes']))
+ continue;
+ if (!empty($modes) && is_array($modes) && !empty($list[$name]['modes']) && (count(array_intersect($modes, $list[$name]['modes'])) == 0))
+ continue;
$_list[$name] = $list[$name]['title'];
}
return $_list;
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/form.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/form.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/form.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -558,6 +558,60 @@
}
/**
+ * Renders the Javascript function needed for client-side for get content
+ *
+ * Additionally, each element class may provide its own "renderGetContentJS" method
+ * that is supposed to return custom get content code for the element.
+ *
+ * The element get content code can assume that the JS "myform" variable points to the form.
+ * Exemple:
+ * <script type='text/javascript'>
+ * var content[];
+ * content = " . renderGetContentJS(false) . "
+ * </script>
+ *
+ * A basic element get content method may contain something like this:
+ * <code>
+ * function renderGetContentJS() {
+ * $name = $this->getName();
+ * return "myform.{$name}.value;";
+ * }
+ * </code>
+ *
+ * @param boolean $withtags Include the < javascript > tags in the returned string
+ * @return string
+ */
+ function renderGetContentJS($withtags = true)
+ {
+ $js = '';
+ if ($withtags) {
+ $js .= "\n<!-- Start Form GetContent JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
+ }
+ $formname = $this->getName();
+ $js .= "function xoopsFormGetContent_{$formname}() {\n";
+ $js .= " var myform = window.document.{$formname};\n";
+ $js .= " var content = new Object();\n";
+ $elements = $this->getElements(true);
+ foreach($elements as $elt) {
+ if (method_exists($elt, 'renderGetContentJS') && ($elt->getName() != '')) {
+ $js .= " content['" . $elt->getName() . "'] = " . $elt->renderGetContentJS() . ";\n";
+ }
+ }
+ $js .= " Object.size = function(obj) {\n";
+ $js .= " var size = 0, key;\n";
+ $js .= " for (key in obj)\n";
+ $js .= " {if (obj.hasOwnProperty(key)) size++;}\n";
+ $js .= " return size;\n";
+ $js .= " };\n";
+ $js .= " return (Object.size(content) > 0) ? content : null;\n";
+ $js .= "}\n";
+ if ($withtags) {
+ $js .= "//--></script>\n<!-- End Form GetContent JavaScript //-->\n";
+ }
+ return $js;
+ }
+
+ /**
* assign to smarty form template instead of displaying directly
*
* @param object $ &$tpl reference to a {@link Smarty} object
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcheckbox.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcheckbox.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcheckbox.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -242,4 +242,33 @@
}
return '';
}
+
+ /**
+ * Render custom javascript validation code
+ *
+ * @seealso XoopsForm::renderValidationJS
+ */
+ function renderGetContentJS()
+ {
+ // render custom validation code if any
+ if (! empty($this->customGetContentCode)) {
+ return implode(NWLINE, $this->customGetContentCode);
+ // generate get content code if required
+ } else {
+ $eltname = $this->getName();
+ return NWLINE . "
+ function(){
+ var checkboxesChecked = [];
+ var checkBox = myform.elements['{$eltname}'];
+ if (checkBox.length) {
+ for (var i = 0; i < checkBox.length; i++) {
+ if (checkBox[i].checked == true) {
+ checkboxesChecked.push(checkboxes[i]);
+ }
+ }
+ }
+ return (checkboxesChecked.length > 0) ? checkboxesChecked : null;
+ }";
+ }
+ }
}
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcolorpicker.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcolorpicker.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formcolorpicker.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -72,4 +72,18 @@
return "if ( !(new RegExp(\"^#[0-9a-fA-F]{6}\",\"i\").test(myform.{$eltname}.value)) ) { window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }";
}
+
+ /**
+ * Returns custom get content Javascript
+ *
+ * @return string Element get content Javascript
+ */
+ function renderGetContentJS()
+ {
+ $eltname = $this->getName();
+ return NWLINE . "
+ function(){
+ return ( (new RegExp(\"^#[0-9a-fA-F]{6}\",\"i\").test(myform.{$eltname}.value))) ? myform.{$eltname}.value : null;
+ }";
+ }
}
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formdhtmltextarea.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formdhtmltextarea.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formdhtmltextarea.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -265,4 +265,19 @@
}
return parent::renderValidationJS();
}
+
+ /**
+ * XoopsFormDhtmlTextArea::renderGetContentJS()
+ *
+ * @return
+ */
+ function renderGetContentJS()
+ {
+ if ($this->htmlEditor && is_object($this->htmlEditor) && method_exists($this->htmlEditor, 'renderGetContentJS')) {
+ if (! isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
+ return $this->htmlEditor->renderGetContentJS();
+ }
+ }
+ return parent::renderGetContentJS();
+ }
}
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formeditor.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formeditor.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formeditor.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -57,7 +57,7 @@
/**
* renderValidationJS
- * TEMPORARY SOLUTION to 'override' original renderValidationJS method
+ * override original renderValidationJS method
* with custom XoopsEditor's renderValidationJS method
*/
function renderValidationJS()
@@ -77,6 +77,27 @@
}
/**
+ * renderGetContentJS
+ * 'override' original renderGetContentJS method
+ * with custom XoopsEditor's renderGetContentJS method
+ */
+ function renderGetContentJS()
+ {
+ if (is_object($this->editor)) {
+ if (method_exists($this->editor, 'renderGetContentJS')) {
+ $this->editor->setName($this->getName());
+ $this->editor->setCaption($this->getCaption());
+ $this->editor->_required = $this->isRequired();
+ $ret = $this->editor->renderGetContentJS();
+ return $ret;
+ } else {
+ parent::renderGetContentJS();
+ }
+ }
+ return false;
+ }
+
+ /**
* XoopsFormEditor::render()
*
* @return
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formelement.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formelement.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formelement.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -434,6 +434,37 @@
}
/**
+ * Render custom javascript get content code
+ *
+ * @seealso XoopsForm::renderGetContentJS
+ */
+ function renderGetContentJS()
+ {
+ // render custom get content code if any
+ if (!empty($this->customGetContentCode)) {
+ return implode(NWLINE, $this->customGetContentCode);
+ // generate get content code if required
+ } else if ($eltname = $this->getName()) {
+ // $eltname = $this->getName();
+ $eltcaption = $this->getCaption();
+ $eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
+ $eltmsg = str_replace(array(':' , '?' , '%'), '', $eltmsg);
+ $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
+ $eltmsg = strip_tags($eltmsg);
+ echo $this->getFormType();
+ switch ($this->getFormType()) {
+ case 'checkbox':
+ return "myform.{$eltname}.value";
+ break;
+ default:
+ return "myform.{$eltname}.value";
+ break;
+ } // switch
+ }
+ return false;
+ }
+
+ /**
* Generates output for the element.
*
* This method is abstract and must be overwritten by the child classes.
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselect.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselect.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselect.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -238,4 +238,34 @@
}
return '';
}
+
+ /**
+ * Render custom javascript get content code
+ *
+ * @seealso XoopsForm::renderGetContentJS
+ */
+ function renderGetContentJS()
+ {
+ // render custom validation code if any
+ if (! empty($this->customGetContentCode)) {
+ return implode("\n", $this->customGetContentCode);
+ // generate validation code if required
+ } else {
+ $eltname = $this->getName();
+ return "
+ function(){
+ var selectSelected = [];
+ var selectBox = myform.elements['{$eltname}'];
+ if (selectBox.length) {
+ for (i = 0; i < selectBox.options.length; i++ ) {
+ if (selectBox.options[i].selected == true && selectBox.options[i].value != '') {
+ selectSelected.push(selectBox.options[i].value);
+ }
+ }
+ }
+ return (selectSelected.length > 0) ? selectSelected : null;
+ }";
+ }
+ return '';
+ }
}
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselecteditor.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselecteditor.php 2013-09-26 12:38:11 UTC (rev 12095)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselecteditor.php 2013-09-26 12:53:19 UTC (rev 12096)
@@ -27,6 +27,7 @@
*/
class XoopsFormSelectEditor extends XoopsFormElementTray
{
+ var $modes = null;
var $allowed_editors = array();
var $form;
var $value;
@@ -40,10 +41,13 @@
* @param string $name editor name
* @param string $value Pre-selected text value
* @param bool $noHtml dohtml disabled
+ * @param array $allowed_editors
+ * @param mixed $modes Requested mode (or array of them)
*/
- function XoopsFormSelectEditor(&$form, $name = 'editor', $value = null, $nohtml = false, $allowed_editors = array())
+ function XoopsFormSelectEditor(&$form, $name = 'editor', $value = null, $nohtml = false, $allowed_editors = array(), $modes = null)
{
$this->XoopsFormElementTray(_SELECT);
+ $this->modes = $modes;
$this->allowed_editors = $allowed_editors;
$this->form = &$form;
$this->name = $name;
@@ -61,12 +65,13 @@
xoops_load('XoopsEditorHandler');
$editor_handler = XoopsEditorHandler::getInstance();
$editor_handler->allowed_editors = $this->allowed_editors;
+ $editor_handler->modes = $this->modes;
$option_select = new XoopsFormSelect("", $this->name, $this->value);
$extra = 'onchange="if(this.options[this.selectedIndex].value.length > 0 ){
window.document.forms.' . $this->form->getName() . '.submit();
}"';
$option_select->setExtra($extra);
- $option_select->addOptionArray($editor_handler->getList($this->nohtml));
+ $option_select->addOptionArray($editor_handler->getList($this->nohtml, $this->modes));
$this->addElement($option_select);
return parent::render();
}
|