BxBaseModProfileUploaderCoverCrop.php 3.4 KB

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