| Total Complexity | 55 |
| Total Lines | 529 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 49 | class GroupsHandler extends XoopsPersistableObjectHandler |
||
| 50 | { |
||
| 51 | public $helper; |
||
| 52 | public $isAdmin; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor |
||
| 56 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 57 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * create a new Groups |
||
| 75 | * |
||
| 76 | * @param bool $isNew flag the new objects as "new"? |
||
| 77 | * @return \XoopsObject Groups |
||
| 78 | */ |
||
| 79 | public function create( |
||
| 80 | $isNew = true |
||
| 81 | ) { |
||
| 82 | $obj = parent::create($isNew); |
||
| 83 | if ($isNew) { |
||
| 84 | $obj->setNew(); |
||
| 85 | } else { |
||
| 86 | $obj->unsetNew(); |
||
| 87 | } |
||
| 88 | $obj->helper = $this->helper; |
||
| 89 | return $obj; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * retrieve a Groups |
||
| 94 | * |
||
| 95 | * @param int $id of the Groups |
||
| 96 | * @param null $fields |
||
| 97 | * @return mixed reference to the {@link Groups} object, FALSE if failed |
||
| 98 | */ |
||
| 99 | public function get2( |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * insert a new Groups in the database |
||
| 118 | * |
||
| 119 | * @param \XoopsObject $xoopsObject reference to the {@link Groups} |
||
| 120 | * object |
||
| 121 | * @param bool $force |
||
| 122 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 123 | */ |
||
| 124 | public function insert2( |
||
| 125 | XoopsObject $xoopsObject, |
||
| 126 | $force = false |
||
| 127 | ) { |
||
| 128 | global $xoopsConfig; |
||
| 129 | if (!$xoopsObject instanceof Groups) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | if (!$xoopsObject->isDirty()) { |
||
| 133 | return true; |
||
| 134 | } |
||
| 135 | if (!$xoopsObject->cleanVars()) { |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 139 | ${$k} = $v; |
||
| 140 | } |
||
| 141 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 142 | if ($xoopsObject->isNew()) { |
||
| 143 | // ajout/modification d'un Groups |
||
| 144 | $xoopsObject = new Groups(); |
||
| 145 | $format = 'INSERT INTO %s (group_id, owner_uid, group_title, group_desc, group_img)'; |
||
| 146 | $format .= 'VALUES (%u, %u, %s, %s, %s)'; |
||
| 147 | $sql = \sprintf( |
||
| 148 | $format, |
||
| 149 | $this->db->prefix('suico_groups'), |
||
| 150 | $group_id, |
||
| 151 | $owner_uid, |
||
| 152 | $this->db->quoteString($group_title), |
||
| 153 | $this->db->quoteString($group_desc), |
||
| 154 | $this->db->quoteString($group_img) |
||
| 155 | ); |
||
| 156 | $force = true; |
||
| 157 | } else { |
||
| 158 | $format = 'UPDATE %s SET '; |
||
| 159 | $format .= 'group_id=%u, owner_uid=%u, group_title=%s, group_desc=%s, group_img=%s'; |
||
| 160 | $format .= ' WHERE group_id = %u'; |
||
| 161 | $sql = \sprintf( |
||
| 162 | $format, |
||
| 163 | $this->db->prefix('suico_groups'), |
||
| 164 | $group_id, |
||
| 165 | $owner_uid, |
||
| 166 | $this->db->quoteString($group_title), |
||
| 167 | $this->db->quoteString($group_desc), |
||
| 168 | $this->db->quoteString($group_img), |
||
| 169 | $group_id |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | if ($force) { |
||
| 173 | $result = $this->db->queryF($sql); |
||
| 174 | } else { |
||
| 175 | $result = $this->db->query($sql); |
||
| 176 | } |
||
| 177 | if (!$result) { |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | if (empty($group_id)) { |
||
| 181 | $group_id = $this->db->getInsertId(); |
||
| 182 | } |
||
| 183 | $xoopsObject->assignVar('group_id', $group_id); |
||
| 184 | return true; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * delete a Groups from the database |
||
| 189 | * |
||
| 190 | * @param \XoopsObject $xoopsObject reference to the Groups to delete |
||
| 191 | * @param bool $force |
||
| 192 | * @return bool FALSE if failed. |
||
| 193 | */ |
||
| 194 | public function delete( |
||
| 195 | XoopsObject $xoopsObject, |
||
| 196 | $force = false |
||
| 197 | ) { |
||
| 198 | if (!$xoopsObject instanceof Groups) { |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | $sql = \sprintf( |
||
| 202 | 'DELETE FROM %s WHERE group_id = %u', |
||
| 203 | $this->db->prefix('suico_groups'), |
||
| 204 | $xoopsObject->getVar('group_id') |
||
| 205 | ); |
||
| 206 | if ($force) { |
||
| 207 | $result = $this->db->queryF($sql); |
||
| 208 | } else { |
||
| 209 | $result = $this->db->query($sql); |
||
| 210 | } |
||
| 211 | if (!$result) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * retrieve suico_groupss from the database |
||
| 219 | * |
||
| 220 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 221 | * @param bool $id_as_key use the UID as key for the array? |
||
| 222 | * @param bool $as_object |
||
| 223 | * @return array array of {@link Groups} objects |
||
| 224 | */ |
||
| 225 | public function &getObjects( |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * retrieve suico_groupss from the database |
||
| 260 | * |
||
| 261 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 262 | * @param bool $id_as_key use the UID as key for the array? |
||
| 263 | * @return array array of {@link Groups} objects |
||
| 264 | */ |
||
| 265 | public function getGroups( |
||
| 266 | $criteria = null, |
||
| 267 | $id_as_key = false |
||
| 268 | ) { |
||
| 269 | $ret = []; |
||
| 270 | $sort = 'group_title'; |
||
| 271 | $order = 'ASC'; |
||
| 272 | $limit = $start = 0; |
||
| 273 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_groups'); |
||
| 274 | if (isset($criteria) && $criteria instanceof CriteriaElement) { |
||
| 275 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 276 | if ('' !== $sort) { |
||
| 277 | $sql .= ' ORDER BY ' . $sort . ' ' . $order; |
||
| 278 | } |
||
| 279 | $limit = $criteria->getLimit(); |
||
| 280 | $start = $criteria->getStart(); |
||
| 281 | } |
||
| 282 | $result = $this->db->query($sql, $limit, $start); |
||
| 283 | if (!$result) { |
||
| 284 | return $ret; |
||
| 285 | } |
||
| 286 | $i = 0; |
||
| 287 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 288 | $ret[$i]['id'] = $myrow['group_id']; |
||
| 289 | $ret[$i]['title'] = $myrow['group_title']; |
||
| 290 | $ret[$i]['img'] = $myrow['group_img']; |
||
| 291 | $ret[$i]['desc'] = $myrow['group_desc']; |
||
| 292 | $ret[$i]['uid'] = $myrow['owner_uid']; |
||
| 293 | $groupid = $myrow['group_id']; |
||
| 294 | $query = 'SELECT COUNT(rel_id) AS grouptotalmembers FROM ' . $GLOBALS['xoopsDB']->prefix('suico_relgroupuser') . ' WHERE rel_group_id=' . $groupid . ''; |
||
| 295 | $queryresult = $GLOBALS['xoopsDB']->query($query); |
||
| 296 | $row = $GLOBALS['xoopsDB']->fetchArray($queryresult); |
||
| 297 | $group_total_members = $row['grouptotalmembers']; |
||
| 298 | |||
| 299 | if ($group_total_members > 0) { |
||
| 300 | if (1 == $group_total_members) { |
||
| 301 | $ret[$i]['group_total_members'] ='' . _MD_SUICO_ONEMEMBER . ' '; |
||
| 302 | } else { |
||
| 303 | $ret[$i]['group_total_members'] ='' . $group_total_members . ' ' . _MD_SUICO_GROUPMEMBERS . ' '; |
||
| 304 | } |
||
| 305 | } else { |
||
| 306 | $ret[$i]['group_total_members'] ='' . _MD_SUICO_NO_MEMBER . ' '; |
||
| 307 | } |
||
| 308 | $i++; |
||
| 309 | } |
||
| 310 | return $ret; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * count suico_groupss matching a condition |
||
| 315 | * |
||
| 316 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 317 | * @return int count of suico_groupss |
||
| 318 | */ |
||
| 319 | public function getCount( |
||
| 320 | ?CriteriaElement $criteriaElement = null |
||
| 321 | ) { |
||
| 322 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_groups'); |
||
| 323 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 324 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 325 | } |
||
| 326 | $result = $this->db->query($sql); |
||
| 327 | if (!$result) { |
||
| 328 | return 0; |
||
| 329 | } |
||
| 330 | [$count] = $this->db->fetchRow($result); |
||
| 331 | return $count; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * delete suico_groupss matching a set of conditions |
||
| 336 | * |
||
| 337 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 338 | * @param bool $force |
||
| 339 | * @param bool $asObject |
||
| 340 | * @return bool FALSE if deletion failed |
||
| 341 | */ |
||
| 342 | 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 | return true; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $group |
||
| 386 | * @param $maxbytes |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | public function renderFormEdit( |
||
| 390 | $group, |
||
| 391 | $maxbytes |
||
| 392 | ) { |
||
| 393 | $form = new XoopsThemeForm(\_MD_SUICO_EDIT_GROUP, 'form_editgroup', 'editgroup.php', 'post', true); |
||
| 394 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 395 | $field_groupid = new XoopsFormHidden('group_id', $group->getVar('group_id')); |
||
| 396 | $field_url = new XoopsFormFile(\_MD_SUICO_GROUP_IMAGE, 'img', $maxbytes); |
||
| 397 | $field_url->setExtra('style="visibility:hidden;"'); |
||
| 398 | $field_title = new XoopsFormText(\_MD_SUICO_GROUP_TITLE, 'title', 35, 55, $group->getVar('group_title')); |
||
| 399 | $field_desc = new XoopsFormTextArea(\_MD_SUICO_GROUP_DESC, 'desc', $group->getVar('group_desc')); |
||
| 400 | $field_marker = new XoopsFormHidden('marker', '1'); |
||
| 401 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADGROUP, 'submit'); |
||
| 402 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 403 | $field_oldpicture = new XoopsFormLabel( |
||
| 404 | \_MD_SUICO_GROUP_IMAGE, '<img src="' . \XOOPS_UPLOAD_URL . '/' . $group->getVar( |
||
| 405 | 'group_img' |
||
| 406 | ) . '">' |
||
| 407 | ); |
||
| 408 | $field_maintainimage = new XoopsFormLabel( |
||
| 409 | \_MD_SUICO_MAINTAIN_OLD_IMAGE, "<input type='checkbox' value='1' id='flag_oldimg' name='flag_oldimg' onclick=\"groupImgSwitch(img)\" checked>" |
||
| 410 | ); |
||
| 411 | $form->addElement($field_oldpicture); |
||
| 412 | $form->addElement($field_maintainimage); |
||
| 413 | $form->addElement($field_warning); |
||
| 414 | $form->addElement($field_url); |
||
| 415 | $form->addElement($field_groupid); |
||
| 416 | $form->addElement($field_title); |
||
| 417 | $form->addElement($field_desc); |
||
| 418 | $form->addElement($field_marker); |
||
| 419 | $form->addElement($buttonSend); |
||
| 420 | $form->display(); |
||
| 421 | echo " |
||
| 422 | <!-- Start Form Validation JavaScript //--> |
||
| 423 | <script type='text/javascript'> |
||
| 424 | <!--// |
||
| 425 | function groupImgSwitch(img) { |
||
| 426 | |||
| 427 | var elestyle = xoopsGetElementById(img).style; |
||
| 428 | |||
| 429 | if (elestyle.visibility == \"hidden\") { |
||
| 430 | elestyle.visibility = \"visible\"; |
||
| 431 | } else { |
||
| 432 | elestyle.visibility = \"hidden\"; |
||
| 433 | } |
||
| 434 | |||
| 435 | |||
| 436 | } |
||
| 437 | //--></script> |
||
| 438 | <!-- End Form Validation JavaScript //--> |
||
| 439 | "; |
||
| 440 | return true; |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $group_title |
||
| 445 | * @param string $group_desc |
||
| 446 | * @param string $group_img |
||
| 447 | * @param string $path_upload |
||
| 448 | * @param int $maxfilebytes |
||
| 449 | * @param int $maxfilewidth |
||
| 450 | * @param int $maxfileheight |
||
| 451 | * @param int $change_img |
||
| 452 | * @param string $group |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | public function receiveGroup( |
||
| 543 | } |
||
| 544 | |||
| 545 | |||
| 546 | public function isGroupMember($owner_id) |
||
| 547 | { |
||
| 548 | $query = 'SELECT COUNT(rel_id) AS grouptotalmembers FROM ' . $GLOBALS['xoopsDB']->prefix('suico_relgroupuser') . ' WHERE rel_group_id=' . $group_id . ''; |
||
| 549 | $queryresult = $GLOBALS['xoopsDB']->query($query); |
||
| 550 | $row = $GLOBALS['xoopsDB']->fetchArray($queryresult); |
||
| 551 | $group_total_members = $row['grouptotalmembers']; |
||
| 552 | |||
| 553 | return $group_total_members; |
||
| 554 | } |
||
| 555 | |||
| 556 | |||
| 557 | public function getComment($group_id) |
||
| 558 | { |
||
| 559 | $module_handler = xoops_gethandler("module"); |
||
| 560 | $mod_suico = $module_handler->getByDirname('suico'); |
||
| 561 | $sql= "SELECT count(com_id) FROM ".$GLOBALS['xoopsDB']->prefix('xoopscomments')." WHERE com_modid = '".$mod_suico->getVar('mid')."' AND com_itemid = '".$group_id."'"; |
||
| 562 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
| 563 | while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) { |
||
| 564 | $group_total_comments=$row['count(com_id)']; |
||
| 565 | } |
||
| 566 | |||
| 567 | return $group_total_comments; |
||
| 568 | } |
||
| 569 | |||
| 570 | public function getGroupTotalMembers($group_id) |
||
| 578 | } |
||
| 579 | |||
| 580 | } |
||
| 581 |