| Total Complexity | 49 |
| Total Lines | 645 |
| 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\Yogurt\Helper|null $helper |
||
| 63 | */ |
||
| 64 | |||
| 65 | public function __construct( |
||
| 66 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 67 | $helper = null |
||
| 68 | ) { |
||
| 69 | /** @var \XoopsModules\Yogurt\Helper $this ->helper */ |
||
| 70 | |||
| 71 | if (null === $helper) { |
||
| 72 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 73 | } else { |
||
| 74 | $this->helper = $helper; |
||
| 75 | } |
||
| 76 | |||
| 77 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 78 | |||
| 79 | parent::__construct($xoopsDatabase, 'yogurt_groups', Groups::class, 'group_id', 'group_title'); |
||
| 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( |
||
| 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('yogurt_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('yogurt_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('yogurt_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 yogurt_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( |
||
| 270 | ?CriteriaElement $criteriaElement = null, |
||
| 271 | $id_as_key = false, |
||
| 272 | $as_object = true |
||
| 273 | ) { |
||
| 274 | $ret = []; |
||
| 275 | |||
| 276 | $limit = $start = 0; |
||
| 277 | |||
| 278 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_groups'); |
||
| 279 | |||
| 280 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 281 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 282 | |||
| 283 | if ('' !== $criteriaElement->getSort()) { |
||
| 284 | $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder(); |
||
| 285 | } |
||
| 286 | |||
| 287 | $limit = $criteriaElement->getLimit(); |
||
| 288 | |||
| 289 | $start = $criteriaElement->getStart(); |
||
| 290 | } |
||
| 291 | |||
| 292 | $result = $this->db->query($sql, $limit, $start); |
||
| 293 | |||
| 294 | if (!$result) { |
||
| 295 | return $ret; |
||
| 296 | } |
||
| 297 | |||
| 298 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 299 | $yogurt_groups = new Groups(); |
||
| 300 | |||
| 301 | $yogurt_groups->assignVars($myrow); |
||
| 302 | |||
| 303 | if (!$id_as_key) { |
||
| 304 | $ret[] = &$yogurt_groups; |
||
| 305 | } else { |
||
| 306 | $ret[$myrow['group_id']] = &$yogurt_groups; |
||
| 307 | } |
||
| 308 | |||
| 309 | unset($yogurt_groups); |
||
| 310 | } |
||
| 311 | |||
| 312 | return $ret; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * retrieve yogurt_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('yogurt_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('yogurt_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_YOGURT_GROUPMEMBERS; |
||
| 374 | $i++; |
||
| 375 | } |
||
| 376 | |||
| 377 | return $ret; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * count yogurt_groupss matching a condition |
||
| 382 | * |
||
| 383 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 384 | * @return int count of yogurt_groupss |
||
| 385 | */ |
||
| 386 | |||
| 387 | public function getCount( |
||
| 388 | ?CriteriaElement $criteriaElement = null |
||
| 389 | ) { |
||
| 390 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_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 yogurt_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( |
||
| 417 | ?CriteriaElement $criteriaElement = null, |
||
| 418 | $force = true, |
||
| 419 | $asObject = false |
||
| 420 | ) { |
||
| 421 | $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_groups'); |
||
| 422 | |||
| 423 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 424 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 425 | } |
||
| 426 | |||
| 427 | if (!$result = $this->db->query($sql)) { |
||
| 428 | return false; |
||
| 429 | } |
||
| 430 | |||
| 431 | return true; |
||
| 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_YOGURT_SUBMIT_GROUP, 'form_group', 'submitGroup.php', 'post', true); |
||
| 445 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 446 | |||
| 447 | $field_url = new XoopsFormFile(\_MD_YOGURT_GROUP_IMAGE, 'group_img', $maxbytes); |
||
| 448 | $field_title = new XoopsFormText(\_MD_YOGURT_GROUP_TITLE, 'group_title', 35, 55); |
||
| 449 | $field_desc = new XoopsFormText(\_MD_YOGURT_GROUP_DESC, 'group_desc', 35, 55); |
||
| 450 | $field_marker = new XoopsFormHidden('marker', '1'); |
||
| 451 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_YOGURT_UPLOADGROUP, 'submit'); |
||
| 452 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_YOGURT_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( |
||
| 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( |
||
| 698 | } |
||
| 699 | } |
||
| 700 |