| 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 |
||
| 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) |
||
| 264 | { |
||
| 265 | global $moduleDirName; |
||
| 266 | $sql = 'DELETE FROM ' . $this->db->prefix('adslight_pictures'); |
||
| 267 | if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
||
| 268 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 269 | } |
||
| 270 | if (!$result = $this->db->query($sql)) { |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | |||
| 274 | return true; |
||
| 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) |
||
| 332 | { |
||
| 333 | global $moduleDirName, $main_lang; |
||
| 334 | |||
| 335 | $form = new \XoopsThemeForm(_ADSLIGHT_EDIT_CAPTION, 'form_picture', 'editdesc.php', 'post', true); |
||
| 336 | $field_desc = new \XoopsFormText($caption, 'caption', 35, 55); |
||
| 337 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 338 | $button_send = new \XoopsFormButton(_ADSLIGHT_EDIT, 'submit_button', _SUBMIT, 'submit'); |
||
| 339 | //@todo - replace alt with language string |
||
| 340 | $field_warning = new \XoopsFormLabel("<img src='{$filename}' alt='sssss'>"); |
||
| 341 | $field_cod_img = new \XoopsFormHidden('cod_img', $cod_img); |
||
| 342 | // $field_lid = new \XoopsFormHidden('lid', $lid); |
||
| 343 | $field_marker = new \XoopsFormHidden('marker', 1); |
||
| 344 | |||
| 345 | $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML(); |
||
| 346 | |||
| 347 | $form->addElement($field_warning); |
||
| 348 | $form->addElement($field_desc); |
||
| 349 | $form->addElement($field_cod_img); |
||
| 350 | $form->addElement($field_marker); |
||
| 351 | $form->addElement($field_token); |
||
| 352 | $form->addElement($button_send); |
||
| 353 | $form->display(); |
||
| 354 | |||
| 355 | return true; |
||
| 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) |
||
| 373 | { |
||
| 374 | global $xoopsDB, $lid; |
||
| 375 | //busca id do user logado |
||
| 376 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 377 | $lid = Request::getInt('lid', 0, 'POST'); |
||
| 378 | //create a hash so it does not erase another file |
||
| 379 | $hash1 = time(); |
||
| 380 | $hash = mb_substr($hash1, 0, 4); |
||
| 381 | // mimetypes and settings put this in admin part later |
||
| 382 | $allowed_mimetypes = [ |
||
| 383 | 'image/jpeg', |
||
| 384 | 'image/gif', |
||
| 385 | ]; |
||
| 386 | $maxfilesize = $maxfilebytes; |
||
| 387 | // create the object to upload |
||
| 388 | $uploader = new \XoopsMediaUploader($path_upload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight); |
||
| 389 | // fetch the media |
||
| 390 | if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) { |
||
| 391 | //lets create a name for it |
||
| 392 | $uploader->setPrefix("pic_{$lid}_"); |
||
| 393 | //now let s upload the file |
||
| 394 | if (!$uploader->upload()) { |
||
| 395 | // if there are errors lets return them |
||
| 396 | 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>'; |
||
| 397 | |||
| 398 | return false; |
||
| 399 | } |
||
| 400 | // now let s create a new object picture and set its variables |
||
| 401 | $picture = $this->create(); |
||
| 402 | $url = $uploader->getSavedFileName(); |
||
| 403 | $picture->setVar('url', $url); |
||
| 404 | $picture->setVar('title', $title); |
||
| 405 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 406 | $lid = $lid; |
||
| 407 | $picture->setVar('lid', $lid); |
||
| 408 | $picture->setVar('uid_owner', $uid); |
||
| 409 | $this->insert($picture); |
||
| 410 | $saved_destination = $uploader->getSavedDestination(); |
||
| 411 | $this->resizeImage($saved_destination, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $path_upload); |
||
| 412 | } else { |
||
| 413 | 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>'; |
||
| 414 | |||
| 415 | return false; |
||
| 416 | } |
||
| 417 | |||
| 418 | return true; |
||
| 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) |
||
| 468 | } |
||
| 469 | } |
||
| 470 |