| Total Complexity | 55 |
| Total Lines | 560 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GroupsHandler 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 GroupsHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 39 | class GroupsHandler extends XoopsPersistableObjectHandler |
||
| 40 | { |
||
| 41 | public Helper $helper; |
||
| 42 | public $isAdmin; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 47 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 48 | */ |
||
| 49 | public function __construct( |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * create a new Groups |
||
| 65 | * |
||
| 66 | * @param bool $isNew flag the new objects as "new"? |
||
| 67 | * @return \XoopsObject Groups |
||
| 68 | */ |
||
| 69 | public function create( |
||
| 70 | $isNew = true |
||
| 71 | ) { |
||
| 72 | $obj = parent::create($isNew); |
||
| 73 | if ($isNew) { |
||
| 74 | $obj->setNew(); |
||
| 75 | } else { |
||
| 76 | $obj->unsetNew(); |
||
| 77 | } |
||
| 78 | $obj->helper = $this->helper; |
||
| 79 | |||
| 80 | return $obj; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * retrieve a Groups |
||
| 85 | * |
||
| 86 | * @param int $id of the Groups |
||
| 87 | * @param null $fields |
||
|
|
|||
| 88 | * @return false|\XoopsModules\Suico\Groups reference to the {@link Groups} object, FALSE if failed |
||
| 89 | */ |
||
| 90 | public function get2( |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * insert a new Groups in the database |
||
| 111 | * |
||
| 112 | * @param \XoopsObject $object reference to the {@link Groups} |
||
| 113 | * object |
||
| 114 | * @param bool $force |
||
| 115 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 116 | */ |
||
| 117 | public function insert2( |
||
| 118 | XoopsObject $object, |
||
| 119 | $force = false |
||
| 120 | ) { |
||
| 121 | global $xoopsConfig; |
||
| 122 | if (!$object instanceof Groups) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | if (!$object->isDirty()) { |
||
| 126 | return true; |
||
| 127 | } |
||
| 128 | if (!$object->cleanVars()) { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | foreach ($object->cleanVars as $k => $v) { |
||
| 132 | ${$k} = $v; |
||
| 133 | } |
||
| 134 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 135 | if ($object->isNew()) { |
||
| 136 | // ajout/modification d'un Groups |
||
| 137 | $object = new Groups(); |
||
| 138 | $format = 'INSERT INTO %s (group_id, owner_uid, group_title, group_desc, group_img)'; |
||
| 139 | $format .= 'VALUES (%u, %u, %s, %s, %s)'; |
||
| 140 | $sql = \sprintf( |
||
| 141 | $format, |
||
| 142 | $this->db->prefix('suico_groups'), |
||
| 143 | $group_id, |
||
| 144 | $owner_uid, |
||
| 145 | $this->db->quoteString($group_title), |
||
| 146 | $this->db->quoteString($group_desc), |
||
| 147 | $this->db->quoteString($group_img) |
||
| 148 | ); |
||
| 149 | $force = true; |
||
| 150 | } else { |
||
| 151 | $format = 'UPDATE %s SET '; |
||
| 152 | $format .= 'group_id=%u, owner_uid=%u, group_title=%s, group_desc=%s, group_img=%s'; |
||
| 153 | $format .= ' WHERE group_id = %u'; |
||
| 154 | $sql = \sprintf( |
||
| 155 | $format, |
||
| 156 | $this->db->prefix('suico_groups'), |
||
| 157 | $group_id, |
||
| 158 | $owner_uid, |
||
| 159 | $this->db->quoteString($group_title), |
||
| 160 | $this->db->quoteString($group_desc), |
||
| 161 | $this->db->quoteString($group_img), |
||
| 162 | $group_id |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | if ($force) { |
||
| 166 | $result = $this->db->queryF($sql); |
||
| 167 | } else { |
||
| 168 | $result = $this->db->query($sql); |
||
| 169 | } |
||
| 170 | if (!$result) { |
||
| 171 | return false; |
||
| 172 | } |
||
| 173 | if (empty($group_id)) { |
||
| 174 | $group_id = $this->db->getInsertId(); |
||
| 175 | } |
||
| 176 | $object->assignVar('group_id', $group_id); |
||
| 177 | |||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * delete a Groups from the database |
||
| 183 | * |
||
| 184 | * @param \XoopsObject $object reference to the Groups to delete |
||
| 185 | * @param bool $force |
||
| 186 | * @return bool FALSE if failed. |
||
| 187 | */ |
||
| 188 | public function delete( |
||
| 189 | XoopsObject $object, |
||
| 190 | $force = false |
||
| 191 | ) { |
||
| 192 | if (!$object instanceof Groups) { |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | $sql = \sprintf( |
||
| 196 | 'DELETE FROM %s WHERE group_id = %u', |
||
| 197 | $this->db->prefix('suico_groups'), |
||
| 198 | (int)$object->getVar('group_id') |
||
| 199 | ); |
||
| 200 | if ($force) { |
||
| 201 | $result = $this->db->queryF($sql); |
||
| 202 | } else { |
||
| 203 | $result = $this->db->query($sql); |
||
| 204 | } |
||
| 205 | if (!$result) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | return true; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * retrieve suico_groupss from the database |
||
| 214 | * |
||
| 215 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 216 | * @param bool $id_as_key use the UID as key for the array? |
||
| 217 | * @param bool $as_object |
||
| 218 | * @return array array of {@link Groups} objects |
||
| 219 | */ |
||
| 220 | public function &getObjects( |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * retrieve suico_groupss from the database |
||
| 257 | * |
||
| 258 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 259 | * @param bool $id_as_key use the UID as key for the array? |
||
| 260 | * @return array array of {@link Groups} objects |
||
| 261 | */ |
||
| 262 | public function getGroups( |
||
| 263 | $criteria = null, |
||
| 264 | $id_as_key = false |
||
| 265 | ) { |
||
| 266 | $ret = []; |
||
| 267 | $sort = 'group_title'; |
||
| 268 | $order = 'ASC'; |
||
| 269 | $start = 0; |
||
| 270 | $limit = 0; |
||
| 271 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_groups'); |
||
| 272 | if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
||
| 273 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 274 | if ('' !== $sort) { |
||
| 275 | $sql .= ' ORDER BY ' . $sort . ' ' . $order; |
||
| 276 | } |
||
| 277 | $limit = $criteria->getLimit(); |
||
| 278 | $start = $criteria->getStart(); |
||
| 279 | } |
||
| 280 | $result = $this->db->query($sql, $limit, $start); |
||
| 281 | if (!$result) { |
||
| 282 | return $ret; |
||
| 283 | } |
||
| 284 | $i = 0; |
||
| 285 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 286 | $ret[$i]['id'] = $myrow['group_id']; |
||
| 287 | $ret[$i]['title'] = $myrow['group_title']; |
||
| 288 | $ret[$i]['img'] = $myrow['group_img']; |
||
| 289 | $ret[$i]['desc'] = $myrow['group_desc']; |
||
| 290 | $ret[$i]['uid'] = $myrow['owner_uid']; |
||
| 291 | $groupid = $myrow['group_id']; |
||
| 292 | $query = 'SELECT COUNT(rel_id) AS grouptotalmembers FROM ' . $GLOBALS['xoopsDB']->prefix('suico_relgroupuser') . ' WHERE rel_group_id=' . $groupid . ''; |
||
| 293 | $queryresult = $GLOBALS['xoopsDB']->query($query); |
||
| 294 | $row = $GLOBALS['xoopsDB']->fetchArray($queryresult); |
||
| 295 | $group_total_members = $row['grouptotalmembers']; |
||
| 296 | if ($group_total_members > 0) { |
||
| 297 | if (1 == $group_total_members) { |
||
| 298 | $ret[$i]['group_total_members'] = '' . \_MD_SUICO_ONEMEMBER . ' '; |
||
| 299 | } else { |
||
| 300 | $ret[$i]['group_total_members'] = '' . $group_total_members . ' ' . \_MD_SUICO_GROUPMEMBERS . ' '; |
||
| 301 | } |
||
| 302 | } else { |
||
| 303 | $ret[$i]['group_total_members'] = '' . \_MD_SUICO_NO_MEMBER . ' '; |
||
| 304 | } |
||
| 305 | $i++; |
||
| 306 | } |
||
| 307 | |||
| 308 | return $ret; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * count suico_groupss matching a condition |
||
| 313 | * |
||
| 314 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match |
||
| 315 | * @return int count of suico_groupss |
||
| 316 | */ |
||
| 317 | public function getCount( |
||
| 318 | ?CriteriaElement $criteria = null |
||
| 319 | ) { |
||
| 320 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_groups'); |
||
| 321 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 322 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 323 | } |
||
| 324 | $result = $this->db->query($sql); |
||
| 325 | if (!$result) { |
||
| 326 | return 0; |
||
| 327 | } |
||
| 328 | [$count] = $this->db->fetchRow($result); |
||
| 329 | |||
| 330 | return $count; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * delete suico_groupss matching a set of conditions |
||
| 335 | * |
||
| 336 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} |
||
| 337 | * @param bool $force |
||
| 338 | * @param bool $asObject |
||
| 339 | * @return bool FALSE if deletion failed |
||
| 340 | */ |
||
| 341 | public function deleteAll( |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param $maxbytes |
||
| 359 | * @param $xoopsTpl |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | public function renderFormSubmit( |
||
| 363 | $maxbytes, |
||
| 364 | $xoopsTpl |
||
| 365 | ) { |
||
| 366 | $form = new XoopsThemeForm(\_MD_SUICO_SUBMIT_GROUP, 'form_group', 'submitGroup.php', 'post', true); |
||
| 367 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 368 | $field_url = new XoopsFormFile(\_MD_SUICO_GROUP_IMAGE, 'group_img', $maxbytes); |
||
| 369 | $field_title = new XoopsFormText(\_MD_SUICO_GROUP_TITLE, 'group_title', 35, 55); |
||
| 370 | $field_desc = new XoopsFormText(\_MD_SUICO_GROUP_DESC, 'group_desc', 35, 55); |
||
| 371 | $field_marker = new XoopsFormHidden('marker', '1'); |
||
| 372 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADGROUP, 'submit'); |
||
| 373 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 374 | $form->addElement($field_warning); |
||
| 375 | $form->addElement($field_url, true); |
||
| 376 | $form->addElement($field_title); |
||
| 377 | $form->addElement($field_desc); |
||
| 378 | $form->addElement($field_marker); |
||
| 379 | $form->addElement($buttonSend); |
||
| 380 | $form->display(); |
||
| 381 | |||
| 382 | return true; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param $group |
||
| 387 | * @param $maxbytes |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | public function renderFormEdit( |
||
| 391 | $group, |
||
| 392 | $maxbytes |
||
| 393 | ) { |
||
| 394 | $form = new XoopsThemeForm(\_MD_SUICO_EDIT_GROUP, 'form_editgroup', 'editgroup.php', 'post', true); |
||
| 395 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 396 | $field_groupid = new XoopsFormHidden('group_id', $group->getVar('group_id')); |
||
| 397 | $field_url = new XoopsFormFile(\_MD_SUICO_GROUP_IMAGE, 'img', $maxbytes); |
||
| 398 | $field_url->setExtra('style="visibility:hidden;"'); |
||
| 399 | $field_title = new XoopsFormText(\_MD_SUICO_GROUP_TITLE, 'title', 35, 55, $group->getVar('group_title')); |
||
| 400 | $field_desc = new XoopsFormTextArea(\_MD_SUICO_GROUP_DESC, 'desc', $group->getVar('group_desc')); |
||
| 401 | $field_marker = new XoopsFormHidden('marker', '1'); |
||
| 402 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADGROUP, 'submit'); |
||
| 403 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 404 | $field_oldpicture = new XoopsFormLabel( |
||
| 405 | \_MD_SUICO_GROUP_IMAGE, |
||
| 406 | '<img src="' . \XOOPS_UPLOAD_URL . '/' . $group->getVar( |
||
| 407 | 'group_img' |
||
| 408 | ) . '">' |
||
| 409 | ); |
||
| 410 | $field_maintainimage = new XoopsFormLabel( |
||
| 411 | \_MD_SUICO_MAINTAIN_OLD_IMAGE, |
||
| 412 | "<input type='checkbox' value='1' id='flag_oldimg' name='flag_oldimg' onclick=\"groupImgSwitch(img)\" checked>" |
||
| 413 | ); |
||
| 414 | $form->addElement($field_oldpicture); |
||
| 415 | $form->addElement($field_maintainimage); |
||
| 416 | $form->addElement($field_warning); |
||
| 417 | $form->addElement($field_url); |
||
| 418 | $form->addElement($field_groupid); |
||
| 419 | $form->addElement($field_title); |
||
| 420 | $form->addElement($field_desc); |
||
| 421 | $form->addElement($field_marker); |
||
| 422 | $form->addElement($buttonSend); |
||
| 423 | $form->display(); |
||
| 424 | echo " |
||
| 425 | <!-- Start Form Validation JavaScript //--> |
||
| 426 | <script type='text/javascript'> |
||
| 427 | <!--// |
||
| 428 | function groupImgSwitch(img) { |
||
| 429 | |||
| 430 | var elestyle = xoopsGetElementById(img).style; |
||
| 431 | |||
| 432 | if (elestyle.visibility == \"hidden\") { |
||
| 433 | elestyle.visibility = \"visible\"; |
||
| 434 | } else { |
||
| 435 | elestyle.visibility = \"hidden\"; |
||
| 436 | } |
||
| 437 | |||
| 438 | |||
| 439 | } |
||
| 440 | //--></script> |
||
| 441 | <!-- End Form Validation JavaScript //--> |
||
| 442 | "; |
||
| 443 | |||
| 444 | return true; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $group_title |
||
| 449 | * @param string $group_desc |
||
| 450 | * @param string $group_img |
||
| 451 | * @param string $path_upload |
||
| 452 | * @param int $maxfilebytes |
||
| 453 | * @param int $maxfilewidth |
||
| 454 | * @param int $maxfileheight |
||
| 455 | * @param int $change_img |
||
| 456 | * @param string|Group $group |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | public function receiveGroup( |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param $owner_id |
||
| 559 | * @return mixed |
||
| 560 | */ |
||
| 561 | public function isGroupMember($owner_id) |
||
| 562 | { |
||
| 563 | $query = 'SELECT COUNT(rel_id) AS grouptotalmembers FROM ' . $GLOBALS['xoopsDB']->prefix('suico_relgroupuser') . ' WHERE rel_group_id=' . $group_id . ''; |
||
| 564 | $queryresult = $GLOBALS['xoopsDB']->query($query); |
||
| 565 | $row = $GLOBALS['xoopsDB']->fetchArray($queryresult); |
||
| 566 | $group_total_members = $row['grouptotalmembers']; |
||
| 567 | |||
| 568 | return $group_total_members; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param $group_id |
||
| 573 | * @return mixed |
||
| 574 | */ |
||
| 575 | public function getComment($group_id) |
||
| 576 | { |
||
| 577 | $moduleSuico = Helper::getInstance()->getModule(); |
||
| 578 | $sql = 'SELECT count(com_id) FROM ' . $GLOBALS['xoopsDB']->prefix('xoopscomments') . " WHERE com_modid = '" . $moduleSuico->getVar('mid') . "' AND com_itemid = '" . $group_id . "'"; |
||
| 579 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 580 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
| 581 | $group_total_comments = $row['count(com_id)']; |
||
| 582 | } |
||
| 583 | |||
| 584 | return $group_total_comments; |
||
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param $group_id |
||
| 589 | * @return mixed |
||
| 590 | */ |
||
| 591 | public function getGroupTotalMembers($group_id) |
||
| 599 | } |
||
| 600 | } |
||
| 601 |