BxBaseModProfileFormEntry.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  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 (!empty($CNF['FIELD_PICTURE']) && isset($this->aInputs[$CNF['FIELD_PICTURE']])) {
  23. $this->_aImageFields[$CNF['FIELD_PICTURE']] = array (
  24. 'storage_object' => $CNF['OBJECT_STORAGE'],
  25. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_THUMB'],
  26. 'uploaders' => $CNF['OBJECT_UPLOADERS_PICTURE'],
  27. );
  28. }
  29. if (!empty($CNF['FIELD_COVER']) && isset($this->aInputs[$CNF['FIELD_COVER']])) {
  30. $this->_aImageFields[$CNF['FIELD_COVER']] = array (
  31. 'storage_object' => $CNF['OBJECT_STORAGE_COVER'],
  32. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_COVER_THUMB'],
  33. 'uploaders' => $CNF['OBJECT_UPLOADERS_COVER'],
  34. );
  35. }
  36. foreach ($this->_aImageFields as $sField => $aParams) {
  37. $this->aInputs[$sField]['storage_object'] = $aParams['storage_object'];
  38. $this->aInputs[$sField]['uploaders'] = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $aParams['uploaders'];
  39. $this->aInputs[$sField]['images_transcoder'] = $aParams['images_transcoder'];
  40. $this->aInputs[$sField]['storage_private'] = 0;
  41. $this->aInputs[$sField]['multiple'] = false;
  42. $this->aInputs[$sField]['content_id'] = 0;
  43. $this->aInputs[$sField]['ghost_template'] = '';
  44. }
  45. $oAccountProfile = BxDolProfile::getInstanceAccountProfile();
  46. if ($oAccountProfile)
  47. $this->_iAccountProfileId = $oAccountProfile->id();
  48. }
  49. function initChecker ($aValues = array (), $aSpecificValues = array())
  50. {
  51. $CNF = &$this->_oModule->_oConfig->CNF;
  52. $aContentInfo = isset($CNF['FIELD_ID']) && isset($aValues[$CNF['FIELD_ID']]) ? $this->_oModule->_oDb->getContentInfoById ($aValues[$CNF['FIELD_ID']]) : array();
  53. foreach ($this->_aImageFields as $sField => $aParams) {
  54. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  55. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  56. $this->aInputs[$sField]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $this->_getProfilePhotoGhostTmplVars($sField, $aContentInfo));
  57. }
  58. parent::initChecker($aValues, $aSpecificValues);
  59. }
  60. public function insert ($aValsToAdd = array(), $isIgnore = false)
  61. {
  62. $CNF = &$this->_oModule->_oConfig->CNF;
  63. return parent::insert ($aValsToAdd, $isIgnore);
  64. }
  65. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  66. {
  67. $CNF = &$this->_oModule->_oConfig->CNF;
  68. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  69. }
  70. function delete ($iContentId, $aContentInfo = array())
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  74. foreach ($this->_aImageFields as $sField => $aParams) {
  75. $oStorage = BxDolStorage::getObjectInstance($aParams['storage_object']);
  76. $aFiles = $oStorage->getGhosts($iProfileId, $iContentId);
  77. foreach ($aFiles as $aFile) {
  78. if (!$oStorage->getFile($aFile['id']))
  79. continue;
  80. $bRet = $oStorage->deleteFile($aFile['id'], $this->_iAccountProfileId);
  81. }
  82. }
  83. return parent::delete($iContentId, $aContentInfo);
  84. }
  85. protected function genCustomViewRowValueProfileEmail($aInput)
  86. {
  87. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  88. }
  89. protected function genCustomViewRowValueProfileIp($aInput)
  90. {
  91. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  92. }
  93. private function genCustomViewRowValueProfileEmailOrIp($aInput)
  94. {
  95. $CNF = &$this->_oModule->_oConfig->CNF;
  96. if(empty($aInput['value']))
  97. return '';
  98. $sValue = $aInput['value'];
  99. $sModuleAccounts = 'bx_accounts';
  100. if(!BxDolModuleQuery::getInstance()->isEnabledByName($sModuleAccounts))
  101. return $sValue;
  102. $oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
  103. if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION']))
  104. return $sValue;
  105. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  106. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION'], array(
  107. 'filter' => urlencode($sValue)
  108. )),
  109. 'title' => '',
  110. 'content' => $sValue
  111. ));
  112. }
  113. protected function genCustomViewRowValueProfileStatus($aInput)
  114. {
  115. $CNF = &$this->_oModule->_oConfig->CNF;
  116. if(empty($aInput['value']))
  117. return '';
  118. $sStatus = _t('_sys_profile_status_' . $aInput['value']);
  119. if(empty($CNF['URL_MANAGE_ADMINISTRATION']) || empty($CNF['FIELD_TITLE']) && !empty($this->aInputs[$CNF['FIELD_TITLE']]['value']))
  120. return $sStatus;
  121. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  122. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE_ADMINISTRATION'], array(
  123. 'filter' => urlencode($this->aInputs[$CNF['FIELD_TITLE']]['value'])
  124. )),
  125. 'title' => '',
  126. 'content' => $sStatus
  127. ));
  128. }
  129. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  130. {
  131. $CNF = &$this->_oModule->_oConfig->CNF;
  132. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  133. $bResult = (int)$this->_oModule->_oDb->updateContentPictureById($iContentId, 0/*$iProfileId*/, $iFileId, $sPictureField) > 0;
  134. if($bResult) {
  135. $sModule = $this->_oModule->getName();
  136. switch($sPictureField) {
  137. case $CNF['FIELD_PICTURE']:
  138. bx_alert($sModule, 'profile_picture_changed', $iFileId, $iProfileId, array('content' => $iContentId, 'field' => $CNF['FIELD_PICTURE']));
  139. break;
  140. case $CNF['FIELD_COVER']:
  141. bx_alert($sModule, 'profile_cover_changed', $iFileId, $iProfileId, array('content' => $iContentId, 'field' => $CNF['FIELD_COVER']));
  142. break;
  143. }
  144. }
  145. }
  146. protected function _getProfilePhotoGhostTmplVars($sField, $aContentInfo = array())
  147. {
  148. $CNF = &$this->_oModule->_oConfig->CNF;
  149. return array (
  150. 'name' => $this->aInputs[$sField]['name'],
  151. 'content_id' => $this->aInputs[$sField]['content_id'],
  152. 'bx_if:set_thumb' => array (
  153. 'condition' => false,
  154. 'content' => array (),
  155. ),
  156. );
  157. }
  158. protected function _isAdmin ($iContentId = 0)
  159. {
  160. if (parent::_isAdmin ($iContentId))
  161. return true;
  162. if (!$iContentId || !($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
  163. return false;
  164. return CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);
  165. }
  166. }
  167. /** @} */