BxBaseModFilesFormUpload.php 3.3 KB

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