1
0

BxBaseModProfileFormEntry.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseProfile Base classes for profile modules
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxBaseModGeneralFormEntry');
  12. bx_import('BxDolTranscoderImage');
  13. /**
  14. * Create/edit profile form.
  15. */
  16. class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
  17. {
  18. protected $_iAccountProfileId = 0;
  19. protected $_aImageFields = array ();
  20. public function __construct($aInfo, $oTemplate = false)
  21. {
  22. parent::__construct($aInfo, $oTemplate);
  23. $CNF = &$this->_oModule->_oConfig->CNF;
  24. if (!empty($CNF['FIELD_PICTURE'])) {
  25. $this->_aImageFields[$CNF['FIELD_PICTURE']] = array (
  26. 'storage_object' => $CNF['OBJECT_STORAGE'],
  27. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_THUMB'],
  28. 'field_preview' => $CNF['FIELD_PICTURE_PREVIEW'],
  29. );
  30. }
  31. if (!empty($CNF['FIELD_COVER'])) {
  32. $this->_aImageFields[$CNF['FIELD_COVER']] = array (
  33. 'storage_object' => $CNF['OBJECT_STORAGE_COVER'],
  34. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_COVER_THUMB'],
  35. 'field_preview' => $CNF['FIELD_COVER_PREVIEW'],
  36. );
  37. }
  38. if (!empty($CNF['FIELD_PICTURE_PREVIEW']))
  39. $this->_aImageFields[$CNF['FIELD_PICTURE_PREVIEW']] = $this->_aImageFields[$CNF['FIELD_PICTURE']];
  40. if (!empty($CNF['FIELD_COVER_PREVIEW']))
  41. $this->_aImageFields[$CNF['FIELD_COVER_PREVIEW']] = $this->_aImageFields[$CNF['FIELD_COVER']];
  42. $oAccountProfile = BxDolProfile::getInstanceAccountProfile();
  43. if ($oAccountProfile)
  44. $this->_iAccountProfileId = $oAccountProfile->id();
  45. }
  46. function initChecker ($aValues = array (), $aSpecificValues = array())
  47. {
  48. parent::initChecker($aValues, $aSpecificValues);
  49. $CNF = &$this->_oModule->_oConfig->CNF;
  50. foreach ($this->_aImageFields as $sField => $aVals) {
  51. if (!isset($this->aInputs[$sField]))
  52. continue;
  53. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  54. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  55. $sErrorString = '';
  56. $this->aInputs[$sField]['file_id'] = $this->_processFile ($sField, isset($aValues[$sField]) ? $aValues[$sField] : 0, $sErrorString);
  57. if ($sErrorString) {
  58. $this->aInputs[$sField]['error'] = $sErrorString;
  59. $this->setValid(false);
  60. }
  61. if (!isset($this->aInputs[$aVals['field_preview']]) || !empty($this->aInputs[$aVals['field_preview']]['content']))
  62. continue;
  63. $oTranscoder = BxDolTranscoderImage::getObjectInstance($aVals['images_transcoder']);
  64. $aVars = array (
  65. 'bx_if:picture' => array (
  66. 'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] ? true : false,
  67. 'content' => array (
  68. 'picture_url' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] ? $oTranscoder->getFileUrl($aValues[$sField]) : '',
  69. ),
  70. ),
  71. 'bx_if:no_picture' => array (
  72. 'condition' => !$oTranscoder || !isset($aValues[$sField]) || !$aValues[$sField] ? true : false,
  73. 'content' => array (),
  74. ),
  75. );
  76. $this->aInputs[$aVals['field_preview']]['content'] = $this->_oModule->_oTemplate->parseHtmlByName('picture_preview.html', $aVars);
  77. }
  78. }
  79. public function insert ($aValsToAdd = array(), $isIgnore = false)
  80. {
  81. $CNF = &$this->_oModule->_oConfig->CNF;
  82. if (!empty($this->aInputs[$CNF['FIELD_PICTURE']])) {
  83. $aValsToAdd = array_merge($aValsToAdd, array (
  84. $CNF['FIELD_PICTURE'] => $this->aInputs[$CNF['FIELD_PICTURE']]['file_id'],
  85. ));
  86. }
  87. if (!empty($this->aInputs[$CNF['FIELD_COVER']])) {
  88. $aValsToAdd = array_merge($aValsToAdd, array (
  89. $CNF['FIELD_COVER'] => $this->aInputs[$CNF['FIELD_COVER']]['file_id'],
  90. ));
  91. }
  92. return parent::insert ($aValsToAdd, $isIgnore);
  93. }
  94. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  95. {
  96. $CNF = &$this->_oModule->_oConfig->CNF;
  97. if (!empty($this->aInputs[$CNF['FIELD_COVER']]))
  98. $aValsToAdd[$CNF['FIELD_COVER']] = $this->aInputs[$CNF['FIELD_COVER']]['file_id'];
  99. if (!empty($this->aInputs[$CNF['FIELD_PICTURE']]))
  100. $aValsToAdd[$CNF['FIELD_PICTURE']] = $this->aInputs[$CNF['FIELD_PICTURE']]['file_id'];
  101. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  102. }
  103. function delete ($iContentId, $aContentInfo = array())
  104. {
  105. $CNF = &$this->_oModule->_oConfig->CNF;
  106. foreach ($this->_aImageFields as $sField => $aVals) {
  107. if (isset($aContentInfo[$sField]) && $aContentInfo[$sField])
  108. $this->_deleteFile ($aContentInfo[$sField], $aVals['storage_object']);
  109. }
  110. bx_import('BxDolView');
  111. BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId)->onObjectDelete();
  112. return parent::delete($iContentId);
  113. }
  114. function _processFile ($sField, $iFileIdOld, &$sErrorString)
  115. {
  116. if (empty($_FILES[$sField]['tmp_name']))
  117. return $iFileIdOld;
  118. $oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sField]['storage_object']);
  119. if (!$oStorage)
  120. return $iFileIdOld;
  121. // delete previous file
  122. $this->_deleteFile($iFileIdOld, $this->_aImageFields[$sField]['storage_object']);
  123. // process new file and return new file id
  124. if (!($iFileId = $oStorage->storeFileFromForm($_FILES[$sField], false, $this->_iAccountProfileId))) {
  125. $sErrorString = $oStorage->getErrorString();
  126. return 0;
  127. }
  128. return $iFileId;
  129. }
  130. function _deleteFile ($iFileId, $sStorageObject)
  131. {
  132. if (!$iFileId)
  133. return true;
  134. if (!($oStorage = BxDolStorage::getObjectInstance($sStorageObject)))
  135. return false;
  136. if (!$oStorage->getFile($iFileId))
  137. return true;
  138. return $oStorage->deleteFile($iFileId, $this->_iAccountProfileId);
  139. }
  140. }
  141. /** @} */