| Total Complexity | 47 |
| Total Lines | 439 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PicturesHandler 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 PicturesHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 51 | class PicturesHandler extends \XoopsPersistableObjectHandler |
||
| 52 | { |
||
| 53 | private const TABLE = 'adslight_pictures'; |
||
| 54 | private const ENTITY = Pictures::class; |
||
| 55 | private const ENTITYNAME = 'Pictures'; |
||
| 56 | private const KEYNAME = 'cod_img'; |
||
| 57 | private const IDENTIFIER = 'title'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Class constructor |
||
| 61 | * @param \XoopsDatabase|null $db |
||
| 62 | */ |
||
| 63 | public function __construct(\XoopsDatabase $db) |
||
| 64 | { |
||
| 65 | $this->db = $db; |
||
| 66 | parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * create a new light_pictures |
||
| 71 | * |
||
| 72 | * @param bool $isNew flag the new objects as "new"? |
||
| 73 | * |
||
| 74 | * @return \XoopsObject |
||
| 75 | */ |
||
| 76 | public function create($isNew = true) |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * retrieve a light_pictures |
||
| 90 | * |
||
| 91 | * @param int|null $id of the light_pictures |
||
| 92 | * @param array|null $fields |
||
| 93 | * @return false|\XoopsModules\Adslight\Pictures reference to the {@link light_pictures} object, FALSE if failed |
||
| 94 | */ |
||
| 95 | public function get($id = null, $fields = null) |
||
| 96 | { |
||
| 97 | // $sql = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures') . ' WHERE cod_img=' . $id . ' AND lid=' . $lid . ' '; |
||
| 98 | $sql = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures') . ' WHERE cod_img=' . $id . ' '; |
||
| 99 | $result = $this->db->query($sql); |
||
| 100 | if (!$this->db->isResultSet($result)) { |
||
| 101 | // \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | $numrows = $this->db->getRowsNum($result); |
||
| 105 | if (1 === $numrows) { |
||
| 106 | $adslightPictures = new Pictures(); |
||
| 107 | $adslightPictures->assignVars($this->db->fetchArray($result)); |
||
| 108 | |||
| 109 | return $adslightPictures; |
||
| 110 | } |
||
| 111 | |||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * insert a new AdslightPicture object into the database |
||
| 117 | * |
||
| 118 | * @param bool $force |
||
| 119 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 120 | */ |
||
| 121 | public function insert(\XoopsObject $object, $force = false): bool |
||
| 122 | { |
||
| 123 | global $lid; |
||
| 124 | if (!$object instanceof Pictures) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | if (!$object->isDirty()) { |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | if (!$object->cleanVars()) { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | foreach ($object->cleanVars as $k => $v) { |
||
| 134 | ${$k} = $v; |
||
| 135 | } |
||
| 136 | $now = \time(); |
||
| 137 | if ($object->isNew()) { |
||
| 138 | // add/modify of Pictures |
||
| 139 | $object = new Pictures(); |
||
| 140 | |||
| 141 | $format = 'INSERT INTO `%s` (cod_img, title, date_created, date_updated, lid, uid_owner, url)'; |
||
| 142 | $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s)'; |
||
| 143 | $sql = \sprintf($format, $this->db->prefix('adslight_pictures'), $cod_img, $this->db->quoteString($title), $now, $now, $this->db->quoteString($lid), $this->db->quoteString($uid_owner), $this->db->quoteString($url)); |
||
| 144 | $force = true; |
||
| 145 | } else { |
||
| 146 | $format = 'UPDATE `%s` SET '; |
||
| 147 | $format .= 'cod_img=%u, title=%s, date_created=%s, date_updated=%s, lid=%s, uid_owner=%s, url=%s'; |
||
| 148 | $format .= ' WHERE cod_img = %u'; |
||
| 149 | $sql = \sprintf($format, $this->db->prefix('adslight_pictures'), $cod_img, $this->db->quoteString($title), $now, $now, $this->db->quoteString($lid), $this->db->quoteString($uid_owner), $this->db->quoteString($url), $cod_img); |
||
| 150 | } |
||
| 151 | if ($force) { |
||
| 152 | $result = $this->db->queryF($sql); |
||
| 153 | } else { |
||
| 154 | $result = $this->db->query($sql); |
||
| 155 | } |
||
| 156 | if (!$result) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | if (empty($cod_img)) { |
||
| 160 | $cod_img = $this->db->getInsertId(); |
||
| 161 | } |
||
| 162 | $object->assignVars([ |
||
| 163 | 'cod_img' => $cod_img, |
||
| 164 | 'lid' => $lid, |
||
| 165 | 'url' => $url, |
||
| 166 | ]); |
||
| 167 | |||
| 168 | return true; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * delete Pictures object from the database |
||
| 173 | * |
||
| 174 | * @param \XoopsObject $object reference to the Pictures to delete |
||
| 175 | * @param bool $force |
||
| 176 | * @return bool FALSE if failed. |
||
| 177 | */ |
||
| 178 | public function delete(\XoopsObject $object, $force = false): bool |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * retrieve Pictures object(s) from the database |
||
| 198 | * |
||
| 199 | * @param \CriteriaCompo|\CriteriaElement|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 200 | * @param bool $id_as_key use the UID as key for the array? |
||
| 201 | * @param mixed $as_object |
||
| 202 | * @return array array of {@link Pictures} objects |
||
| 203 | */ |
||
| 204 | public function &getObjects($criteria = null, $id_as_key = false, $as_object = true) //&getObjects(?\CriteriaElement $criteria = null, $id_as_key = false): array |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * count Pictures matching a condition |
||
| 238 | * |
||
| 239 | * @param \CriteriaElement|null $criteria {@link \CriteriaElement} to match |
||
| 240 | * @return int count of Pictures |
||
| 241 | */ |
||
| 242 | public function getCount(?\CriteriaElement $criteria = null): int |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * delete Pictures matching a set of conditions |
||
| 259 | * |
||
| 260 | * @param \CriteriaCompo|\CriteriaElement|null $criteria {@link \CriteriaElement} |
||
| 261 | * @param mixed $force |
||
| 262 | * @param mixed $asObject |
||
| 263 | * @return bool FALSE if deletion failed |
||
| 264 | */ |
||
| 265 | // public function deleteAll(?\CriteriaElement $criteria = null): bool |
||
| 266 | public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false) |
||
| 267 | { |
||
| 268 | $sql = 'DELETE FROM ' . $this->db->prefix('adslight_pictures'); |
||
| 269 | if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) { |
||
| 270 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 271 | } |
||
| 272 | if (!$result = $this->db->query($sql)) { |
||
| 273 | return false; |
||
| 274 | } |
||
| 275 | |||
| 276 | return true; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Render a form to send pictures |
||
| 281 | * |
||
| 282 | * @param int $uid |
||
| 283 | * @param int $lid |
||
| 284 | * @param int $maxbytes the maximum size of a picture |
||
| 285 | * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
| 286 | * @return bool TRUE |
||
| 287 | * |
||
| 288 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
| 289 | */ |
||
| 290 | public function renderFormSubmit( |
||
| 291 | $uid, |
||
| 292 | $lid, |
||
| 293 | $maxbytes, |
||
| 294 | $xoopsTpl |
||
| 295 | ): bool { |
||
| 296 | global $xoopsUser; |
||
| 297 | $uid = (int)$uid; |
||
| 298 | $lid = (int)$lid; |
||
| 299 | $form = new \XoopsThemeForm(\_ADSLIGHT_SUBMIT_PIC_TITLE, 'form_picture', XOOPS_URL . "/modules/adslight/add_photo.php?lid={$lid}&uid=" . $xoopsUser->getVar('uid'), 'post', true); |
||
| 300 | $field_url = new \XoopsFormFile(\_ADSLIGHT_SELECT_PHOTO, 'sel_photo', 2000000); |
||
| 301 | $field_desc = new \XoopsFormText(\_ADSLIGHT_CAPTION, 'caption', 35, 55); |
||
| 302 | |||
| 303 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 304 | $button_send = new \XoopsFormButton('', 'submit_button', \_ADSLIGHT_UPLOADPICTURE, 'submit'); |
||
| 305 | $field_warning = new \XoopsFormLabel(\sprintf(\_ADSLIGHT_YOUCANUPLOAD, $maxbytes / 1024)); |
||
| 306 | $field_lid = new \XoopsFormHidden('lid', $lid); |
||
| 307 | $field_uid = new \XoopsFormHidden('uid', $uid); |
||
| 308 | |||
| 309 | $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML(); |
||
| 310 | |||
| 311 | $form->addElement($field_warning); |
||
| 312 | $form->addElement($field_url, true); |
||
| 313 | $form->addElement($field_desc, true); |
||
| 314 | $form->addElement($field_lid, true); |
||
| 315 | $form->addElement($field_uid, true); |
||
| 316 | |||
| 317 | $form->addElement($field_token, true); |
||
| 318 | |||
| 319 | $form->addElement($button_send); |
||
| 320 | if (\str_replace('.', '', \PHP_VERSION) > 499) { |
||
| 321 | $form->assign($xoopsTpl); |
||
| 322 | } else { |
||
| 323 | $form->display(); |
||
| 324 | } |
||
| 325 | |||
| 326 | return true; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Render a form to edit the description of the pictures |
||
| 331 | * |
||
| 332 | * @param string $caption The description of the picture |
||
| 333 | * @param int $cod_img the id of the image in database |
||
| 334 | * @param string $filename the url to the thumb of the image so it can be displayed |
||
| 335 | * @return bool TRUE |
||
| 336 | */ |
||
| 337 | public function renderFormEdit( |
||
| 338 | $caption, |
||
| 339 | $cod_img, |
||
| 340 | $filename |
||
| 341 | ): bool { |
||
| 342 | $form = new \XoopsThemeForm(\_ADSLIGHT_EDIT_CAPTION, 'form_picture', 'editdesc.php', 'post', true); |
||
| 343 | $field_desc = new \XoopsFormText($caption, 'caption', 35, 55); |
||
| 344 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 345 | $button_send = new \XoopsFormButton(\_ADSLIGHT_EDIT, 'submit_button', \_SUBMIT, 'submit'); |
||
| 346 | //@todo - replace alt with language string |
||
| 347 | $field_warning = new \XoopsFormLabel("<img src='{$filename}' alt='sssss'>"); |
||
| 348 | $field_cod_img = new \XoopsFormHidden('cod_img', $cod_img); |
||
| 349 | // $field_lid = new \XoopsFormHidden('lid', $lid); |
||
| 350 | $field_marker = new \XoopsFormHidden('marker', 1); |
||
| 351 | |||
| 352 | $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML(); |
||
| 353 | |||
| 354 | $form->addElement($field_warning); |
||
| 355 | $form->addElement($field_desc); |
||
| 356 | $form->addElement($field_cod_img); |
||
| 357 | $form->addElement($field_marker); |
||
| 358 | $form->addElement($field_token); |
||
| 359 | $form->addElement($button_send); |
||
| 360 | $form->display(); |
||
| 361 | |||
| 362 | return true; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Upload the file and Save into database |
||
| 367 | * |
||
| 368 | * @param string $title A litle description of the file |
||
| 369 | * @param string $pathUpload The path to where the file should be uploaded |
||
| 370 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 371 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 372 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 373 | * @param int $pictheight the height in pixels that the pic will have |
||
| 374 | * @param int $maxfilebytes the maximum size a file can have to be uploaded in bytes |
||
| 375 | * @param int $maxfilewidth the maximum width in pixels that a pic can have |
||
| 376 | * @param int $maxfileheight the maximum height in pixels that a pic can have |
||
| 377 | * @return bool FALSE if upload fails or database fails |
||
| 378 | */ |
||
| 379 | public function receivePicture( |
||
| 380 | $title, |
||
| 381 | $pathUpload, |
||
| 382 | $thumbwidth, |
||
| 383 | $thumbheight, |
||
| 384 | $pictwidth, |
||
| 385 | $pictheight, |
||
| 386 | $maxfilebytes, |
||
| 387 | $maxfilewidth, |
||
| 388 | $maxfileheight |
||
| 389 | ): bool { |
||
| 390 | global $lid; |
||
| 391 | //busca id do user logado |
||
| 392 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 393 | $lid = Request::getInt('lid', 0, 'POST'); |
||
| 394 | //create a hash so it does not erase another file |
||
| 395 | $hash1 = \time(); |
||
| 396 | $hash = \mb_substr((string)$hash1, 0, 4); |
||
| 397 | // mimetypes and settings put this in admin part later |
||
| 398 | $allowed_mimetypes = [ |
||
| 399 | 'image/jpeg', |
||
| 400 | 'image/gif', |
||
| 401 | ]; |
||
| 402 | $maxfilesize = $maxfilebytes; |
||
| 403 | // create the object to upload |
||
| 404 | $uploader = new \XoopsMediaUploader($pathUpload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight); |
||
| 405 | // fetch the media |
||
| 406 | if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) { |
||
| 407 | //let'screate a name for it |
||
| 408 | $uploader->setPrefix("pic_{$lid}_"); |
||
| 409 | //now let s upload the file |
||
| 410 | if (!$uploader->upload()) { |
||
| 411 | // if there are errors let'sreturn them |
||
| 412 | echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center;"><p>' . $uploader->getErrors() . '</p></div>'; |
||
| 413 | |||
| 414 | return false; |
||
| 415 | } |
||
| 416 | // now let s create a new object picture and set its variables |
||
| 417 | $picture = $this->create(); |
||
| 418 | $url = $uploader->getSavedFileName(); |
||
| 419 | $picture->setVar('url', $url); |
||
| 420 | $picture->setVar('title', $title); |
||
| 421 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 422 | // $lid = $lid; |
||
| 423 | $picture->setVar('lid', $lid); |
||
| 424 | $picture->setVar('uid_owner', $uid); |
||
| 425 | $this->insert($picture); |
||
| 426 | $saved_destination = $uploader->getSavedDestination(); |
||
| 427 | $this->resizeImage($saved_destination, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $pathUpload); |
||
| 428 | } else { |
||
| 429 | echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center;"><p>' . $uploader->getErrors() . '</p></div>'; |
||
| 430 | |||
| 431 | return false; |
||
| 432 | } |
||
| 433 | |||
| 434 | return true; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Resize a picture and save it to $pathUpload |
||
| 439 | * |
||
| 440 | * @param string $img the path to the file |
||
| 441 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 442 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 443 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 444 | * @param int $pictheight the height in pixels that the pic will have |
||
| 445 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
| 446 | */ |
||
| 447 | public function resizeImage( |
||
| 490 | } |
||
| 491 | } |
||
| 492 |