BxBaseServiceProfiles.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System service for profiles handling functionality.
  11. */
  12. class BxBaseServiceProfiles extends BxDol
  13. {
  14. protected $_bIsApi;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->_bIsApi = bx_is_api();
  19. }
  20. public function serviceGetProfileTypes()
  21. {
  22. $aTypes = array(
  23. '' => _t('_Select_one')
  24. );
  25. $aModules = $this->_getProfilesModules();
  26. foreach($aModules as $aModule) {
  27. if(empty($aModule) || !is_array($aModule))
  28. continue;
  29. $sTitleKey = '_' . $aModule['name'];
  30. $sTitle = _t($sTitleKey);
  31. $aTypes[$aModule['name']] = !empty($sTitle) && $sTitle != $sTitleKey ? $sTitle : $aModule['title'];
  32. }
  33. return $aTypes;
  34. }
  35. public function serviceProfileAvatar ($iProfileId = 0)
  36. {
  37. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  38. return '';
  39. $oProfile = BxDolProfile::getInstance($iProfileId);
  40. if(!$oProfile)
  41. return '';
  42. if(bx_is_api())
  43. return [
  44. bx_api_get_block ('profile_avatar', $oProfile->getData())
  45. ];
  46. $oTemplate = BxDolTemplate::getInstance();
  47. $sSwitcher = '';
  48. $aSwitcher = bx_srv('system', 'account_profile_switcher', array(false, null, '', true), 'TemplServiceProfiles');
  49. if($aSwitcher !== false)
  50. $sSwitcher = BxTemplFunctions::getInstance()->popupBox('bx-profile-switcher', _t('_sys_txt_switch_profiles'), $oTemplate->parseHtmlByName('profile_avatar_switcher.html', array(
  51. 'profile_switcher' => $aSwitcher['content'],
  52. 'bx_if:multiple_profiles_mode' => array(
  53. 'condition' => empty($aSwitcher['content']) || BxDolAccount::isAllowedCreateMultiple($oProfile->id()),
  54. 'content' => array(
  55. 'url_switch_profile' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher')
  56. )
  57. )
  58. )), true);
  59. $bSwitcher = !empty($sSwitcher);
  60. $sSwitcherUrl = $bSwitcher ? 'javascript:void(0)' : $sUrl;
  61. $sSwitcherOnclick = $bSwitcher ? "javascript:$('#bx-profile-switcher').dolPopup({});" : "";
  62. $sDisplayName = $oProfile->getDisplayName();
  63. $sUrl = $oProfile->getUrl();
  64. $oAcl = BxDolAcl::getInstance();
  65. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  66. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  67. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
  68. $aVars = array(
  69. 'profile_id' => $oProfile->id(),
  70. 'profile_url' => $sUrl,
  71. 'profile_edit_url' => $oProfile->getEditUrl(),
  72. 'profile_title' => $sDisplayName,
  73. 'profile_title_attr' => bx_html_attribute($sDisplayName),
  74. 'profile_ava_url' => $oProfile->getAvatar(),
  75. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  76. 'name' => 'unit_wo_info_links',
  77. 'size' => 'thumb'
  78. ))),
  79. 'profile_acl_title' => _t($aAclInfo['name']),
  80. 'bx_if:image' => array (
  81. 'condition' => (bool)$sIconUrl,
  82. 'content' => array('icon_url' => $sIconUrl),
  83. ),
  84. 'bx_if:image_inline' => array (
  85. 'condition' => false,
  86. 'content' => array('image' => ''),
  87. ),
  88. 'bx_if:icon' => array (
  89. 'condition' => (bool)$sIcon,
  90. 'content' => array('icon' => $sIcon),
  91. ),
  92. 'bx_if:icon-a' => array (
  93. 'condition' => (bool)$sIconA,
  94. 'content' => array('icon-a' => $sIconA),
  95. ),
  96. 'bx_if:icon-html' => array (
  97. 'condition' => (bool)$sIconHtml,
  98. 'content' => array('icon' => $sIconHtml),
  99. ),
  100. 'switcher_url' => $sSwitcherUrl,
  101. 'switcher_onclick' => $sSwitcherOnclick,
  102. 'bx_if:show_switcher_icon' => array(
  103. 'condition' => $bSwitcher,
  104. 'content' => array(
  105. 'switcher_url' => $sSwitcherUrl,
  106. 'switcher_onclick' => $bSwitcher ? "javascript:$('#bx-profile-switcher').dolPopup({});" : "",
  107. )
  108. ),
  109. 'switcher' => $sSwitcher
  110. );
  111. return $oTemplate->parseHtmlByName('profile_avatar.html', $aVars);
  112. }
  113. public function serviceProfileMenu ($iProfileId = 0)
  114. {
  115. if(($oMenu = BxDolMenu::getObjectInstance('sys_profile_stats')) !== false) {
  116. if(bx_is_api())
  117. return [
  118. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI())
  119. ];
  120. return $oMenu->getCode();
  121. }
  122. else
  123. return '';
  124. }
  125. public function serviceProfileFollowings ($iProfileId = 0)
  126. {
  127. if(($oMenu = BxDolMenu::getObjectInstance('sys_profile_followings')) !== false)
  128. return $oMenu->getCode();
  129. else
  130. return '';
  131. }
  132. public function serviceProfileStats ($iProfileId = 0)
  133. {
  134. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  135. return '';
  136. $oProfile = BxDolProfile::getInstance($iProfileId);
  137. if(!$oProfile)
  138. return '';
  139. $oMenu = BxDolMenu::getObjectInstance('sys_profile_stats');
  140. if(!$oMenu)
  141. return '';
  142. if(bx_is_api())
  143. return [
  144. bx_api_get_block ('profile_avatar', $oProfile->getData(), ['id' => 1]),
  145. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI(), ['id' => 2])
  146. ];
  147. $oAcl = BxDolAcl::getInstance();
  148. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  149. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  150. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
  151. $aVars = array(
  152. 'profile_id' => $oProfile->id(),
  153. 'profile_url' => $oProfile->getUrl(),
  154. 'profile_edit_url' => $oProfile->getEditUrl(),
  155. 'profile_title' => $oProfile->getDisplayName(),
  156. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  157. 'profile_ava_url' => $oProfile->getAvatar(),
  158. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  159. 'name' => 'unit_wo_info',
  160. 'size' => 'ava'
  161. ))),
  162. 'profile_acl_title' => _t($aAclInfo['name']),
  163. 'menu' => $oMenu->getCode(),
  164. );
  165. $aVars['bx_if:image'] = array (
  166. 'condition' => (bool)$sIconUrl,
  167. 'content' => array('icon_url' => $sIconUrl),
  168. );
  169. $aVars['bx_if:image_inline'] = array (
  170. 'condition' => false,
  171. 'content' => array('image' => ''),
  172. );
  173. $aVars['bx_if:icon'] = array (
  174. 'condition' => (bool)$sIcon,
  175. 'content' => array('icon' => $sIcon),
  176. );
  177. $aVars['bx_if:icon-html'] = array (
  178. 'condition' => (bool)$sIconHtml,
  179. 'content' => array('icon' => $sIconHtml),
  180. );
  181. $aVars['bx_if:icon-a'] = array (
  182. 'condition' => (bool)$sIconA,
  183. 'content' => array('icon-a' => $sIconA),
  184. );
  185. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  186. }
  187. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  188. {
  189. /**
  190. * Disabled.
  191. */
  192. return '';
  193. if(empty($sCaption))
  194. $sCaption = _t('_Edit');
  195. $oProfile = BxDolProfile::getInstance($iProfileId);
  196. if(!$oProfile)
  197. return '';
  198. $sModule = $oProfile->getModule();
  199. $sMethod = 'profile_edit_url';
  200. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  201. return '';
  202. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  203. }
  204. public function serviceGetMenuAddonProfileConnections($iProfileId = 0)
  205. {
  206. $oProfile = BxDolProfile::getInstance($iProfileId);
  207. if(!$oProfile)
  208. return '';
  209. $iCount = 0;
  210. //--- Add friend requests.
  211. $iCount += (int)bx_srv('system', 'get_unconfirmed_connections_num', ['sys_profiles_friends'], 'TemplServiceConnections');
  212. //--- Add subscriptions.
  213. $iCount += (int)bx_srv('system', 'get_connected_content_num', ['sys_profiles_subscriptions'], 'TemplServiceConnections');
  214. //--- Add subscribed me.
  215. $iCount += (int)bx_srv('system', 'get_connected_initiators_num', ['sys_profiles_subscriptions'], 'TemplServiceConnections');
  216. return $iCount > 0 ? $iCount : '';
  217. }
  218. /**
  219. * @page service Service Calls
  220. * @section bx_system_general System Services
  221. * @subsection bx_system_general-profiles Profiles
  222. * @subsubsection bx_system_general-profile_membership profile_membership
  223. *
  224. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  225. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  226. *
  227. * Get membership level for specified profile.
  228. * @param $iProfileId profile ID
  229. *
  230. * @see BxBaseServiceProfiles::serviceProfileMembership
  231. */
  232. /**
  233. * @ref bx_system_general-profile_membership "profile_membership"
  234. */
  235. public function serviceProfileMembership ($iProfileId = 0)
  236. {
  237. if (!$iProfileId)
  238. $iProfileId = bx_get_logged_profile_id();
  239. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  240. }
  241. public function serviceProfileMembershipStats ($iProfileId = 0)
  242. {
  243. if(!$iProfileId)
  244. $iProfileId = bx_get_logged_profile_id();
  245. $sTxtUnlimited = _t('_unlimited');
  246. $sTxtFiles = _t('_sys_storage_files');
  247. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  248. $aTmplVarsStats = array();
  249. if(!empty($aQuota['quota_size'])) {
  250. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  251. $sPercent = $iWidth . '%';
  252. }
  253. else {
  254. $iWidth = 0;
  255. $sPercent = $sTxtUnlimited;
  256. }
  257. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  258. if(!empty($aQuota['quota_number'])) {
  259. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  260. $sPercent = $iWidth . '%';
  261. }
  262. else {
  263. $iWidth = 0;
  264. $sPercent = $sTxtUnlimited;
  265. }
  266. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  267. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  268. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  269. 'bx_repeat:stat_items' => $aTmplVarsStats
  270. ));
  271. }
  272. /**
  273. * @page service Service Calls
  274. * @section bx_system_general System Services
  275. * @subsection bx_system_general-profiles Profiles
  276. * @subsubsection bx_system_general-profile_notifications profile_notifications
  277. *
  278. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  279. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  280. *
  281. * Get number of unread notifications for logged in profile
  282. *
  283. * @see BxBaseServiceProfiles::serviceProfileNotifications
  284. */
  285. /**
  286. * @ref bx_system_general-profile_notifications "profile_notifications"
  287. */
  288. public function serviceProfileNotifications ($iProfileId = 0)
  289. {
  290. if (!$iProfileId)
  291. $iProfileId = bx_get_logged_profile_id();
  292. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  293. if(!$oMenu)
  294. return 0;
  295. $iNum = 0;
  296. $aMenuItems = $oMenu->getMenuItems ();
  297. foreach ($aMenuItems as $r) {
  298. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  299. $iNum += $r['bx_if:addon']['content']['addon'];
  300. }
  301. return $iNum;
  302. }
  303. /**
  304. * @page service Service Calls
  305. * @section bx_system_general System Services
  306. * @subsection bx_system_general-profiles Profiles
  307. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  308. *
  309. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  310. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  311. *
  312. * Get number of online profiles
  313. *
  314. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  315. */
  316. /**
  317. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  318. */
  319. public function serviceGetCountOnlineProfiles ()
  320. {
  321. $oProfilesQuery = BxDolProfileQuery::getInstance();
  322. return $oProfilesQuery->getOnlineCount();
  323. }
  324. public function serviceGetProfilesModules ($bForceActAsProfile = true)
  325. {
  326. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  327. $oDb = BxDolDb::getInstance();
  328. $oCache = $oDb->getDbCacheObject ();
  329. $sKey = $oDb->genDbCacheKey('profiles_' . (!$bForceActAsProfile ? 'all_' : '') . 'modules_array');
  330. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  331. $mixedRetTs = $oCache->getData($sKeyTs);
  332. $mixedRet = $oCache->getData($sKey);
  333. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  334. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  335. $aModulesArray = $mixedRet;
  336. } else {
  337. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  338. $oCache->setData($sKey, $aModulesArray);
  339. $oCache->setData($sKeyTs, $iNewestModuleTs);
  340. }
  341. } else {
  342. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  343. }
  344. return $aModulesArray;
  345. }
  346. public function serviceProfilesFriends ($iLimit = 20)
  347. {
  348. if (!($iProfileId = bx_get_logged_profile_id()))
  349. return array();
  350. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  351. if (!$oConnection)
  352. return array();
  353. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  354. return array();
  355. $aRet = array();
  356. foreach ($a as $iId) {
  357. $oProfile = BxDolProfile::getInstance($iId);
  358. $aRet[] = array (
  359. 'label' => $oProfile->getDisplayName(),
  360. 'value' => $iId,
  361. 'url' => $oProfile->getUrl(),
  362. 'thumb' => $oProfile->getThumb(),
  363. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  364. );
  365. }
  366. return $aRet;
  367. }
  368. public function serviceProfilesSearch ($sTerm, $mixedParems = [])
  369. {
  370. $iLimit = 20;
  371. if(is_int($mixedParems))
  372. $iLimit = (int)$mixedParems;
  373. else if(is_array($mixedParems) && isset($mixedParems['limit']))
  374. $iLimit = (int)$mixedParems['limit'];
  375. // display friends by default
  376. if (!$sTerm)
  377. return $this->serviceProfilesFriends($iLimit);
  378. // get list of "profiles" modules
  379. $aModules = $this->serviceGetProfilesModules();
  380. // search in each module
  381. $a = array();
  382. foreach ($aModules as $aModule) {
  383. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  384. continue;
  385. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  386. }
  387. // sort result
  388. usort($a, function($r1, $r2) {
  389. return strcmp($r1['label'], $r2['label']);
  390. });
  391. bx_alert('system', 'profiles_search', 0, 0, array(
  392. 'module' => is_array($mixedParems) && isset($mixedParems['module']) ? $mixedParems['module'] : '',
  393. 'term' => $sTerm,
  394. 'result' => &$a
  395. ));
  396. // return as array
  397. return array_slice($a, 0, $iLimit);
  398. }
  399. public function serviceProfilesList ($iAccountId = 0)
  400. {
  401. $oProfilesQuery = BxDolProfileQuery::getInstance();
  402. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  403. if (!$aProfiles)
  404. return false;
  405. $s = '';
  406. foreach ($aProfiles as $aProfile)
  407. if ($aProfile['type'] != 'system')
  408. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  409. if (!$s)
  410. $s = MsgBox(_t('_sys_txt_empty'));
  411. return $s . '<div class="bx-clear"></div>';
  412. }
  413. public function serviceBrowseFriends ($iProfileId = 0, $aParams = [])
  414. {
  415. if($this->_bIsApi)
  416. $aParams = bx_api_get_browse_params($aParams);
  417. if(!$iProfileId)
  418. $iProfileId = bx_get_logged_profile_id();
  419. if(!$iProfileId)
  420. return '';
  421. $aParams = array_merge([
  422. 'params' => false,
  423. 'design_box' => BX_DB_PADDING_DEF,
  424. 'empty_message' => false,
  425. 'ajax_paginate' => true,
  426. ], $aParams);
  427. $aParamsBrowse = ['profile' => $iProfileId];
  428. if(!empty($aParams['params']) && is_array($aParams['params']))
  429. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  430. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  431. $bEmpty = (bool)$aParams['empty_message'];
  432. $mixedResult = $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  433. if(empty($mixedResult) && $bEmpty)
  434. $mixedResult = MsgBox($sEmpty);
  435. return $mixedResult;
  436. }
  437. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  438. {
  439. if($this->_bIsApi)
  440. $aParams = bx_api_get_browse_params($aParams);
  441. if(!$iProfileId)
  442. $iProfileId = bx_get_logged_profile_id();
  443. if(!$iProfileId)
  444. return '';
  445. $aParams = array_merge([
  446. 'params' => false,
  447. 'design_box' => BX_DB_PADDING_DEF,
  448. 'empty_message' => false,
  449. 'ajax_paginate' => true,
  450. ], $aParams);
  451. $aParamsBrowse = ['profile' => $iProfileId];
  452. if(!empty($aParams['params']) && is_array($aParams['params']))
  453. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  454. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  455. $bEmpty = (bool)$aParams['empty_message'];
  456. $mixedResult = $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  457. if(empty($mixedResult) && $bEmpty)
  458. $mixedResult = MsgBox($sEmpty);
  459. return $mixedResult;
  460. }
  461. public function serviceBrowseFriendRequested ($iProfileId = 0, $aParams = [])
  462. {
  463. if($this->_bIsApi)
  464. $aParams = bx_api_get_browse_params($aParams);
  465. if(!$iProfileId)
  466. $iProfileId = bx_get_logged_profile_id();
  467. if(!$iProfileId)
  468. return '';
  469. $aParams = array_merge([
  470. 'params' => false,
  471. 'design_box' => BX_DB_PADDING_DEF,
  472. 'empty_message' => false,
  473. 'ajax_paginate' => true,
  474. ], $aParams);
  475. $aParamsBrowse = ['profile' => $iProfileId];
  476. if(!empty($aParams['params']) && is_array($aParams['params']))
  477. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  478. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  479. $bEmpty = (bool)$aParams['empty_message'];
  480. $mixedResult = $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  481. if(empty($mixedResult) && $bEmpty)
  482. $mixedResult = MsgBox($sEmpty);
  483. return $mixedResult;
  484. }
  485. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  486. {
  487. if($this->_bIsApi)
  488. $aParams = bx_api_get_browse_params($aParams);
  489. if(!$iProfileId)
  490. $iProfileId = bx_get_logged_profile_id();
  491. if(!$iProfileId)
  492. return '';
  493. $aParams = array_merge([
  494. 'params' => false,
  495. 'design_box' => BX_DB_PADDING_DEF,
  496. 'empty_message' => false,
  497. 'ajax_paginate' => true,
  498. ], $aParams);
  499. $aParamsBrowse = ['profile' => $iProfileId];
  500. if(!empty($aParams['params']) && is_array($aParams['params']))
  501. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  502. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  503. $bEmpty = (bool)$aParams['empty_message'];
  504. $mixedResult = $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  505. if(empty($mixedResult) && $bEmpty)
  506. $mixedResult = MsgBox($sEmpty);
  507. return $mixedResult;
  508. }
  509. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  510. {
  511. if($this->_bIsApi)
  512. $aParams = bx_api_get_browse_params($aParams);
  513. if(!$iProfileId)
  514. $iProfileId = bx_get_logged_profile_id();
  515. if(!$iProfileId)
  516. return '';
  517. $aParams = array_merge([
  518. 'params' => false,
  519. 'design_box' => BX_DB_PADDING_DEF,
  520. 'empty_message' => false,
  521. 'ajax_paginate' => true,
  522. ], $aParams);
  523. $aParamsBrowse = ['profile' => $iProfileId];
  524. if(!empty($aParams['params']) && is_array($aParams['params']))
  525. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  526. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  527. $bEmpty = (bool)$aParams['empty_message'];
  528. $mixedResult = $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  529. if(empty($mixedResult) && $bEmpty)
  530. $mixedResult = MsgBox($sEmpty);
  531. return $mixedResult;
  532. }
  533. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  534. {
  535. if($this->_bIsApi)
  536. $aParams = bx_api_get_browse_params($aParams);
  537. if(!$iProfileId)
  538. $iProfileId = bx_get_logged_profile_id();
  539. if(!$iProfileId)
  540. return '';
  541. $aParams = array_merge([
  542. 'params' => false,
  543. 'design_box' => BX_DB_PADDING_DEF,
  544. 'empty_message' => false,
  545. 'ajax_paginate' => true,
  546. ], $aParams);
  547. $aParamsBrowse = [
  548. 'profile' => $iProfileId,
  549. 'object' => $sObject
  550. ];
  551. if(!empty($aParams['params']) && is_array($aParams['params']))
  552. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  553. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  554. $bEmpty = (bool)$aParams['empty_message'];
  555. $mixedResult = $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  556. if(empty($mixedResult) && $bEmpty)
  557. $mixedResult = MsgBox($sEmpty);
  558. return $mixedResult;
  559. }
  560. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  561. {
  562. if($this->_bIsApi)
  563. $aParams = bx_api_get_browse_params($aParams);
  564. if(!$iProfileId)
  565. $iProfileId = bx_get_logged_profile_id();
  566. if(!$iProfileId)
  567. return '';
  568. $aParams = array_merge([
  569. 'params' => false,
  570. 'design_box' => BX_DB_PADDING_DEF,
  571. 'empty_message' => false,
  572. 'ajax_paginate' => true,
  573. ], $aParams);
  574. $aParamsBrowse = [
  575. 'profile' => $iProfileId,
  576. 'object' => 'sys_profiles_friends',
  577. 'type' => 'content',
  578. 'mutual' => false,
  579. 'profile2' => 0
  580. ];
  581. if(!empty($aParams['params']) && is_array($aParams['params']))
  582. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  583. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  584. $bEmpty = (bool)$aParams['empty_message'];
  585. $mixedResult = $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  586. if(empty($mixedResult) && $bEmpty)
  587. $mixedResult = MsgBox($sEmpty);
  588. return $mixedResult;
  589. }
  590. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  591. {
  592. if($this->_bIsApi)
  593. $aParams = bx_api_get_browse_params($aParams, true);
  594. if(!$iProfileId)
  595. $iProfileId = bx_get_logged_profile_id();
  596. if(!$iProfileId)
  597. return '';
  598. $aParams = array_merge([
  599. 'empty_message' => false,
  600. 'start' => 0,
  601. 'per_page' => 0
  602. ], $aParams);
  603. if(($iStartGet = bx_get('start')) !== false)
  604. $aParams['start'] = (int)$iStartGet;
  605. if(($iPerPageGet = bx_get('per_page')) !== false)
  606. $aParams['per_page'] = (int)$iPerPageGet;
  607. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  608. if(!$oRecommendation)
  609. return false;
  610. if(bx_is_api()) {
  611. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams['start'], $aParams['per_page']);
  612. $aData = array_merge($aData, [
  613. 'module' => 'system',
  614. 'unit' => 'mixed',
  615. 'request_url' => '/api.php?r=system/browse_recommendations_friends/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  616. ]);
  617. return [bx_api_get_block('browse', $aData)];
  618. }
  619. $sCode = $oRecommendation->getCode($iProfileId, $aParams['start'], $aParams['per_page']);
  620. if(!$sCode && $aParams['empty_message'])
  621. $sCode = MsgBox(_t(!empty($aParams['empty_message_text']) ? $aParams['empty_message_text'] : '_Empty'));
  622. return $sCode;
  623. }
  624. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  625. {
  626. if($this->_bIsApi)
  627. $aParams = bx_api_get_browse_params($aParams, true);
  628. if(!$iProfileId)
  629. $iProfileId = bx_get_logged_profile_id();
  630. if(!$iProfileId)
  631. return '';
  632. $aParams = array_merge([
  633. 'empty_message' => false,
  634. 'start' => 0,
  635. 'per_page' => 0
  636. ], $aParams);
  637. if(($iStartGet = bx_get('start')) !== false)
  638. $aParams['start'] = (int)$iStartGet;
  639. if(($iPerPageGet = bx_get('per_page')) !== false)
  640. $aParams['per_page'] = (int)$iPerPageGet;
  641. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  642. if(!$oRecommendation)
  643. return false;
  644. if(bx_is_api()) {
  645. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams['start'], $aParams['per_page']);
  646. $aData = array_merge($aData, [
  647. 'module' => 'system',
  648. 'unit' => 'mixed',
  649. 'request_url' => '/api.php?r=system/browse_recommendations_subscriptions/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  650. ]);
  651. return [bx_api_get_block('browse', $aData)];
  652. }
  653. $sCode = $oRecommendation->getCode($iProfileId, $aParams['start'], $aParams['per_page']);
  654. if(!$sCode && $aParams['empty_message'])
  655. $sCode = MsgBox(_t(!empty($aParams['empty_message_text']) ? $aParams['empty_message_text'] : '_Empty'));
  656. return $sCode;
  657. }
  658. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  659. {
  660. $oTemplate = BxDolTemplate::getInstance();
  661. $oProfilesQuery = BxDolProfileQuery::getInstance();
  662. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  663. if (!$aProfiles)
  664. return false;
  665. if (null === $iActiveProfileId)
  666. $iActiveProfileId = bx_get_logged_profile_id();
  667. $oModuleDb = BxDolModuleQuery::getInstance();
  668. $aVars = array (
  669. 'bx_repeat:row' => array(),
  670. );
  671. bx_alert('system', 'account_profile_switcher', 0, false, array(
  672. 'account_id' => $iAccountId,
  673. 'active_profile_id' => &$iActiveProfileId,
  674. 'url_profile_action' => &$sUrlProfileAction,
  675. 'show_all' => &$bShowAll,
  676. 'button_title' => &$sButtonTitle,
  677. 'profile_template' => &$sProfileTemplate,
  678. 'profiles' => &$aProfiles
  679. ));
  680. foreach ($aProfiles as $aProfile) {
  681. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  682. continue;
  683. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  684. continue;
  685. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  686. continue;
  687. if($this->_bIsApi){
  688. $o = BxDolProfile::getInstance($aProfile['id']);
  689. $aProfilesData[] = [
  690. 'id' => $o->id(),
  691. 'display_name' => $o->getDisplayName(),
  692. 'url' => bx_api_get_relative_url($o->getUrl()),
  693. 'avatar' => $o->getAvatar()
  694. ];
  695. }
  696. $aVars['bx_repeat:row'][] = array (
  697. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  698. 'bx_if:active' => array (
  699. 'condition' => $iActiveProfileId == $aProfile['id'],
  700. 'content' => array('id' => $aProfile['id']),
  701. ),
  702. 'bx_if:inactive' => array (
  703. 'condition' => $iActiveProfileId != $aProfile['id'],
  704. 'content' => array(
  705. 'id' => $aProfile['id'],
  706. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  707. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  708. ),
  709. ),
  710. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  711. );
  712. }
  713. if($this->_bIsApi){
  714. return [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
  715. }
  716. $oTemplate->addCss('account.css');
  717. return array(
  718. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  719. );
  720. }
  721. public function serviceAccountProfileSwitcherAll ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = true, $sButtonTitle = '', $sProfileTemplate = '')
  722. {
  723. return $this->serviceAccountProfileSwitcher($iAccountId, $iActiveProfileId, $sUrlProfileAction, $bShowAll, $sButtonTitle, $sProfileTemplate);
  724. }
  725. public function serviceProfileSettingsCfilter($iProfileId = false)
  726. {
  727. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  728. BxDolTemplate::getInstance()->displayPageNotFound();
  729. exit;
  730. }
  731. // set settings submenu
  732. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  733. if ($oMenuSubmenu) {
  734. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  735. 'title' => _t('_sys_menu_item_title_account_settings'),
  736. 'link' => BX_DOL_URL_ROOT . 'member.php',
  737. 'icon' => '',
  738. ));
  739. }
  740. if($iProfileId === false)
  741. $iProfileId = bx_get_logged_profile_id();
  742. $oProfile = BxDolProfile::getInstance($iProfileId);
  743. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  744. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  745. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  746. $sForm = 'sys_profile';
  747. $sFormDisplay = 'sys_profile_cf_set';
  748. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  749. if(!$oForm)
  750. return MsgBox(_t('_sys_txt_error_occured'));
  751. $oForm->initChecker($aProfileInfo);
  752. if(!$oForm->isSubmittedAndValid())
  753. return $sCode = $oForm->getCode();
  754. if(!$oForm->update($iProfileId)) {
  755. if (!$oForm->isValid())
  756. return $oForm->getCode();
  757. else
  758. return MsgBox(_t('_sys_txt_error_profile_update'));
  759. }
  760. // display result message
  761. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  762. return $sMsg . $oForm->getCode();
  763. }
  764. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  765. {
  766. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  767. return false;
  768. $iAge = 0;
  769. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  770. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  771. $iResult = 0;
  772. switch($iValue) {
  773. case 1:
  774. $iResult = $iValue;
  775. break;
  776. case 2:
  777. if($iAge >= 6)
  778. $iResult = $iValue;
  779. break;
  780. case 3:
  781. if($iAge >= 13)
  782. $iResult = $iValue;
  783. break;
  784. case 4:
  785. if($iAge >= 17)
  786. $iResult = $iValue;
  787. break;
  788. case 5:
  789. if($iAge >= 21)
  790. $iResult = $iValue;
  791. break;
  792. }
  793. if($iResult != 0)
  794. $iResult = 1 << ($iResult - 1);
  795. return $iResult;
  796. }
  797. public function serviceIsEnabledCfilter()
  798. {
  799. return BxDolContentFilter::getInstance()->isEnabled();
  800. }
  801. public function serviceUpdateRecommendations($iProfileId)
  802. {
  803. $aObjects = BxDolRecommendationQuery::getObjects();
  804. foreach($aObjects as $aObject)
  805. BxDolRecommendation::getObjectInstance($aObject['name'])->processCriteria($iProfileId);
  806. }
  807. protected function _getIcon ($sIcon)
  808. {
  809. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  810. }
  811. protected function _getLatestModuleTimestamp ()
  812. {
  813. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  814. if (empty($aModules))
  815. return 0;
  816. $aModuleNewest = array_pop($aModules);
  817. return $aModuleNewest['date'];
  818. }
  819. protected function _getProfilesModules($bForceActAsProfile = true)
  820. {
  821. $aRet = array();
  822. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  823. foreach($aModules as $aModule) {
  824. $oModule = BxDolModule::getInstance($aModule['name']);
  825. if(!($oModule instanceof iBxDolProfileService))
  826. continue;
  827. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  828. continue;
  829. $aRet[] = $aModule;
  830. }
  831. return $aRet;
  832. }
  833. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  834. {
  835. $sClass = 'BxTemplProfileSearchResult';
  836. bx_import($sClass);
  837. $o = new $sClass($sMode, $aParams);
  838. $o->setDesignBoxTemplateId($iDesignBox);
  839. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  840. $o->setAjaxPaginate($bAjaxPaginate);
  841. $o->setUnitParams(['context' => $sMode]);
  842. if ($o->isError)
  843. return '';
  844. if ($s = $o->processing()) {
  845. if(!bx_is_api())
  846. return $s;
  847. return [bx_api_get_block('browse', $s)];
  848. }
  849. else
  850. return '';
  851. }
  852. }
  853. /** @} */