BxBaseModProfileFormEntry.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_BADGE'],
  56. ];
  57. }
  58. if (($sKey = 'FIELD_BADGE_LINK_SELECT') && !empty($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]])) {
  59. $oProfile = BxDolProfile::getInstance();
  60. $iProfile = $oProfile->id();
  61. $aValues = [
  62. ['key' => '', 'value' => _t('_sys_please_select')]
  63. ];
  64. $aContexts = bx_srv('system', 'get_modules_by_type', ['context']);
  65. foreach($aContexts as $aContext) {
  66. $aCpIds = bx_srv($aContext['name'], 'get_participating_profiles', [$iProfile]);
  67. foreach($aCpIds as $iCpId)
  68. if(($sCpLink = $oProfile->getUrl($iCpId)) && ($sCpName = $oProfile->getDisplayName($iCpId)))
  69. $aValues[] = [
  70. 'key' => $sCpLink,
  71. 'value' => $sCpName
  72. ];
  73. }
  74. $this->aInputs[$CNF[$sKey]] = array_merge($this->aInputs[$CNF[$sKey]], [
  75. 'values' => $aValues
  76. ]);
  77. }
  78. if (($sKey = 'FIELD_BADGE_LINK_CUSTOM') && !empty($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]])) {
  79. if(!empty($this->aInputs[$CNF[$sKey]]['attrs']['placeholder']))
  80. $this->aInputs[$CNF[$sKey]]['attrs']['placeholder'] = _t($this->aInputs[$CNF[$sKey]]['attrs']['placeholder']);
  81. }
  82. foreach ($this->_aImageFields as $sField => $aParams) {
  83. $this->aInputs[$sField]['storage_object'] = $aParams['storage_object'];
  84. $this->aInputs[$sField]['uploaders'] = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $aParams['uploaders'];
  85. $this->aInputs[$sField]['images_transcoder'] = $aParams['images_transcoder'];
  86. $this->aInputs[$sField]['uploaders_id'] = isset($aParams['uploaders_id']) ? $aParams['uploaders_id'] : '';
  87. $this->aInputs[$sField]['storage_private'] = 0;
  88. $this->aInputs[$sField]['multiple'] = false;
  89. $this->aInputs[$sField]['content_id'] = 0;
  90. $this->aInputs[$sField]['ghost_template'] = '';
  91. }
  92. if(($sField = 'friends_count') && isset($this->aInputs[$sField]) && !$this->_oModule->_oConfig->isFriends())
  93. unset($this->aInputs[$sField]);
  94. $oAccountProfile = BxDolProfile::getInstanceAccountProfile();
  95. if ($oAccountProfile)
  96. $this->_iAccountProfileId = $oAccountProfile->id();
  97. }
  98. public function getUploadersInfo($sField = '')
  99. {
  100. if(empty($sField))
  101. return $this->_aUploadersInfo;
  102. $aUploaders = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $this->_oModule->_oConfig->getUploaders($sField);
  103. return $this->_aUploadersInfo[array_shift($aUploaders)];
  104. }
  105. function initChecker ($aValues = array (), $aSpecificValues = array())
  106. {
  107. $CNF = &$this->_oModule->_oConfig->CNF;
  108. $aContentInfo = isset($CNF['FIELD_ID']) && isset($aValues[$CNF['FIELD_ID']]) ? $this->_oModule->_oDb->getContentInfoById ($aValues[$CNF['FIELD_ID']]) : array();
  109. foreach ($this->_aImageFields as $sField => $aParams) {
  110. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  111. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  112. $this->aInputs[$sField]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $this->_getProfilePhotoGhostTmplVars($sField, $aContentInfo));
  113. }
  114. parent::initChecker($aValues, $aSpecificValues);
  115. 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']))
  116. $this->aInputs[$CNF[$sField]]['value'] = !is_array($sValue) ? explode(',', $sValue) : [];
  117. if(($sDisplay = 'OBJECT_FORM_ENTRY_DISPLAY_EDIT_BADGE') && !empty($CNF[$sDisplay]) && $this->aParams['display'] == $CNF[$sDisplay]) {
  118. $sBadgeLink = $aContentInfo[$CNF['FIELD_BADGE_LINK']] ?? '';
  119. $bFilledIn = false;
  120. if(($sKey = 'FIELD_BADGE_LINK_SELECT') && !empty($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]))
  121. foreach($this->aInputs[$CNF[$sKey]]['values'] as $aValue)
  122. if($aValue['key'] == $sBadgeLink) {
  123. $this->aInputs[$CNF[$sKey]]['value'] = $sBadgeLink;
  124. $bFilledIn = true;
  125. break;
  126. }
  127. if(!$bFilledIn && ($sKey = 'FIELD_BADGE_LINK_CUSTOM') && !empty($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]))
  128. $this->aInputs[$CNF[$sKey]]['value'] = $sBadgeLink;
  129. }
  130. }
  131. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  132. {
  133. $CNF = &$this->_oModule->_oConfig->CNF;
  134. if(($sField = 'FIELD_STG_TABS') && !empty($CNF[$sField]) && empty($aValsToAdd[$CNF[$sField]])) {
  135. $mixedValue = $this->getCleanValue($CNF[$sField]);
  136. if(is_array($mixedValue))
  137. self::setSubmittedValue($CNF[$sField], implode(',', $mixedValue), $this->aFormAttrs['method']);
  138. }
  139. if((($sFldS = 'FIELD_BADGE_LINK_SELECT') && !empty($CNF[$sFldS]) && ($sValS = $this->getCleanValue($CNF[$sFldS]))) || (($sFldC = 'FIELD_BADGE_LINK_CUSTOM') && !empty($CNF[$sFldC]) && ($sValC = $this->getCleanValue($CNF[$sFldC])))) {
  140. $aValsToAdd[$CNF['FIELD_BADGE_LINK']] = $sValS ?: ($sValC ?: '');
  141. }
  142. return parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  143. }
  144. function delete ($iContentId, $aContentInfo = array())
  145. {
  146. $CNF = &$this->_oModule->_oConfig->CNF;
  147. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  148. foreach ($this->_aImageFields as $sField => $aParams) {
  149. $oStorage = BxDolStorage::getObjectInstance($aParams['storage_object']);
  150. $aFiles = $oStorage->getGhosts($iProfileId, $iContentId);
  151. foreach ($aFiles as $aFile) {
  152. if (!$oStorage->getFile($aFile['id']))
  153. continue;
  154. $bRet = $oStorage->deleteFile($aFile['id'], $this->_iAccountProfileId);
  155. }
  156. }
  157. return parent::delete($iContentId, $aContentInfo);
  158. }
  159. protected function genCustomViewRowValueProfileEmail($aInput)
  160. {
  161. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  162. }
  163. protected function genCustomViewRowValueProfileIp($aInput)
  164. {
  165. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  166. }
  167. protected function genCustomViewRowValueFriendsCount($aInput)
  168. {
  169. $CNF = &$this->_oModule->_oConfig->CNF;
  170. if(!isset($CNF['URI_VIEW_FRIENDS']))
  171. return '';
  172. if(($oProfile = $this->_oModule->getProfileByCurrentUrl()) !== false)
  173. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', [
  174. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_FRIENDS'] . '&profile_id=' . $oProfile->id())),
  175. 'title' => '',
  176. 'content' => BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContentCount($oProfile->id(), true)
  177. ]);
  178. return '';
  179. }
  180. protected function genCustomViewRowValueFollowersCount($aInput)
  181. {
  182. $CNF = &$this->_oModule->_oConfig->CNF;
  183. if(!isset($CNF['URI_VIEW_SUBSCRIPTIONS']))
  184. return '';
  185. if(($oProfile = $this->_oModule->getProfileByCurrentUrl()) !== false)
  186. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  187. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_SUBSCRIPTIONS'] . '&profile_id=' . $oProfile->id())),
  188. 'title' => '',
  189. 'content' => BxDolConnection::getObjectInstance('sys_profiles_subscriptions')->getConnectedInitiatorsCount($oProfile->id())
  190. ));
  191. return '';
  192. }
  193. private function genCustomViewRowValueProfileEmailOrIp($aInput)
  194. {
  195. $CNF = &$this->_oModule->_oConfig->CNF;
  196. if(empty($aInput['value']))
  197. return '';
  198. $sValue = $aInput['value'];
  199. $sModuleAccounts = 'bx_accounts';
  200. if(!BxDolModuleQuery::getInstance()->isEnabledByName($sModuleAccounts))
  201. return $sValue;
  202. $oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
  203. if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION']))
  204. return $sValue;
  205. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  206. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION'], array(
  207. 'filter' => urlencode($sValue)
  208. ))),
  209. 'title' => '',
  210. 'content' => $sValue
  211. ));
  212. }
  213. protected function genCustomViewRowValueProfileStatus($aInput)
  214. {
  215. $CNF = &$this->_oModule->_oConfig->CNF;
  216. if(empty($aInput['value']))
  217. return '';
  218. $sStatus = _t('_sys_profile_status_' . $aInput['value']);
  219. if(empty($CNF['URL_MANAGE_ADMINISTRATION']) || empty($CNF['FIELD_TITLE']) || empty($this->aInputs[$CNF['FIELD_TITLE']]['value']))
  220. return $sStatus;
  221. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  222. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE_ADMINISTRATION'], array(
  223. 'filter' => urlencode($this->aInputs[$CNF['FIELD_TITLE']]['value'])
  224. ))),
  225. 'title' => '',
  226. 'content' => $sStatus
  227. ));
  228. }
  229. protected function genCustomViewRowValueProfileLastActive($aInput)
  230. {
  231. $iLastActiveSession = 0;
  232. if(!empty($this->_iContentId) && ($oProfile = BxDolProfile::getInstanceByContentAndType($this->_iContentId, $this->MODULE)) !== false)
  233. $iLastActiveSession = (new BxDolSessionQuery())->getLastActivityAccount($oProfile->getAccountId());
  234. $iLastActive = max($iLastActiveSession, (int)$aInput['value']);
  235. return !empty($iLastActive) ? bx_time_js($iLastActive) : '';
  236. }
  237. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  238. {
  239. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  240. $bResult = (int)$this->_oModule->_oDb->updateContentPictureById($iContentId, 0/*$iProfileId*/, $iFileId, $sPictureField) > 0;
  241. if(!$bResult)
  242. return;
  243. $this->_oModule->onUpdateImage($iContentId, $sPictureField, $iFileId, $iProfileId);
  244. }
  245. protected function _getProfilePhotoGhostTmplVars($sField, $aContentInfo = array())
  246. {
  247. $CNF = &$this->_oModule->_oConfig->CNF;
  248. return [
  249. 'name' => $this->aInputs[$sField]['name'],
  250. 'content_id' => $this->aInputs[$sField]['content_id'],
  251. 'bx_if:set_thumb' => [
  252. 'condition' => false,
  253. 'content' => [],
  254. ]
  255. ];
  256. }
  257. protected function _isAdmin ($iContentId = 0)
  258. {
  259. if (parent::_isAdmin ($iContentId))
  260. return true;
  261. if (!$iContentId || !($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
  262. return false;
  263. return CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);
  264. }
  265. protected function _getPrivacyFields($aKeysF2O = array())
  266. {
  267. if(empty($aKeysF2O))
  268. $aKeysF2O = array(
  269. 'FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW',
  270. 'FIELD_ALLOW_POST_TO' => 'OBJECT_PRIVACY_POST',
  271. 'FIELD_ALLOW_CONTACT_TO' => 'OBJECT_PRIVACY_CONTACT'
  272. );
  273. return parent::_getPrivacyFields($aKeysF2O);
  274. }
  275. }
  276. /** @} */