BxBaseModProfileUploaderCrop.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModProfileUploaderCrop extends BxTemplUploaderCrop
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sImage;
  16. protected $_aOtherImages;
  17. public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  18. {
  19. if(!empty($this->_sModule) && empty($this->_oModule))
  20. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  21. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate ? $oTemplate : $this->_oModule->_oTemplate);
  22. }
  23. public function deleteGhostsForProfile($iProfileId, $mixedContent = false)
  24. {
  25. if(!$mixedContent)
  26. return parent::deleteGhostsForProfile($iProfileId, $mixedContent);
  27. $iCount = 0;
  28. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  29. if($oStorage === false)
  30. return $iCount;
  31. $iContentId = (int)(is_array($mixedContent) ? reset($mixedContent) : $mixedContent);
  32. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  33. $aGhosts = $oStorage->getGhosts($iProfileId, $mixedContent, $iContentId ? true : false);
  34. foreach ($aGhosts as $aFile) {
  35. // for requested image type delete only unassigned ghosts and currently set image will be deleted while saving
  36. if($aFile['id'] != $aContentInfo[$this->_sImage] && !in_array($aFile['id'], array_map(function($sField) use ($aContentInfo) {
  37. return $aContentInfo[$sField];
  38. }, $this->_aOtherImages)))
  39. $iCount += $oStorage->deleteFile($aFile['id']);
  40. }
  41. return $iCount;
  42. }
  43. public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
  44. {
  45. $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
  46. if(!$s || !$iContentId) // if we're creating new profile return all ghosts
  47. return $s;
  48. $a = [];
  49. if($sFormat == 'array')
  50. $a = $s;
  51. else if($sFormat == 'json')
  52. $a = json_decode($s, true);
  53. if(!$a)
  54. return $s;
  55. // filter out thumbnails
  56. $iThreshold = 0;
  57. if(($iLifetime = BxDolStorage::getGhostLifetime()) != 0)
  58. $iThreshold = time() - $iLifetime;
  59. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  60. $aResult = [];
  61. foreach ($a as $aFile) {
  62. // for requested image type show only (1) currently set images and (2) unassigned ghosts by uploader
  63. if(($bCurrent = $aFile['file_id'] == $aContentInfo[$this->_sImage]) || (!in_array($aFile['file_id'], array_map(function($sField) use ($aContentInfo) {
  64. return $aContentInfo[$sField];
  65. }, $this->_aOtherImages)) && $aFile['uploader_id'] == $this->_iId)) {
  66. // delete outdated ghost if ghost lifetime is set
  67. if(!$bCurrent && $iThreshold && $aFile['file_created'] < $iThreshold) {
  68. if(($oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject)) !== false)
  69. $oStorage->deleteFile($aFile['file_id']);
  70. continue;
  71. }
  72. // put only the newest file in results in case of non-multiple mode and all files otherwise
  73. if(!$this->isMultiple()) {
  74. if(!$aResult || $aFile['file_created'] > reset($aResult)['file_created'])
  75. $aResult = [
  76. $aFile['file_id'] => $aFile
  77. ];
  78. }
  79. else
  80. $aResult[$aFile['file_id']] = $aFile;
  81. }
  82. }
  83. switch($sFormat) {
  84. case 'array':
  85. break;
  86. case 'json':
  87. $aResult = json_encode($aResult);
  88. break;
  89. default:
  90. $aResult = false; // html format is not suported for this data type
  91. }
  92. return $aResult;
  93. }
  94. protected function isAdmin ($iContentId = 0)
  95. {
  96. return $this->_oModule->_isModerator (false);
  97. }
  98. }
  99. /** @} */