BxBaseModProfileFormEntry.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /**
  12. * Create/edit profile form.
  13. */
  14. class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
  15. {
  16. protected $_iAccountProfileId = 0;
  17. protected $_aImageFields = array ();
  18. public function __construct($aInfo, $oTemplate = false)
  19. {
  20. parent::__construct($aInfo, $oTemplate);
  21. $CNF = &$this->_oModule->_oConfig->CNF;
  22. if (isset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]) && $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) {
  23. $sInfo = $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]['info'];
  24. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']] = $oPrivacy->getGroupChooser($CNF['OBJECT_PRIVACY_VIEW']);
  25. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]['db']['pass'] = 'Xss';
  26. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]['info'] = $sInfo;
  27. }
  28. if (!empty($CNF['FIELD_PICTURE']) && isset($this->aInputs[$CNF['FIELD_PICTURE']])) {
  29. $this->aInputs[$CNF['FIELD_PICTURE']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  30. $this->aInputs[$CNF['FIELD_PICTURE']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PICTURE']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PICTURE']]['value']) : $CNF['OBJECT_UPLOADERS'];
  31. $this->aInputs[$CNF['FIELD_PICTURE']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_THUMB'];
  32. $this->aInputs[$CNF['FIELD_PICTURE']]['storage_private'] = 0;
  33. $this->aInputs[$CNF['FIELD_PICTURE']]['multiple'] = false;
  34. $this->aInputs[$CNF['FIELD_PICTURE']]['content_id'] = 0;
  35. $this->aInputs[$CNF['FIELD_PICTURE']]['ghost_template'] = '';
  36. }
  37. if (!empty($CNF['FIELD_COVER'])) {
  38. $this->_aImageFields[$CNF['FIELD_COVER']] = array (
  39. 'storage_object' => $CNF['OBJECT_STORAGE_COVER'],
  40. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_COVER_THUMB'],
  41. 'field_preview' => $CNF['FIELD_COVER_PREVIEW'],
  42. );
  43. }
  44. if (!empty($CNF['FIELD_COVER_PREVIEW']))
  45. $this->_aImageFields[$CNF['FIELD_COVER_PREVIEW']] = $this->_aImageFields[$CNF['FIELD_COVER']];
  46. $oAccountProfile = BxDolProfile::getInstanceAccountProfile();
  47. if ($oAccountProfile)
  48. $this->_iAccountProfileId = $oAccountProfile->id();
  49. }
  50. function initChecker ($aValues = array (), $aSpecificValues = array())
  51. {
  52. $CNF = &$this->_oModule->_oConfig->CNF;
  53. if (isset($this->aInputs[$CNF['FIELD_PICTURE']])) {
  54. $aContentInfo = false;
  55. if ($aValues && !empty($aValues[$CNF['FIELD_ID']])) {
  56. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues[$CNF['FIELD_ID']]);
  57. $this->aInputs[$CNF['FIELD_PICTURE']]['content_id'] = $aValues[$CNF['FIELD_ID']];
  58. }
  59. $this->aInputs[$CNF['FIELD_PICTURE']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $this->_getPhotoGhostTmplVars($aContentInfo));
  60. }
  61. parent::initChecker($aValues, $aSpecificValues);
  62. foreach ($this->_aImageFields as $sField => $aVals) {
  63. if (!isset($this->aInputs[$sField]))
  64. continue;
  65. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  66. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  67. $sErrorString = '';
  68. $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);
  69. if ($sErrorString) {
  70. $this->aInputs[$sField]['error'] = $sErrorString;
  71. $this->setValid(false);
  72. }
  73. if (!isset($this->aInputs[$aVals['field_preview']]) || !empty($this->aInputs[$aVals['field_preview']]['content']))
  74. continue;
  75. $oTranscoder = BxDolTranscoderImage::getObjectInstance($aVals['images_transcoder']);
  76. $aVars = array (
  77. 'bx_if:picture' => array (
  78. 'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] ? true : false,
  79. 'content' => array (
  80. 'picture_url' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] ? $oTranscoder->getFileUrl($aValues[$sField]) : '',
  81. ),
  82. ),
  83. 'bx_if:no_picture' => array (
  84. 'condition' => !$oTranscoder || !isset($aValues[$sField]) || !$aValues[$sField] ? true : false,
  85. 'content' => array (),
  86. ),
  87. 'bx_if:delete' => array (
  88. 'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] && $sField == $CNF['FIELD_COVER'] ? true : false,
  89. '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 : ''),
  90. ),
  91. );
  92. $this->aInputs[$aVals['field_preview']]['content'] = $this->_oModule->_oTemplate->parseHtmlByName('picture_preview.html', $aVars);
  93. }
  94. }
  95. public function insert ($aValsToAdd = array(), $isIgnore = false)
  96. {
  97. $CNF = &$this->_oModule->_oConfig->CNF;
  98. if (!empty($this->aInputs[$CNF['FIELD_COVER']]) && !empty($this->aInputs[$CNF['FIELD_COVER']]['file_id'])) {
  99. $aValsToAdd = array_merge($aValsToAdd, array (
  100. $CNF['FIELD_COVER'] => $this->aInputs[$CNF['FIELD_COVER']]['file_id'],
  101. ));
  102. }
  103. if (isset($CNF['FIELD_PICTURE']))
  104. $aValsToAdd[$CNF['FIELD_PICTURE']] = 0; // we will update this field later, since we don't know content id yet
  105. return parent::insert ($aValsToAdd, $isIgnore);
  106. }
  107. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. if (!empty($this->aInputs[$CNF['FIELD_COVER']]) && !empty($this->aInputs[$CNF['FIELD_COVER']]['file_id']))
  111. $aValsToAdd[$CNF['FIELD_COVER']] = $this->aInputs[$CNF['FIELD_COVER']]['file_id'];
  112. if (isset($CNF['FIELD_PICTURE']))
  113. $aValsToAdd[$CNF['FIELD_PICTURE']] = 0; // we will update this field later
  114. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  115. }
  116. function delete ($iContentId, $aContentInfo = array())
  117. {
  118. $CNF = &$this->_oModule->_oConfig->CNF;
  119. foreach ($this->_aImageFields as $sField => $aVals) {
  120. if (isset($aContentInfo[$sField]) && $aContentInfo[$sField])
  121. $this->_deleteFile ($iContentId, $sField, $aContentInfo[$sField]);
  122. }
  123. if (isset($CNF['FIELD_PICTURE']) && isset($CNF['OBJECT_STORAGE']) && $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])) {
  124. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  125. $aFiles = $oStorage->getGhosts($iProfileId, $iContentId);
  126. foreach ($aFiles as $aFile) {
  127. if (!$oStorage->getFile($aFile['id']))
  128. continue;
  129. $bRet = $oStorage->deleteFile($aFile['id'], $this->_iAccountProfileId);
  130. }
  131. }
  132. return parent::delete($iContentId, $aContentInfo);
  133. }
  134. function _processFile ($iContentId, $sField, $iFileIdOld, &$sErrorString)
  135. {
  136. if (empty($_FILES[$sField]['tmp_name']))
  137. return $iFileIdOld;
  138. $oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sField]['storage_object']);
  139. if (!$oStorage)
  140. return $iFileIdOld;
  141. // delete previous file
  142. $this->_deleteFile($iContentId, $sField, $iFileIdOld);
  143. // process new file and return new file id
  144. if (!($iFileId = $oStorage->storeFileFromForm($_FILES[$sField], false, $this->_iAccountProfileId))) {
  145. $sErrorString = $oStorage->getErrorString();
  146. return 0;
  147. }
  148. return $iFileId;
  149. }
  150. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId)
  151. {
  152. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId);
  153. $this->_oModule->_oDb->updateContentPictureById($iContentId, 0/*$iProfileId*/, $iFileId, $this->_oModule->_oConfig->CNF['FIELD_PICTURE']);
  154. }
  155. function _deleteFile ($iContentId, $sFieldPicture, $iFileId, $bForceFieldUpdate = false)
  156. {
  157. if (!$iFileId)
  158. return true;
  159. if (!$this->_aImageFields[$sFieldPicture]['storage_object'])
  160. return false;
  161. if (!($oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sFieldPicture]['storage_object'])))
  162. return false;
  163. if (!$oStorage->getFile($iFileId))
  164. return true;
  165. if (($bRet = $oStorage->deleteFile($iFileId, $this->_iAccountProfileId)) && $bForceFieldUpdate) {
  166. $this->_oModule->_oDb->updateContentPictureById($iContentId, 0, 0, $sFieldPicture);
  167. }
  168. return $bRet;
  169. }
  170. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  171. {
  172. $CNF = &$this->_oModule->_oConfig->CNF;
  173. return array (
  174. 'name' => $this->aInputs[$CNF['FIELD_PICTURE']]['name'],
  175. 'content_id' => $this->aInputs[$CNF['FIELD_PICTURE']]['content_id'],
  176. 'bx_if:set_thumb' => array (
  177. 'condition' => false,
  178. 'content' => array (),
  179. ),
  180. );
  181. }
  182. }
  183. /** @} */