BxBaseModFilesFormUpload.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  23. public function insert ($aValsToAdd = array(), $isIgnore = false)
  24. {
  25. $CNF = &$this->_oModule->_oConfig->CNF;
  26. return $this->processFiles ($CNF['FIELD_PHOTO'], 0, true);
  27. }
  28. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  29. {
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. if (!isset($this->aInputs[$sFieldFile]))
  32. return true;
  33. $mixedFileIds = $this->getCleanValue($sFieldFile);
  34. if(!$mixedFileIds)
  35. return true;
  36. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  37. if (!$oStorage)
  38. return false;
  39. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  40. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true);
  41. if (!$aGhostFiles)
  42. return true;
  43. bx_import('BxDolPrivacy');
  44. // get values form main form to pass it to each file later
  45. $aFormValues = array();
  46. foreach ($this->aInputs as $aInput) {
  47. if(in_array($aInput['name'], array('do_submit', 'profile_id', $CNF['FIELD_PHOTO'])))
  48. continue;
  49. $aFormValues[$aInput['name']] = $this->getCleanValue($aInput['name']);
  50. }
  51. $aContentIds = array();
  52. foreach ($aGhostFiles as $aFile) {
  53. if (CHECK_ACTION_RESULT_ALLOWED !== $this->_oModule->checkAllowedAdd())
  54. continue;
  55. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  56. continue;
  57. $iContentId = 0;
  58. if ($isAssociateWithContent)
  59. $iContentId = BxBaseModGeneralFormEntry::insert (array_merge(array(
  60. $CNF['FIELD_FOR_STORING_FILE_ID'] => $aFile['id'],
  61. $CNF['FIELD_TITLE'] => $this->getCleanValue('title-' . $aFile['id']),
  62. $CNF['FIELD_AUTHOR'] => bx_get('profile_id') && $this->_oModule->serviceIsAllowedAddContentToProfile(bx_get('profile_id')) ? bx_get('profile_id') : '',
  63. $CNF['FIELD_STATUS_ADMIN'] => !$this->_oModule->_isModerator() && !$this->_oModule->_oConfig->isAutoApproveEnabled() ? BX_BASE_MOD_GENERAL_STATUS_PENDING : BX_BASE_MOD_TEXT_STATUS_ACTIVE
  64. ), $aFormValues));
  65. if (!$iContentId)
  66. continue;
  67. $aContentIds[] = $iContentId;
  68. if ($aFile['private'] || (isset($aFormValues[$CNF['FIELD_ALLOW_VIEW_TO']]) && BX_DOL_PG_ALL !== $aFormValues[$CNF['FIELD_ALLOW_VIEW_TO']]))
  69. $oStorage->setFilePrivate ($aFile['id'], true);
  70. if (!$iContentId)
  71. continue;
  72. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  73. $this->_oModule->checkAllowedAdd(true);
  74. }
  75. return $aContentIds;
  76. }
  77. }
  78. /** @} */