wysiwyg.php 5.86 KB
Newer Older
1
2
3
<?php
/**
 *
4
5
 * @package    mahara
 * @subpackage form
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
 *
 */

12
13
$_PIEFORM_WYSIWYGS = array();

14
/**
15
 * Renders a textarea, but with extra javascript to turn it into a wysiwyg
16
17
 * textarea.
 *
18
19
 * @todo support resizable.
 *
20
21
22
 * @param array   $element The element to render
 * @param Pieform $form    The form to render the element for
 * @return string          The HTML for the element
23
 */
24
function pieform_element_wysiwyg(Pieform $form, $element) {
25
    global $_PIEFORM_WYSIWYGS;
26
    $_PIEFORM_WYSIWYGS[] = $form->get_name() . '_' . $element['name'];
27
    if (is_html_editor_enabled()) {
28
29
30
        if (!$form->get_property('elementclasses')) {
            $element['class'] = isset($element['class']) && $element['class'] !== '' ? $element['class'] . ' wysiwyg' : 'wysiwyg';
        }
31
    }
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    $rows = $cols = $style = '';
    if (isset($element['height'])) {
        $style .= 'height:' . $element['height'] . ';';
        $rows   = (intval($element['height'] > 0)) ? ceil(intval($element['height']) / 10) : 1;
    }
    elseif (isset($element['rows'])) {
        $rows = $element['rows'];
    }
    else {
        log_warn('No value for rows or height specified for textarea ' . $element['name']);
    }

    if (isset($element['width'])) {
        $style .= 'width:' . $element['width'] . ';';
        $cols   = (intval($element['width'] > 0)) ? ceil(intval($element['width']) / 10) : 1;
    }
    elseif (isset($element['cols'])) {
        $cols = $element['cols'];
    }
    else {
        log_warn('No value for cols or width specified for textarea ' . $element['name']);
    }
    $element['style'] = (isset($element['style'])) ? $style . $element['style'] : $style;
55

56
    if (is_html_editor_enabled()) {
57
58
59
60
61
62
63
64
65
66
67
68
        $value = Pieform::hsc($form->get_value($element));
    }
    else {
        // Replace <br>s as added by wysiwyg editor or nl2br with a newline
        $value = preg_replace("#<br />\s#", "\n", $form->get_value($element));
        // As placed in the value by the wysiwyg editor
        $value = str_replace('</p><p>', "\n\n", $value);
        // Find the last </p> and replace with newlines
        $value = preg_replace('#</p>\s#', "\n", $value);
        $value = strip_tags($value);
    }

69
70
71
    return '<textarea'
        . (($rows) ? ' rows="' . $rows . '"' : '')
        . (($cols) ? ' cols="' . $cols . '"' : '')
72
        . $form->element_attributes($element, array('maxlength', 'size'))
73
        . '>' . $value . '</textarea>';
74
75
}

76
function pieform_element_wysiwyg_rule_required(Pieform $form, $value, $element, $check) {
77
    return $check && strip_tags($value, '<img><iframe><object><embed>') === '' ? $form->i18n('rule', 'required', 'required', $element) : '';
78
79
}

80
function pieform_element_wysiwyg_get_headdata() {
81
    global $_PIEFORM_WYSIWYGS;
82
    if (is_html_editor_enabled() && !empty($_PIEFORM_WYSIWYGS)) {
83
        $result = '<script type="application/javascript">'
84
         . "\nvar editor_to_focus;"
85
         . "\nPieformManager.connect('onsubmit', null, tinyMCE.triggerSave);"
86
87
         . "\nPieformManager.connect('onload', null, function() {\n";
        foreach ($_PIEFORM_WYSIWYGS as $editor) {
88
            $result .= "    tinyMCE.execCommand('mceAddEditor', false, '$editor');\n";
89
            $result .= "    $('{$editor}').focus = function() {\n";
90
            $result .= "        editor_to_focus = '$editor';\n";
91
            $result .= "    };\n";
92
93
94
        }
        $result .= "});\nPieformManager.connect('onreply', null, function() {\n";
        foreach ($_PIEFORM_WYSIWYGS as $editor) {
95
            $result .= "    tinyMCE.execCommand('mceRemoveEditor', false, '$editor');\n";
96
97
        }
        $result .= "});</script>";
98
        safe_require('artefact', 'file');
99
100
101
102
103
104
105
106
107
        $strings = PluginArtefactFile::jsstrings('filebrowser');
        $jsstrings = '';
        foreach ($strings as $section => $sectionstrings) {
            foreach ($sectionstrings as $s) {
                $jsstrings .= "strings.$s=" . json_encode(get_raw_string($s, $section)) . ';';
            }
        }
        $headdata = '<script type="application/javascript">' . $jsstrings . '</script>';
        return array('tinymce', $result, $headdata);
108
109
110
111
    }
    return array();
}

112
113
114
function pieform_element_wysiwyg_get_value(Pieform $form, $element) {
    $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
    if (isset($element['value'])) {
115
        return clean_html($element['value']);
116
    }
117
    else if ($form->is_submitted() && isset($global[$element['name']])) {
118
        $value = $global[$element['name']];
119
        if (!is_html_editor_enabled()) {
120
121
122
123
124
            $value = format_whitespace($value);
        }
        return $value;
    }
    else if (isset($element['defaultvalue'])) {
125
        return clean_html($element['defaultvalue']);
126
127
128
129
    }
    return null;
}

130
131
132
133
134
135
136
137
138
139
140
/**
 * Extension by Mahara. This api function returns the javascript required to
 * set up the element, assuming the element has been placed in the page using
 * javascript. This feature is used in the views interface.
 *
 * In theory, this could go upstream to pieforms itself
 *
 * @param Pieform $form     The form
 * @param array   $element  The element
 */
function pieform_element_wysiwyg_views_js(Pieform $form, $element) {
141
    if (is_html_editor_enabled()) {
142
143
144
        $formname = json_encode($form->get_name());
        $editor = json_encode($form->get_name() . '_' . $element['name']);
        return "\ntinyMCE.idCounter=0;"
145
            . "\ntinyMCE.execCommand('mceAddEditor', false, $editor);"
146
147
            . "\nPieformManager.connect('onsubmit', $formname, tinyMCE.triggerSave);"
            . "\nPieformManager.connect('onreply', $formname, function () {"
148
            . "\n  tinyMCE.execCommand('mceRemoveEditor', false, $editor);"
149
150
151
152
            . "});";
    }
    return '';
}