Commit 24939aa8 authored by Aaron Wells's avatar Aaron Wells Committed by Gerrit Code Review
Browse files

Change the Wall block from using BBCode to TinyMCE

Bug 1415711

Change-Id: Ia8ec1085df32cf7453ac0f669dca1a926f8f5efc
parent b6839bb0
......@@ -138,10 +138,9 @@ class PluginBlocktypeWall extends SystemBlocktype {
'jssuccesscallback' => 'wallpost_success',
'elements' => array(
'text' => array(
'type' => 'textarea',
'type' => 'wysiwyg',
'title' => get_string('Post', 'blocktype.wall'),
'hiddenlabel' => true,
'description' => bbcode_format_post_message(),
'rows' => 3,
'cols' => 50,
'defaultvalue' => '',
......@@ -183,11 +182,16 @@ function wallpost_success(form, data) {
if ($('wall') && data.posts && data.block) {
var wall = getFirstElementByTagAndClassName('div', 'wall', 'blockinstance_' + data.block);
var temp = DIV();
var textareaid = 'wallpost_' + data.block + '_text';
temp.innerHTML = data.posts;
newposts = getElementsByTagAndClassName('li', 'wallpost', temp);
replaceChildNodes(wall, newposts);
if ($('wallpost_' + data.block + '_text')) {
$('wallpost_' + data.block + '_text').value = '';
if ($(textareaid)) {
$(textareaid).value = '';
// Clear TinyMCE
if (typeof(tinyMCE) != 'undefined' && typeof(tinyMCE.get(textareaid)) != 'undefined') {
tinyMCE.activeEditor.setContent('');
}
}
formSuccess(form, data);
}
......
<!-- @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. -->
<h3>BBCode</h3>
<p>BBCode gives you a way to include formatting like <strong>bold</strong> or <em>italics</em> in your messages.</p>
<p>For example, to make a word bold, you would write it like this: [b]hello[/b]. In your message, the word will show up as <strong>hello</strong>. The BBCode markers - the [b] and [/b] - disappear.</p>
<p>The following BBCodes are available:</p>
<ul>
<li><strong>[b]</strong> and <strong>[/b]</strong> to make words <strong>bold</strong> - e.g. [b]ponies[/b].</li>
<li><strong>[i]</strong> and <strong>[/i]</strong> to make words <em>italic</em> - e.g. [i]amazing![/i].</li>
<li><strong>[img]</strong> and <strong>[/img]</strong> to insert an image - e.g. [img]http://mahara.org/favicon.ico[/img]</li>
<li><strong>[url]</strong> and <strong>[/url]</strong> to insert a link - e.g. [url]http://google.co.nz[/url] or [url=http://google.co.nz/]Google[/url]</li>
</ul>
<p>Note that links in your message will automatically be made clickable.</p>
......@@ -1081,7 +1081,6 @@ $string['done'] = 'Done';
$string['back'] = 'Back';
$string['backto'] = 'Back to %s';
$string['alphabet'] = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z';
$string['formatpostbbcode'] = 'You can format your post using %sBBCode%s.';
$string['Created'] = 'Created';
$string['Updated'] = 'Updated';
$string['Total'] = 'Total';
......
......@@ -407,6 +407,7 @@ function xmldb_core_upgrade($oldversion=0) {
}
if ($oldversion < 2009101600) {
require_once(get_config('docroot').'/lib/stringparser_bbcode/lib.php');
// Remove bbcode formatting from existing feedback
if ($records = get_records_sql_array("SELECT * FROM {view_feedback} WHERE message LIKE '%[%'", array())) {
foreach ($records as &$r) {
......@@ -3942,5 +3943,16 @@ function xmldb_core_upgrade($oldversion=0) {
execute_Sql("UPDATE {view} SET skin = NULL WHERE id IN ( SELECT vid FROM (SELECT id AS vid FROM {view} WHERE type = 'dashboard' AND skin IS NOT NULL) AS temptable)");
}
if ($oldversion < 2015021900) {
require_once(get_config('docroot').'/lib/stringparser_bbcode/lib.php');
// Remove bbcode formatting from existing wall posts
if ($records = get_records_sql_array("SELECT id, text FROM {blocktype_wall_post} WHERE text LIKE '%[%'", array())) {
foreach ($records as &$r) {
$r->text = parse_bbcode($r->text);
update_record('blocktype_wall_post', $r);
}
}
}
return $status;
}
<?php
/**
* Converts bbcodes in the given text to HTML. Also auto-links URLs.
*
* @param string $text The text to parse
* @return string
*/
function parse_bbcode($text) {
require_once('stringparser_bbcode/stringparser_bbcode.class.php');
$bbcode = new StringParser_BBCode();
$bbcode->setGlobalCaseSensitive(false);
$bbcode->setRootParagraphHandling(true);
// Convert all newlines to a common form
$bbcode->addFilter(STRINGPARSER_FILTER_PRE, create_function('$a', 'return preg_replace("/\015\012|015\012/", "\n", $a);'));
$bbcode->addParser(array('block', 'inline'), 'format_whitespace');
$bbcode->addParser(array('block', 'inline'), 'autolink_text');
// The bbcodes themselves
$bbcode->addCode('b', 'simple_replace', null, array ('start_tag' => '<strong>', 'end_tag' => '</strong>'),
'inline', array('listitem', 'block', 'inline', 'link'), array());
$bbcode->addCode ('i', 'simple_replace', null, array ('start_tag' => '<em>', 'end_tag' => '</em>'),
'inline', array('listitem', 'block', 'inline', 'link'), array());
$bbcode->addCode ('url', 'usecontent?', 'bbcode_url', array('usecontent_param' => 'default'),
'link', array('listitem', 'block', 'inline'), array('link'));
$bbcode->addCode ('img', 'usecontent', 'bbcode_img', array(),
'image', array ('listitem', 'block', 'inline', 'link'), array());
$text = $bbcode->parse($text);
return $text;
}
/**
* Given some text, locates URLs in it and converts them to HTML
*
* @param string $text The text to locate URLs in
* @return string
*
* {@internal{Note, it's perhaps unreasonably expected that the input to this
* function is HTML escaped already. Especially because it's expected that
* there are no <a href="...">s in there. This works for now because the bbcode
* parser breaks things out into tokens, but this function might need reworking
* to be more useful in other places.}}
*/
function autolink_text($text) {
$text = preg_replace(
'#(^|.)(https?://\S+)#me',
"_autolink_text_helper('$2', '$1')",
$text
);
return $text;
}
/**
* Helps autolink_text by providing the HTML to link up URLs found.
*
* Intelligently decides what parts of the matched URL should be linked up, to
* get around issues where URLs are surrounded by brackets or have trailing
* punctuation on them
*
* @param string $potentialurl The URL to check. It should already have been run through hsc()
* @param string $leadingcharacter The character (if any) before the URL. Used
* to check for URLs surrounded by brackets
*/
function _autolink_text_helper($potentialurl, $leadingcharacter) {
static $brackets = array('(' => ')', '{' => '}', '[' => ']', "'" => "'");
$trailingcharacter = substr($potentialurl, -1);
$startofurl = substr($potentialurl, 0, -1);
// Attempt to intelligently handle several annoyances that happen with URL
// auto linking. We don't want to link up brackets if the URL is enclosed
// in them. We also don't want to link up punctuation after URLs
if (in_array($leadingcharacter, array_keys($brackets)) &&
in_array($trailingcharacter, $brackets)) {
// The URL was surrounded by brackets
return $leadingcharacter . '<a href="' . $startofurl . '">' . $startofurl . '</a>' . $trailingcharacter;
}
else {
foreach($brackets as $opener => $closer) {
if ($trailingcharacter == $closer &&
false === strpos($startofurl, $opener)) {
// The URL ended in a bracket and didn't contain one
// Note that we can't just use this clause without using the clause
// about URLs surrounded by brackets, because otherwise we won't catch
// URLs with balanced brackets in them like http://url/?(foo)&bar=1
return $leadingcharacter . '<a href="' . $startofurl . '">' . $startofurl . '</a>' . $trailingcharacter;
}
}
// Check for trailing punctuation
if (in_array($trailingcharacter, array('.', ',', '!', '?'))) {
return $leadingcharacter . '<a href="' . $startofurl . '">' . $startofurl . '</a>' . $trailingcharacter;
}
else {
return $leadingcharacter . '<a href="' . $potentialurl . '">' . $potentialurl . '</a>';
}
}
// Execution should never get here
return $potentialurl;
}
/**
* Callback for StringParser_BBCode to handle [url] and [link] bbcode
*/
function bbcode_url($action, $attributes, $content, $params, $node_object) {
if (!isset ($attributes['default'])) {
$url = $content;
$text = hsc($content);
}
else {
$url = $attributes['default'];
$text = $content;
}
if ($action == 'validate') {
$valid_protos = array('http://', 'https://', 'ftp://');
foreach ($valid_protos as $proto) {
if (substr($url, 0, strlen($proto)) == $proto) {
return true;
}
}
return false;
}
return '<a href="' . hsc($url) . '">' . $text . '</a>';
}
/**
* Callback for StringParser_BBCode to handle [img] bbcode
*/
function bbcode_img($action, $attributes, $content, $params, $node_object) {
if ($action == 'validate') {
$valid_protos = array('http://', 'https://');
foreach ($valid_protos as $proto) {
if (substr($content, 0, strlen($proto)) == $proto) {
return true;
}
}
return false;
}
return '<img src="' . hsc($content) . '" alt="">';
}
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/index.php/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2015021000;
$config->version = 2015021900;
$config->series = '15.04';
$config->release = '15.04dev';
$config->minupgradefrom = 2009022600;
......
......@@ -3244,39 +3244,6 @@ function has_page_help() {
// Cleaning/formatting functions
//
/**
* Converts bbcodes in the given text to HTML. Also auto-links URLs.
*
* @param string $text The text to parse
* @return string
*/
function parse_bbcode($text) {
require_once('stringparser_bbcode/stringparser_bbcode.class.php');
$bbcode = new StringParser_BBCode();
$bbcode->setGlobalCaseSensitive(false);
$bbcode->setRootParagraphHandling(true);
// Convert all newlines to a common form
$bbcode->addFilter(STRINGPARSER_FILTER_PRE, create_function('$a', 'return preg_replace("/\015\012|015\012/", "\n", $a);'));
$bbcode->addParser(array('block', 'inline'), 'format_whitespace');
$bbcode->addParser(array('block', 'inline'), 'autolink_text');
// The bbcodes themselves
$bbcode->addCode('b', 'simple_replace', null, array ('start_tag' => '<strong>', 'end_tag' => '</strong>'),
'inline', array('listitem', 'block', 'inline', 'link'), array());
$bbcode->addCode ('i', 'simple_replace', null, array ('start_tag' => '<em>', 'end_tag' => '</em>'),
'inline', array('listitem', 'block', 'inline', 'link'), array());
$bbcode->addCode ('url', 'usecontent?', 'bbcode_url', array('usecontent_param' => 'default'),
'link', array('listitem', 'block', 'inline'), array('link'));
$bbcode->addCode ('img', 'usecontent', 'bbcode_img', array(),
'image', array ('listitem', 'block', 'inline', 'link'), array());
$text = $bbcode->parse($text);
return $text;
}
/**
* Given some plain text, adds the appropriate HTML to it to make it appear in
* an HTML document with the same formatting
......@@ -3481,126 +3448,6 @@ function html2text($html, $fragment=true) {
return $h2t->text();
}
/**
* Given some text, locates URLs in it and converts them to HTML
*
* @param string $text The text to locate URLs in
* @return string
*
* {@internal{Note, it's perhaps unreasonably expected that the input to this
* function is HTML escaped already. Especially because it's expected that
* there are no <a href="...">s in there. This works for now because the bbcode
* parser breaks things out into tokens, but this function might need reworking
* to be more useful in other places.}}
*/
function autolink_text($text) {
$text = preg_replace(
'#(^|.)(https?://\S+)#me',
"_autolink_text_helper('$2', '$1')",
$text
);
return $text;
}
/**
* Helps autolink_text by providing the HTML to link up URLs found.
*
* Intelligently decides what parts of the matched URL should be linked up, to
* get around issues where URLs are surrounded by brackets or have trailing
* punctuation on them
*
* @param string $potentialurl The URL to check. It should already have been run through hsc()
* @param string $leadingcharacter The character (if any) before the URL. Used
* to check for URLs surrounded by brackets
*/
function _autolink_text_helper($potentialurl, $leadingcharacter) {
static $brackets = array('(' => ')', '{' => '}', '[' => ']', "'" => "'");
$trailingcharacter = substr($potentialurl, -1);
$startofurl = substr($potentialurl, 0, -1);
// Attempt to intelligently handle several annoyances that happen with URL
// auto linking. We don't want to link up brackets if the URL is enclosed
// in them. We also don't want to link up punctuation after URLs
if (in_array($leadingcharacter, array_keys($brackets)) &&
in_array($trailingcharacter, $brackets)) {
// The URL was surrounded by brackets
return $leadingcharacter . '<a href="' . $startofurl . '">' . $startofurl . '</a>' . $trailingcharacter;
}
else {
foreach($brackets as $opener => $closer) {
if ($trailingcharacter == $closer &&
false === strpos($startofurl, $opener)) {
// The URL ended in a bracket and didn't contain one
// Note that we can't just use this clause without using the clause
// about URLs surrounded by brackets, because otherwise we won't catch
// URLs with balanced brackets in them like http://url/?(foo)&bar=1
return $leadingcharacter . '<a href="' . $startofurl . '">' . $startofurl . '</a>' . $trailingcharacter;
}
}
// Check for trailing punctuation
if (in_array($trailingcharacter, array('.', ',', '!', '?'))) {
return $leadingcharacter . '<a href="' . $startofurl . '">' . $startofurl . '</a>' . $trailingcharacter;
}
else {
return $leadingcharacter . '<a href="' . $potentialurl . '">' . $potentialurl . '</a>';
}
}
// Execution should never get here
return $potentialurl;
}
/**
* Callback for StringParser_BBCode to handle [url] and [link] bbcode
*/
function bbcode_url($action, $attributes, $content, $params, $node_object) {
if (!isset ($attributes['default'])) {
$url = $content;
$text = hsc($content);
}
else {
$url = $attributes['default'];
$text = $content;
}
if ($action == 'validate') {
$valid_protos = array('http://', 'https://', 'ftp://');
foreach ($valid_protos as $proto) {
if (substr($url, 0, strlen($proto)) == $proto) {
return true;
}
}
return false;
}
return '<a href="' . hsc($url) . '">' . $text . '</a>';
}
/**
* Callback for StringParser_BBCode to handle [img] bbcode
*/
function bbcode_img($action, $attributes, $content, $params, $node_object) {
if ($action == 'validate') {
$valid_protos = array('http://', 'https://');
foreach ($valid_protos as $proto) {
if (substr($content, 0, strlen($proto)) == $proto) {
return true;
}
}
return false;
}
return '<img src="' . hsc($content) . '" alt="">';
}
/**
* Returns a message that can be used as help text for BBCode
*
* @return string
*/
function bbcode_format_post_message() {
return get_string('formatpostbbcode', 'mahara', '<a href="" onclick="contextualHelp(\'\',\'\',\'core\',\'site\',null,\'bbcode\',this); return false;">', '</a>');
}
/**
* Displays purified html on a page with an explanatory message.
*
......
......@@ -4,7 +4,7 @@
// element, but there's no validation or descriptions on this form currently
echo $form_tag;
echo '<div id="wall"><div class="description">' . $elements['postsizelimit']['html'] . ' ' . $elements['text']['description'] . '</div>';
echo '<div id="wall"><div class="description">' . $elements['postsizelimit']['html'] . '</div>';
echo '<div>' . $elements['text']['labelhtml'] . $elements['text']['html'] .'</div>';
if (isset($elements['text']['error'])) {
echo '<div class="errmsg">' . $elements['text']['error'] . '</div>';
......
......@@ -7,7 +7,7 @@
{foreach from=$wallposts item=wallpost}
<li class="wallpost{if $wallpost->private} private{/if} {cycle name=rows values='r0,r1'}">
<div class="userinfo"><img src="{profile_icon_url user=$wallpost maxheight=25 maxwidth=25}" alt="{str tag=profileimagetext arg1=$wallpost|display_default_name}"><a href="{$wallpost->profileurl}">{$wallpost->displayname}</a> - <span class="postedon">{$wallpost->postdate|format_date}</span></div>
<div class="detail">{$wallpost->text|parse_bbcode|safe}</div>
<div class="detail">{$wallpost->text|safe|clean_html}</div>
<div class="controls">
{* {if $ownwall}
<a href="{$WWWROOT}blocktype/wall/wall.php?instance={$instanceid}&amp;replyto={$wallpost->id}" class="btn-reply">{str tag='reply' section='blocktype.wall'}</a>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment