Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / bbcode / BBCodeHandler.class.php
1 <?php
2 namespace wcf\system\bbcode;
3 use wcf\data\bbcode\BBCodeCache;
4 use wcf\system\SingletonFactory;
5
6 /**
7 * Handles BBCodes displayed as buttons within the WYSIWYG editor.
8 *
9 * @author Alexander Ebert
10 * @copyright 2001-2014 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package com.woltlab.wcf
13 * @subpackage system.bbcode
14 * @category Community Framework
15 */
16 class BBCodeHandler extends SingletonFactory {
17 /**
18 * list of BBCodes allowed for usage
19 * @var array<\wcf\data\bbcode\BBCode>
20 */
21 protected $allowedBBCodes = array();
22
23 /**
24 * list of BBCodes displayed as buttons
25 * @var array<\wcf\data\bbcode\BBCode>
26 */
27 protected $buttonBBCodes = array();
28
29 /**
30 * list of BBCodes which contain raw code (disabled BBCode parsing)
31 * @var array<\wcf\data\bbcode\BBCode>
32 */
33 protected $sourceBBCodes = null;
34
35 /**
36 * @see \wcf\system\SingletonFactory::init()
37 */
38 protected function init() {
39 foreach (BBCodeCache::getInstance()->getBBCodes() as $bbcode) {
40 if ($bbcode->showButton) {
41 $this->buttonBBCodes[] = $bbcode;
42 }
43 }
44 }
45
46 /**
47 * Returns true if the BBCode with the given tag is available in the WYSIWYG editor.
48 *
49 * @param string $bbCodeTag
50 * @return boolean
51 */
52 public function isAvailableBBCode($bbCodeTag) {
53 $bbCode = BBCodeCache::getInstance()->getBBCodeByTag($bbCodeTag);
54 if ($bbCode === null || $bbCode->isDisabled) {
55 return false;
56 }
57
58 if (in_array('all', $this->allowedBBCodes)) {
59 return true;
60 }
61 else if (in_array('none', $this->allowedBBCodes)) {
62 return false;
63 }
64
65 return in_array($bbCodeTag, $this->allowedBBCodes);
66 }
67
68 /**
69 * Returns a list of BBCodes displayed as buttons.
70 *
71 * @return array<\wcf\data\bbcode\BBCode>
72 */
73 public function getButtonBBCodes() {
74 $buttons = array();
75 foreach ($this->buttonBBCodes as $bbcode) {
76 if ($this->isAvailableBBCode($bbcode->bbcodeTag)) {
77 $buttons[] = $bbcode;
78 }
79 }
80
81 return $buttons;
82 }
83
84 /**
85 * Sets the allowed BBCodes.
86 *
87 * @param array<string>
88 */
89 public function setAllowedBBCodes(array $bbCodes) {
90 $this->allowedBBCodes = $bbCodes;
91 }
92
93 /**
94 * Returns a list of BBCodes which contain raw code (disabled BBCode parsing)
95 *
96 * @return array<\wcf\data\bbcode\BBCode>
97 */
98 public function getSourceBBCodes() {
99 if (empty($this->allowedBBCodes)) {
100 return array();
101 }
102
103 if ($this->sourceBBCodes === null) {
104 $this->sourceBBCodes = array();
105
106 foreach (BBCodeCache::getInstance()->getBBCodes() as $bbcode) {
107 if (!$bbcode->isSourceCode) {
108 continue;
109 }
110
111 if ($this->isAvailableBBCode($bbcode->bbcodeTag)) {
112 $this->sourceBBCodes[] = $bbcode;
113 }
114 }
115 }
116
117 return $this->sourceBBCodes;
118 }
119 }