1
0

BxBaseModProfileFormEntry.php 17 KB

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