| Total Complexity | 61 |
| Total Lines | 639 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ImageHandler 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 ImageHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class ImageHandler extends XoopsPersistableObjectHandler |
||
| 55 | { |
||
| 56 | public $helper; |
||
| 57 | public $isAdmin; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor |
||
| 61 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 62 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 63 | */ |
||
| 64 | public function __construct( |
||
| 65 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 66 | $helper = null |
||
| 67 | ) { |
||
| 68 | /** @var \XoopsModules\Suico\Helper $this->helper */ |
||
| 69 | if (null === $helper) { |
||
| 70 | $this->helper = Helper::getInstance(); |
||
| 71 | } else { |
||
| 72 | $this->helper = $helper; |
||
| 73 | } |
||
| 74 | $isAdmin = $this->helper->isUserAdmin(); |
||
|
|
|||
| 75 | parent::__construct($xoopsDatabase, 'suico_images', Image::class, 'image_id', 'title', 'caption'); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * create a new Groups |
||
| 80 | * |
||
| 81 | * @param bool $isNew flag the new objects as "new"? |
||
| 82 | * @return \XoopsObject Groups |
||
| 83 | */ |
||
| 84 | public function create( |
||
| 85 | $isNew = true |
||
| 86 | ) { |
||
| 87 | $obj = parent::create($isNew); |
||
| 88 | // if ($isNew) { |
||
| 89 | // $obj->setDefaultPermissions(); |
||
| 90 | // } |
||
| 91 | if ($isNew) { |
||
| 92 | $obj->setNew(); |
||
| 93 | } else { |
||
| 94 | $obj->unsetNew(); |
||
| 95 | } |
||
| 96 | $obj->helper = $this->helper; |
||
| 97 | return $obj; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * retrieve a Image |
||
| 102 | * |
||
| 103 | * @param int $id of the Image |
||
| 104 | * @param null $fields |
||
| 105 | * @return mixed reference to the {@link Image} object, FALSE if failed |
||
| 106 | */ |
||
| 107 | public function get2( |
||
| 108 | $id = null, |
||
| 109 | $fields = null |
||
| 110 | ) { |
||
| 111 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_images') . ' WHERE image_id=' . $id; |
||
| 112 | if (!$result = $this->db->query($sql)) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | $numrows = $this->db->getRowsNum($result); |
||
| 116 | if (1 === $numrows) { |
||
| 117 | $image = new Image(); |
||
| 118 | $image->assignVars($this->db->fetchArray($result)); |
||
| 119 | return $image; |
||
| 120 | } |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * insert a new Image in the database |
||
| 126 | * |
||
| 127 | * @param \XoopsObject $xoopsObject reference to the {@link Image} object |
||
| 128 | * @param bool $force |
||
| 129 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 130 | */ |
||
| 131 | public function insert2( |
||
| 132 | XoopsObject $xoopsObject, |
||
| 133 | $force = false |
||
| 134 | ) { |
||
| 135 | global $xoopsConfig; |
||
| 136 | if (!$xoopsObject instanceof Image) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | if (!$xoopsObject->isDirty()) { |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | if (!$xoopsObject->cleanVars()) { |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | $image_id = ''; |
||
| 146 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 147 | ${$k} = $v; |
||
| 148 | } |
||
| 149 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 150 | if ($xoopsObject->isNew()) { |
||
| 151 | // ajout/modification d'un Image |
||
| 152 | $xoopsObject = new Image(); |
||
| 153 | $format = 'INSERT INTO %s (image_id, title, caption, date_created, date_updated, uid_owner, filename, private)'; |
||
| 154 | $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s, 0)'; |
||
| 155 | $sql = \sprintf( |
||
| 156 | $format, |
||
| 157 | $this->db->prefix('suico_images'), |
||
| 158 | $image_id, |
||
| 159 | $this->db->quoteString($title), |
||
| 160 | $this->db->quoteString($caption), |
||
| 161 | \time(),//$now, |
||
| 162 | \time(),//$now, |
||
| 163 | $this->db->quoteString($uid_owner), |
||
| 164 | $this->db->quoteString($filename) |
||
| 165 | ); |
||
| 166 | $force = true; |
||
| 167 | } else { |
||
| 168 | $format = 'UPDATE %s SET '; |
||
| 169 | $format .= 'image_id=%u, title=%s, caption=%s, date_created=%s, date_updated=%s, uid_owner=%s, filename=%s, private=%s'; |
||
| 170 | $format .= ' WHERE image_id = %u'; |
||
| 171 | $sql = \sprintf( |
||
| 172 | $format, |
||
| 173 | $this->db->prefix('suico_images'), |
||
| 174 | $image_id, |
||
| 175 | $this->db->quoteString($title), |
||
| 176 | $this->db->quoteString($caption), |
||
| 177 | $xoopsObject->getVar('date_created'), // $now, |
||
| 178 | $xoopsObject->getVar('date_updated'), // $now, |
||
| 179 | $this->db->quoteString($uid_owner), |
||
| 180 | $this->db->quoteString($filename), |
||
| 181 | $this->db->quoteString($private), |
||
| 182 | $image_id |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | if ($force) { |
||
| 186 | $result = $this->db->queryF($sql); |
||
| 187 | } else { |
||
| 188 | $result = $this->db->query($sql); |
||
| 189 | } |
||
| 190 | if (!$result) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | if (empty($image_id)) { |
||
| 194 | $image_id = $this->db->getInsertId(); |
||
| 195 | } |
||
| 196 | $xoopsObject->assignVar('image_id', $image_id); |
||
| 197 | return true; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * delete a Image from the database |
||
| 202 | * |
||
| 203 | * @param \XoopsObject $xoopsObject reference to the Image to delete |
||
| 204 | * @param bool $force |
||
| 205 | * @return bool FALSE if failed. |
||
| 206 | */ |
||
| 207 | public function delete( |
||
| 208 | XoopsObject $xoopsObject, |
||
| 209 | $force = false |
||
| 210 | ) { |
||
| 211 | if (!$xoopsObject instanceof Image) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | $sql = \sprintf( |
||
| 215 | 'DELETE FROM %s WHERE image_id = %u', |
||
| 216 | $this->db->prefix('suico_images'), |
||
| 217 | $xoopsObject->getVar('image_id') |
||
| 218 | ); |
||
| 219 | if ($force) { |
||
| 220 | $result = $this->db->queryF($sql); |
||
| 221 | } else { |
||
| 222 | $result = $this->db->query($sql); |
||
| 223 | } |
||
| 224 | if (!$result) { |
||
| 225 | return false; |
||
| 226 | } |
||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * retrieve suico_imagess from the database |
||
| 232 | * |
||
| 233 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 234 | * @param bool $id_as_key use the UID as key for the array? |
||
| 235 | * @param bool $as_object |
||
| 236 | * @return array array of {@link Image} objects |
||
| 237 | */ |
||
| 238 | public function &getObjects( |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * count suico_imagess matching a condition |
||
| 273 | * |
||
| 274 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 275 | * @return int count of suico_imagess |
||
| 276 | */ |
||
| 277 | public function getCount( |
||
| 278 | ?CriteriaElement $criteriaElement = null |
||
| 279 | ) { |
||
| 280 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_images'); |
||
| 281 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 282 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 283 | } |
||
| 284 | $result = $this->db->query($sql); |
||
| 285 | if (!$result) { |
||
| 286 | return 0; |
||
| 287 | } |
||
| 288 | [$count] = $this->db->fetchRow($result); |
||
| 289 | return (int)$count; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * delete suico_imagess matching a set of conditions |
||
| 294 | * |
||
| 295 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 296 | * @param bool $force |
||
| 297 | * @param bool $asObject |
||
| 298 | * @return bool FALSE if deletion failed |
||
| 299 | */ |
||
| 300 | public function deleteAll( |
||
| 301 | ?CriteriaElement $criteriaElement = null, |
||
| 302 | $force = true, |
||
| 303 | $asObject = false |
||
| 304 | ) { |
||
| 305 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_images'); |
||
| 306 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 307 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 308 | } |
||
| 309 | if (!$result = $this->db->query($sql)) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | return true; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Render a form to send pictures |
||
| 317 | * |
||
| 318 | * @param int $maxbytes the maximum size of a picture |
||
| 319 | * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
| 320 | * @return bool TRUE |
||
| 321 | * |
||
| 322 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
| 323 | */ |
||
| 324 | public function renderFormSubmit( |
||
| 325 | $maxbytes, |
||
| 326 | $xoopsTpl |
||
| 327 | ) { |
||
| 328 | $form = new XoopsThemeForm(\_MD_SUICO_SUBMIT_PIC_TITLE, 'form_picture', 'submitImage.php', 'post', true); |
||
| 329 | $field_url = new XoopsFormFile(\_MD_SUICO_SELECT_PHOTO, 'sel_photo', 2000000); |
||
| 330 | $field_title = new XoopsFormText(\_MD_SUICO_PHOTOTITLE, 'title', 35, 55); |
||
| 331 | $field_caption = new XoopsFormText(\_MD_SUICO_CAPTION, 'caption', 35, 55); |
||
| 332 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 333 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADPICTURE, 'submit'); |
||
| 334 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 335 | $form->addElement($field_warning); |
||
| 336 | $form->addElement($field_url, true); |
||
| 337 | $form->addElement($field_title); |
||
| 338 | $form->addElement($field_caption); |
||
| 339 | $form->addElement($buttonSend); |
||
| 340 | $form->assign($xoopsTpl); //If your server is php 5 |
||
| 341 | return true; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Render a form to edit the description of the pictures |
||
| 346 | * |
||
| 347 | * @param $title |
||
| 348 | * @param string $caption The description of the picture |
||
| 349 | * @param int $image_id the id of the image in database |
||
| 350 | * @param string $filename the url to the thumb of the image so it can be displayed |
||
| 351 | * @return bool TRUE |
||
| 352 | */ |
||
| 353 | public function renderFormEdit( |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Upload the file and Save into database |
||
| 379 | * |
||
| 380 | * @param string $title A litle title of the file |
||
| 381 | * @param string $caption A litle description of the file |
||
| 382 | * @param string $pathUpload The path to where the file should be uploaded |
||
| 383 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 384 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 385 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 386 | * @param int $pictheight the height in pixels that the pic will have |
||
| 387 | * @param int $maxfilebytes the maximum size a file can have to be uploaded in bytes |
||
| 388 | * @param int $maxfilewidth the maximum width in pixels that a pic can have |
||
| 389 | * @param int $maxfileheight the maximum height in pixels that a pic can have |
||
| 390 | * @return bool FALSE if upload fails or database fails |
||
| 391 | */ |
||
| 392 | public function receivePicture( |
||
| 393 | $title, |
||
| 394 | $caption, |
||
| 395 | $pathUpload, |
||
| 396 | $thumbwidth, |
||
| 397 | $thumbheight, |
||
| 398 | $pictwidth, |
||
| 399 | $pictheight, |
||
| 400 | $maxfilebytes, |
||
| 401 | $maxfilewidth, |
||
| 402 | $maxfileheight |
||
| 403 | ) { |
||
| 404 | global $xoopsUser, $xoopsDB; |
||
| 405 | //search logged user id |
||
| 406 | $uid = $xoopsUser->getVar('uid'); |
||
| 407 | //create a hash so it does not erase another file |
||
| 408 | //$hash1 = date(); |
||
| 409 | //$hash = substr($hash1,0,4); |
||
| 410 | // mimetypes and settings put this in admin part later |
||
| 411 | $allowed_mimetypes = Helper::getInstance()->getConfig( |
||
| 412 | 'mimetypes' |
||
| 413 | ); |
||
| 414 | $maxfilesize = $maxfilebytes; |
||
| 415 | // $uploadDir = \XOOPS_UPLOAD_PATH . '/suico/images/'; |
||
| 416 | // create the object to upload |
||
| 417 | $uploader = new XoopsMediaUploader( |
||
| 418 | $pathUpload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight |
||
| 419 | ); |
||
| 420 | // fetch the media |
||
| 421 | if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', '', 'POST')[0])) { |
||
| 422 | //lets create a name for it |
||
| 423 | $uploader->setPrefix('pic_' . $uid . '_'); |
||
| 424 | //now let s upload the file |
||
| 425 | if (!$uploader->upload()) { |
||
| 426 | // if there are errors lets return them |
||
| 427 | 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>'; |
||
| 428 | return false; |
||
| 429 | } |
||
| 430 | // now let s create a new object picture and set its variables |
||
| 431 | $picture = $this->create(); |
||
| 432 | $filename = $uploader->getSavedFileName(); |
||
| 433 | $picture->setVar('filename', $filename); |
||
| 434 | $picture->setVar('title', $title); |
||
| 435 | $picture->setVar('caption', $caption); |
||
| 436 | $picture->setVar('date_created', \time()); |
||
| 437 | $picture->setVar('date_updated', \time()); |
||
| 438 | $picture->setVar('private', 0); |
||
| 439 | $uid = $xoopsUser->getVar('uid'); |
||
| 440 | $picture->setVar('uid_owner', $uid); |
||
| 441 | $this->insert($picture); |
||
| 442 | $saved_destination = $uploader->getSavedDestination(); |
||
| 443 | //print_r($_FILES); |
||
| 444 | //$this->resizeImage($saved_destination,false, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload); |
||
| 445 | //$this->resizeImage($saved_destination,true, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload); |
||
| 446 | $this->resizeImage( |
||
| 447 | $saved_destination, |
||
| 448 | $thumbwidth, |
||
| 449 | $thumbheight, |
||
| 450 | $pictwidth, |
||
| 451 | $pictheight, |
||
| 452 | $pathUpload |
||
| 453 | ); |
||
| 454 | } else { |
||
| 455 | 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>'; |
||
| 456 | return false; |
||
| 457 | } |
||
| 458 | return true; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Resize a picture and save it to $pathUpload |
||
| 463 | * |
||
| 464 | * @param string $img the path to the file |
||
| 465 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 466 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 467 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 468 | * @param int $pictheight the height in pixels that the pic will have |
||
| 469 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
| 470 | */ |
||
| 471 | public function resizeImage( |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param $limit |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | public function getLastPictures($limit) |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param $limit |
||
| 565 | * @return array |
||
| 566 | */ |
||
| 567 | public function getLastPicturesForBlock($limit) |
||
| 568 | { |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Resize a picture and save it to $pathUpload |
||
| 624 | * |
||
| 625 | * @param string $img the path to the file |
||
| 626 | * @param $width |
||
| 627 | * @param $height |
||
| 628 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
| 629 | */ |
||
| 630 | public function makeAvatar( |
||
| 696 |