BxBaseModFilesFormUpload.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 BaseFile Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Files upload form
  13. */
  14. class BxBaseModFilesFormUpload extends BxBaseModTextFormEntry
  15. {
  16. public function __construct($aInfo, $oTemplate = false)
  17. {
  18. parent::__construct($aInfo, $oTemplate);
  19. $this->_sGhostTemplate = 'form_ghost_template_upload.html';
  20. if (isset($this->aInputs['profile_id']))
  21. $this->aInputs['profile_id']['value'] = bx_get('profile_id');
  22. $CNF = &$this->_oModule->_oConfig->CNF;
  23. if (isset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]) && $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) {
  24. $oProfile = bx_get('profile_id') ? BxDolProfile::getInstance(bx_get('profile_id')) : false;
  25. $oPrivacy->setCustomPrivacy(bx_get('profile_id') && $oProfile && BxDolService::call($oProfile->getModule(), 'is_group_profile') ? true : false);
  26. $aSave = array('db' => array('pass' => 'Xss'));
  27. array_walk($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], function ($a, $k, $aSave) {
  28. if (in_array($k, array('info', 'caption', 'value')))
  29. $aSave[0][$k] = $a;
  30. }, array(&$aSave));
  31. $aGroupChooser = $oPrivacy->getGroupChooserCustom($CNF['OBJECT_PRIVACY_VIEW'], $oPrivacy->isCustomPrivacy());
  32. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']] = array_merge($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], $aGroupChooser, $aSave);
  33. }
  34. }
  35. public function insert ($aValsToAdd = array(), $isIgnore = false)
  36. {
  37. $CNF = &$this->_oModule->_oConfig->CNF;
  38. return $this->processFiles ($CNF['FIELD_PHOTO'], 0, true);
  39. }
  40. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  41. {
  42. $CNF = &$this->_oModule->_oConfig->CNF;
  43. if (!isset($this->aInputs[$sFieldFile]))
  44. return true;
  45. $mixedFileIds = $this->getCleanValue($sFieldFile);
  46. if(!$mixedFileIds)
  47. return true;
  48. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  49. if (!$oStorage)
  50. return false;
  51. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  52. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true);
  53. if (!$aGhostFiles)
  54. return true;
  55. bx_import('BxDolPrivacy');
  56. // get values form main form to pass it to each file later
  57. $aFormValues = array();
  58. foreach ($this->aInputs as $aInput) {
  59. if ($aInput['name'] && isset($aInput['value']) && $aInput['value'] && !is_array($aInput['value']) && $aInput['name'] != 'do_submit' && $aInput['name'] != $CNF['FIELD_PHOTO'] && $aInput['name'] != 'profile_id')
  60. $aFormValues[$aInput['name']] = $aInput['value'];
  61. }
  62. $aContentIds = array();
  63. foreach ($aGhostFiles as $aFile) {
  64. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  65. continue;
  66. $iContentId = 0;
  67. if ($isAssociateWithContent)
  68. $iContentId = BxBaseModGeneralFormEntry::insert (array_merge(array(
  69. $CNF['FIELD_FOR_STORING_FILE_ID'] => $aFile['id'],
  70. $CNF['FIELD_TITLE'] => $this->getCleanValue('title-' . $aFile['id']),
  71. $CNF['FIELD_AUTHOR'] => bx_get('profile_id') && $this->_oModule->serviceIsAllowedAddContentToProfile(bx_get('profile_id')) ? bx_get('profile_id') : '',
  72. ), $aFormValues));
  73. if (!$iContentId)
  74. continue;
  75. $aContentIds[] = $iContentId;
  76. if ($aFile['private'] || (isset($aFormValues[$CNF['FIELD_ALLOW_VIEW_TO']]) && BX_DOL_PG_ALL !== $aFormValues[$CNF['FIELD_ALLOW_VIEW_TO']]))
  77. $oStorage->setFilePrivate ($aFile['id'], true);
  78. if ($iContentId)
  79. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  80. }
  81. return $aContentIds;
  82. }
  83. }
  84. /** @} */