]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16698 formslib: skeleton of new editor element - no JS or html editor integration yet
authorskodak <skodak>
Wed, 19 Nov 2008 20:02:02 +0000 (20:02 +0000)
committerskodak <skodak>
Wed, 19 Nov 2008 20:02:02 +0000 (20:02 +0000)
lib/form/editor.php
lib/formslib.php

index 23dfca6ffc44e4cb53c756c579834ba0e3181784..e56f14e9b7dbf86db06171ef9e66923fe3feb9a2 100644 (file)
-<?php
-global $CFG;
-require_once("$CFG->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;
-                    }
-                }
+<?php  // $Id$
+
+require_once('HTML/QuickForm/element.php');
+
+
+//TODO:
+//  * locking
+//  * freezing
+//  * ajax format conversion
+//  * tinymce integration
+//  * better area files handling
+
+class MoodleQuickForm_editor extends HTML_QuickForm_element {
+    protected $_helpbutton = '';
+    protected $_options    = array('subdirs'=>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 .= '<div>';
+
+
+    /// first print text area - TODO: add on-the-fly switching to tinymce, size configuration
+        $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="15" cols="80" />';
+        $str .= s($text);
+        $str .= '</textarea></div>';
+
+    /// 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 .= '<div>';
+        if ($changeformat) {
+            $str .= '<select name="'.$elname.'[format]">';
+            foreach ($formats as $key=>$desc) {
+                $selected = ($format == $key) ? 'selected="selected"' : '';
+                $str .= '<option value="'.s($key).'">'.$desc.'</ option>';
+            }
+            $str .= '</select>';
         } else {
-            return $this->_getTabs() .
-                    '<input type="hidden" name="filearea" value="'. $this->_options['filearea'] .'" />'."\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)/", '&#010;',$this->getValue()),
-                                    0, // unused anymore
-                                    true,
-                                    $this->getAttribute('id'));
+            // no changes of format allowed
+            $str .= $formats[$format];
+        }
+        $str .= '</div>';
+
+    /// 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 .= '<div>';
+            $editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$itemid&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes";
+            $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$itemid.'" />';
+            $str .= '<object type="text/html" data="'.$editorurl.'" height="160" width="600" style="border:1px solid #000">Error</object>'; // TODO: localise, fix styles, etc.
+            $str .= '</div>';
         }
-    } //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 .= '</div>';
+
+        return $str;
+    }
+
 }
-?>
index 568876d224597772b17c9828792142cc2088fd51..0de0f92ef2404e902578cdfa18f98feee66f46cb 100644 (file)
@@ -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');