BxBaseModProfileUploaderPictureCrop.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 BxBaseModProfileUploaderPictureCrop extends BxTemplUploaderCrop
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_aOtherImages;
  16. public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  17. {
  18. if(!empty($this->_sModule))
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate);
  21. $CNF = $this->_oModule->_oConfig->CNF;
  22. $this->_aOtherImages = [$CNF['FIELD_COVER']];
  23. if(!empty($CNF['FIELD_BADGE']))
  24. $this->_aOtherImages[] = $CNF['FIELD_BADGE'];
  25. }
  26. public function deleteGhostsForProfile($iProfileId, $iContentId = false)
  27. {
  28. if (!$iContentId)
  29. return parent::deleteGhostsForProfile($iProfileId, $iContentId);
  30. $CNF = $this->_oModule->_oConfig->CNF;
  31. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  32. $iCount = 0;
  33. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  34. $aGhosts = $oStorage->getGhosts($iProfileId, $iContentId, $iContentId ? true : false);
  35. foreach ($aGhosts as $aFile) {
  36. // for profile image delete only unassigned ghosts and currently set profile pictures
  37. if ($aFile['id'] == $aContentInfo[$CNF['FIELD_PICTURE']] || !in_array($aFile['id'], array_map(function($sField) use ($aContentInfo) {
  38. return $aContentInfo[$sField];
  39. }, $this->_aOtherImages)))
  40. $iCount += $oStorage->deleteFile($aFile['id']);
  41. }
  42. return $iCount;
  43. }
  44. public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
  45. {
  46. $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
  47. if (!$s || !$iContentId) // if we're creating new profile return all ghosts
  48. return $s;
  49. $a = array();
  50. if ($sFormat == 'array')
  51. $a = $s;
  52. else if ($sFormat == 'json')
  53. $a = json_decode($s, true);
  54. if (!$a)
  55. return $s;
  56. // filter out thumbnails
  57. $CNF = $this->_oModule->_oConfig->CNF;
  58. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  59. $aResult = array();
  60. foreach ($a as $aFile) {
  61. // for profile pictures show only unassigned ghosts and currently set profile pictures
  62. if (($aFile['file_id'] == $aContentInfo[$CNF['FIELD_PICTURE']] || !in_array($aFile['file_id'], array_map(function($sField) use ($aContentInfo) {
  63. return $aContentInfo[$sField];
  64. }, $this->_aOtherImages))))
  65. $aResult[$aFile['file_id']] = $aFile;
  66. }
  67. if ('array' == $sFormat) {
  68. return $aResult;
  69. }
  70. else if ('json' == $sFormat) {
  71. return json_encode($aResult);
  72. } else { // html format is not suported for this data type
  73. return false;
  74. }
  75. }
  76. protected function isAdmin ($iContentId = 0)
  77. {
  78. return $this->_oModule->_isModerator (false);
  79. }
  80. }
  81. /** @} */