| Total Complexity | 49 |
| Total Lines | 617 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 53 | class GroupsHandler extends XoopsPersistableObjectHandler |
||
| 54 | { |
||
| 55 | public $helper; |
||
| 56 | |||
| 57 | public $isAdmin; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor |
||
| 61 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 62 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 63 | */ |
||
| 64 | |||
| 65 | public function __construct( |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * create a new Groups |
||
| 84 | * |
||
| 85 | * @param bool $isNew flag the new objects as "new"? |
||
| 86 | * @return \XoopsObject Groups |
||
| 87 | */ |
||
| 88 | |||
| 89 | public function create( |
||
| 90 | $isNew = true |
||
| 91 | ) { |
||
| 92 | $obj = parent::create($isNew); |
||
| 93 | |||
| 94 | if ($isNew) { |
||
| 95 | $obj->setNew(); |
||
| 96 | } else { |
||
| 97 | $obj->unsetNew(); |
||
| 98 | } |
||
| 99 | |||
| 100 | $obj->helper = $this->helper; |
||
| 101 | |||
| 102 | return $obj; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * retrieve a Groups |
||
| 107 | * |
||
| 108 | * @param int $id of the Groups |
||
| 109 | * @param null $fields |
||
| 110 | * @return mixed reference to the {@link Groups} object, FALSE if failed |
||
| 111 | */ |
||
| 112 | |||
| 113 | public function get2( |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * insert a new Groups in the database |
||
| 138 | * |
||
| 139 | * @param \XoopsObject $xoopsObject reference to the {@link Groups} |
||
| 140 | * object |
||
| 141 | * @param bool $force |
||
| 142 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 143 | */ |
||
| 144 | |||
| 145 | public function insert2( |
||
| 146 | XoopsObject $xoopsObject, |
||
| 147 | $force = false |
||
| 148 | ) { |
||
| 149 | global $xoopsConfig; |
||
| 150 | |||
| 151 | if (!$xoopsObject instanceof Groups) { |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | if (!$xoopsObject->isDirty()) { |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (!$xoopsObject->cleanVars()) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 164 | ${$k} = $v; |
||
| 165 | } |
||
| 166 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 167 | if ($xoopsObject->isNew()) { |
||
| 168 | // ajout/modification d'un Groups |
||
| 169 | |||
| 170 | $xoopsObject = new Groups(); |
||
| 171 | |||
| 172 | $format = 'INSERT INTO %s (group_id, owner_uid, group_title, group_desc, group_img)'; |
||
| 173 | |||
| 174 | $format .= 'VALUES (%u, %u, %s, %s, %s)'; |
||
| 175 | |||
| 176 | $sql = \sprintf( |
||
| 177 | $format, |
||
| 178 | $this->db->prefix('suico_groups'), |
||
| 179 | $group_id, |
||
| 180 | $owner_uid, |
||
| 181 | $this->db->quoteString($group_title), |
||
| 182 | $this->db->quoteString($group_desc), |
||
| 183 | $this->db->quoteString($group_img) |
||
| 184 | ); |
||
| 185 | |||
| 186 | $force = true; |
||
| 187 | } else { |
||
| 188 | $format = 'UPDATE %s SET '; |
||
| 189 | |||
| 190 | $format .= 'group_id=%u, owner_uid=%u, group_title=%s, group_desc=%s, group_img=%s'; |
||
| 191 | |||
| 192 | $format .= ' WHERE group_id = %u'; |
||
| 193 | |||
| 194 | $sql = \sprintf( |
||
| 195 | $format, |
||
| 196 | $this->db->prefix('suico_groups'), |
||
| 197 | $group_id, |
||
| 198 | $owner_uid, |
||
| 199 | $this->db->quoteString($group_title), |
||
| 200 | $this->db->quoteString($group_desc), |
||
| 201 | $this->db->quoteString($group_img), |
||
| 202 | $group_id |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($force) { |
||
| 207 | $result = $this->db->queryF($sql); |
||
| 208 | } else { |
||
| 209 | $result = $this->db->query($sql); |
||
| 210 | } |
||
| 211 | |||
| 212 | if (!$result) { |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | |||
| 216 | if (empty($group_id)) { |
||
| 217 | $group_id = $this->db->getInsertId(); |
||
| 218 | } |
||
| 219 | |||
| 220 | $xoopsObject->assignVar('group_id', $group_id); |
||
| 221 | |||
| 222 | return true; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * delete a Groups from the database |
||
| 227 | * |
||
| 228 | * @param \XoopsObject $xoopsObject reference to the Groups to delete |
||
| 229 | * @param bool $force |
||
| 230 | * @return bool FALSE if failed. |
||
| 231 | */ |
||
| 232 | |||
| 233 | public function delete( |
||
| 234 | XoopsObject $xoopsObject, |
||
| 235 | $force = false |
||
| 236 | ) { |
||
| 237 | if (!$xoopsObject instanceof Groups) { |
||
| 238 | return false; |
||
| 239 | } |
||
| 240 | |||
| 241 | $sql = \sprintf( |
||
| 242 | 'DELETE FROM %s WHERE group_id = %u', |
||
| 243 | $this->db->prefix('suico_groups'), |
||
| 244 | $xoopsObject->getVar('group_id') |
||
| 245 | ); |
||
| 246 | |||
| 247 | if ($force) { |
||
| 248 | $result = $this->db->queryF($sql); |
||
| 249 | } else { |
||
| 250 | $result = $this->db->query($sql); |
||
| 251 | } |
||
| 252 | |||
| 253 | if (!$result) { |
||
| 254 | return false; |
||
| 255 | } |
||
| 256 | |||
| 257 | return true; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * retrieve suico_groupss from the database |
||
| 262 | * |
||
| 263 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 264 | * @param bool $id_as_key use the UID as key for the array? |
||
| 265 | * @param bool $as_object |
||
| 266 | * @return array array of {@link Groups} objects |
||
| 267 | */ |
||
| 268 | |||
| 269 | public function &getObjects( |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * retrieve suico_groupss from the database |
||
| 317 | * |
||
| 318 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 319 | * @param bool $id_as_key use the UID as key for the array? |
||
| 320 | * @return array array of {@link Groups} objects |
||
| 321 | */ |
||
| 322 | |||
| 323 | public function getGroups( |
||
| 324 | $criteria = null, |
||
| 325 | $id_as_key = false |
||
| 326 | ) { |
||
| 327 | $ret = []; |
||
| 328 | |||
| 329 | $limit = $start = 0; |
||
| 330 | |||
| 331 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_groups'); |
||
| 332 | |||
| 333 | if (isset($criteria) && $criteria instanceof CriteriaElement) { |
||
| 334 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 335 | |||
| 336 | if ('' !== $criteria->getSort()) { |
||
| 337 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 338 | } |
||
| 339 | |||
| 340 | $limit = $criteria->getLimit(); |
||
| 341 | |||
| 342 | $start = $criteria->getStart(); |
||
| 343 | } |
||
| 344 | |||
| 345 | $result = $this->db->query($sql, $limit, $start); |
||
| 346 | |||
| 347 | if (!$result) { |
||
| 348 | return $ret; |
||
| 349 | } |
||
| 350 | |||
| 351 | $i = 0; |
||
| 352 | |||
| 353 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 354 | $ret[$i]['id'] = $myrow['group_id']; |
||
| 355 | |||
| 356 | $ret[$i]['title'] = $myrow['group_title']; |
||
| 357 | |||
| 358 | $ret[$i]['img'] = $myrow['group_img']; |
||
| 359 | |||
| 360 | $ret[$i]['desc'] = $myrow['group_desc']; |
||
| 361 | |||
| 362 | $ret[$i]['uid'] = $myrow['owner_uid']; |
||
| 363 | |||
| 364 | $groupid = $myrow['group_id']; |
||
| 365 | |||
| 366 | $query = 'SELECT COUNT(rel_id) AS grouptotalmembers FROM ' . $GLOBALS['xoopsDB']->prefix('suico_relgroupuser') . ' WHERE rel_group_id=' . $groupid . ''; |
||
| 367 | |||
| 368 | $queryresult = $GLOBALS['xoopsDB']->query($query); |
||
| 369 | |||
| 370 | $row = $GLOBALS['xoopsDB']->fetchArray($queryresult); |
||
| 371 | |||
| 372 | $grouptotalmembers = $row['grouptotalmembers']; |
||
| 373 | $ret[$i]['grouptotalmembers'] = $grouptotalmembers . ' ' . \_MD_SUICO_GROUPMEMBERS; |
||
| 374 | $i++; |
||
| 375 | } |
||
| 376 | |||
| 377 | return $ret; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * count suico_groupss matching a condition |
||
| 382 | * |
||
| 383 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 384 | * @return int count of suico_groupss |
||
| 385 | */ |
||
| 386 | |||
| 387 | public function getCount( |
||
| 388 | ?CriteriaElement $criteriaElement = null |
||
| 389 | ) { |
||
| 390 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_groups'); |
||
| 391 | |||
| 392 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 393 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 394 | } |
||
| 395 | |||
| 396 | $result = $this->db->query($sql); |
||
| 397 | |||
| 398 | if (!$result) { |
||
| 399 | return 0; |
||
| 400 | } |
||
| 401 | |||
| 402 | [$count] = $this->db->fetchRow($result); |
||
| 403 | |||
| 404 | return $count; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * delete suico_groupss matching a set of conditions |
||
| 409 | * |
||
| 410 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 411 | * @param bool $force |
||
| 412 | * @param bool $asObject |
||
| 413 | * @return bool FALSE if deletion failed |
||
| 414 | */ |
||
| 415 | |||
| 416 | public function deleteAll( |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param $maxbytes |
||
| 436 | * @param $xoopsTpl |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | |||
| 440 | public function renderFormSubmit( |
||
| 441 | $maxbytes, |
||
| 442 | $xoopsTpl |
||
| 443 | ) { |
||
| 444 | $form = new XoopsThemeForm(\_MD_SUICO_SUBMIT_GROUP, 'form_group', 'submitGroup.php', 'post', true); |
||
| 445 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 446 | |||
| 447 | $field_url = new XoopsFormFile(\_MD_SUICO_GROUP_IMAGE, 'group_img', $maxbytes); |
||
| 448 | $field_title = new XoopsFormText(\_MD_SUICO_GROUP_TITLE, 'group_title', 35, 55); |
||
| 449 | $field_desc = new XoopsFormText(\_MD_SUICO_GROUP_DESC, 'group_desc', 35, 55); |
||
| 450 | $field_marker = new XoopsFormHidden('marker', '1'); |
||
| 451 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADGROUP, 'submit'); |
||
| 452 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 453 | |||
| 454 | $form->addElement($field_warning); |
||
| 455 | |||
| 456 | $form->addElement($field_url, true); |
||
| 457 | |||
| 458 | $form->addElement($field_title); |
||
| 459 | |||
| 460 | $form->addElement($field_desc); |
||
| 461 | |||
| 462 | $form->addElement($field_marker); |
||
| 463 | $form->addElement($buttonSend); |
||
| 464 | $form->display(); |
||
| 465 | |||
| 466 | return true; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param $group |
||
| 471 | * @param $maxbytes |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | |||
| 475 | public function renderFormEdit( |
||
| 476 | $group, |
||
| 477 | $maxbytes |
||
| 478 | ) { |
||
| 479 | $form = new XoopsThemeForm(\_MD_SUICO_EDIT_GROUP, 'form_editgroup', 'editgroup.php', 'post', true); |
||
| 480 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 481 | |||
| 482 | $field_groupid = new XoopsFormHidden('group_id', $group->getVar('group_id')); |
||
| 483 | $field_url = new XoopsFormFile(\_MD_SUICO_GROUP_IMAGE, 'img', $maxbytes); |
||
| 484 | $field_url->setExtra('style="visibility:hidden;"'); |
||
| 485 | $field_title = new XoopsFormText(\_MD_SUICO_GROUP_TITLE, 'title', 35, 55, $group->getVar('group_title')); |
||
| 486 | $field_desc = new XoopsFormTextArea(\_MD_SUICO_GROUP_DESC, 'desc', $group->getVar('group_desc')); |
||
| 487 | $field_marker = new XoopsFormHidden('marker', '1'); |
||
| 488 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADGROUP, 'submit'); |
||
| 489 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 490 | |||
| 491 | $field_oldpicture = new XoopsFormLabel( |
||
| 492 | \_MD_SUICO_GROUP_IMAGE, '<img src="' . \XOOPS_UPLOAD_URL . '/' . $group->getVar( |
||
| 493 | 'group_img' |
||
| 494 | ) . '">' |
||
| 495 | ); |
||
| 496 | |||
| 497 | $field_maintainimage = new XoopsFormLabel( |
||
| 498 | \_MD_SUICO_MAINTAIN_OLD_IMAGE, "<input type='checkbox' value='1' id='flag_oldimg' name='flag_oldimg' onclick=\"groupImgSwitch(img)\" checked>" |
||
| 499 | ); |
||
| 500 | |||
| 501 | $form->addElement($field_oldpicture); |
||
| 502 | |||
| 503 | $form->addElement($field_maintainimage); |
||
| 504 | |||
| 505 | $form->addElement($field_warning); |
||
| 506 | |||
| 507 | $form->addElement($field_url); |
||
| 508 | |||
| 509 | $form->addElement($field_groupid); |
||
| 510 | |||
| 511 | $form->addElement($field_title); |
||
| 512 | |||
| 513 | $form->addElement($field_desc); |
||
| 514 | |||
| 515 | $form->addElement($field_marker); |
||
| 516 | $form->addElement($buttonSend); |
||
| 517 | $form->display(); |
||
| 518 | |||
| 519 | echo " |
||
| 520 | <!-- Start Form Validation JavaScript //--> |
||
| 521 | <script type='text/javascript'> |
||
| 522 | <!--// |
||
| 523 | function groupImgSwitch(img) { |
||
| 524 | |||
| 525 | var elestyle = xoopsGetElementById(img).style; |
||
| 526 | |||
| 527 | if (elestyle.visibility == \"hidden\") { |
||
| 528 | elestyle.visibility = \"visible\"; |
||
| 529 | } else { |
||
| 530 | elestyle.visibility = \"hidden\"; |
||
| 531 | } |
||
| 532 | |||
| 533 | |||
| 534 | } |
||
| 535 | //--></script> |
||
| 536 | <!-- End Form Validation JavaScript //--> |
||
| 537 | "; |
||
| 538 | |||
| 539 | return true; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param string $group_title |
||
| 544 | * @param string $group_desc |
||
| 545 | * @param string $group_img |
||
| 546 | * @param string $path_upload |
||
| 547 | * @param int $maxfilebytes |
||
| 548 | * @param int $maxfilewidth |
||
| 549 | * @param int $maxfileheight |
||
| 550 | * @param int $change_img |
||
| 551 | * @param string $group |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | |||
| 555 | public function receiveGroup( |
||
| 670 | } |
||
| 671 | } |
||
| 672 |