Adds availability consideration for BBCodes in CKEditor toolbar
authorMatthias Schmidt <gravatronics@live.com>
Fri, 28 Jun 2013 09:30:35 +0000 (11:30 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 28 Jun 2013 09:30:35 +0000 (11:30 +0200)
com.woltlab.wcf/templates/wysiwyg.tpl
com.woltlab.wcf/templates/wysiwygToolbar.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/form/MessageForm.class.php
wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php

index 06d9dead5d7002c456aa660c7f9912b0948ca633..c89e28f720017d8536b2f2fe91c0cfa739c6b3ee 100644 (file)
@@ -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 (file)
index 0000000..6c7583d
--- /dev/null
@@ -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']);
index 1e79b36c4fdeeb5e340cecdbd7da255f5ec3a163..309e2e49af78585dbb5414aa9cc0d500bc784cf7 100644 (file)
@@ -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)));
+               }
        }
        
        /**
index 1277dcec2afe8d28f3193c0034448180fe9cf600..f8924d67809363a5a0020e95d53c910a9e3cd0a7 100644 (file)
@@ -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<string>
+        */
+       public function setAllowedBBCodes(array $bbCodes) {
+               $this->allowedBBCodes = $bbCodes;
+       }
 }