| Total Complexity | 75 |
| Total Lines | 389 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 32 | class XoopsTopic |
||
| 33 | { |
||
| 34 | public $table; |
||
| 35 | public $topic_id; |
||
| 36 | public $topic_pid; |
||
| 37 | public $topic_title; |
||
| 38 | public $topic_imgurl; |
||
| 39 | public $prefix; // only used in topic tree |
||
| 40 | public $use_permission = false; |
||
| 41 | public $mid; // module id used for setting permission |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param $table |
||
| 45 | * @param int $topicid |
||
| 46 | */ |
||
| 47 | public function __construct($table, $topicid = 0) |
||
| 48 | { |
||
| 49 | /** @var \XoopsMySQLDatabase $db */ |
||
| 50 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
|
|
|||
| 51 | $this->table = $table; |
||
| 52 | if (\is_array($topicid)) { |
||
| 53 | $this->makeTopic($topicid); |
||
| 54 | } elseif (0 != $topicid) { |
||
| 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) |
||
| 65 | { |
||
| 66 | $this->topic_title = $value; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param $value |
||
| 71 | */ |
||
| 72 | public function setTopicImgurl($value) |
||
| 73 | { |
||
| 74 | $this->topic_imgurl = $value; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $value |
||
| 79 | */ |
||
| 80 | public function setTopicPid($value) |
||
| 81 | { |
||
| 82 | $this->topic_pid = $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 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
| 93 | $this->makeTopic($array); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $array |
||
| 98 | */ |
||
| 99 | public function makeTopic($array) |
||
| 100 | { |
||
| 101 | foreach ($array as $key => $value) { |
||
| 102 | $this->$key = $value; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param $mid |
||
| 108 | */ |
||
| 109 | public function usePermission($mid) |
||
| 110 | { |
||
| 111 | $this->mid = $mid; |
||
| 112 | $this->use_permission = true; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function store() |
||
| 119 | { |
||
| 120 | $myts = \MyTextSanitizer::getInstance(); |
||
| 121 | $title = ''; |
||
| 122 | $imgurl = ''; |
||
| 123 | if (isset($this->topic_title) && '' !== $this->topic_title) { |
||
| 124 | $title = $myts->addSlashes($this->topic_title); |
||
| 125 | } |
||
| 126 | if (isset($this->topic_imgurl) && '' !== $this->topic_imgurl) { |
||
| 127 | $imgurl = $myts->addSlashes($this->topic_imgurl); |
||
| 128 | } |
||
| 129 | if (!isset($this->topic_pid) || !\is_numeric($this->topic_pid)) { |
||
| 130 | $this->topic_pid = 0; |
||
| 131 | } |
||
| 132 | if (empty($this->topic_id)) { |
||
| 133 | $this->topic_id = $this->db->genId($this->table . '_topic_id_seq'); |
||
| 134 | $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); |
||
| 135 | } else { |
||
| 136 | $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); |
||
| 137 | } |
||
| 138 | if (!$result = $this->db->query($sql)) { |
||
| 139 | // TODO: Replace with something else |
||
| 140 | // ErrorHandler::show('0022'); |
||
| 141 | |||
| 142 | $helper = \XoopsModules\News\Helper::getHelper($dirname); |
||
| 143 | $helper->redirect('admin/index.php', 5, $this->db->error()); |
||
| 144 | } |
||
| 145 | if (true === $this->use_permission) { |
||
| 146 | if (empty($this->topic_id)) { |
||
| 147 | $this->topic_id = $this->db->getInsertId(); |
||
| 148 | } |
||
| 149 | $xt = new \XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
| 150 | $parent_topics = $xt->getAllParentId($this->topic_id); |
||
| 151 | if (!empty($this->m_groups) && \is_array($this->m_groups)) { |
||
| 152 | foreach ($this->m_groups as $m_g) { |
||
| 153 | $moderate_topics = \XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g); |
||
| 154 | $add = true; |
||
| 155 | // only grant this permission when the group has this permission in all parent topics of the created topic |
||
| 156 | foreach ($parent_topics as $p_topic) { |
||
| 157 | if (!\in_array($p_topic, $moderate_topics)) { |
||
| 158 | $add = false; |
||
| 159 | continue; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | if (true === $add) { |
||
| 163 | $xp = new \XoopsPerms(); |
||
| 164 | $xp->setModuleId($this->mid); |
||
| 165 | $xp->setName('ModInTopic'); |
||
| 166 | $xp->setItemId($this->topic_id); |
||
| 167 | $xp->store(); |
||
| 168 | $xp->addGroup($m_g); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | if (!empty($this->s_groups) && \is_array($this->s_groups)) { |
||
| 173 | foreach ($s_groups as $s_g) { |
||
| 174 | $submit_topics = \XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g); |
||
| 175 | $add = true; |
||
| 176 | foreach ($parent_topics as $p_topic) { |
||
| 177 | if (!\in_array($p_topic, $submit_topics)) { |
||
| 178 | $add = false; |
||
| 179 | continue; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | if (true === $add) { |
||
| 183 | $xp = new \XoopsPerms(); |
||
| 184 | $xp->setModuleId($this->mid); |
||
| 185 | $xp->setName('SubmitInTopic'); |
||
| 186 | $xp->setItemId($this->topic_id); |
||
| 187 | $xp->store(); |
||
| 188 | $xp->addGroup($s_g); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | if (!empty($this->r_groups) && \is_array($this->r_groups)) { |
||
| 193 | foreach ($r_groups as $r_g) { |
||
| 194 | $read_topics = \XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g); |
||
| 195 | $add = true; |
||
| 196 | foreach ($parent_topics as $p_topic) { |
||
| 197 | if (!\in_array($p_topic, $read_topics)) { |
||
| 198 | $add = false; |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | if (true === $add) { |
||
| 203 | $xp = new \XoopsPerms(); |
||
| 204 | $xp->setModuleId($this->mid); |
||
| 205 | $xp->setName('ReadInTopic'); |
||
| 206 | $xp->setItemId($this->topic_id); |
||
| 207 | $xp->store(); |
||
| 208 | $xp->addGroup($r_g); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function delete() |
||
| 218 | { |
||
| 219 | $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->table, $this->topic_id); |
||
| 220 | $this->db->query($sql); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function topic_id() |
||
| 227 | { |
||
| 228 | return $this->topic_id; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function topic_pid() |
||
| 232 | { |
||
| 233 | return $this->topic_pid; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $format |
||
| 238 | * |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | public function topic_title($format = 'S') |
||
| 242 | { |
||
| 243 | $myts = \MyTextSanitizer::getInstance(); |
||
| 244 | switch ($format) { |
||
| 245 | case 'S': |
||
| 246 | case 'E': |
||
| 247 | $title = htmlspecialchars($this->topic_title, ENT_QUOTES | ENT_HTML5); |
||
| 248 | break; |
||
| 249 | case 'P': |
||
| 250 | case 'F': |
||
| 251 | $title = htmlspecialchars($this->topic_title, ENT_QUOTES | ENT_HTML5); |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | |||
| 255 | return $title; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $format |
||
| 260 | * |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | public function topic_imgurl($format = 'S') |
||
| 264 | { |
||
| 265 | $myts = \MyTextSanitizer::getInstance(); |
||
| 266 | switch ($format) { |
||
| 267 | case 'S': |
||
| 268 | case 'E': |
||
| 269 | $imgurl = htmlspecialchars($this->topic_imgurl, ENT_QUOTES | ENT_HTML5); |
||
| 270 | break; |
||
| 271 | case 'P': |
||
| 272 | case 'F': |
||
| 273 | $imgurl = htmlspecialchars($this->topic_imgurl, ENT_QUOTES | ENT_HTML5); |
||
| 274 | break; |
||
| 275 | } |
||
| 276 | |||
| 277 | return $imgurl; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return null |
||
| 282 | */ |
||
| 283 | public function prefix() |
||
| 284 | { |
||
| 285 | if (isset($this->prefix)) { |
||
| 286 | return $this->prefix; |
||
| 287 | } |
||
| 288 | |||
| 289 | return null; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function getFirstChildTopics() |
||
| 296 | { |
||
| 297 | $ret = []; |
||
| 298 | $xt = new \XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
| 299 | $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title'); |
||
| 300 | if (\is_array($topic_arr) && \count($topic_arr)) { |
||
| 301 | foreach ($topic_arr as $topic) { |
||
| 302 | $ret[] = new self($this->table, $topic); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | return $ret; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getAllChildTopics() |
||
| 313 | { |
||
| 314 | $ret = []; |
||
| 315 | $xt = new \XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
| 316 | $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title'); |
||
| 317 | if (\is_array($topic_arr) && \count($topic_arr)) { |
||
| 318 | foreach ($topic_arr as $topic) { |
||
| 319 | $ret[] = new self($this->table, $topic); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | return $ret; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function getChildTopicsTreeArray() |
||
| 330 | { |
||
| 331 | $ret = []; |
||
| 332 | $xt = new \XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
| 333 | $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title'); |
||
| 334 | if (\is_array($topic_arr) && \count($topic_arr)) { |
||
| 335 | foreach ($topic_arr as $topic) { |
||
| 336 | $ret[] = new self($this->table, $topic); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | return $ret; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param int $none |
||
| 345 | * @param $seltopic |
||
| 346 | * @param string $selname |
||
| 347 | * @param string $onchange |
||
| 348 | */ |
||
| 349 | public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '') |
||
| 350 | { |
||
| 351 | $xt = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid'); |
||
| 352 | if (-1 != $seltopic) { |
||
| 353 | $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange); |
||
| 354 | } elseif (!empty($this->topic_id)) { |
||
| 355 | $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange); |
||
| 356 | } else { |
||
| 357 | $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | //generates nicely formatted linked path from the root id to a given id |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $funcURL |
||
| 365 | * |
||
| 366 | * @return mixed |
||
| 367 | */ |
||
| 368 | public function getNiceTopicPathFromId($funcURL) |
||
| 369 | { |
||
| 370 | $xt = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid'); |
||
| 371 | $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL); |
||
| 372 | |||
| 373 | return $ret; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public function getAllChildTopicsId() |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function getTopicsList() |
||
| 391 | { |
||
| 392 | $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table); |
||
| 393 | $ret = []; |
||
| 394 | $myts = \MyTextSanitizer::getInstance(); |
||
| 395 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 396 | $ret[$myrow['topic_id']] = [ |
||
| 397 | 'title' => htmlspecialchars($myrow['topic_title'], ENT_QUOTES | ENT_HTML5), |
||
| 398 | 'pid' => $myrow['topic_pid'], |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param $pid |
||
| 407 | * @param $title |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function topicExists($pid, $title) |
||
| 421 | } |
||
| 422 | } |
||
| 423 |