BxBaseModProfileFormEntry.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 TridentModules
  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 (!empty($aValues[$CNF['FIELD_ID']]) ? $aValues[$CNF['FIELD_ID']] : 0, $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. 'bx_if:delete' => array (
  76. 'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] && $sField == $CNF['FIELD_COVER'] ? true : false,
  77. 'content' => array ('action_ajax' => isset($aValues[$sField]) ? BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'delete_profile_img/' . $aValues[$sField] . '/' . (!empty($aValues[$CNF['FIELD_ID']]) ? $aValues[$CNF['FIELD_ID']] : 0) . '/' . $sField : ''),
  78. ),
  79. );
  80. $this->aInputs[$aVals['field_preview']]['content'] = $this->_oModule->_oTemplate->parseHtmlByName('picture_preview.html', $aVars);
  81. }
  82. }
  83. public function insert ($aValsToAdd = array(), $isIgnore = false)
  84. {
  85. $CNF = &$this->_oModule->_oConfig->CNF;
  86. if (!empty($this->aInputs[$CNF['FIELD_PICTURE']])) {
  87. $aValsToAdd = array_merge($aValsToAdd, array (
  88. $CNF['FIELD_PICTURE'] => $this->aInputs[$CNF['FIELD_PICTURE']]['file_id'],
  89. ));
  90. }
  91. if (!empty($this->aInputs[$CNF['FIELD_COVER']])) {
  92. $aValsToAdd = array_merge($aValsToAdd, array (
  93. $CNF['FIELD_COVER'] => $this->aInputs[$CNF['FIELD_COVER']]['file_id'],
  94. ));
  95. }
  96. return parent::insert ($aValsToAdd, $isIgnore);
  97. }
  98. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  99. {
  100. $CNF = &$this->_oModule->_oConfig->CNF;
  101. if (!empty($this->aInputs[$CNF['FIELD_COVER']]))
  102. $aValsToAdd[$CNF['FIELD_COVER']] = $this->aInputs[$CNF['FIELD_COVER']]['file_id'];
  103. if (!empty($this->aInputs[$CNF['FIELD_PICTURE']]))
  104. $aValsToAdd[$CNF['FIELD_PICTURE']] = $this->aInputs[$CNF['FIELD_PICTURE']]['file_id'];
  105. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  106. }
  107. function delete ($iContentId, $aContentInfo = array())
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. foreach ($this->_aImageFields as $sField => $aVals) {
  111. if (isset($aContentInfo[$sField]) && $aContentInfo[$sField])
  112. $this->_deleteFile ($iContentId, $sField, $aContentInfo[$sField]);
  113. }
  114. return parent::delete($iContentId, $aContentInfo);
  115. }
  116. function _processFile ($iContentId, $sField, $iFileIdOld, &$sErrorString)
  117. {
  118. if (empty($_FILES[$sField]['tmp_name']))
  119. return $iFileIdOld;
  120. $oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sField]['storage_object']);
  121. if (!$oStorage)
  122. return $iFileIdOld;
  123. // delete previous file
  124. $this->_deleteFile($iContentId, $sField, $iFileIdOld);
  125. // process new file and return new file id
  126. if (!($iFileId = $oStorage->storeFileFromForm($_FILES[$sField], false, $this->_iAccountProfileId))) {
  127. $sErrorString = $oStorage->getErrorString();
  128. return 0;
  129. }
  130. return $iFileId;
  131. }
  132. function _deleteFile ($iContentId, $sFieldPicture, $iFileId, $bForceFieldUpdate = false)
  133. {
  134. if (!$iFileId)
  135. return true;
  136. if (!$this->_aImageFields[$sFieldPicture]['storage_object'])
  137. return false;
  138. if (!($oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sFieldPicture]['storage_object'])))
  139. return false;
  140. if (!$oStorage->getFile($iFileId))
  141. return true;
  142. if (($bRet = $oStorage->deleteFile($iFileId, $this->_iAccountProfileId)) && $bForceFieldUpdate) {
  143. $this->_oModule->_oDb->updateContentPictureById($iContentId, 0, 0, $sFieldPicture);
  144. }
  145. return $bRet;
  146. }
  147. }
  148. /** @} */