BxBaseServiceProfiles.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. /**
  205. * @page service Service Calls
  206. * @section bx_system_general System Services
  207. * @subsection bx_system_general-profiles Profiles
  208. * @subsubsection bx_system_general-profile_membership profile_membership
  209. *
  210. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  211. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  212. *
  213. * Get membership level for specified profile.
  214. * @param $iProfileId profile ID
  215. *
  216. * @see BxBaseServiceProfiles::serviceProfileMembership
  217. */
  218. /**
  219. * @ref bx_system_general-profile_membership "profile_membership"
  220. */
  221. public function serviceProfileMembership ($iProfileId = 0)
  222. {
  223. if (!$iProfileId)
  224. $iProfileId = bx_get_logged_profile_id();
  225. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  226. }
  227. public function serviceProfileMembershipStats ($iProfileId = 0)
  228. {
  229. if(!$iProfileId)
  230. $iProfileId = bx_get_logged_profile_id();
  231. $sTxtUnlimited = _t('_unlimited');
  232. $sTxtFiles = _t('_sys_storage_files');
  233. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  234. $aTmplVarsStats = array();
  235. if(!empty($aQuota['quota_size'])) {
  236. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  237. $sPercent = $iWidth . '%';
  238. }
  239. else {
  240. $iWidth = 0;
  241. $sPercent = $sTxtUnlimited;
  242. }
  243. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  244. if(!empty($aQuota['quota_number'])) {
  245. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  246. $sPercent = $iWidth . '%';
  247. }
  248. else {
  249. $iWidth = 0;
  250. $sPercent = $sTxtUnlimited;
  251. }
  252. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  253. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  254. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  255. 'bx_repeat:stat_items' => $aTmplVarsStats
  256. ));
  257. }
  258. /**
  259. * @page service Service Calls
  260. * @section bx_system_general System Services
  261. * @subsection bx_system_general-profiles Profiles
  262. * @subsubsection bx_system_general-profile_notifications profile_notifications
  263. *
  264. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  265. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  266. *
  267. * Get number of unread notifications for logged in profile
  268. *
  269. * @see BxBaseServiceProfiles::serviceProfileNotifications
  270. */
  271. /**
  272. * @ref bx_system_general-profile_notifications "profile_notifications"
  273. */
  274. public function serviceProfileNotifications ($iProfileId = 0)
  275. {
  276. if (!$iProfileId)
  277. $iProfileId = bx_get_logged_profile_id();
  278. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  279. if(!$oMenu)
  280. return 0;
  281. $iNum = 0;
  282. $aMenuItems = $oMenu->getMenuItems ();
  283. foreach ($aMenuItems as $r) {
  284. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  285. $iNum += $r['bx_if:addon']['content']['addon'];
  286. }
  287. return $iNum;
  288. }
  289. /**
  290. * @page service Service Calls
  291. * @section bx_system_general System Services
  292. * @subsection bx_system_general-profiles Profiles
  293. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  294. *
  295. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  296. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  297. *
  298. * Get number of online profiles
  299. *
  300. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  301. */
  302. /**
  303. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  304. */
  305. public function serviceGetCountOnlineProfiles ()
  306. {
  307. $oProfilesQuery = BxDolProfileQuery::getInstance();
  308. return $oProfilesQuery->getOnlineCount();
  309. }
  310. public function serviceGetProfilesModules ($bForceActAsProfile = true)
  311. {
  312. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  313. $oDb = BxDolDb::getInstance();
  314. $oCache = $oDb->getDbCacheObject ();
  315. $sKey = $oDb->genDbCacheKey('profiles_' . (!$bForceActAsProfile ? 'all_' : '') . 'modules_array');
  316. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  317. $mixedRetTs = $oCache->getData($sKeyTs);
  318. $mixedRet = $oCache->getData($sKey);
  319. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  320. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  321. $aModulesArray = $mixedRet;
  322. } else {
  323. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  324. $oCache->setData($sKey, $aModulesArray);
  325. $oCache->setData($sKeyTs, $iNewestModuleTs);
  326. }
  327. } else {
  328. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  329. }
  330. return $aModulesArray;
  331. }
  332. public function serviceProfilesFriends ($iLimit = 20)
  333. {
  334. if (!($iProfileId = bx_get_logged_profile_id()))
  335. return array();
  336. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  337. if (!$oConnection)
  338. return array();
  339. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  340. return array();
  341. $aRet = array();
  342. foreach ($a as $iId) {
  343. $oProfile = BxDolProfile::getInstance($iId);
  344. $aRet[] = array (
  345. 'label' => $oProfile->getDisplayName(),
  346. 'value' => $iId,
  347. 'url' => $oProfile->getUrl(),
  348. 'thumb' => $oProfile->getThumb(),
  349. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  350. );
  351. }
  352. return $aRet;
  353. }
  354. public function serviceProfilesSearch ($sTerm, $mixedParems = [])
  355. {
  356. $iLimit = 20;
  357. if(is_int($mixedParems))
  358. $iLimit = (int)$mixedParems;
  359. else if(is_array($mixedParems) && isset($mixedParems['limit']))
  360. $iLimit = (int)$mixedParems['limit'];
  361. // display friends by default
  362. if (!$sTerm)
  363. return $this->serviceProfilesFriends($iLimit);
  364. // get list of "profiles" modules
  365. $aModules = $this->serviceGetProfilesModules();
  366. // search in each module
  367. $a = array();
  368. foreach ($aModules as $aModule) {
  369. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  370. continue;
  371. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  372. }
  373. // sort result
  374. usort($a, function($r1, $r2) {
  375. return strcmp($r1['label'], $r2['label']);
  376. });
  377. bx_alert('system', 'profiles_search', 0, 0, array(
  378. 'module' => is_array($mixedParems) && isset($mixedParems['module']) ? $mixedParems['module'] : '',
  379. 'term' => $sTerm,
  380. 'result' => &$a
  381. ));
  382. // return as array
  383. return array_slice($a, 0, $iLimit);
  384. }
  385. public function serviceProfilesList ($iAccountId = 0)
  386. {
  387. $oProfilesQuery = BxDolProfileQuery::getInstance();
  388. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  389. if (!$aProfiles)
  390. return false;
  391. $s = '';
  392. foreach ($aProfiles as $aProfile)
  393. if ($aProfile['type'] != 'system')
  394. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  395. if (!$s)
  396. $s = MsgBox(_t('_sys_txt_empty'));
  397. return $s . '<div class="bx-clear"></div>';
  398. }
  399. public function serviceBrowseFriends ($iProfileId = 0, $aParams = [])
  400. {
  401. if($this->_bIsApi)
  402. $aParams = bx_api_get_browse_params($aParams);
  403. if(!$iProfileId)
  404. $iProfileId = bx_get_logged_profile_id();
  405. if(!$iProfileId)
  406. return '';
  407. $aParams = array_merge([
  408. 'params' => false,
  409. 'design_box' => BX_DB_PADDING_DEF,
  410. 'empty_message' => false,
  411. 'ajax_paginate' => true,
  412. ], $aParams);
  413. $aParamsBrowse = ['profile' => $iProfileId];
  414. if(!empty($aParams['params']) && is_array($aParams['params']))
  415. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  416. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  417. $bEmpty = (bool)$aParams['empty_message'];
  418. $mixedResult = $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  419. if(empty($mixedResult) && $bEmpty)
  420. $mixedResult = MsgBox($sEmpty);
  421. return $mixedResult;
  422. }
  423. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  424. {
  425. if($this->_bIsApi)
  426. $aParams = bx_api_get_browse_params($aParams);
  427. if(!$iProfileId)
  428. $iProfileId = bx_get_logged_profile_id();
  429. if(!$iProfileId)
  430. return '';
  431. $aParams = array_merge([
  432. 'params' => false,
  433. 'design_box' => BX_DB_PADDING_DEF,
  434. 'empty_message' => false,
  435. 'ajax_paginate' => true,
  436. ], $aParams);
  437. $aParamsBrowse = ['profile' => $iProfileId];
  438. if(!empty($aParams['params']) && is_array($aParams['params']))
  439. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  440. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  441. $bEmpty = (bool)$aParams['empty_message'];
  442. $mixedResult = $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  443. if(empty($mixedResult) && $bEmpty)
  444. $mixedResult = MsgBox($sEmpty);
  445. return $mixedResult;
  446. }
  447. public function serviceBrowseFriendRequested ($iProfileId = 0, $aParams = [])
  448. {
  449. if($this->_bIsApi)
  450. $aParams = bx_api_get_browse_params($aParams);
  451. if(!$iProfileId)
  452. $iProfileId = bx_get_logged_profile_id();
  453. if(!$iProfileId)
  454. return '';
  455. $aParams = array_merge([
  456. 'params' => false,
  457. 'design_box' => BX_DB_PADDING_DEF,
  458. 'empty_message' => false,
  459. 'ajax_paginate' => true,
  460. ], $aParams);
  461. $aParamsBrowse = ['profile' => $iProfileId];
  462. if(!empty($aParams['params']) && is_array($aParams['params']))
  463. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  464. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  465. $bEmpty = (bool)$aParams['empty_message'];
  466. $mixedResult = $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  467. if(empty($mixedResult) && $bEmpty)
  468. $mixedResult = MsgBox($sEmpty);
  469. return $mixedResult;
  470. }
  471. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  472. {
  473. if($this->_bIsApi)
  474. $aParams = bx_api_get_browse_params($aParams);
  475. if(!$iProfileId)
  476. $iProfileId = bx_get_logged_profile_id();
  477. if(!$iProfileId)
  478. return '';
  479. $aParams = array_merge([
  480. 'params' => false,
  481. 'design_box' => BX_DB_PADDING_DEF,
  482. 'empty_message' => false,
  483. 'ajax_paginate' => true,
  484. ], $aParams);
  485. $aParamsBrowse = ['profile' => $iProfileId];
  486. if(!empty($aParams['params']) && is_array($aParams['params']))
  487. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  488. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  489. $bEmpty = (bool)$aParams['empty_message'];
  490. $mixedResult = $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  491. if(empty($mixedResult) && $bEmpty)
  492. $mixedResult = MsgBox($sEmpty);
  493. return $mixedResult;
  494. }
  495. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  496. {
  497. if($this->_bIsApi)
  498. $aParams = bx_api_get_browse_params($aParams);
  499. if(!$iProfileId)
  500. $iProfileId = bx_get_logged_profile_id();
  501. if(!$iProfileId)
  502. return '';
  503. $aParams = array_merge([
  504. 'params' => false,
  505. 'design_box' => BX_DB_PADDING_DEF,
  506. 'empty_message' => false,
  507. 'ajax_paginate' => true,
  508. ], $aParams);
  509. $aParamsBrowse = ['profile' => $iProfileId];
  510. if(!empty($aParams['params']) && is_array($aParams['params']))
  511. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  512. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  513. $bEmpty = (bool)$aParams['empty_message'];
  514. $mixedResult = $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  515. if(empty($mixedResult) && $bEmpty)
  516. $mixedResult = MsgBox($sEmpty);
  517. return $mixedResult;
  518. }
  519. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  520. {
  521. if($this->_bIsApi)
  522. $aParams = bx_api_get_browse_params($aParams);
  523. if(!$iProfileId)
  524. $iProfileId = bx_get_logged_profile_id();
  525. if(!$iProfileId)
  526. return '';
  527. $aParams = array_merge([
  528. 'params' => false,
  529. 'design_box' => BX_DB_PADDING_DEF,
  530. 'empty_message' => false,
  531. 'ajax_paginate' => true,
  532. ], $aParams);
  533. $aParamsBrowse = [
  534. 'profile' => $iProfileId,
  535. 'object' => $sObject
  536. ];
  537. if(!empty($aParams['params']) && is_array($aParams['params']))
  538. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  539. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  540. $bEmpty = (bool)$aParams['empty_message'];
  541. $mixedResult = $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  542. if(empty($mixedResult) && $bEmpty)
  543. $mixedResult = MsgBox($sEmpty);
  544. return $mixedResult;
  545. }
  546. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  547. {
  548. if($this->_bIsApi)
  549. $aParams = bx_api_get_browse_params($aParams);
  550. if(!$iProfileId)
  551. $iProfileId = bx_get_logged_profile_id();
  552. if(!$iProfileId)
  553. return '';
  554. $aParams = array_merge([
  555. 'params' => false,
  556. 'design_box' => BX_DB_PADDING_DEF,
  557. 'empty_message' => false,
  558. 'ajax_paginate' => true,
  559. ], $aParams);
  560. $aParamsBrowse = [
  561. 'profile' => $iProfileId,
  562. 'object' => 'sys_profiles_friends',
  563. 'type' => 'content',
  564. 'mutual' => false,
  565. 'profile2' => 0
  566. ];
  567. if(!empty($aParams['params']) && is_array($aParams['params']))
  568. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  569. $sEmpty = !empty($aParams['empty_message_text']) ? _t($aParams['empty_message_text']) : '';
  570. $bEmpty = (bool)$aParams['empty_message'];
  571. $mixedResult = $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], empty($sEmpty) ? $bEmpty : false, $aParams['ajax_paginate']);
  572. if(empty($mixedResult) && $bEmpty)
  573. $mixedResult = MsgBox($sEmpty);
  574. return $mixedResult;
  575. }
  576. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  577. {
  578. if($this->_bIsApi)
  579. $aParams = bx_api_get_browse_params($aParams, true);
  580. if(!$iProfileId)
  581. $iProfileId = bx_get_logged_profile_id();
  582. if(!$iProfileId)
  583. return '';
  584. $aParams = array_merge([
  585. 'empty_message' => false,
  586. 'start' => 0,
  587. 'per_page' => 0
  588. ], $aParams);
  589. if(($iStartGet = bx_get('start')) !== false)
  590. $aParams['start'] = (int)$iStartGet;
  591. if(($iPerPageGet = bx_get('per_page')) !== false)
  592. $aParams['per_page'] = (int)$iPerPageGet;
  593. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  594. if(!$oRecommendation)
  595. return false;
  596. if(bx_is_api()) {
  597. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams['start'], $aParams['per_page']);
  598. $aData = array_merge($aData, [
  599. 'module' => 'system',
  600. 'unit' => 'mixed',
  601. 'request_url' => '/api.php?r=system/browse_recommendations_friends/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  602. ]);
  603. return [bx_api_get_block('browse', $aData)];
  604. }
  605. $sCode = $oRecommendation->getCode($iProfileId, $aParams['start'], $aParams['per_page']);
  606. if(!$sCode && $aParams['empty_message'])
  607. $sCode = MsgBox(_t(!empty($aParams['empty_message_text']) ? $aParams['empty_message_text'] : '_Empty'));
  608. return $sCode;
  609. }
  610. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  611. {
  612. if($this->_bIsApi)
  613. $aParams = bx_api_get_browse_params($aParams, true);
  614. if(!$iProfileId)
  615. $iProfileId = bx_get_logged_profile_id();
  616. if(!$iProfileId)
  617. return '';
  618. $aParams = array_merge([
  619. 'empty_message' => false,
  620. 'start' => 0,
  621. 'per_page' => 0
  622. ], $aParams);
  623. if(($iStartGet = bx_get('start')) !== false)
  624. $aParams['start'] = (int)$iStartGet;
  625. if(($iPerPageGet = bx_get('per_page')) !== false)
  626. $aParams['per_page'] = (int)$iPerPageGet;
  627. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  628. if(!$oRecommendation)
  629. return false;
  630. if(bx_is_api()) {
  631. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams['start'], $aParams['per_page']);
  632. $aData = array_merge($aData, [
  633. 'module' => 'system',
  634. 'unit' => 'mixed',
  635. 'request_url' => '/api.php?r=system/browse_recommendations_subscriptions/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  636. ]);
  637. return [bx_api_get_block('browse', $aData)];
  638. }
  639. $sCode = $oRecommendation->getCode($iProfileId, $aParams['start'], $aParams['per_page']);
  640. if(!$sCode && $aParams['empty_message'])
  641. $sCode = MsgBox(_t(!empty($aParams['empty_message_text']) ? $aParams['empty_message_text'] : '_Empty'));
  642. return $sCode;
  643. }
  644. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  645. {
  646. $oTemplate = BxDolTemplate::getInstance();
  647. $oProfilesQuery = BxDolProfileQuery::getInstance();
  648. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  649. if (!$aProfiles)
  650. return false;
  651. if (null === $iActiveProfileId)
  652. $iActiveProfileId = bx_get_logged_profile_id();
  653. $oModuleDb = BxDolModuleQuery::getInstance();
  654. $aVars = array (
  655. 'bx_repeat:row' => array(),
  656. );
  657. bx_alert('system', 'account_profile_switcher', 0, false, array(
  658. 'account_id' => $iAccountId,
  659. 'active_profile_id' => &$iActiveProfileId,
  660. 'url_profile_action' => &$sUrlProfileAction,
  661. 'show_all' => &$bShowAll,
  662. 'button_title' => &$sButtonTitle,
  663. 'profile_template' => &$sProfileTemplate,
  664. 'profiles' => &$aProfiles
  665. ));
  666. foreach ($aProfiles as $aProfile) {
  667. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  668. continue;
  669. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  670. continue;
  671. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  672. continue;
  673. if($this->_bIsApi){
  674. $o = BxDolProfile::getInstance($aProfile['id']);
  675. $aProfilesData[] = [
  676. 'id' => $o->id(),
  677. 'display_name' => $o->getDisplayName(),
  678. 'url' => bx_api_get_relative_url($o->getUrl()),
  679. 'avatar' => $o->getAvatar()
  680. ];
  681. }
  682. $aVars['bx_repeat:row'][] = array (
  683. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  684. 'bx_if:active' => array (
  685. 'condition' => $iActiveProfileId == $aProfile['id'],
  686. 'content' => array('id' => $aProfile['id']),
  687. ),
  688. 'bx_if:inactive' => array (
  689. 'condition' => $iActiveProfileId != $aProfile['id'],
  690. 'content' => array(
  691. 'id' => $aProfile['id'],
  692. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  693. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  694. ),
  695. ),
  696. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  697. );
  698. }
  699. if($this->_bIsApi){
  700. return [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
  701. }
  702. $oTemplate->addCss('account.css');
  703. return array(
  704. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  705. );
  706. }
  707. public function serviceProfileSettingsCfilter($iProfileId = false)
  708. {
  709. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  710. BxDolTemplate::getInstance()->displayPageNotFound();
  711. exit;
  712. }
  713. // set settings submenu
  714. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  715. if ($oMenuSubmenu) {
  716. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  717. 'title' => _t('_sys_menu_item_title_account_settings'),
  718. 'link' => BX_DOL_URL_ROOT . 'member.php',
  719. 'icon' => '',
  720. ));
  721. }
  722. if($iProfileId === false)
  723. $iProfileId = bx_get_logged_profile_id();
  724. $oProfile = BxDolProfile::getInstance($iProfileId);
  725. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  726. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  727. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  728. $sForm = 'sys_profile';
  729. $sFormDisplay = 'sys_profile_cf_set';
  730. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  731. if(!$oForm)
  732. return MsgBox(_t('_sys_txt_error_occured'));
  733. $oForm->initChecker($aProfileInfo);
  734. if(!$oForm->isSubmittedAndValid())
  735. return $sCode = $oForm->getCode();
  736. if(!$oForm->update($iProfileId)) {
  737. if (!$oForm->isValid())
  738. return $oForm->getCode();
  739. else
  740. return MsgBox(_t('_sys_txt_error_profile_update'));
  741. }
  742. // display result message
  743. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  744. return $sMsg . $oForm->getCode();
  745. }
  746. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  747. {
  748. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  749. return false;
  750. $iAge = 0;
  751. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  752. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  753. $iResult = 0;
  754. switch($iValue) {
  755. case 1:
  756. $iResult = $iValue;
  757. break;
  758. case 2:
  759. if($iAge >= 6)
  760. $iResult = $iValue;
  761. break;
  762. case 3:
  763. if($iAge >= 13)
  764. $iResult = $iValue;
  765. break;
  766. case 4:
  767. if($iAge >= 17)
  768. $iResult = $iValue;
  769. break;
  770. case 5:
  771. if($iAge >= 21)
  772. $iResult = $iValue;
  773. break;
  774. }
  775. if($iResult != 0)
  776. $iResult = 1 << ($iResult - 1);
  777. return $iResult;
  778. }
  779. public function serviceIsEnabledCfilter()
  780. {
  781. return BxDolContentFilter::getInstance()->isEnabled();
  782. }
  783. public function serviceUpdateRecommendations($iProfileId)
  784. {
  785. $aObjects = BxDolRecommendationQuery::getObjects();
  786. foreach($aObjects as $aObject)
  787. BxDolRecommendation::getObjectInstance($aObject['name'])->processCriteria($iProfileId);
  788. }
  789. protected function _getIcon ($sIcon)
  790. {
  791. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  792. }
  793. protected function _getLatestModuleTimestamp ()
  794. {
  795. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  796. if (empty($aModules))
  797. return 0;
  798. $aModuleNewest = array_pop($aModules);
  799. return $aModuleNewest['date'];
  800. }
  801. protected function _getProfilesModules($bForceActAsProfile = true)
  802. {
  803. $aRet = array();
  804. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  805. foreach($aModules as $aModule) {
  806. $oModule = BxDolModule::getInstance($aModule['name']);
  807. if(!($oModule instanceof iBxDolProfileService))
  808. continue;
  809. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  810. continue;
  811. $aRet[] = $aModule;
  812. }
  813. return $aRet;
  814. }
  815. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  816. {
  817. $sClass = 'BxTemplProfileSearchResult';
  818. bx_import($sClass);
  819. $o = new $sClass($sMode, $aParams);
  820. $o->setDesignBoxTemplateId($iDesignBox);
  821. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  822. $o->setAjaxPaginate($bAjaxPaginate);
  823. $o->setUnitParams(['context' => $sMode]);
  824. if ($o->isError)
  825. return '';
  826. if ($s = $o->processing()) {
  827. if(!bx_is_api())
  828. return $s;
  829. return [bx_api_get_block('browse', $s)];
  830. }
  831. else
  832. return '';
  833. }
  834. }
  835. /** @} */