Merge branch 'wip-MDL-31948-master' of git://github.com/phalacee/moodle
[moodle.git] / mod / wiki / editors / wiki_editor.php
blob8a21a0c6f328e6b2f4ca792a25f9b52380dca919
1 <?php
2 /**
3 * This file defines a simple editor
5 * @author Jordi Piguillem
6 * @author Kenneth Riba
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package wiki
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.
25 function wiki_print_editor_wiki($pageid, $content, $editor, $version = -1, $section = null, $upload = false, $deleteuploads = array(), $comesfrom = 'editorview', $commentid = 0) {
26 global $CFG, $OUTPUT, $PAGE;
28 if ($comesfrom == 'editcomments') {
29 $action = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=edit';
30 } else if ($comesfrom == 'addcomments') {
31 $action = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=add';
32 } else {
33 $action = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $pageid;
36 if (!empty($section)) {
37 $action .= "&amp;section=" . urlencode($section);
40 ///Get tags for every element we are displaying
41 $tag = getTokens($editor, 'bold');
42 $wiki_editor['bold'] = array('ed_bold.gif', get_string('wikiboldtext', 'wiki'), $tag[0], $tag[1], get_string('wikiboldtext', 'wiki'));
43 $tag = getTokens($editor, 'italic');
44 $wiki_editor['italic'] = array('ed_italic.gif', get_string('wikiitalictext', 'wiki'), $tag[0], $tag[1], get_string('wikiitalictext', 'wiki'));
45 $tag = getTokens($editor, 'link');
46 $wiki_editor['internal'] = array('ed_internal.gif', get_string('wikiinternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiinternalurl', 'wiki'));
47 $tag = getTokens($editor, 'url');
48 $wiki_editor['external'] = array('ed_external.gif', get_string('wikiexternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiexternalurl', 'wiki'));
49 $tag = getTokens($editor, 'list');
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], '', '');
52 $tag = getTokens($editor, 'image');
53 $wiki_editor['image'] = array('ed_img.gif', get_string('wikiimage', 'wiki'), $tag[0], $tag[1], get_string('wikiimage', 'wiki'));
54 $tag = getTokens($editor, 'header');
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));
58 $tag = getTokens($editor, 'line_break');
59 $wiki_editor['hr'] = array('ed_hr.gif', get_string('wikihr', 'wiki'), '\\n' . $tag . '\\n', '', '');
60 $tag = getTokens($editor, 'nowiki');
61 $wiki_editor['nowiki'] = array('ed_nowiki.gif', get_string('wikinowikitext', 'wiki'), $tag[0], $tag[1], get_string('wikinowikitext', 'wiki'));
63 $OUTPUT->heading(strtoupper(get_string('format' . $editor, 'wiki')));
65 $PAGE->requires->js('/mod/wiki/editors/wiki/buttons.js');
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>";
74 echo $OUTPUT->container_end();
76 echo $OUTPUT->container_start('mdl-align');
77 echo '<form method="post" id="mform1" 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();
86 /**
87 * Returns escaped token used by a wiki language to represent a given tag or "object" (bold -> **)
89 * @param string $format format of page
90 * @param array|string $token format tokens which needs to be escaped
91 * @return array|string
93 function getTokens($format, $token) {
94 $tokens = wiki_parser_get_token($format, $token);
96 if (is_array($tokens)) {
97 foreach ($tokens as $key => $value) {
98 $tokens[$key] = urlencode(str_replace("'", "\'", $value));
100 } else {
101 urlencode(str_replace("'", "\'", $token));
104 return $tokens;