From 0f236232f1fdcad99c2b438b4a97eb94bc5472f4 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 28 Jun 2013 11:30:35 +0200 Subject: [PATCH] Adds availability consideration for BBCodes in CKEditor toolbar --- com.woltlab.wcf/templates/wysiwyg.tpl | 11 +-- com.woltlab.wcf/templates/wysiwygToolbar.tpl | 84 +++++++++++++++++++ .../files/lib/form/MessageForm.class.php | 5 ++ .../lib/system/bbcode/BBCodeHandler.class.php | 31 +++++++ 4 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 com.woltlab.wcf/templates/wysiwygToolbar.tpl diff --git a/com.woltlab.wcf/templates/wysiwyg.tpl b/com.woltlab.wcf/templates/wysiwyg.tpl index 06d9dead5d..c89e28f720 100644 --- a/com.woltlab.wcf/templates/wysiwyg.tpl +++ b/com.woltlab.wcf/templates/wysiwyg.tpl @@ -15,15 +15,8 @@ $(function() { return; } - var __CKEDITOR_TOOLBAR = [ - ['Source', '-', 'Undo', 'Redo'], - ['Bold', 'Italic', 'Underline', '-', 'Strike', 'Subscript','Superscript'], - ['NumberedList', 'BulletedList', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'], - '/', - ['Font', 'FontSize', 'TextColor'], - ['Link', 'Unlink', 'Image', 'Table', 'Smiley'], - ['Maximize'] - ]; + {include file='wysiwygToolbar'} + if (__CKEDITOR_BUTTONS.length) { var $buttons = [ ]; diff --git a/com.woltlab.wcf/templates/wysiwygToolbar.tpl b/com.woltlab.wcf/templates/wysiwygToolbar.tpl new file mode 100644 index 0000000000..6c7583d7b4 --- /dev/null +++ b/com.woltlab.wcf/templates/wysiwygToolbar.tpl @@ -0,0 +1,84 @@ +var $textStyles1 = [ ]; +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('b')} + $textStyles1.push('Bold'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('i')} + $textStyles1.push('Italic'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('u')} + $textStyles1.push('Underline'); +{/if} + +var $textStyles2 = [ ]; +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('s')} + $textStyles2.push('Strike'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('sub')} + $textStyles2.push('Subscript'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('sup')} + $textStyles2.push('Superscript'); +{/if} + +if ($textStyles2.length) { + $textStyles1.push('-'); + $textStyles1 = $textStyles1.concat($textStyles2); +} + +var $formatting = [ ]; +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('list')} + $formatting.push('NumberedList'); + $formatting.push('BulletedList'); + $formatting.push('-'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('align')} + $formatting.push('JustifyLeft'); + $formatting.push('JustifyCenter'); + $formatting.push('JustifyRight'); + $formatting.push('JustifyBlock'); +{/if} + +var $font = [ ]; +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('font')} + $font.push('Font'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('size')} + $font.push('FontSize'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('color')} + $font.push('TextColor'); +{/if} + +var $other = [ ]; +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('url')} + $other.push('Link'); + $other.push('Unlink'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('img')} + $other.push('Image'); +{/if} +{if $__wcf->getBBCodeHandler()->isAvailableBBCode('table')} + $other.push('Table'); +{/if} +{if MODULE_SMILEY && $__wcf->getSession()->getPermission($permissionCanUseSmilies) && $smileyCategories|count} + $other.push('Smiley'); +{/if} + +var __CKEDITOR_TOOLBAR = [ ]; +__CKEDITOR_TOOLBAR.push(['Source', '-', 'Undo', 'Redo']); +if ($textStyles1.length) { + __CKEDITOR_TOOLBAR.push($textStyles1); +} +if ($formatting.length) { + __CKEDITOR_TOOLBAR.push($formatting); +} +if (__CKEDITOR_TOOLBAR.length > 1) { + __CKEDITOR_TOOLBAR.push('/'); +} +if ($font.length) { + __CKEDITOR_TOOLBAR.push($font); +} +if ($other.length) { + __CKEDITOR_TOOLBAR.push($other); +} +__CKEDITOR_TOOLBAR.push(['Maximize']); diff --git a/wcfsetup/install/files/lib/form/MessageForm.class.php b/wcfsetup/install/files/lib/form/MessageForm.class.php index 1e79b36c4f..309e2e49af 100644 --- a/wcfsetup/install/files/lib/form/MessageForm.class.php +++ b/wcfsetup/install/files/lib/form/MessageForm.class.php @@ -3,6 +3,7 @@ namespace wcf\form; use wcf\data\smiley\SmileyCache; use wcf\system\attachment\AttachmentHandler; use wcf\system\bbcode\BBCodeParser; +use wcf\system\bbcode\BBCodeHandler; use wcf\system\bbcode\PreParser; use wcf\system\exception\UserInputException; use wcf\system\language\LanguageFactory; @@ -349,6 +350,10 @@ abstract class MessageForm extends RecaptchaForm { $this->defaultSmilies = SmileyCache::getInstance()->getCategorySmilies($firstCategory->categoryID ?: null); } } + + if ($this->enableBBCodes && $this->allowedBBCodesPermission) { + BBCodeHandler::getInstance()->setAllowedBBCodes(explode(',', WCF::getSession()->getPermission($this->allowedBBCodesPermission))); + } } /** diff --git a/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php b/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php index 1277dcec2a..f8924d6780 100644 --- a/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php @@ -31,6 +31,28 @@ class BBCodeHandler extends SingletonFactory { } } + /** + * Returns true if the BBCode with the given tag is available in the WYSIWYG editor. + * + * @param string $bbCodeTag + * @return boolean + */ + public function isAvailableBBCode($bbCodeTag) { + $bbCode = BBCodeCache::getInstance()->getBBCodeByTag($bbCodeTag); + if ($bbCode === null || $bbCode->isDisabled) { + return false; + } + + if (in_array('all', $this->allowedBBCodes)) { + return true; + } + else if (in_array('none', $this->allowedBBCodes)) { + return false; + } + + return in_array($bbCodeTag, $this->allowedBBCodes); + } + /** * Returns a list of BBCodes displayed as buttons. * @@ -39,4 +61,13 @@ class BBCodeHandler extends SingletonFactory { public function getButtonBBCodes() { return $this->buttonBBCodes; } + + /** + * Sets the allowed BBCodes. + * + * @param array + */ + public function setAllowedBBCodes(array $bbCodes) { + $this->allowedBBCodes = $bbCodes; + } } -- 2.20.1