BxBaseModProfileUploaderCrop.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 $_bSubmitted;
  16. protected $_sImage;
  17. protected $_aOtherImages;
  18. public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  19. {
  20. if(!empty($this->_sModule) && empty($this->_oModule))
  21. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  22. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate ? $oTemplate : $this->_oModule->_oTemplate);
  23. $this->_bSubmitted = false;
  24. }
  25. public function isSubmitted()
  26. {
  27. return $this->_bSubmitted;
  28. }
  29. public function setSubmitted($bSubmitted)
  30. {
  31. $this->_bSubmitted = (bool)$bSubmitted;
  32. }
  33. public function deleteGhostsForProfile($iProfileId, $mixedContent = false)
  34. {
  35. if(!$mixedContent)
  36. return parent::deleteGhostsForProfile($iProfileId, $mixedContent);
  37. $iCount = 0;
  38. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  39. if($oStorage === false)
  40. return $iCount;
  41. $iContentId = (int)(is_array($mixedContent) ? reset($mixedContent) : $mixedContent);
  42. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  43. $aGhosts = $oStorage->getGhosts($iProfileId, $mixedContent, $iContentId ? true : false);
  44. foreach ($aGhosts as $aFile) {
  45. // for requested image type delete only unassigned ghosts and currently set image will be deleted while saving
  46. if($aFile['id'] != $aContentInfo[$this->_sImage] && !in_array($aFile['id'], array_map(function($sField) use ($aContentInfo) {
  47. return $aContentInfo[$sField];
  48. }, $this->_aOtherImages)))
  49. $iCount += $oStorage->deleteFile($aFile['id']);
  50. }
  51. return $iCount;
  52. }
  53. public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
  54. {
  55. $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
  56. if(!$s || !$iContentId) // if we're creating new profile return all ghosts
  57. return $s;
  58. $a = [];
  59. if($sFormat == 'array')
  60. $a = $s;
  61. else if($sFormat == 'json')
  62. $a = json_decode($s, true);
  63. if(!$a)
  64. return $s;
  65. // filter out thumbnails
  66. $iThreshold = 0;
  67. if(($iLifetime = BxDolStorage::getGhostLifetime()) != 0)
  68. $iThreshold = time() - $iLifetime;
  69. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  70. $aResult = [];
  71. foreach ($a as $aFile) {
  72. // for requested image type show only (1) currently set images and (2) unassigned ghosts by uploader
  73. if(($bCurrent = $aFile['file_id'] == $aContentInfo[$this->_sImage]) || (!in_array($aFile['file_id'], array_map(function($sField) use ($aContentInfo) {
  74. return $aContentInfo[$sField];
  75. }, $this->_aOtherImages)) && $aFile['uploader_id'] == $this->_iId)) {
  76. // delete outdated ghost if ghost lifetime is set
  77. if(!$bCurrent && $iThreshold && $aFile['file_created'] < $iThreshold) {
  78. if(($oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject)) !== false)
  79. $oStorage->deleteFile($aFile['file_id']);
  80. continue;
  81. }
  82. // put only the newest file in results in case of non-multiple mode and all files otherwise
  83. if(!$this->isMultiple()) {
  84. if($this->_bIsApi && !$this->isSubmitted()) {
  85. if($bCurrent)
  86. $aResult = [
  87. $aFile['file_id'] => $aFile
  88. ];
  89. else
  90. continue;
  91. }
  92. else if(!$aResult || $aFile['file_created'] > reset($aResult)['file_created'])
  93. $aResult = [
  94. $aFile['file_id'] => $aFile
  95. ];
  96. }
  97. else
  98. $aResult[$aFile['file_id']] = $aFile;
  99. }
  100. }
  101. switch($sFormat) {
  102. case 'array':
  103. break;
  104. case 'json':
  105. $aResult = json_encode($aResult);
  106. break;
  107. default:
  108. $aResult = false; // html format is not suported for this data type
  109. }
  110. return $aResult;
  111. }
  112. protected function isAdmin ($iContentId = 0)
  113. {
  114. return $this->_oModule->_isModerator (false);
  115. }
  116. }
  117. /** @} */