| Total Complexity | 119 |
| Total Lines | 1104 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PhotoHandler 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 PhotoHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class PhotoHandler extends Extgallery\PersistableObjectHandler |
||
| 60 | { |
||
| 61 | public $photoUploader = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param \XoopsDatabase|null $db |
||
| 65 | * @param $type |
||
| 66 | */ |
||
| 67 | public function __construct(XoopsDatabase $db, $type) |
||
| 68 | { |
||
| 69 | parent::__construct($db, 'extgallery_' . $type . 'photo', \ucfirst($type) . 'Photo', 'photo_id'); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $data |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function createPhoto($data) |
||
| 78 | { |
||
| 79 | $photo = $this->create(); |
||
| 80 | $photo->setVars($data); |
||
| 81 | |||
| 82 | return $this->insert($photo, true); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $photoId |
||
| 87 | * @param $data |
||
| 88 | * |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public function modifyPhoto($photoId, $data) |
||
| 92 | { |
||
| 93 | $photo = $this->get($photoId); |
||
| 94 | $photo->setVars($data); |
||
| 95 | |||
| 96 | return $this->insert($photo, true); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param $photo |
||
| 101 | */ |
||
| 102 | public function deletePhoto($photo) |
||
| 103 | { |
||
| 104 | if ('' == $photo->getVar('photo_serveur')) { |
||
| 105 | $this->deleteFile($photo); |
||
| 106 | } |
||
| 107 | $this->deleteById($photo->getVar('photo_id'), true); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $catId |
||
| 112 | */ |
||
| 113 | public function deletePhotoByCat($catId) |
||
| 114 | { |
||
| 115 | $criteria = new Criteria('cat_id', $catId); |
||
| 116 | $photos = $this->getObjects($criteria); |
||
| 117 | foreach ($photos as $photo) { |
||
| 118 | $this->deletePhoto($photo); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function deleteFile() |
||
| 123 | { |
||
| 124 | exit('deleteFile() method must be defined on sub classes'); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param $photoId |
||
| 129 | * |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function getPhoto($photoId) |
||
| 133 | { |
||
| 134 | $criteria = new CriteriaCompo(); |
||
| 135 | $criteria->add(new Criteria('photo_id', $photoId)); |
||
| 136 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 137 | |||
| 138 | $photo = $this->getObjects($criteria); |
||
| 139 | if (1 != \count($photo)) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | return $photo[0]; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $cat |
||
| 148 | * |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function nbPhoto($cat) |
||
| 152 | { |
||
| 153 | $criteria = new Criteria('cat_id', $cat->getVar('cat_id')); |
||
| 154 | |||
| 155 | return $this->getCount($criteria); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $catId |
||
| 160 | * @param $start |
||
| 161 | * @param $sortby |
||
| 162 | * @param $orderby |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function getAlbumPhotoPage($catId, $start, $sortby, $orderby) |
||
| 167 | { |
||
| 168 | $criteria = new CriteriaCompo(); |
||
| 169 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 170 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 171 | $criteria->setStart($start); |
||
| 172 | $criteria->setLimit($GLOBALS['xoopsModuleConfig']['nb_column'] * $GLOBALS['xoopsModuleConfig']['nb_line']); |
||
| 173 | if ('' == $criteria->getSort()) { |
||
| 174 | $criteria->setSort($sortby); |
||
| 175 | $criteria->setOrder($orderby); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this->getObjects($criteria); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $catId |
||
| 183 | * @param $start |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getAlbumPhotoAdminPage($catId, $start) |
||
| 188 | { |
||
| 189 | $criteria = new CriteriaCompo(); |
||
| 190 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 191 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 192 | $criteria->setStart($start); |
||
| 193 | $criteria->setLimit($GLOBALS['xoopsModuleConfig']['admin_nb_photo']); |
||
| 194 | $criteria->setSort('photo_weight, photo_id'); |
||
| 195 | $criteria->setOrder($GLOBALS['xoopsModuleConfig']['display_set_order']); |
||
| 196 | |||
| 197 | return $this->getObjects($criteria); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param $catId |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function getSlideshowAlbumPhoto($catId) |
||
| 206 | { |
||
| 207 | $criteria = new CriteriaCompo(); |
||
| 208 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 209 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 210 | $criteria->setSort('photo_weight, photo_id'); |
||
| 211 | $criteria->setOrder($GLOBALS['xoopsModuleConfig']['display_set_order']); |
||
| 212 | |||
| 213 | return $this->getObjects($criteria, false, false); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param $catId |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getPhotoAlbumId($catId) |
||
| 222 | { |
||
| 223 | $ret = []; |
||
| 224 | $criteria = new CriteriaCompo(); |
||
| 225 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 226 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 227 | |||
| 228 | $sql = 'SELECT photo_id FROM ' . $this->db->prefix('extgallery_publicphoto') . ' ' . $criteria->renderWhere() . ' ORDER BY photo_weight, photo_id ASC;'; |
||
| 229 | |||
| 230 | $result = $this->db->query($sql); |
||
| 231 | if ($result instanceof \mysqli_result) { |
||
| 232 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 233 | $ret[] = (int)$myrow['photo_id']; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | return $ret; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $catId |
||
| 241 | * @param $photoId |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getAlbumPrevPhoto($catId, $photoId) |
||
| 246 | { |
||
| 247 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 248 | |||
| 249 | $criteria = new CriteriaCompo(); |
||
| 250 | $criteria->add($catHandler->getCatRestrictCriteria()); |
||
| 251 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 252 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 253 | $criteria->add(new Criteria('photo_id', $photoId, '<')); |
||
| 254 | $criteria->setSort('photo_weight, photo_id'); |
||
| 255 | $criteria->setOrder('DESC'); |
||
| 256 | $criteria->setLimit(1); |
||
| 257 | |||
| 258 | return $this->getObjects($criteria); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param $catId |
||
| 263 | * @param $photoId |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function getAlbumNextPhoto($catId, $photoId) |
||
| 268 | { |
||
| 269 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 270 | |||
| 271 | $criteria = new CriteriaCompo(); |
||
| 272 | $criteria->add($catHandler->getCatRestrictCriteria()); |
||
| 273 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 274 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 275 | $criteria->add(new Criteria('photo_id', $photoId, '>')); |
||
| 276 | $criteria->setSort('photo_weight, photo_id'); |
||
| 277 | $criteria->setOrder('ASC'); |
||
| 278 | $criteria->setLimit(1); |
||
| 279 | |||
| 280 | return $this->getObjects($criteria); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param $catId |
||
| 285 | * @param $photoId |
||
| 286 | * |
||
| 287 | * @return int |
||
| 288 | */ |
||
| 289 | public function getAlbumCurrentPhotoPlace($catId, $photoId) |
||
| 290 | { |
||
| 291 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 292 | |||
| 293 | $criteria = new CriteriaCompo(); |
||
| 294 | $criteria->add($catHandler->getCatRestrictCriteria()); |
||
| 295 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 296 | $criteria->add(new Criteria('cat_id', $catId)); |
||
| 297 | $criteria->add(new Criteria('photo_id', $photoId, '<=')); |
||
| 298 | $criteria->setSort('photo_weight, photo_id'); |
||
| 299 | $criteria->setOrder('DESC'); |
||
| 300 | |||
| 301 | return $this->getCount($criteria); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param $catId |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function getAlbumPhoto($catId) |
||
| 310 | { |
||
| 311 | $criteria = new Criteria('cat_id', $catId); |
||
| 312 | $criteria->setSort('photo_weight, photo_id'); |
||
| 313 | $criteria->setOrder('ASC'); |
||
| 314 | |||
| 315 | return $this->getObjects($criteria); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param $category |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | public function getCatPhoto($category) |
||
| 324 | { |
||
| 325 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 326 | |||
| 327 | $criteria = new CriteriaCompo(); |
||
| 328 | $criteria->add(new Criteria('nleft', $category->getVar('nleft'), '>=')); |
||
| 329 | $criteria->add(new Criteria('nright', $category->getVar('nright'), '<=')); |
||
| 330 | |||
| 331 | $cats = $catHandler->getObjects($criteria); |
||
| 332 | |||
| 333 | $count = \count($cats); |
||
| 334 | if ($count > 0) { |
||
| 335 | $in = '(' . $cats[0]->getVar('cat_id'); |
||
| 336 | \array_shift($cats); |
||
| 337 | /** @var Extgallery\Category $cat */ |
||
| 338 | foreach ($cats as $cat) { |
||
| 339 | $in .= ',' . $cat->getVar('cat_id'); |
||
| 340 | } |
||
| 341 | $in .= ')'; |
||
| 342 | $criteria = new Criteria('cat_id', $in, 'IN'); |
||
| 343 | } else { |
||
| 344 | $criteria = new Criteria('cat_id', '(0)', 'IN'); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $this->getObjects($criteria); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param $catId |
||
| 352 | * |
||
| 353 | * @return int |
||
| 354 | */ |
||
| 355 | public function getAlbumCount($catId) |
||
| 356 | { |
||
| 357 | $criteria = new Criteria('cat_id', $catId); |
||
| 358 | |||
| 359 | return $this->getCount($criteria); |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param $photoId |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function updateHits($photoId) |
||
| 368 | { |
||
| 369 | $criteria = new Criteria('photo_id', $photoId); |
||
| 370 | |||
| 371 | return $this->updateCounter('photo_hits', $criteria); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param $photoId |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function updateNbRating($photoId) |
||
| 380 | { |
||
| 381 | $criteria = new Criteria('photo_id', $photoId); |
||
| 382 | |||
| 383 | return $this->updateCounter('photo_nbrating', $criteria); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param $photoId |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | public function updateDownload($photoId) |
||
| 392 | { |
||
| 393 | $criteria = new Criteria('photo_id', $photoId); |
||
| 394 | |||
| 395 | return $this->updateCounter('photo_download', $criteria); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param $photoId |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | public function updateEcard($photoId) |
||
| 404 | { |
||
| 405 | $criteria = new Criteria('photo_id', $photoId); |
||
| 406 | |||
| 407 | return $this->updateCounter('photo_ecard', $criteria); |
||
| 408 | } |
||
| 409 | |||
| 410 | public function getAllSize() |
||
| 411 | { |
||
| 412 | exit('getAllSize() method must be defined on sub classes'); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $imageTransform |
||
| 417 | */ |
||
| 418 | public function _makeWatermark($imageTransform) |
||
| 419 | { |
||
| 420 | if (!\function_exists('imagettfbbox')) { |
||
| 421 | return; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** @var Extgallery\Helper $helper */ |
||
| 425 | $helper = Extgallery\Helper::getInstance(); |
||
| 426 | |||
| 427 | /* Text position param |
||
| 428 | / |
||
| 429 | / 0 : orig |
||
| 430 | / -1 : opposit |
||
| 431 | / 1 : center |
||
| 432 | / |
||
| 433 | */ |
||
| 434 | if ('tl' === $helper->getConfig('watermark_position')) { |
||
| 435 | $x = 0; |
||
| 436 | $y = 0; |
||
| 437 | } elseif ('tr' === $helper->getConfig('watermark_position')) { |
||
| 438 | $x = -1; |
||
| 439 | $y = 0; |
||
| 440 | } elseif ('bl' === $helper->getConfig('watermark_position')) { |
||
| 441 | $x = 0; |
||
| 442 | $y = -1; |
||
| 443 | } elseif ('br' === $helper->getConfig('watermark_position')) { |
||
| 444 | $x = -1; |
||
| 445 | $y = -1; |
||
| 446 | } elseif ('tc' === $helper->getConfig('watermark_position')) { |
||
| 447 | $x = 1; |
||
| 448 | $y = 0; |
||
| 449 | } elseif ('bc' === $helper->getConfig('watermark_position')) { |
||
| 450 | $x = 1; |
||
| 451 | $y = -1; |
||
| 452 | } elseif ('lc' === $helper->getConfig('watermark_position')) { |
||
| 453 | $x = 0; |
||
| 454 | $y = 1; |
||
| 455 | } elseif ('rc' === $helper->getConfig('watermark_position')) { |
||
| 456 | $x = -1; |
||
| 457 | $y = 1; |
||
| 458 | } elseif ('cc' === $helper->getConfig('watermark_position')) { |
||
| 459 | $x = 1; |
||
| 460 | $y = 1; |
||
| 461 | } |
||
| 462 | |||
| 463 | $text = (0 == $helper->getConfig('watermark_type')) ? $GLOBALS['xoopsUser']->getVar('uname') : $helper->getConfig('watermark_text'); |
||
| 464 | |||
| 465 | $watermarkParams = [ |
||
| 466 | 'text' => $text, |
||
| 467 | 'x' => $x, |
||
| 468 | 'y' => $y, |
||
| 469 | 'color' => $helper->getConfig('watermark_color'), |
||
| 470 | 'font' => XOOPS_ROOT_PATH . '/modules/extgallery/fonts/' . $helper->getConfig('watermark_font'), |
||
| 471 | 'size' => $helper->getConfig('watermark_fontsize'), |
||
| 472 | 'resize_first' => false, |
||
| 473 | 'padding' => $helper->getConfig('watermark_padding'), |
||
| 474 | ]; |
||
| 475 | $imageTransform->addText($watermarkParams); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param $imageTransform |
||
| 480 | */ |
||
| 481 | public function _makeBorder($imageTransform) |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | public function getUploadPhotoPath() |
||
| 502 | { |
||
| 503 | exit('getUploadPhotoPath() method must be defined on sub classes'); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param $photoName |
||
| 508 | */ |
||
| 509 | public function _largePhotoTreatment($photoName) |
||
| 510 | { |
||
| 511 | /** @var Extgallery\Helper $helper */ |
||
| 512 | $helper = Extgallery\Helper::getInstance(); |
||
| 513 | |||
| 514 | // Check if must save large photo |
||
| 515 | if ($helper->getConfig('save_large')) { |
||
| 516 | // Define Graphical library path |
||
| 517 | if (!\defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $helper->getConfig('graphic_lib')) { |
||
| 518 | \define('IMAGE_TRANSFORM_IM_PATH', $helper->getConfig('graphic_lib_path')); |
||
| 519 | } |
||
| 520 | $imageFactory = new Image_Transform(); |
||
| 521 | $imageTransform = $imageFactory->factory($helper->getConfig('graphic_lib')); |
||
| 522 | |||
| 523 | $filePath = $this->getUploadPhotoPath(); |
||
| 524 | $imageTransform->load($filePath . $photoName); |
||
| 525 | |||
| 526 | // Save large photo only if it's bigger than medium size |
||
| 527 | if ($imageTransform->getImageWidth() > $helper->getConfig('medium_width') |
||
| 528 | || $imageTransform->getImageHeight() > $helper->getConfig('medium_heigth')) { |
||
| 529 | // Make watermark |
||
| 530 | if ($helper->getConfig('enable_large_watermark')) { |
||
| 531 | $this->_makeWatermark($imageTransform); |
||
| 532 | } |
||
| 533 | |||
| 534 | // Make border |
||
| 535 | if ($helper->getConfig('enable_large_border')) { |
||
| 536 | $this->_makeBorder($imageTransform); |
||
| 537 | } |
||
| 538 | |||
| 539 | $largeFilePath = $filePath . 'large/large_' . $photoName; |
||
| 540 | $imageTransform->save($largeFilePath, '', 100); |
||
| 541 | $imageTransform->free(); |
||
| 542 | } |
||
| 543 | } |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param $photoName |
||
| 548 | * @param null|string $filePath |
||
| 549 | * @param null $mediumFilePath |
||
| 550 | */ |
||
| 551 | public function _mediumPhotoTreatment($photoName, $filePath = null, $mediumFilePath = null) |
||
| 552 | { |
||
| 553 | /** @var Extgallery\Helper $helper */ |
||
| 554 | $helper = Extgallery\Helper::getInstance(); |
||
| 555 | |||
| 556 | // Define Graphical library path |
||
| 557 | if (!\defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $helper->getConfig('graphic_lib')) { |
||
| 558 | \define('IMAGE_TRANSFORM_IM_PATH', $helper->getConfig('graphic_lib_path')); |
||
| 559 | } |
||
| 560 | $imageFactory = new Image_Transform(); |
||
| 561 | $imageTransform = $imageFactory->factory($helper->getConfig('graphic_lib')); |
||
| 562 | |||
| 563 | if (null === $filePath) { |
||
| 564 | $filePath = $this->getUploadPhotoPath(); |
||
| 565 | } |
||
| 566 | if (null === $mediumFilePath) { |
||
| 567 | $mediumFilePath = $filePath . 'medium/' . $photoName; |
||
| 568 | } |
||
| 569 | $imageTransform->load($filePath . $photoName); |
||
| 570 | |||
| 571 | // Fitting image to desired size |
||
| 572 | if ($helper->getConfig('enable_medium_border')) { |
||
| 573 | $borderSize = ($helper->getConfig('inner_border_size') * 2) + ($helper->getConfig('outer_border_size') * 2); |
||
| 574 | } else { |
||
| 575 | $borderSize = 0; |
||
| 576 | } |
||
| 577 | $imageTransform->fit($helper->getConfig('medium_width') - $borderSize, $helper->getConfig('medium_heigth') - $borderSize); |
||
| 578 | $imageTransform->save($mediumFilePath, '', $helper->getConfig('medium_quality')); |
||
| 579 | $imageTransform->free(); |
||
| 580 | |||
| 581 | if ($helper->getConfig('enable_medium_watermark') || $helper->getConfig('enable_medium_border')) { |
||
| 582 | $imageTransform->load($mediumFilePath); |
||
| 583 | |||
| 584 | // Make watermark |
||
| 585 | if ($helper->getConfig('enable_medium_watermark')) { |
||
| 586 | $this->_makeWatermark($imageTransform); |
||
| 587 | } |
||
| 588 | |||
| 589 | // Make border |
||
| 590 | if ($helper->getConfig('enable_medium_border')) { |
||
| 591 | $this->_makeBorder($imageTransform); |
||
| 592 | } |
||
| 593 | |||
| 594 | $imageTransform->save($mediumFilePath, '', $helper->getConfig('medium_quality')); |
||
| 595 | $imageTransform->free(); |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param $photoName |
||
| 601 | */ |
||
| 602 | public function _makeThumb($photoName) |
||
| 603 | { |
||
| 604 | /** @var Extgallery\Helper $helper */ |
||
| 605 | $helper = Extgallery\Helper::getInstance(); |
||
| 606 | |||
| 607 | // Define Graphical library path |
||
| 608 | if (!\defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $helper->getConfig('graphic_lib')) { |
||
| 609 | \define('IMAGE_TRANSFORM_IM_PATH', $helper->getConfig('graphic_lib_path')); |
||
| 610 | } |
||
| 611 | $imageFactory = new Image_Transform(); |
||
| 612 | $imageTransform = $imageFactory->factory($helper->getConfig('graphic_lib')); |
||
| 613 | |||
| 614 | $filePath = $this->getUploadPhotoPath() . 'medium/' . $photoName; |
||
| 615 | $thumbPath = $this->getUploadPhotoPath() . 'thumb/thumb_' . $photoName; |
||
| 616 | |||
| 617 | $imageTransform->load($filePath); |
||
| 618 | $imageTransform->fit($helper->getConfig('thumb_width'), $helper->getConfig('thumb_heigth')); |
||
| 619 | $imageTransform->save($thumbPath, '', $helper->getConfig('thumb_quality')); |
||
| 620 | $imageTransform->free(); |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @param $photoName |
||
| 625 | * |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | public function _haveLargePhoto($photoName) |
||
| 629 | { |
||
| 630 | return \file_exists($this->getUploadPhotoPath() . 'large/large_' . $photoName); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param $photoName |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | public function _getImageDimension($photoName) |
||
| 639 | { |
||
| 640 | /** @var Extgallery\Helper $helper */ |
||
| 641 | $helper = Extgallery\Helper::getInstance(); |
||
| 642 | |||
| 643 | // Define Graphical library path |
||
| 644 | if (!\defined('IMAGE_TRANSFORM_IM_PATH') && 'imagick' === $helper->getConfig('graphic_lib')) { |
||
| 645 | \define('IMAGE_TRANSFORM_IM_PATH', $helper->getConfig('graphic_lib_path')); |
||
| 646 | } |
||
| 647 | $imageFactory = new Image_Transform(); |
||
| 648 | $imageTransform = $imageFactory->factory($helper->getConfig('graphic_lib')); |
||
| 649 | |||
| 650 | $ret = []; |
||
| 651 | if ($this->_haveLargePhoto($photoName)) { |
||
| 652 | $imageTransform->load($this->getUploadPhotoPath() . 'large/large_' . $photoName); |
||
| 653 | $ret['width'] = $imageTransform->getImageWidth(); |
||
| 654 | $ret['height'] = $imageTransform->getImageHeight(); |
||
| 655 | } else { |
||
| 656 | $imageTransform->load($this->getUploadPhotoPath() . 'medium/' . $photoName); |
||
| 657 | $ret['width'] = $imageTransform->getImageWidth(); |
||
| 658 | $ret['height'] = $imageTransform->getImageHeight(); |
||
| 659 | } |
||
| 660 | $imageTransform->free(); |
||
| 661 | |||
| 662 | return $ret; |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @param $photoName |
||
| 667 | * |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | public function getAutoDescription($photoName) |
||
| 671 | { |
||
| 672 | /** @var Extgallery\Helper $helper */ |
||
| 673 | $helper = Extgallery\Helper::getInstance(); |
||
| 674 | |||
| 675 | //DNPROSSI |
||
| 676 | /*if ($helper->getConfig('enable_longdesc')) { |
||
| 677 | $newphotoname = ''; |
||
| 678 | $newnewphotoname = ''; |
||
| 679 | $patterns = []; |
||
| 680 | $patterns[0] = "/-/"; |
||
| 681 | $patterns[1] = "/_/"; |
||
| 682 | $replacements = []; |
||
| 683 | $replacements[0] = " "; |
||
| 684 | $replacements[1] = "'"; |
||
| 685 | $newphotoName = substr($photoName, strpos($photoName, "-") + 1); |
||
| 686 | $newphotoName = substr($newphotoName, strpos($newphotoName, "-") + 1); |
||
| 687 | |||
| 688 | return preg_replace($patterns, $replacements, substr($newphotoName,0,-12)); |
||
| 689 | } else { */ |
||
| 690 | $matches = []; |
||
| 691 | \preg_match_all($helper->getConfig('photoname_pattern'), mb_substr($photoName, 0, -12), $matches); |
||
| 692 | \preg_match_all($helper->getConfig('photoname_pattern'), $photoName, $matches); |
||
| 693 | |||
| 694 | return \implode(' ', $matches[1]); |
||
| 695 | //} |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @param $fileName |
||
| 700 | * |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | public function makeFileName($fileName) |
||
| 704 | { |
||
| 705 | //DNPROSSI |
||
| 706 | //$fileName = preg_replace("/[^a-zA-Z0-9()_\.-]/", "-", $fileName); |
||
| 707 | $fileName = \preg_replace("/[^a-zA-Z0-9_\.-]/", '-', $fileName); |
||
| 708 | |||
| 709 | $fileName = \explode('.', $fileName); |
||
| 710 | $userId = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
| 711 | |||
| 712 | return $fileName[0] . '_' . $userId . '_' . mb_substr(md5(\uniqid(\mt_rand(), true)), 27) . '.' . $fileName[1]; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @param $photoName |
||
| 717 | * |
||
| 718 | * @return float |
||
| 719 | */ |
||
| 720 | public function getPhotoSize($photoName) |
||
| 721 | { |
||
| 722 | if ($this->_haveLargePhoto($photoName)) { |
||
| 723 | return $this->getFileSize('large/large_' . $photoName); |
||
| 724 | } |
||
| 725 | |||
| 726 | return $this->getFileSize($photoName); |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * @param $fileName |
||
| 731 | * |
||
| 732 | * @return float |
||
| 733 | */ |
||
| 734 | public function getFileSize($fileName) |
||
| 735 | { |
||
| 736 | return \round(\filesize($this->getUploadPhotoPath() . $fileName) / 1024, 2); |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @param $catId |
||
| 741 | */ |
||
| 742 | public function rebuildThumbnail($catId) |
||
| 743 | { |
||
| 744 | $photos = $this->getAlbumPhoto($catId); |
||
| 745 | foreach ($photos as $photo) { |
||
| 746 | $this->_makeThumb($photo->getVar('photo_name')); |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | /* Return Code : |
||
| 751 | 0 : Photo added |
||
| 752 | 1 : Photo pending |
||
| 753 | 2 : This is not an album |
||
| 754 | 3 : HTTP Upload error |
||
| 755 | 4 : File rejected |
||
| 756 | 5 : File chunk receive |
||
| 757 | */ |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @param $file |
||
| 761 | * @param bool $checkMd5 |
||
| 762 | * |
||
| 763 | * @return int |
||
| 764 | */ |
||
| 765 | public function postPhotoTraitement($file, $checkMd5 = false) |
||
| 766 | { |
||
| 767 | // require_once XOOPS_ROOT_PATH.'/modules/extgallery/class/photoUploader.php'; |
||
| 768 | |||
| 769 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 770 | |||
| 771 | $catId = Request::getInt('cat_id', 0, 'POST'); |
||
| 772 | |||
| 773 | // If isn't an album when stop the traitment |
||
| 774 | $cat = $catHandler->getCat($catId); |
||
| 775 | if (null !== $cat && (1 != $cat->getVar('nright') - $cat->getVar('nleft'))) { |
||
| 776 | return 2; |
||
| 777 | } |
||
| 778 | |||
| 779 | $allowedMimeTypes = ['image/jpeg', 'image/jpg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png']; |
||
| 780 | // $allowedMimeTypes = array('jpg/jpeg', 'image/bmp', 'image/gif', 'image/jpeg', 'image/jpg', 'image/x-png', 'image/png'); |
||
| 781 | |||
| 782 | $uploadDir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/'; |
||
| 783 | |||
| 784 | // $this->photoUploader = new Extgallery\PhotoUploader($uploadDir, 50000000, 5000, 5000); |
||
| 785 | // $this->photoUploader->checkMd5 = $checkMd5; |
||
| 786 | // $this->photoUploader->fetchPhoto($_FILES[$file]); |
||
| 787 | |||
| 788 | //------------------------ |
||
| 789 | require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
||
| 790 | $this->photoUploader = new XoopsMediaUploader($uploadDir, $allowedMimeTypes, 50000000, 5000, 5000); |
||
| 791 | |||
| 792 | $jupart = Request::getInt('jupart', 0, 'POST'); |
||
| 793 | $jufinal = Request::getInt('jufinal', 1, 'POST'); |
||
| 794 | |||
| 795 | if ($this->photoUploader->fetchMedia($file) && $this->photoUploader->upload()) { |
||
| 796 | } else { |
||
| 797 | // We got a chunk, so we don't add photo to database |
||
| 798 | if ($jupart && !$jufinal) { |
||
| 799 | return 5; |
||
| 800 | } |
||
| 801 | |||
| 802 | return 4; |
||
| 803 | } |
||
| 804 | |||
| 805 | //--------------------------- |
||
| 806 | |||
| 807 | /* |
||
| 808 | |||
| 809 | $jupart = (isset($_POST['jupart'])) ? (int) $_POST['jupart'] : 0; |
||
| 810 | $jufinal = (isset($_POST['jufinal'])) ? (int) $_POST['jufinal'] : 1; |
||
| 811 | |||
| 812 | if ($this->photoUploader->isError()) { |
||
| 813 | return 4; |
||
| 814 | // We got a chunk, so we don't add photo to database |
||
| 815 | } elseif ($jupart && !$jufinal) { |
||
| 816 | return 5; |
||
| 817 | } |
||
| 818 | */ |
||
| 819 | |||
| 820 | //DNPROSSI - add missing title and description on upload |
||
| 821 | $photoTitle = ''; |
||
| 822 | $photoDesc = ''; |
||
| 823 | $photoExtra = ''; |
||
| 824 | $photoTag = ''; |
||
| 825 | |||
| 826 | if (Request::hasVar('photo_title', 'POST')) { |
||
| 827 | $photoTitle = $_POST['photo_title']; |
||
| 828 | } |
||
| 829 | if (Request::hasVar('photo_desc', 'POST')) { |
||
| 830 | $photoDesc = $_POST['photo_desc']; |
||
| 831 | } |
||
| 832 | if (Request::hasVar('photo_extra', 'POST')) { |
||
| 833 | $photoExtra = $_POST['photo_extra']; |
||
| 834 | } |
||
| 835 | if (Request::hasVar('tag', 'POST')) { |
||
| 836 | $photoTag = $_POST['tag']; |
||
| 837 | } |
||
| 838 | |||
| 839 | $photoStatus = $this->addLocalPhoto($catId, $this->photoUploader->getSavedFileName(), $photoTitle, $photoDesc, $photoExtra, $photoTag); |
||
| 840 | /** @var Extgallery\Category $cat */ |
||
| 841 | $cat = $catHandler->getCat($catId); |
||
| 842 | $cat->setVar('cat_isalbum', 1); |
||
| 843 | $catHandler->insert($cat); |
||
| 844 | |||
| 845 | /** @var \XoopsNotificationHandler $notificationHandler */ |
||
| 846 | $notificationHandler = \xoops_getHandler('notification'); |
||
| 847 | $extraTags = [ |
||
| 848 | 'X_ITEM_CAT' => $cat->getVar('cat_name'), |
||
| 849 | 'X_ITEM_NBPHOTO' => 1, |
||
| 850 | ]; |
||
| 851 | |||
| 852 | if (1 == $photoStatus) { |
||
| 853 | $extraTags['X_ITEM_URL'] = XOOPS_URL . '/modules/extgallery/public-album.php?id=' . $cat->getVar('cat_id'); |
||
| 854 | $notificationHandler->triggerEvent('global', 0, 'new_photo', $extraTags); |
||
| 855 | $notificationHandler->triggerEvent('album', $cat->getVar('cat_id'), 'new_photo_album', $extraTags); |
||
| 856 | |||
| 857 | // Update album count |
||
| 858 | if (0 == $cat->getVar('cat_nb_photo')) { |
||
| 859 | $criteria = new CriteriaCompo(); |
||
| 860 | $criteria->add(new Criteria('nleft', $cat->getVar('nleft'), '<')); |
||
| 861 | $criteria->add(new Criteria('nright', $cat->getVar('nright'), '>')); |
||
| 862 | $catHandler->updateFieldValue('cat_nb_album', 'cat_nb_album + 1', $criteria); |
||
| 863 | } |
||
| 864 | |||
| 865 | // Update photo count |
||
| 866 | $criteria = new CriteriaCompo(); |
||
| 867 | $criteria->add(new Criteria('nleft', $cat->getVar('nleft'), '<=')); |
||
| 868 | $criteria->add(new Criteria('nright', $cat->getVar('nright'), '>=')); |
||
| 869 | $catHandler->updateFieldValue('cat_nb_photo', 'cat_nb_photo + 1', $criteria); |
||
| 870 | |||
| 871 | return 0; |
||
| 872 | } |
||
| 873 | $extraTags['X_ITEM_URL'] = XOOPS_URL . '/modules/extgallery/admin/photo.php'; |
||
| 874 | $notificationHandler->triggerEvent('global', 0, 'new_photo_pending', $extraTags); |
||
| 875 | |||
| 876 | return 1; |
||
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * @param $catId |
||
| 881 | * @param $dirtyPhotoName |
||
| 882 | * @param string $photoTitle |
||
| 883 | * @param string $photoDesc |
||
| 884 | * @param string $photoExtra |
||
| 885 | * @param string $photoTag |
||
| 886 | * |
||
| 887 | * @return mixed |
||
| 888 | */ |
||
| 889 | public function addLocalPhoto( |
||
| 960 | } |
||
| 961 | |||
| 962 | /** |
||
| 963 | * @param $queryArray |
||
| 964 | * @param $condition |
||
| 965 | * @param $limit |
||
| 966 | * @param $start |
||
| 967 | * @param $userId |
||
| 968 | * |
||
| 969 | * @return array |
||
| 970 | */ |
||
| 971 | public function getSearchedPhoto($queryArray, $condition, $limit, $start, $userId) |
||
| 972 | { |
||
| 973 | $criteria = new CriteriaCompo(); |
||
| 974 | if ($userId > 0) { |
||
| 975 | $criteria->add(new Criteria('uid', $userId)); |
||
| 976 | } |
||
| 977 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 978 | if ($queryArray && \is_array($queryArray)) { |
||
| 979 | $subCriteria = new CriteriaCompo(); |
||
| 980 | foreach ($queryArray as $keyWord) { |
||
| 981 | $keyWordCriteria = new CriteriaCompo(); |
||
| 982 | $keyWordCriteria->add(new Criteria('photo_title', '%' . $keyWord . '%', 'LIKE')); |
||
| 983 | $keyWordCriteria->add(new Criteria('photo_desc', '%' . $keyWord . '%', 'LIKE'), 'OR'); |
||
| 984 | $keyWordCriteria->add(new Criteria('photo_name', '%' . $keyWord . '%', 'LIKE'), 'OR'); |
||
| 985 | $subCriteria->add($keyWordCriteria, $condition); |
||
| 986 | unset($keyWordCriteria); |
||
| 987 | } |
||
| 988 | $criteria->add($subCriteria); |
||
| 989 | } |
||
| 990 | $criteria->setStart($start); |
||
| 991 | $criteria->setLimit($limit); |
||
| 992 | $criteria->setSort('photo_date'); |
||
| 993 | |||
| 994 | $photos = $this->getObjects($criteria); |
||
| 995 | |||
| 996 | $ret = []; |
||
| 997 | foreach ($photos as $photo) { |
||
| 998 | if ($photo->getVar('photo_title')) { |
||
| 999 | $title = $photo->getVar('photo_title'); |
||
| 1000 | } else { |
||
| 1001 | $title = $photo->getVar('photo_desc'); |
||
| 1002 | } |
||
| 1003 | $data = [ |
||
| 1004 | 'image' => 'assets/images/extgallery-posticon.gif', |
||
| 1005 | 'link' => 'public-photo.php?photoId=' . $photo->getVar('photo_id'), |
||
| 1006 | 'title' => $title, |
||
| 1007 | 'time' => $photo->getVar('photo_date'), |
||
| 1008 | 'uid' => $photo->getVar('uid'), |
||
| 1009 | ]; |
||
| 1010 | $ret[] = $data; |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | return $ret; |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * @return array |
||
| 1018 | */ |
||
| 1019 | public function getPendingPhoto() |
||
| 1020 | { |
||
| 1021 | $criteria = new Criteria('photo_approved', 0); |
||
| 1022 | |||
| 1023 | return $this->getObjects($criteria); |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * @param $criteria |
||
| 1028 | * @param $data |
||
| 1029 | */ |
||
| 1030 | public function addInCriteria($criteria, $data) |
||
| 1031 | { |
||
| 1032 | $count = \count($data); |
||
| 1033 | if ($count > 0) { |
||
| 1034 | $in = '(' . $data[0]; |
||
| 1035 | \array_shift($data); |
||
| 1036 | foreach ($data as $elmt) { |
||
| 1037 | $in .= ',' . $elmt; |
||
| 1038 | } |
||
| 1039 | $in .= ')'; |
||
| 1040 | $criteria->add(new Criteria('cat_id', $in, 'IN')); |
||
| 1041 | } |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * @param $param |
||
| 1046 | * |
||
| 1047 | * @return array |
||
| 1048 | */ |
||
| 1049 | public function getRandomPhoto($param) |
||
| 1050 | { |
||
| 1051 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 1052 | $criteria = new CriteriaCompo(); |
||
| 1053 | if (false !== $catHandler->getCatRestrictCriteria()) { |
||
| 1054 | $criteria->add($catHandler->getCatRestrictCriteria()); |
||
| 1055 | } |
||
| 1056 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 1057 | $this->addInCriteria($criteria, $param['cat']); |
||
| 1058 | $criteria->setSort('RAND()'); |
||
| 1059 | $criteria->setLimit($param['limit']); |
||
| 1060 | |||
| 1061 | return $this->getObjects($criteria); |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * @param $param |
||
| 1066 | * |
||
| 1067 | * @return array |
||
| 1068 | */ |
||
| 1069 | public function getLastPhoto($param) |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * @param $param |
||
| 1086 | * |
||
| 1087 | * @return array |
||
| 1088 | */ |
||
| 1089 | public function getTopViewPhoto($param) |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @param $param |
||
| 1106 | * |
||
| 1107 | * @return array |
||
| 1108 | */ |
||
| 1109 | public function getTopRatedPhoto($param) |
||
| 1110 | { |
||
| 1111 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 1112 | |||
| 1113 | $criteria = new CriteriaCompo(); |
||
| 1114 | $criteria->add($catHandler->getCatRestrictCriteria()); |
||
| 1115 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 1116 | $this->addInCriteria($criteria, $param['cat']); |
||
| 1117 | $criteria->setSort('photo_rating'); |
||
| 1118 | $criteria->setOrder('DESC'); |
||
| 1119 | $criteria->setLimit($param['limit']); |
||
| 1120 | |||
| 1121 | return $this->getObjects($criteria); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * @param $param |
||
| 1126 | * |
||
| 1127 | * @return array |
||
| 1128 | */ |
||
| 1129 | public function getTopEcardPhoto($param) |
||
| 1130 | { |
||
| 1131 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 1132 | |||
| 1133 | $criteria = new CriteriaCompo(); |
||
| 1134 | $criteria->add($catHandler->getCatRestrictCriteria()); |
||
| 1135 | $criteria->add(new Criteria('photo_approved', 1)); |
||
| 1136 | $this->addInCriteria($criteria, $param['cat']); |
||
| 1137 | $criteria->setSort('photo_ecard'); |
||
| 1138 | $criteria->setOrder('DESC'); |
||
| 1139 | $criteria->setLimit($param['limit']); |
||
| 1140 | |||
| 1141 | return $this->getObjects($criteria); |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * @param $param |
||
| 1146 | */ |
||
| 1147 | public function getTopSubmitter($param) |
||
| 1148 | { |
||
| 1149 | $catHandler = Extgallery\Helper::getInstance()->getHandler('PublicCategory'); |
||
| 1150 | |||
| 1151 | $criteria = new Criteria(''); |
||
| 1152 | $this->addInCriteria($criteria, $param['cat']); |
||
| 1153 | |||
| 1154 | echo $criteria->renderWhere(); |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * @return mixed |
||
| 1159 | */ |
||
| 1160 | public function getInsertId() |
||
| 1163 | } |
||
| 1164 | } |
||
| 1165 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: