BxBaseModProfileFormEntry.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. protected $_aUploadersInfo = [];
  19. public function __construct($aInfo, $oTemplate = false)
  20. {
  21. if (!isset($this->_bAllowChangeUserForAdmins))
  22. $this->_bAllowChangeUserForAdmins = false;
  23. parent::__construct($aInfo, $oTemplate);
  24. $this->_sAuthorKey = 'profile_id';
  25. $CNF = &$this->_oModule->_oConfig->CNF;
  26. if (!empty($CNF['FIELD_PICTURE']) && isset($this->aInputs[$CNF['FIELD_PICTURE']])) {
  27. $this->_aImageFields[$CNF['FIELD_PICTURE']] = array (
  28. 'storage_object' => $CNF['OBJECT_STORAGE'],
  29. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_THUMB'],
  30. 'uploaders' => $CNF['OBJECT_UPLOADERS_PICTURE'],
  31. );
  32. }
  33. if (!empty($CNF['FIELD_COVER']) && isset($this->aInputs[$CNF['FIELD_COVER']])) {
  34. $sStorage = $this->_oModule->_oConfig->getObject($CNF['OBJECT_STORAGE_COVER']);
  35. $sUploadersId = genRndPwd(8, false);
  36. $aUploaders = !empty($this->aInputs[$CNF['FIELD_COVER']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_COVER']]['value']) : $this->_oModule->_oConfig->getUploaders($CNF['FIELD_COVER']);
  37. foreach($aUploaders as $sUploader){
  38. $this->_aUploadersInfo[$sUploader] = array(
  39. 'id' => $sUploadersId,
  40. 'name' => $sUploader,
  41. 'js_object' => BxDolUploader::getObjectInstance($sUploader, $sStorage, $sUploadersId)->getNameJsInstanceUploader()
  42. );
  43. }
  44. $this->_aImageFields[$CNF['FIELD_COVER']] = array (
  45. 'storage_object' => $CNF['OBJECT_STORAGE_COVER'],
  46. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_COVER_THUMB'],
  47. 'uploaders_id' => $sUploadersId,
  48. 'uploaders' => $aUploaders
  49. );
  50. }
  51. if (($sKey = 'FIELD_BADGE') && !empty($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]])) {
  52. $this->_aImageFields[$CNF[$sKey]] = [
  53. 'storage_object' => $CNF['OBJECT_STORAGE_BADGE'],
  54. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_BADGE'],
  55. 'uploaders' => $CNF['OBJECT_UPLOADERS_PICTURE'],
  56. ];
  57. }
  58. foreach ($this->_aImageFields as $sField => $aParams) {
  59. $this->aInputs[$sField]['storage_object'] = $aParams['storage_object'];
  60. $this->aInputs[$sField]['uploaders'] = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $aParams['uploaders'];
  61. $this->aInputs[$sField]['images_transcoder'] = $aParams['images_transcoder'];
  62. $this->aInputs[$sField]['uploaders_id'] = isset($aParams['uploaders_id']) ? $aParams['uploaders_id'] : '';
  63. $this->aInputs[$sField]['storage_private'] = 0;
  64. $this->aInputs[$sField]['multiple'] = false;
  65. $this->aInputs[$sField]['content_id'] = 0;
  66. $this->aInputs[$sField]['ghost_template'] = '';
  67. }
  68. if(($sField = 'friends_count') && isset($this->aInputs[$sField]) && !$this->_oModule->_oConfig->isFriends())
  69. unset($this->aInputs[$sField]);
  70. $oAccountProfile = BxDolProfile::getInstanceAccountProfile();
  71. if ($oAccountProfile)
  72. $this->_iAccountProfileId = $oAccountProfile->id();
  73. }
  74. public function getUploadersInfo($sField = '')
  75. {
  76. if(empty($sField))
  77. return $this->_aUploadersInfo;
  78. $aUploaders = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $this->_oModule->_oConfig->getUploaders($sField);
  79. return $this->_aUploadersInfo[array_shift($aUploaders)];
  80. }
  81. function initChecker ($aValues = array (), $aSpecificValues = array())
  82. {
  83. $CNF = &$this->_oModule->_oConfig->CNF;
  84. $aContentInfo = isset($CNF['FIELD_ID']) && isset($aValues[$CNF['FIELD_ID']]) ? $this->_oModule->_oDb->getContentInfoById ($aValues[$CNF['FIELD_ID']]) : array();
  85. foreach ($this->_aImageFields as $sField => $aParams) {
  86. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  87. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  88. $this->aInputs[$sField]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $this->_getProfilePhotoGhostTmplVars($sField, $aContentInfo));
  89. }
  90. parent::initChecker($aValues, $aSpecificValues);
  91. if(($sField = 'FIELD_STG_TABS') && !empty($CNF[$sField]) && !empty($this->aInputs[$CNF[$sField]]) && is_array($this->aInputs[$CNF[$sField]]) && ($sValue = $this->aInputs[$CNF[$sField]]['value']))
  92. $this->aInputs[$CNF[$sField]]['value'] = !is_array($sValue) ? explode(',', $sValue) : [];
  93. }
  94. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  95. {
  96. $CNF = &$this->_oModule->_oConfig->CNF;
  97. if(($sField = 'FIELD_STG_TABS') && !empty($CNF[$sField]) && empty($aValsToAdd[$CNF[$sField]])) {
  98. $mixedValue = $this->getCleanValue($CNF[$sField]);
  99. if(is_array($mixedValue))
  100. self::setSubmittedValue($CNF[$sField], implode(',', $mixedValue), $this->aFormAttrs['method']);
  101. }
  102. return parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  103. }
  104. function delete ($iContentId, $aContentInfo = array())
  105. {
  106. $CNF = &$this->_oModule->_oConfig->CNF;
  107. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  108. foreach ($this->_aImageFields as $sField => $aParams) {
  109. $oStorage = BxDolStorage::getObjectInstance($aParams['storage_object']);
  110. $aFiles = $oStorage->getGhosts($iProfileId, $iContentId);
  111. foreach ($aFiles as $aFile) {
  112. if (!$oStorage->getFile($aFile['id']))
  113. continue;
  114. $bRet = $oStorage->deleteFile($aFile['id'], $this->_iAccountProfileId);
  115. }
  116. }
  117. return parent::delete($iContentId, $aContentInfo);
  118. }
  119. protected function genCustomViewRowValueProfileEmail($aInput)
  120. {
  121. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  122. }
  123. protected function genCustomViewRowValueProfileIp($aInput)
  124. {
  125. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  126. }
  127. protected function genCustomViewRowValueFriendsCount($aInput)
  128. {
  129. $CNF = &$this->_oModule->_oConfig->CNF;
  130. if(!isset($CNF['URI_VIEW_FRIENDS']))
  131. return '';
  132. if(($oProfile = $this->_oModule->getProfileByCurrentUrl()) !== false)
  133. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', [
  134. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_FRIENDS'] . '&profile_id=' . $oProfile->id())),
  135. 'title' => '',
  136. 'content' => BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContentCount($oProfile->id(), true)
  137. ]);
  138. return '';
  139. }
  140. protected function genCustomViewRowValueFollowersCount($aInput)
  141. {
  142. $CNF = &$this->_oModule->_oConfig->CNF;
  143. if(!isset($CNF['URI_VIEW_SUBSCRIPTIONS']))
  144. return '';
  145. if(($oProfile = $this->_oModule->getProfileByCurrentUrl()) !== false)
  146. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  147. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_SUBSCRIPTIONS'] . '&profile_id=' . $oProfile->id())),
  148. 'title' => '',
  149. 'content' => BxDolConnection::getObjectInstance('sys_profiles_subscriptions')->getConnectedInitiatorsCount($oProfile->id())
  150. ));
  151. return '';
  152. }
  153. private function genCustomViewRowValueProfileEmailOrIp($aInput)
  154. {
  155. $CNF = &$this->_oModule->_oConfig->CNF;
  156. if(empty($aInput['value']))
  157. return '';
  158. $sValue = $aInput['value'];
  159. $sModuleAccounts = 'bx_accounts';
  160. if(!BxDolModuleQuery::getInstance()->isEnabledByName($sModuleAccounts))
  161. return $sValue;
  162. $oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
  163. if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION']))
  164. return $sValue;
  165. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  166. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION'], array(
  167. 'filter' => urlencode($sValue)
  168. ))),
  169. 'title' => '',
  170. 'content' => $sValue
  171. ));
  172. }
  173. protected function genCustomViewRowValueProfileStatus($aInput)
  174. {
  175. $CNF = &$this->_oModule->_oConfig->CNF;
  176. if(empty($aInput['value']))
  177. return '';
  178. $sStatus = _t('_sys_profile_status_' . $aInput['value']);
  179. if(empty($CNF['URL_MANAGE_ADMINISTRATION']) || empty($CNF['FIELD_TITLE']) || empty($this->aInputs[$CNF['FIELD_TITLE']]['value']))
  180. return $sStatus;
  181. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  182. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE_ADMINISTRATION'], array(
  183. 'filter' => urlencode($this->aInputs[$CNF['FIELD_TITLE']]['value'])
  184. ))),
  185. 'title' => '',
  186. 'content' => $sStatus
  187. ));
  188. }
  189. protected function genCustomViewRowValueProfileLastActive($aInput)
  190. {
  191. $iLastActiveSession = 0;
  192. if(!empty($this->_iContentId) && ($oProfile = BxDolProfile::getInstanceByContentAndType($this->_iContentId, $this->MODULE)) !== false)
  193. $iLastActiveSession = (new BxDolSessionQuery())->getLastActivityAccount($oProfile->getAccountId());
  194. $iLastActive = max($iLastActiveSession, (int)$aInput['value']);
  195. return !empty($iLastActive) ? bx_time_js($iLastActive) : '';
  196. }
  197. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  198. {
  199. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  200. $bResult = (int)$this->_oModule->_oDb->updateContentPictureById($iContentId, 0/*$iProfileId*/, $iFileId, $sPictureField) > 0;
  201. if(!$bResult)
  202. return;
  203. $this->_oModule->onUpdateImage($iContentId, $sPictureField, $iFileId, $iProfileId);
  204. }
  205. protected function _getProfilePhotoGhostTmplVars($sField, $aContentInfo = array())
  206. {
  207. $CNF = &$this->_oModule->_oConfig->CNF;
  208. return [
  209. 'name' => $this->aInputs[$sField]['name'],
  210. 'content_id' => $this->aInputs[$sField]['content_id'],
  211. 'bx_if:set_thumb' => [
  212. 'condition' => false,
  213. 'content' => [],
  214. ]
  215. ];
  216. }
  217. protected function _isAdmin ($iContentId = 0)
  218. {
  219. if (parent::_isAdmin ($iContentId))
  220. return true;
  221. if (!$iContentId || !($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
  222. return false;
  223. return CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);
  224. }
  225. protected function _getPrivacyFields($aKeysF2O = array())
  226. {
  227. if(empty($aKeysF2O))
  228. $aKeysF2O = array(
  229. 'FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW',
  230. 'FIELD_ALLOW_POST_TO' => 'OBJECT_PRIVACY_POST',
  231. 'FIELD_ALLOW_CONTACT_TO' => 'OBJECT_PRIVACY_CONTACT'
  232. );
  233. return parent::_getPrivacyFields($aKeysF2O);
  234. }
  235. }
  236. /** @} */