MDL-28101 wiki2.0: escape bold and italics for NWIKI formatting
[moodle.git] / mod / wiki / editors / wiki_editor.php
CommitLineData
00710f4c
DC
1<?php
2/**
3 * This file defines a simple editor
4 *
5 * @author Jordi Piguillem
6 * @author Kenneth Riba
7 *
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package wiki
10 *
11 */
12
13/**
14 * Printing wiki editor.
15 * Depending on where it is called , action will go to different destinations.
16 * If it is called from comments section, the return will be in comments section
17 * in any other case it will be in edit view section.
18 * @param $pageid. Current pageid
19 * @param $content. Content to be edited.
20 * @param $section. Current section, default null
21 * @param $comesfrom. Information about where the function call is made
22 * @param commentid. id comment of comment that will be edited.
23 */
24
25function wiki_print_editor_wiki($pageid, $content, $editor, $version = -1, $section = null, $upload = false, $deleteuploads = array(), $comesfrom = 'editorview', $commentid = 0) {
26 global $CFG, $OUTPUT, $PAGE;
27
e0f0c304 28 if ($comesfrom == 'editcomments') {
00710f4c 29 $action = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=edit';
e0f0c304
DC
30 } else if ($comesfrom == 'addcomments') {
31 $action = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=add';
00710f4c
DC
32 } else {
33 $action = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $pageid;
34 }
35
36 if (!empty($section)) {
37 $action .= "&amp;section=" . urlencode($section);
38 }
39
40 ///Get tags for every element we are displaying
b7c3ab01 41 $tag = getTokens($editor, 'bold');
00710f4c 42 $wiki_editor['bold'] = array('ed_bold.gif', get_string('wikiboldtext', 'wiki'), $tag[0], $tag[1], get_string('wikiboldtext', 'wiki'));
b7c3ab01 43 $tag = getTokens($editor, 'italic');
00710f4c 44 $wiki_editor['italic'] = array('ed_italic.gif', get_string('wikiitalictext', 'wiki'), $tag[0], $tag[1], get_string('wikiitalictext', 'wiki'));
b7c3ab01 45 $tag = getTokens($editor, 'link');
00710f4c 46 $wiki_editor['internal'] = array('ed_internal.gif', get_string('wikiinternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiinternalurl', 'wiki'));
b7c3ab01 47 $tag = getTokens($editor, 'url');
00710f4c 48 $wiki_editor['external'] = array('ed_external.gif', get_string('wikiexternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiexternalurl', 'wiki'));
b7c3ab01 49 $tag = getTokens($editor, 'list');
00710f4c
DC
50 $wiki_editor['u_list'] = array('ed_ul.gif', get_string('wikiunorderedlist', 'wiki'), '\\n' . $tag[0], '', '');
51 $wiki_editor['o_list'] = array('ed_ol.gif', get_string('wikiorderedlist', 'wiki'), '\\n' . $tag[1], '', '');
b7c3ab01 52 $tag = getTokens($editor, 'image');
00710f4c 53 $wiki_editor['image'] = array('ed_img.gif', get_string('wikiimage', 'wiki'), $tag[0], $tag[1], get_string('wikiimage', 'wiki'));
b7c3ab01 54 $tag = getTokens($editor, 'header');
00710f4c
DC
55 $wiki_editor['h1'] = array('ed_h1.gif', get_string('wikiheader', 'wiki', 1), '\\n' . $tag . ' ', ' ' . $tag . '\\n', get_string('wikiheader', 'wiki', 1));
56 $wiki_editor['h2'] = array('ed_h2.gif', get_string('wikiheader', 'wiki', 2), '\\n' . $tag . $tag . ' ', ' ' . $tag . $tag . '\\n', get_string('wikiheader', 'wiki', 2));
57 $wiki_editor['h3'] = array('ed_h3.gif', get_string('wikiheader', 'wiki', 3), '\\n' . $tag . $tag . $tag . ' ', ' ' . $tag . $tag . $tag . '\\n', get_string('wikiheader', 'wiki', 3));
b7c3ab01 58 $tag = getTokens($editor, 'line_break');
00710f4c 59 $wiki_editor['hr'] = array('ed_hr.gif', get_string('wikihr', 'wiki'), '\\n' . $tag . '\\n', '', '');
b7c3ab01 60 $tag = getTokens($editor, 'nowiki');
00710f4c
DC
61 $wiki_editor['nowiki'] = array('ed_nowiki.gif', get_string('wikinowikitext', 'wiki'), $tag[0], $tag[1], get_string('wikinowikitext', 'wiki'));
62
63 $OUTPUT->heading(strtoupper(get_string('format' . $editor, 'wiki')));
64
65 $PAGE->requires->js('/mod/wiki/editors/wiki/buttons.js');
66
67 echo $OUTPUT->container_start('mdl-align');
68 foreach ($wiki_editor as $button) {
69 echo "<a href=\"javascript:insertTags";
70 echo "('" . $button[2] . "','" . $button[3] . "','" . $button[4] . "');\">";
71 echo "<img width=\"23\" height=\"22\" src=\"$CFG->wwwroot/mod/wiki/editors/wiki/images/$button[0]\" alt=\"" . $button[1] . "\" title=\"" . $button[1] . "\" />";
72 echo "</a>";
73 }
74 echo $OUTPUT->container_end();
75
76 echo $OUTPUT->container_start('mdl-align');
77 echo '<form method="post" id="wikiform" action="' . $action . '">';
78 echo $OUTPUT->container(print_textarea(false, 20, 60, 0, 0, "newcontent", $content, 0, true), false, 'wiki_editor');
79 echo $OUTPUT->container_start();
80 wiki_print_edit_form_default_fields($editor, $pageid, $version, $upload, $deleteuploads);
81 echo $OUTPUT->container_end();
82 echo '</form>';
83 echo $OUTPUT->container_end();
84}
b7c3ab01
RT
85
86/**
87 * Returns escaped token used by a wiki language to represent a given tag or "object" (bold -> **)
88 *
89 * @param string $format format of page
90 * @param array|string $token format tokens which needs to be escaped
91 * @return array|string
92 */
93function getTokens($format, $token) {
94 $tokens = wiki_parser_get_token($format, $token);
95
96 if (is_array($tokens)) {
97 foreach ($tokens as $key => $value) {
98 $tokens[$key] = urlencode(str_replace("'", "\'", $value));
99 }
100 } else {
101 urlencode(str_replace("'", "\'", $token));
102 }
103
104 return $tokens;
105}