Total Complexity | 77 |
Total Lines | 396 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like XoopsTopic often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsTopic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class XoopsTopic |
||
30 | { |
||
31 | /** |
||
32 | * @var \XoopsMySQLDatabase |
||
33 | */ |
||
34 | public $db; |
||
35 | public $table; |
||
36 | public $topic_id; |
||
37 | public $topic_pid; |
||
38 | public $topic_title; |
||
39 | public $topic_imgurl; |
||
40 | public $prefix; // only used in topic tree |
||
41 | public $use_permission = false; |
||
42 | public $mid; // module id used for setting permission |
||
43 | |||
44 | /** |
||
45 | * @param $table |
||
46 | * @param int|array $topicid |
||
47 | */ |
||
48 | public function __construct($table, $topicid = 0) |
||
49 | { |
||
50 | $this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
51 | $this->table = $table; |
||
52 | if (is_array($topicid)) { |
||
53 | $this->makeTopic($topicid); |
||
54 | } elseif ($topicid != 0) { |
||
55 | $this->getTopic((int) $topicid); |
||
56 | } else { |
||
57 | $this->topic_id = $topicid; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param $value |
||
63 | */ |
||
64 | public function setTopicTitle($value) |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param $value |
||
71 | */ |
||
72 | public function setTopicImgurl($value) |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $value |
||
79 | */ |
||
80 | public function setTopicPid($value) |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $topicid |
||
87 | */ |
||
88 | public function getTopic($topicid) |
||
89 | { |
||
90 | $topicid = (int) $topicid; |
||
91 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . ''; |
||
92 | $result = $this->db->query($sql); |
||
93 | if (!$this->db->isResultSet($result)) { |
||
94 | throw new \RuntimeException( |
||
95 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), |
||
96 | E_USER_ERROR, |
||
97 | ); |
||
98 | } |
||
99 | $array = $this->db->fetchArray($result); |
||
|
|||
100 | $this->makeTopic($array); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param $array |
||
105 | */ |
||
106 | public function makeTopic($array) |
||
107 | { |
||
108 | foreach ($array as $key => $value) { |
||
109 | $this->$key = $value; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param $mid |
||
115 | */ |
||
116 | public function usePermission($mid) |
||
117 | { |
||
118 | $this->mid = $mid; |
||
119 | $this->use_permission = true; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function store() |
||
126 | { |
||
127 | $myts = \MyTextSanitizer::getInstance(); |
||
128 | $title = ''; |
||
129 | $imgurl = ''; |
||
130 | if (isset($this->topic_title) && $this->topic_title != '') { |
||
131 | $title = $myts->addSlashes($this->topic_title); |
||
132 | } |
||
133 | if (isset($this->topic_imgurl) && $this->topic_imgurl != '') { |
||
134 | $imgurl = $myts->addSlashes($this->topic_imgurl); |
||
135 | } |
||
136 | if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) { |
||
137 | $this->topic_pid = 0; |
||
138 | } |
||
139 | if (empty($this->topic_id)) { |
||
140 | $this->topic_id = $this->db->genId($this->table . '_topic_id_seq'); |
||
141 | $sql = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title); |
||
142 | } else { |
||
143 | $sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id); |
||
144 | } |
||
145 | if (!$result = $this->db->query($sql)) { |
||
146 | ErrorHandler::show('0022'); |
||
147 | } |
||
148 | if ($this->use_permission == true) { |
||
149 | if (empty($this->topic_id)) { |
||
150 | $this->topic_id = $this->db->getInsertId(); |
||
151 | } |
||
152 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
153 | $parent_topics = $xt->getAllParentId($this->topic_id); |
||
154 | if (!empty($this->m_groups) && \is_array($this->m_groups)) { |
||
155 | foreach ($this->m_groups as $m_g) { |
||
156 | $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g); |
||
157 | $add = true; |
||
158 | // only grant this permission when the group has this permission in all parent topics of the created topic |
||
159 | foreach ($parent_topics as $p_topic) { |
||
160 | if (!in_array($p_topic, $moderate_topics)) { |
||
161 | $add = false; |
||
162 | continue; |
||
163 | } |
||
164 | } |
||
165 | if ($add == true) { |
||
166 | $xp = new XoopsPerms(); |
||
167 | $xp->setModuleId($this->mid); |
||
168 | $xp->setName('ModInTopic'); |
||
169 | $xp->setItemId($this->topic_id); |
||
170 | $xp->store(); |
||
171 | $xp->addGroup($m_g); |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | if (!empty($this->s_groups) && \is_array($this->s_groups)) { |
||
176 | foreach ($s_groups as $s_g) { |
||
177 | $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g); |
||
178 | $add = true; |
||
179 | foreach ($parent_topics as $p_topic) { |
||
180 | if (!in_array($p_topic, $submit_topics)) { |
||
181 | $add = false; |
||
182 | continue; |
||
183 | } |
||
184 | } |
||
185 | if ($add == true) { |
||
186 | $xp = new XoopsPerms(); |
||
187 | $xp->setModuleId($this->mid); |
||
188 | $xp->setName('SubmitInTopic'); |
||
189 | $xp->setItemId($this->topic_id); |
||
190 | $xp->store(); |
||
191 | $xp->addGroup($s_g); |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | if (!empty($this->r_groups) && \is_array($this->r_groups)) { |
||
196 | foreach ($r_groups as $r_g) { |
||
197 | $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g); |
||
198 | $add = true; |
||
199 | foreach ($parent_topics as $p_topic) { |
||
200 | if (!in_array($p_topic, $read_topics)) { |
||
201 | $add = false; |
||
202 | continue; |
||
203 | } |
||
204 | } |
||
205 | if ($add == true) { |
||
206 | $xp = new XoopsPerms(); |
||
207 | $xp->setModuleId($this->mid); |
||
208 | $xp->setName('ReadInTopic'); |
||
209 | $xp->setItemId($this->topic_id); |
||
210 | $xp->store(); |
||
211 | $xp->addGroup($r_g); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | return true; |
||
218 | } |
||
219 | |||
220 | public function delete() |
||
221 | { |
||
222 | $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id); |
||
223 | $this->db->query($sql); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return int |
||
228 | */ |
||
229 | public function topic_id() |
||
230 | { |
||
231 | return $this->topic_id; |
||
232 | } |
||
233 | |||
234 | public function topic_pid() |
||
235 | { |
||
236 | return $this->topic_pid; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param string $format |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | public function topic_title($format = 'S') |
||
245 | { |
||
246 | $myts = \MyTextSanitizer::getInstance(); |
||
247 | switch ($format) { |
||
248 | case 'S': |
||
249 | case 'E': |
||
250 | $title = $myts->htmlSpecialChars($this->topic_title); |
||
251 | break; |
||
252 | case 'P': |
||
253 | case 'F': |
||
254 | $title = $myts->htmlSpecialChars($this->topic_title); |
||
255 | break; |
||
256 | } |
||
257 | |||
258 | return $title; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $format |
||
263 | * |
||
264 | * @return mixed |
||
265 | */ |
||
266 | public function topic_imgurl($format = 'S') |
||
267 | { |
||
268 | $myts = \MyTextSanitizer::getInstance(); |
||
269 | switch ($format) { |
||
270 | case 'S': |
||
271 | case 'E': |
||
272 | $imgurl = $myts->htmlSpecialChars($this->topic_imgurl); |
||
273 | break; |
||
274 | case 'P': |
||
275 | case 'F': |
||
276 | $imgurl = $myts->htmlSpecialChars($this->topic_imgurl); |
||
277 | break; |
||
278 | } |
||
279 | |||
280 | return $imgurl; |
||
281 | } |
||
282 | |||
283 | public function prefix() |
||
284 | { |
||
285 | return $this->prefix ?? null; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getFirstChildTopics() |
||
292 | { |
||
293 | $ret = []; |
||
294 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
295 | $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title'); |
||
296 | if (!empty($topic_arr) && \is_array($topic_arr)) { |
||
297 | foreach ($topic_arr as $topic) { |
||
298 | $ret[] = new XoopsTopic($this->table, $topic); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | return $ret; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @return array |
||
307 | */ |
||
308 | public function getAllChildTopics() |
||
309 | { |
||
310 | $ret = []; |
||
311 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
312 | $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title'); |
||
313 | if (!empty($topic_arr) && \is_array($topic_arr)) { |
||
314 | foreach ($topic_arr as $topic) { |
||
315 | $ret[] = new XoopsTopic($this->table, $topic); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return $ret; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getChildTopicsTreeArray() |
||
326 | { |
||
327 | $ret = []; |
||
328 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
329 | $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title'); |
||
330 | if (!empty($topic_arr) && \is_array($topic_arr)) { |
||
331 | foreach ($topic_arr as $topic) { |
||
332 | $ret[] = new XoopsTopic($this->table, $topic); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | return $ret; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @param int $none |
||
341 | * @param $seltopic |
||
342 | * @param string $selname |
||
343 | * @param string $onchange |
||
344 | */ |
||
345 | public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '') |
||
346 | { |
||
347 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
348 | if ($seltopic != -1) { |
||
349 | $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange); |
||
350 | } elseif (!empty($this->topic_id)) { |
||
351 | $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange); |
||
352 | } else { |
||
353 | $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange); |
||
354 | } |
||
355 | } |
||
356 | |||
357 | //generates nicely formatted linked path from the root id to a given id |
||
358 | /** |
||
359 | * @param $funcURL |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | public function getNiceTopicPathFromId($funcURL) |
||
364 | { |
||
365 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
366 | $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL); |
||
367 | |||
368 | return $ret; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return array |
||
373 | */ |
||
374 | public function getAllChildTopicsId() |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @return array |
||
384 | */ |
||
385 | public function getTopicsList() |
||
386 | { |
||
387 | $sql = 'SELECT topic_id, topic_pid, topic_title FROM ' . $this->table; |
||
388 | $result = $this->db->query($sql); |
||
389 | if (!$this->db->isResultSet($result)) { |
||
390 | throw new \RuntimeException( |
||
391 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), |
||
392 | E_USER_ERROR, |
||
393 | ); |
||
394 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @param $pid |
||
406 | * @param $title |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | public function topicExists($pid, $title) |
||
425 | } |
||
426 | } |
||
428 |