BxFilesFormUpload.php 4.0 KB

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