BxBaseModProfileFormEntry.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. $CNF = &$this->_oModule->_oConfig->CNF;
  49. if (!empty($aValues[$CNF['FIELD_PICTURE']]) && $aValues[$CNF['FIELD_PICTURE']] && isset($this->aInputs[$CNF['FIELD_PICTURE']]) && $this->aInputs[$CNF['FIELD_PICTURE']]) {
  50. $this->aInputs[$CNF['FIELD_PICTURE']]['required'] = false;
  51. unset($this->aInputs[$CNF['FIELD_PICTURE']]['checker']);
  52. }
  53. parent::initChecker($aValues, $aSpecificValues);
  54. foreach ($this->_aImageFields as $sField => $aVals) {
  55. if (!isset($this->aInputs[$sField]))
  56. continue;
  57. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  58. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  59. $sErrorString = '';
  60. $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);
  61. if ($sErrorString) {
  62. $this->aInputs[$sField]['error'] = $sErrorString;
  63. $this->setValid(false);
  64. }
  65. if (!isset($this->aInputs[$aVals['field_preview']]) || !empty($this->aInputs[$aVals['field_preview']]['content']))
  66. continue;
  67. $oTranscoder = BxDolTranscoderImage::getObjectInstance($aVals['images_transcoder']);
  68. $aVars = array (
  69. 'bx_if:picture' => array (
  70. 'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] ? true : false,
  71. 'content' => array (
  72. 'picture_url' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] ? $oTranscoder->getFileUrl($aValues[$sField]) : '',
  73. ),
  74. ),
  75. 'bx_if:no_picture' => array (
  76. 'condition' => !$oTranscoder || !isset($aValues[$sField]) || !$aValues[$sField] ? true : false,
  77. 'content' => array (),
  78. ),
  79. 'bx_if:delete' => array (
  80. 'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] && $sField == $CNF['FIELD_COVER'] ? true : false,
  81. '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 : ''),
  82. ),
  83. );
  84. $this->aInputs[$aVals['field_preview']]['content'] = $this->_oModule->_oTemplate->parseHtmlByName('picture_preview.html', $aVars);
  85. }
  86. }
  87. public function insert ($aValsToAdd = array(), $isIgnore = false)
  88. {
  89. $CNF = &$this->_oModule->_oConfig->CNF;
  90. if (!empty($this->aInputs[$CNF['FIELD_PICTURE']])) {
  91. $aValsToAdd = array_merge($aValsToAdd, array (
  92. $CNF['FIELD_PICTURE'] => $this->aInputs[$CNF['FIELD_PICTURE']]['file_id'],
  93. ));
  94. }
  95. if (!empty($this->aInputs[$CNF['FIELD_COVER']])) {
  96. $aValsToAdd = array_merge($aValsToAdd, array (
  97. $CNF['FIELD_COVER'] => $this->aInputs[$CNF['FIELD_COVER']]['file_id'],
  98. ));
  99. }
  100. return parent::insert ($aValsToAdd, $isIgnore);
  101. }
  102. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  103. {
  104. $CNF = &$this->_oModule->_oConfig->CNF;
  105. if (!empty($this->aInputs[$CNF['FIELD_COVER']]))
  106. $aValsToAdd[$CNF['FIELD_COVER']] = $this->aInputs[$CNF['FIELD_COVER']]['file_id'];
  107. if (!empty($this->aInputs[$CNF['FIELD_PICTURE']]))
  108. $aValsToAdd[$CNF['FIELD_PICTURE']] = $this->aInputs[$CNF['FIELD_PICTURE']]['file_id'];
  109. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  110. }
  111. function delete ($iContentId, $aContentInfo = array())
  112. {
  113. $CNF = &$this->_oModule->_oConfig->CNF;
  114. foreach ($this->_aImageFields as $sField => $aVals) {
  115. if (isset($aContentInfo[$sField]) && $aContentInfo[$sField])
  116. $this->_deleteFile ($iContentId, $sField, $aContentInfo[$sField]);
  117. }
  118. return parent::delete($iContentId, $aContentInfo);
  119. }
  120. function _processFile ($iContentId, $sField, $iFileIdOld, &$sErrorString)
  121. {
  122. if (empty($_FILES[$sField]['tmp_name']))
  123. return $iFileIdOld;
  124. $oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sField]['storage_object']);
  125. if (!$oStorage)
  126. return $iFileIdOld;
  127. // delete previous file
  128. $this->_deleteFile($iContentId, $sField, $iFileIdOld);
  129. // process new file and return new file id
  130. if (!($iFileId = $oStorage->storeFileFromForm($_FILES[$sField], false, $this->_iAccountProfileId))) {
  131. $sErrorString = $oStorage->getErrorString();
  132. return 0;
  133. }
  134. return $iFileId;
  135. }
  136. function _deleteFile ($iContentId, $sFieldPicture, $iFileId, $bForceFieldUpdate = false)
  137. {
  138. if (!$iFileId)
  139. return true;
  140. if (!$this->_aImageFields[$sFieldPicture]['storage_object'])
  141. return false;
  142. if (!($oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sFieldPicture]['storage_object'])))
  143. return false;
  144. if (!$oStorage->getFile($iFileId))
  145. return true;
  146. if (($bRet = $oStorage->deleteFile($iFileId, $this->_iAccountProfileId)) && $bForceFieldUpdate) {
  147. $this->_oModule->_oDb->updateContentPictureById($iContentId, 0, 0, $sFieldPicture);
  148. }
  149. return $bRet;
  150. }
  151. }
  152. /** @} */