From: skodak Date: Wed, 19 Nov 2008 20:02:02 +0000 (+0000) Subject: MDL-16698 formslib: skeleton of new editor element - no JS or html editor integration yet X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=cefe1408a24467ab73d61774133b06d5942f0b12;p=moodle.git MDL-16698 formslib: skeleton of new editor element - no JS or html editor integration yet --- diff --git a/lib/form/editor.php b/lib/form/editor.php index 23dfca6ffc..e56f14e9b7 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -1,98 +1,174 @@ -libdir/form/textarea.php"); - -/** - * HTML class for htmleditor type element - * - * @author Jamie Pratt - * @access public - */ -class MoodleQuickForm_htmleditor extends MoodleQuickForm_textarea{ - var $_type; - var $_canUseHtmlEditor; - var $_options=array('canUseHtmlEditor'=>'detect','rows'=>10, 'cols'=>45, 'width'=>0,'height'=>0, 'filearea'=>''); - function MoodleQuickForm_htmleditor($elementName=null, $elementLabel=null, $options=array(), $attributes=null){ - parent::MoodleQuickForm_textarea($elementName, $elementLabel, $attributes); - // set the options, do not bother setting bogus ones - if (is_array($options)) { - foreach ($options as $name => $value) { - if (array_key_exists($name, $this->_options)) { - if (is_array($value) && is_array($this->_options[$name])) { - $this->_options[$name] = @array_merge($this->_options[$name], $value); - } else { - $this->_options[$name] = $value; - } - } +0, 'maxbytes'=>0, 'changeformat'=>0, 'upload'=>0); + protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null); + + function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $options=null, $attributes=null) { + global $CFG; + + $options = (array)$options; + foreach ($options as $name=>$value) { + if (array_key_exists($name, $this->_options)) { + $this->_options[$name] = $value; } } - if ($this->_options['canUseHtmlEditor']=='detect'){ - $this->_options['canUseHtmlEditor']=can_use_html_editor(); + if (!empty($options['maxbytes'])) { + $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); } - if ($this->_options['canUseHtmlEditor']){ - $this->_type='htmleditor'; - //$this->_elementTemplateType='wide'; - }else{ - $this->_type='textarea'; - } - $this->_canUseHtmlEditor = $this->_options['canUseHtmlEditor']; + parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); } - /** - * set html for help button - * - * @access public - * @param array $help array of arguments to make a help button - * @param string $function function name to call to get html - */ - function setHelpButton($helpbuttonargs, $function='helpbutton'){ - if (!$this->_canUseHtmlEditor){ - if ('editorhelpbutton' == $function){ - $key = array_search('richtext2', $helpbuttonargs); - if ($key !== FALSE){ - array_splice($helpbuttonargs, $key, 1, array('text2', 'emoticons2')); - } - } elseif ('helpbutton' == $function && $helpbuttonargs[0] == 'richtext2' && ((!isset($helpbuttonargs[2])) || $helpbuttonargs[2] == 'moodle')){ - //replace single 'richtext' help button with text and emoticon button when htmleditor off. - return $this->setHelpButton(array('text2', 'emoticons2'), 'editorhelpbutton'); + + function setName($name) { + $this->updateAttributes(array('name'=>$name)); + } + + function getName() { + return $this->getAttribute('name'); + } + + function setValue($values) { + $values = (array)$values; + foreach ($values as $name=>$value) { + if (array_key_exists($name, $this->_values)) { + $this->_values[$name] = $value; } } - return parent::setHelpButton($helpbuttonargs, $function); } - function toHtml(){ - //if ($this->_canUseHtmlEditor && !$this->_flagFrozen){ - // $script = ''; - //} else { - // $script=''; - //} + function getValue() { + return $this->getAttribute('value'); + } + + function getMaxbytes() { + return $this->_options['maxbytes']; + } + + function setMaxbytes($maxbytes) { + global $CFG; + $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes); + } + + function getSubdirs() { + return $this->_options['subdirs']; + } + + function setSubdirs($allow) { + $this->_options['subdirs'] = $allow; + } + + function setHelpButton($_helpbuttonargs, $function='_helpbutton') { + if (!is_array($_helpbuttonargs)) { + $_helpbuttonargs = array($_helpbuttonargs); + } else { + $_helpbuttonargs = $_helpbuttonargs; + } + //we do this to to return html instead of printing it + //without having to specify it in every call to make a button. + if ('_helpbutton' == $function){ + $defaultargs = array('', '', 'moodle', true, false, '', true); + $_helpbuttonargs = $_helpbuttonargs + $defaultargs ; + } + $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs); + } + + function getHelpButton() { + return $this->_helpbutton; + } + + function getElementTemplateType() { + if ($this->_flagFrozen){ + return 'nodisplay'; + } else { + return 'default'; + } + } + + function toHtml() { + global $CFG; + if ($this->_flagFrozen) { return $this->getFrozenHtml(); + } + + $id = $this->_attributes['id']; + $elname = $this->_attributes['name']; + + $subdirs = $this->_options['subdirs']; + $maxbytes = $this->_options['maxbytes']; + $upload = $this->_options['upload']; + $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls + + $text = $this->_values['text']; + $format = $this->_values['format']; + $itemid = $this->_values['itemid']; + + // security - never ever allow guest/not logged in user to upload anything + if (isguestuser() or !isloggedin()) { + $upload = 0; + } + + $str = $this->_getTabs(); + $str .= '
'; + + + /// first print text area - TODO: add on-the-fly switching to tinymce, size configuration + $str .= '
'; + + /// format option - TODO: ajax conversion and switching + $formats = array(FORMAT_MOODLE=>'Moodle', FORMAT_HTML=>'HTML', FORMAT_PLAIN=>'Plaintext', FORMAT_WIKI=>'Wiki'); // TODO: localise & switch to new formats plugins + + if (!isset($formats[$format])) { + $format = FORMAT_MOODLE; + } + + $str .= '
'; + if ($changeformat) { + $str .= ''; } else { - return $this->_getTabs() . - ''."\n". - print_textarea($this->_canUseHtmlEditor, - $this->_options['rows'], - $this->_options['cols'], - $this->_options['width'], - $this->_options['height'], - $this->getName(), - preg_replace("/(\r\n|\n|\r)/", ' ',$this->getValue()), - 0, // unused anymore - true, - $this->getAttribute('id')); + // no changes of format allowed + $str .= $formats[$format]; + } + $str .= '
'; + + /// embedded image files - TODO: hide if tinymce used because it has own image upload/manage dialog + if ($upload) { + if (empty($draftitemid)) { + // no existing area info provided - let's use fresh new draft area + require_once("$CFG->libdir/filelib.php"); + $this->setValue(array('itemid'=>file_get_new_draftitemid())); + $itemid = $this->_values['itemid']; + } + + $str .= '
'; + $editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$itemid&subdirs=$subdirs&maxbytes=$maxbytes"; + $str .= ''; + $str .= 'Error'; // TODO: localise, fix styles, etc. + $str .= '
'; } - } //end func toHtml - - /** - * What to display when element is frozen. - * - * @access public - * @return string - */ - function getFrozenHtml() - { - $html = format_text($this->getValue()); - return $html . $this->_getPersistantData(); - } //end func getFrozenHtml + + + $str .= '
'; + + return $str; + } + } -?> diff --git a/lib/formslib.php b/lib/formslib.php index 568876d224..0de0f92ef2 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -2056,6 +2056,7 @@ MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'Mood MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea'); MoodleQuickForm::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'MoodleQuickForm_date_selector'); MoodleQuickForm::registerElementType('date_time_selector', "$CFG->libdir/form/datetimeselector.php", 'MoodleQuickForm_date_time_selector'); +MoodleQuickForm::registerElementType('editor', "$CFG->libdir/form/editor.php", 'MoodleQuickForm_editor'); MoodleQuickForm::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'MoodleQuickForm_htmleditor'); MoodleQuickForm::registerElementType('format', "$CFG->libdir/form/format.php", 'MoodleQuickForm_format'); MoodleQuickForm::registerElementType('static', "$CFG->libdir/form/static.php", 'MoodleQuickForm_static');