Commit | Line | Data |
---|---|---|
dcc2332d | 1 | <?php |
a9229942 | 2 | |
dcc2332d | 3 | namespace wcf\data\smiley; |
a9229942 | 4 | |
dcc2332d MW |
5 | use wcf\data\category\Category; |
6 | use wcf\data\smiley\category\SmileyCategory; | |
7 | use wcf\system\cache\builder\SmileyCacheBuilder; | |
8 | use wcf\system\category\CategoryHandler; | |
9 | use wcf\system\SingletonFactory; | |
4b0da844 | 10 | use wcf\system\WCF; |
dcc2332d MW |
11 | |
12 | /** | |
13 | * Manages the smiley cache. | |
a9229942 TD |
14 | * |
15 | * @author Marcel Werk | |
16 | * @copyright 2001-2019 WoltLab GmbH | |
17 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
dcc2332d | 18 | */ |
a9229942 TD |
19 | class SmileyCache extends SingletonFactory |
20 | { | |
21 | /** | |
22 | * cached smilies | |
23 | * @var Smiley[][] | |
24 | */ | |
25 | protected $cachedSmilies = []; | |
26 | ||
27 | /** | |
28 | * cached smilies with smiley code as key | |
29 | * @var Smiley[] | |
30 | */ | |
31 | protected $cachedSmileyByCode = []; | |
32 | ||
33 | /** | |
34 | * cached smiley categories | |
35 | * @var SmileyCategory[] | |
36 | */ | |
37 | protected $cachedCategories = []; | |
38 | ||
39 | /** | |
40 | * enabled smiley categories with at least one smiley | |
41 | * @var SmileyCategory[] | |
42 | */ | |
43 | protected $visibleCategories; | |
44 | ||
cee76330 C |
45 | /** |
46 | * @var Smiley[] | |
47 | */ | |
48 | protected array $emojis; | |
49 | ||
a9229942 TD |
50 | /** |
51 | * @inheritDoc | |
52 | */ | |
53 | protected function init() | |
54 | { | |
55 | // get smiley cache | |
56 | $this->cachedSmilies = SmileyCacheBuilder::getInstance()->getData([], 'smilies'); | |
57 | $this->cachedSmileyByCode = SmileyCacheBuilder::getInstance()->getData([], 'codes'); | |
58 | $smileyCategories = CategoryHandler::getInstance()->getCategories('com.woltlab.wcf.bbcode.smiley'); | |
59 | ||
60 | $this->cachedCategories[null] = new SmileyCategory(new Category(null, [ | |
61 | 'categoryID' => null, | |
62 | 'parentCategoryID' => 0, | |
63 | 'title' => 'wcf.acp.smiley.categoryID.default', | |
64 | 'description' => '', | |
65 | 'showOrder' => -1, | |
66 | 'isDisabled' => 0, | |
67 | ])); | |
68 | ||
69 | foreach ($smileyCategories as $key => $smileyCategory) { | |
70 | $this->cachedCategories[$key] = new SmileyCategory($smileyCategory); | |
71 | } | |
72 | } | |
73 | ||
74 | /** | |
75 | * Returns all smilies. | |
76 | * | |
77 | * @return Smiley[][] | |
78 | */ | |
79 | public function getSmilies() | |
80 | { | |
81 | return $this->cachedSmilies; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Returns the smiley with the given smiley code or `null` if no such smiley exists. | |
86 | * | |
87 | * @param string $code | |
88 | * @return Smiley|null | |
89 | */ | |
90 | public function getSmileyByCode($code) | |
91 | { | |
813c41ce | 92 | return $this->cachedSmileyByCode[$code] ?? null; |
a9229942 TD |
93 | } |
94 | ||
95 | /** | |
96 | * Returns all smiley categories. | |
97 | * | |
98 | * @return SmileyCategory[] | |
99 | */ | |
100 | public function getCategories() | |
101 | { | |
102 | return $this->cachedCategories; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Returns all enabled smiley categories with at least one smiley. | |
107 | * | |
108 | * @return SmileyCategory[] | |
109 | */ | |
110 | public function getVisibleCategories() | |
111 | { | |
112 | if ($this->visibleCategories === null) { | |
113 | $this->visibleCategories = []; | |
114 | ||
115 | foreach ($this->cachedCategories as $key => $category) { | |
116 | if (!$category->isDisabled) { | |
117 | $category->loadSmilies(); | |
118 | ||
119 | if (\count($category)) { | |
120 | $this->visibleCategories[$key] = $category; | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | return $this->visibleCategories; | |
127 | } | |
128 | ||
129 | /** | |
130 | * Returns all the smilies of a category. | |
131 | * | |
132 | * @param int $categoryID | |
133 | * @return array | |
134 | */ | |
135 | public function getCategorySmilies($categoryID = null) | |
136 | { | |
137 | if (isset($this->cachedSmilies[$categoryID])) { | |
138 | return $this->cachedSmilies[$categoryID]; | |
139 | } | |
140 | ||
141 | return []; | |
142 | } | |
143 | ||
144 | /** | |
145 | * Assigns the smilies and their categories to the template. | |
146 | * | |
147 | * @since 5.2 | |
148 | */ | |
149 | public function assignVariables() | |
150 | { | |
151 | if (!MODULE_SMILEY) { | |
152 | return; | |
153 | } | |
154 | ||
155 | $smileyCategories = $this->getVisibleCategories(); | |
156 | $firstCategory = \reset($smileyCategories); | |
157 | $defaultSmilies = []; | |
158 | if ($firstCategory) { | |
159 | $defaultSmilies = $this->getCategorySmilies($firstCategory->categoryID ?: null); | |
160 | } | |
161 | ||
162 | WCF::getTPL()->assign([ | |
163 | 'defaultSmilies' => $defaultSmilies, | |
164 | 'smileyCategories' => $smileyCategories, | |
165 | ]); | |
166 | } | |
cee76330 C |
167 | |
168 | /** | |
5d289271 | 169 | * Return all smileys that match `:[a-z]+(?:_[a-z]+)*:`. |
ab44e129 | 170 | * |
cee76330 | 171 | * @return Smiley[] |
5d289271 | 172 | * @since 6.1 |
cee76330 C |
173 | */ |
174 | public function getEmojis(): array | |
175 | { | |
176 | if (!isset($this->emojis)) { | |
177 | $this->emojis = []; | |
178 | foreach ($this->getVisibleCategories() as $category) { | |
179 | foreach ($category as $smiley) { | |
180 | foreach ($smiley->smileyCodes as $smileyCode) { | |
ab44e129 | 181 | if (\preg_match('~^:[a-z]+(?:_[a-z]+)*:$~', $smileyCode)) { |
cee76330 C |
182 | $this->emojis[$smileyCode] = $smiley; |
183 | } | |
184 | } | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | return $this->emojis; | |
190 | } | |
dcc2332d | 191 | } |