Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / bbcode / BBCodeHandler.class.php
CommitLineData
dcc2332d
MW
1<?php
2namespace wcf\system\bbcode;
3use wcf\data\bbcode\BBCodeCache;
4use wcf\system\SingletonFactory;
5
6/**
7 * Handles BBCodes displayed as buttons within the WYSIWYG editor.
8 *
9 * @author Alexander Ebert
ca4ba303 10 * @copyright 2001-2014 WoltLab GmbH
dcc2332d 11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 12 * @package com.woltlab.wcf
dcc2332d
MW
13 * @subpackage system.bbcode
14 * @category Community Framework
15 */
16class BBCodeHandler extends SingletonFactory {
cae0330b
AE
17 /**
18 * list of BBCodes allowed for usage
0ad90fc3 19 * @var array<\wcf\data\bbcode\BBCode>
cae0330b
AE
20 */
21 protected $allowedBBCodes = array();
22
dcc2332d
MW
23 /**
24 * list of BBCodes displayed as buttons
0ad90fc3 25 * @var array<\wcf\data\bbcode\BBCode>
dcc2332d
MW
26 */
27 protected $buttonBBCodes = array();
28
978d789e
AE
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
dcc2332d 35 /**
0ad90fc3 36 * @see \wcf\system\SingletonFactory::init()
dcc2332d
MW
37 */
38 protected function init() {
39 foreach (BBCodeCache::getInstance()->getBBCodes() as $bbcode) {
40 if ($bbcode->showButton) {
41 $this->buttonBBCodes[] = $bbcode;
42 }
43 }
44 }
45
0f236232
MS
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
dcc2332d
MW
68 /**
69 * Returns a list of BBCodes displayed as buttons.
70 *
0ad90fc3 71 * @return array<\wcf\data\bbcode\BBCode>
dcc2332d
MW
72 */
73 public function getButtonBBCodes() {
7f734ff0
MW
74 $buttons = array();
75 foreach ($this->buttonBBCodes as $bbcode) {
76 if ($this->isAvailableBBCode($bbcode->bbcodeTag)) {
77 $buttons[] = $bbcode;
78 }
79 }
80
81 return $buttons;
dcc2332d 82 }
0f236232
MS
83
84 /**
85 * Sets the allowed BBCodes.
86 *
87 * @param array<string>
88 */
89 public function setAllowedBBCodes(array $bbCodes) {
90 $this->allowedBBCodes = $bbCodes;
91 }
978d789e
AE
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 }
dcc2332d 119}