| Total Complexity | 47 |
| Total Lines | 414 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 51 | * |
||
| 52 | * @todo change this to a XoopsPersistableObjectHandler and remove 'most' method overloads |
||
| 53 | */ |
||
| 54 | class PicturesHandler extends \XoopsObjectHandler |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * Class constructor |
||
| 58 | * @param \XoopsDatabase|null $db |
||
| 59 | */ |
||
| 60 | public function __construct($db) |
||
| 61 | { |
||
| 62 | parent::__construct($db, 'adslight_pictures', Pictures::class, 'cod_img', 'title'); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * create a new light_pictures |
||
| 67 | * |
||
| 68 | * @param bool $isNew flag the new objects as "new"? |
||
| 69 | * @return \XoopsObject light_pictures |
||
| 70 | */ |
||
| 71 | public function create($isNew = true) |
||
| 72 | { |
||
| 73 | $adslightPictures = new Adslight\Pictures(); |
||
| 74 | if ($isNew) { |
||
| 75 | $adslightPictures->setNew(); |
||
| 76 | } else { |
||
| 77 | $adslightPictures->unsetNew(); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $adslightPictures; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * retrieve a light_pictures |
||
| 85 | * |
||
| 86 | * @param int $id of the light_pictures |
||
| 87 | * @param $lid |
||
| 88 | * |
||
| 89 | * @return mixed reference to the {@link light_pictures} object, FALSE if failed |
||
| 90 | */ |
||
| 91 | public function get($id, $lid = null) |
||
| 92 | { |
||
| 93 | global $moduleDirName; |
||
| 94 | |||
| 95 | $sql = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures') . ' WHERE cod_img=' . $id . ' AND lid=' . $lid . ' '; |
||
| 96 | if (!$result = $this->db->query($sql)) { |
||
| 97 | return false; |
||
| 98 | } |
||
| 99 | $numrows = $this->db->getRowsNum($result); |
||
| 100 | if (1 == $numrows) { |
||
| 101 | $adslightPictures = new Adslight\Pictures(); |
||
| 102 | $adslightPictures->assignVars($this->db->fetchArray($result)); |
||
| 103 | |||
| 104 | return $adslightPictures; |
||
| 105 | } |
||
| 106 | |||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * insert a new AdslightPicture object into the database |
||
| 112 | * |
||
| 113 | * @param \XoopsObject $adslightPictures |
||
| 114 | * @param bool $force |
||
| 115 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 116 | */ |
||
| 117 | public function insert(\XoopsObject $adslightPictures, $force = false) |
||
| 118 | { |
||
| 119 | global $xoopsConfig, $lid, $moduleDirName; |
||
| 120 | if (!$adslightPictures instanceof Pictures) { |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | if (!$adslightPictures->isDirty()) { |
||
| 124 | return true; |
||
| 125 | } |
||
| 126 | if (!$adslightPictures->cleanVars()) { |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | foreach ($adslightPictures->cleanVars as $k => $v) { |
||
| 130 | ${$k} = $v; |
||
| 131 | } |
||
| 132 | $now = time(); |
||
| 133 | if ($adslightPictures->isNew()) { |
||
| 134 | // add/modify of Pictures |
||
| 135 | $adslightPictures = new Adslight\Pictures(); |
||
| 136 | |||
| 137 | $format = 'INSERT INTO `%s` (cod_img, title, date_added, date_modified, lid, uid_owner, url)'; |
||
| 138 | $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s)'; |
||
| 139 | $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)); |
||
| 140 | $force = true; |
||
| 141 | } else { |
||
| 142 | $format = 'UPDATE `%s` SET '; |
||
| 143 | $format .= 'cod_img=%u, title=%s, date_added=%s, date_modified=%s, lid=%s, uid_owner=%s, url=%s'; |
||
| 144 | $format .= ' WHERE cod_img = %u'; |
||
| 145 | $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); |
||
| 146 | } |
||
| 147 | if (false !== $force) { |
||
| 148 | $result = $this->db->queryF($sql); |
||
| 149 | } else { |
||
| 150 | $result = $this->db->query($sql); |
||
| 151 | } |
||
| 152 | if (!$result) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | if (empty($cod_img)) { |
||
| 156 | $cod_img = $this->db->getInsertId(); |
||
| 157 | } |
||
| 158 | $adslightPictures->assignVars([ |
||
| 159 | 'cod_img' => $cod_img, |
||
| 160 | 'lid' => $lid, |
||
| 161 | 'url' => $url, |
||
| 162 | ]); |
||
| 163 | |||
| 164 | return true; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * delete Pictures object from the database |
||
| 169 | * |
||
| 170 | * @param \XoopsObject $adslightPictures reference to the Pictures to delete |
||
| 171 | * @param bool $force |
||
| 172 | * @return bool FALSE if failed. |
||
| 173 | */ |
||
| 174 | public function delete(\XoopsObject $adslightPictures, $force = false) |
||
| 175 | { |
||
| 176 | global $moduleDirName; |
||
| 177 | |||
| 178 | if (!$adslightPictures instanceof Pictures) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | $sql = sprintf('DELETE FROM `%s` WHERE cod_img = %u', $this->db->prefix('adslight_pictures'), $adslightPictures->getVar('cod_img')); |
||
| 182 | if (false !== $force) { |
||
| 183 | $result = $this->db->queryF($sql); |
||
| 184 | } else { |
||
| 185 | $result = $this->db->query($sql); |
||
| 186 | } |
||
| 187 | if (!$result) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * retrieve Pictures object(s) from the database |
||
| 196 | * |
||
| 197 | * @param \CriteriaElement|\CriteriaCompo $criteria {@link \CriteriaElement} conditions to be met |
||
| 198 | * @param bool $id_as_key use the UID as key for the array? |
||
| 199 | * @return array array of {@link Pictures} objects |
||
| 200 | */ |
||
| 201 | public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false) |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * count Pictures matching a condition |
||
| 236 | * |
||
| 237 | * @param \CriteriaElement|\CriteriaCompo $criteria {@link \CriteriaElement} to match |
||
| 238 | * @return int count of Pictures |
||
| 239 | */ |
||
| 240 | public function getCount(\CriteriaElement $criteria = null) |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * delete Pictures matching a set of conditions |
||
| 259 | * |
||
| 260 | * @param \CriteriaElement $criteria {@link \CriteriaElement} |
||
| 261 | * @return bool FALSE if deletion failed |
||
| 262 | */ |
||
| 263 | public function deleteAll(\CriteriaElement $criteria = null) |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Render a form to send pictures |
||
| 279 | * |
||
| 280 | * @param int $uid |
||
| 281 | * @param int $lid |
||
| 282 | * @param int $maxbytes the maximum size of a picture |
||
| 283 | * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
| 284 | * @return bool TRUE |
||
| 285 | * |
||
| 286 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
| 287 | */ |
||
| 288 | public function renderFormSubmit($uid, $lid, $maxbytes, $xoopsTpl) |
||
| 289 | { |
||
| 290 | global $moduleDirName, $main_lang, $xoopsUser; |
||
| 291 | $uid = (int)$uid; |
||
| 292 | $lid = (int)$lid; |
||
| 293 | $form = new \XoopsThemeForm(_ADSLIGHT_SUBMIT_PIC_TITLE, 'form_picture', XOOPS_URL . "/modules/adslight/add_photo.php?lid={$lid}&uid=" . $xoopsUser->getVar('uid'), 'post', true); |
||
| 294 | $field_url = new \XoopsFormFile(_ADSLIGHT_SELECT_PHOTO, 'sel_photo', 2000000); |
||
| 295 | $field_desc = new \XoopsFormText(_ADSLIGHT_CAPTION, 'caption', 35, 55); |
||
| 296 | |||
| 297 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 298 | $button_send = new \XoopsFormButton('', 'submit_button', _ADSLIGHT_UPLOADPICTURE, 'submit'); |
||
| 299 | $field_warning = new \XoopsFormLabel(sprintf(_ADSLIGHT_YOUCANUPLOAD, $maxbytes / 1024)); |
||
| 300 | $field_lid = new \XoopsFormHidden('lid', $lid); |
||
| 301 | $field_uid = new \XoopsFormHidden('uid', $uid); |
||
| 302 | |||
| 303 | $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML(); |
||
| 304 | |||
| 305 | $form->addElement($field_warning); |
||
| 306 | $form->addElement($field_url, true); |
||
| 307 | $form->addElement($field_desc, true); |
||
| 308 | $form->addElement($field_lid, true); |
||
| 309 | $form->addElement($field_uid, true); |
||
| 310 | |||
| 311 | $form->addElement($field_token, true); |
||
| 312 | |||
| 313 | $form->addElement($button_send); |
||
| 314 | if (str_replace('.', '', PHP_VERSION) > 499) { |
||
| 315 | $form->assign($xoopsTpl); |
||
| 316 | } else { |
||
| 317 | $form->display(); |
||
| 318 | } |
||
| 319 | |||
| 320 | return true; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Render a form to edit the description of the pictures |
||
| 325 | * |
||
| 326 | * @param string $caption The description of the picture |
||
| 327 | * @param int $cod_img the id of the image in database |
||
| 328 | * @param string $filename the url to the thumb of the image so it can be displayed |
||
| 329 | * @return bool TRUE |
||
| 330 | */ |
||
| 331 | public function renderFormEdit($caption, $cod_img, $filename) |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Upload the file and Save into database |
||
| 360 | * |
||
| 361 | * @param string $title A litle description of the file |
||
| 362 | * @param string $path_upload The path to where the file should be uploaded |
||
| 363 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 364 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 365 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 366 | * @param int $pictheight the height in pixels that the pic will have |
||
| 367 | * @param int $maxfilebytes the maximum size a file can have to be uploaded in bytes |
||
| 368 | * @param int $maxfilewidth the maximum width in pixels that a pic can have |
||
| 369 | * @param int $maxfileheight the maximum height in pixels that a pic can have |
||
| 370 | * @return bool FALSE if upload fails or database fails |
||
| 371 | */ |
||
| 372 | public function receivePicture($title, $path_upload, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $maxfilebytes, $maxfilewidth, $maxfileheight) |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Resize a picture and save it to $path_upload |
||
| 423 | * |
||
| 424 | * @param string $img the path to the file |
||
| 425 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 426 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 427 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 428 | * @param int $pictheight the height in pixels that the pic will have |
||
| 429 | * @param string $path_upload The path to where the files should be saved after resizing |
||
| 430 | */ |
||
| 431 | public function resizeImage($img, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $path_upload) |
||
| 432 | { |
||
| 433 | $img2 = $img; |
||
| 434 | $path = pathinfo($img); |
||
| 435 | $img = imagecreatefromjpeg($img); |
||
| 436 | $xratio = $thumbwidth / imagesx($img); |
||
| 437 | $yratio = $thumbheight / imagesy($img); |
||
| 438 | if ($xratio < 1 || $yratio < 1) { |
||
| 439 | if ($xratio < $yratio) { |
||
| 440 | $resized = imagecreatetruecolor($thumbwidth, floor(imagesy($img) * $xratio)); |
||
| 441 | } else { |
||
| 442 | $resized = imagecreatetruecolor(floor(imagesx($img) * $yratio), $thumbheight); |
||
| 443 | } |
||
| 444 | imagecopyresampled($resized, $img, 0, 0, 0, 0, imagesx($resized) + 1, imagesy($resized) + 1, imagesx($img), imagesy($img)); |
||
| 445 | imagejpeg($resized, "{$path_upload}/thumbs/thumb_{$path['basename']}"); |
||
| 446 | imagedestroy($resized); |
||
| 447 | } else { |
||
| 448 | imagejpeg($img, "{$path_upload}/thumbs/thumb_{$path['basename']}"); |
||
| 449 | } |
||
| 450 | imagedestroy($img); |
||
| 451 | $path2 = pathinfo($img2); |
||
| 452 | $img2 = imagecreatefromjpeg($img2); |
||
| 453 | $xratio2 = $pictwidth / imagesx($img2); |
||
| 454 | $yratio2 = $pictheight / imagesy($img2); |
||
| 455 | if ($xratio2 < 1 || $yratio2 < 1) { |
||
| 456 | if ($xratio2 < $yratio2) { |
||
| 457 | $resized2 = imagecreatetruecolor($pictwidth, floor(imagesy($img2) * $xratio2)); |
||
| 458 | } else { |
||
| 459 | $resized2 = imagecreatetruecolor(floor(imagesx($img2) * $yratio2), $pictheight); |
||
| 460 | } |
||
| 461 | imagecopyresampled($resized2, $img2, 0, 0, 0, 0, imagesx($resized2) + 1, imagesy($resized2) + 1, imagesx($img2), imagesy($img2)); |
||
| 462 | imagejpeg($resized2, "{$path_upload}/midsize/resized_{$path2['basename']}"); |
||
| 463 | imagedestroy($resized2); |
||
| 464 | } else { |
||
| 465 | imagejpeg($img2, "{$path_upload}/midsize/resized_{$path2['basename']}"); |
||
| 470 |