BxBaseModProfileFormEntry.php 17 KB

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