BxBaseServiceProfiles.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  403. if(!$iProfileId)
  404. $iProfileId = bx_get_logged_profile_id();
  405. if(!$iProfileId)
  406. return '';
  407. if(is_string($aParams))
  408. $aParams = json_decode($aParams, true);
  409. $aParams = array_merge([
  410. 'params' => false,
  411. 'design_box' => BX_DB_PADDING_DEF,
  412. 'empty_message' => false,
  413. 'ajax_paginate' => true,
  414. ], $aParams);
  415. $aParamsBrowse = ['profile' => $iProfileId];
  416. if(!empty($aParams['params']) && is_array($aParams['params']))
  417. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  418. return $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  419. }
  420. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  421. {
  422. if($this->_bIsApi)
  423. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  424. if(!$iProfileId)
  425. $iProfileId = bx_get_logged_profile_id();
  426. if(!$iProfileId)
  427. return '';
  428. if(is_string($aParams))
  429. $aParams = json_decode($aParams, true);
  430. $aParams = array_merge([
  431. 'params' => false,
  432. 'design_box' => BX_DB_PADDING_DEF,
  433. 'empty_message' => false,
  434. 'ajax_paginate' => true,
  435. ], $aParams);
  436. $aParamsBrowse = ['profile' => $iProfileId];
  437. if(!empty($aParams['params']) && is_array($aParams['params']))
  438. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  439. return $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  440. }
  441. public function serviceBrowseFriendRequested ($iProfileId = 0, $aParams = [])
  442. {
  443. if($this->_bIsApi)
  444. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  445. if(!$iProfileId)
  446. $iProfileId = bx_get_logged_profile_id();
  447. if(!$iProfileId)
  448. return '';
  449. if(is_string($aParams))
  450. $aParams = json_decode($aParams, true);
  451. $aParams = array_merge([
  452. 'params' => false,
  453. 'design_box' => BX_DB_PADDING_DEF,
  454. 'empty_message' => false,
  455. 'ajax_paginate' => true,
  456. ], $aParams);
  457. $aParamsBrowse = ['profile' => $iProfileId];
  458. if(!empty($aParams['params']) && is_array($aParams['params']))
  459. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  460. return $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  461. }
  462. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  463. {
  464. if($this->_bIsApi)
  465. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  466. if(!$iProfileId)
  467. $iProfileId = bx_get_logged_profile_id();
  468. if(!$iProfileId)
  469. return '';
  470. if(is_string($aParams))
  471. $aParams = json_decode($aParams, true);
  472. $aParams = array_merge([
  473. 'params' => false,
  474. 'design_box' => BX_DB_PADDING_DEF,
  475. 'empty_message' => false,
  476. 'ajax_paginate' => true,
  477. ], $aParams);
  478. $aParamsBrowse = ['profile' => $iProfileId];
  479. if(!empty($aParams['params']) && is_array($aParams['params']))
  480. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  481. return $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  482. }
  483. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  484. {
  485. if($this->_bIsApi)
  486. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  487. if(!$iProfileId)
  488. $iProfileId = bx_get_logged_profile_id();
  489. if(!$iProfileId)
  490. return '';
  491. if(is_string($aParams))
  492. $aParams = json_decode($aParams, true);
  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. return $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  503. }
  504. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  505. {
  506. if($this->_bIsApi)
  507. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  508. if(!$iProfileId)
  509. $iProfileId = bx_get_logged_profile_id();
  510. if(!$iProfileId)
  511. return '';
  512. if(is_string($aParams))
  513. $aParams = json_decode($aParams, true);
  514. $aParams = array_merge([
  515. 'params' => false,
  516. 'design_box' => BX_DB_PADDING_DEF,
  517. 'empty_message' => false,
  518. 'ajax_paginate' => true,
  519. ], $aParams);
  520. $aParamsBrowse = [
  521. 'profile' => $iProfileId,
  522. 'object' => $sObject
  523. ];
  524. if(!empty($aParams['params']) && is_array($aParams['params']))
  525. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  526. return $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  527. }
  528. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  529. {
  530. if($this->_bIsApi)
  531. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  532. if(!$iProfileId)
  533. $iProfileId = bx_get_logged_profile_id();
  534. if(!$iProfileId)
  535. return '';
  536. if(is_string($aParams))
  537. $aParams = json_decode($aParams, true);
  538. $aParams = array_merge([
  539. 'params' => false,
  540. 'design_box' => BX_DB_PADDING_DEF,
  541. 'empty_message' => false,
  542. 'ajax_paginate' => true,
  543. ], $aParams);
  544. $aParamsBrowse = [
  545. 'profile' => $iProfileId,
  546. 'object' => 'sys_profiles_friends',
  547. 'type' => 'content',
  548. 'mutual' => false,
  549. 'profile2' => 0
  550. ];
  551. if(!empty($aParams['params']) && is_array($aParams['params']))
  552. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  553. return $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  554. }
  555. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  556. {
  557. if($this->_bIsApi)
  558. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  559. if(!$iProfileId)
  560. $iProfileId = bx_get_logged_profile_id();
  561. if(!$iProfileId)
  562. return '';
  563. $aParams = array_merge([
  564. 'empty_message' => false,
  565. 'start' => 0,
  566. 'per_page' => 0
  567. ], $aParams);
  568. if(($iStartGet = bx_get('start')) !== false)
  569. $aParams['start'] = (int)$iStartGet;
  570. if(($iPerPageGet = bx_get('per_page')) !== false)
  571. $aParams['per_page'] = (int)$iPerPageGet;
  572. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  573. if(!$oRecommendation)
  574. return false;
  575. if(bx_is_api()) {
  576. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams['start'], $aParams['per_page']);
  577. $aData['module'] = 'system';
  578. $aData['unit'] = 'mixed';
  579. $aData['request_url'] = '/api.php?r=system/browse_recommendations_friends/TemplServiceProfiles&params[]=';
  580. return [bx_api_get_block('browse', $aData)];
  581. }
  582. $sCode = $oRecommendation->getCode($iProfileId, $aParams['start'], $aParams['per_page']);
  583. if(!$sCode && $aParams['empty_message'])
  584. $sCode = MsgBox(_t('_Empty'));
  585. return $sCode;
  586. }
  587. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  588. {
  589. if($this->_bIsApi)
  590. list($iProfileId, $aParams) = [0, bx_api_get_browse_params($iProfileId)];
  591. if(!$iProfileId)
  592. $iProfileId = bx_get_logged_profile_id();
  593. if(!$iProfileId)
  594. return '';
  595. $aParams = array_merge([
  596. 'empty_message' => false,
  597. 'start' => 0,
  598. 'per_page' => 0
  599. ], $aParams);
  600. if(($iStartGet = bx_get('start')) !== false)
  601. $aParams['start'] = (int)$iStartGet;
  602. if(($iPerPageGet = bx_get('per_page')) !== false)
  603. $aParams['per_page'] = (int)$iPerPageGet;
  604. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  605. if(!$oRecommendation)
  606. return false;
  607. if(bx_is_api()) {
  608. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams['start'], $aParams['per_page']);
  609. $aData['module'] = 'system';
  610. $aData['unit'] = 'mixed';
  611. $aData['request_url'] = '/api.php?r=system/browse_recommendations_subscriptions/TemplServiceProfiles&params[]=';
  612. return [bx_api_get_block('browse', $aData)];
  613. }
  614. $sCode = $oRecommendation->getCode($iProfileId, $aParams['start'], $aParams['per_page']);
  615. if(!$sCode && $aParams['empty_message'])
  616. $sCode = MsgBox(_t('_Empty'));
  617. return $sCode;
  618. }
  619. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  620. {
  621. $oTemplate = BxDolTemplate::getInstance();
  622. $oProfilesQuery = BxDolProfileQuery::getInstance();
  623. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  624. if (!$aProfiles)
  625. return false;
  626. if (null === $iActiveProfileId)
  627. $iActiveProfileId = bx_get_logged_profile_id();
  628. $oModuleDb = BxDolModuleQuery::getInstance();
  629. $aVars = array (
  630. 'bx_repeat:row' => array(),
  631. );
  632. bx_alert('system', 'account_profile_switcher', 0, false, array(
  633. 'account_id' => $iAccountId,
  634. 'active_profile_id' => &$iActiveProfileId,
  635. 'url_profile_action' => &$sUrlProfileAction,
  636. 'show_all' => &$bShowAll,
  637. 'button_title' => &$sButtonTitle,
  638. 'profile_template' => &$sProfileTemplate,
  639. 'profiles' => &$aProfiles
  640. ));
  641. foreach ($aProfiles as $aProfile) {
  642. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  643. continue;
  644. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  645. continue;
  646. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  647. continue;
  648. $aVars['bx_repeat:row'][] = array (
  649. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  650. 'bx_if:active' => array (
  651. 'condition' => $iActiveProfileId == $aProfile['id'],
  652. 'content' => array('id' => $aProfile['id']),
  653. ),
  654. 'bx_if:inactive' => array (
  655. 'condition' => $iActiveProfileId != $aProfile['id'],
  656. 'content' => array(
  657. 'id' => $aProfile['id'],
  658. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  659. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  660. ),
  661. ),
  662. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  663. );
  664. }
  665. $oTemplate->addCss('account.css');
  666. return array(
  667. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  668. );
  669. }
  670. public function serviceProfileSettingsCfilter($iProfileId = false)
  671. {
  672. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  673. BxDolTemplate::getInstance()->displayPageNotFound();
  674. exit;
  675. }
  676. // set settings submenu
  677. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  678. if ($oMenuSubmenu) {
  679. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  680. 'title' => _t('_sys_menu_item_title_account_settings'),
  681. 'link' => BX_DOL_URL_ROOT . 'member.php',
  682. 'icon' => '',
  683. ));
  684. }
  685. if($iProfileId === false)
  686. $iProfileId = bx_get_logged_profile_id();
  687. $oProfile = BxDolProfile::getInstance($iProfileId);
  688. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  689. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  690. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  691. $sForm = 'sys_profile';
  692. $sFormDisplay = 'sys_profile_cf_set';
  693. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  694. if(!$oForm)
  695. return MsgBox(_t('_sys_txt_error_occured'));
  696. $oForm->initChecker($aProfileInfo);
  697. if(!$oForm->isSubmittedAndValid())
  698. return $sCode = $oForm->getCode();
  699. if(!$oForm->update($iProfileId)) {
  700. if (!$oForm->isValid())
  701. return $oForm->getCode();
  702. else
  703. return MsgBox(_t('_sys_txt_error_profile_update'));
  704. }
  705. // display result message
  706. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  707. return $sMsg . $oForm->getCode();
  708. }
  709. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  710. {
  711. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  712. return false;
  713. $iAge = 0;
  714. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  715. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  716. $iResult = 0;
  717. switch($iValue) {
  718. case 1:
  719. $iResult = $iValue;
  720. break;
  721. case 2:
  722. if($iAge >= 6)
  723. $iResult = $iValue;
  724. break;
  725. case 3:
  726. if($iAge >= 13)
  727. $iResult = $iValue;
  728. break;
  729. case 4:
  730. if($iAge >= 17)
  731. $iResult = $iValue;
  732. break;
  733. case 5:
  734. if($iAge >= 21)
  735. $iResult = $iValue;
  736. break;
  737. }
  738. if($iResult != 0)
  739. $iResult = 1 << ($iResult - 1);
  740. return $iResult;
  741. }
  742. public function serviceIsEnabledCfilter()
  743. {
  744. return BxDolContentFilter::getInstance()->isEnabled();
  745. }
  746. public function serviceUpdateRecommendations($iProfileId)
  747. {
  748. $aObjects = BxDolRecommendationQuery::getObjects();
  749. foreach($aObjects as $aObject)
  750. BxDolRecommendation::getObjectInstance($aObject['name'])->processCriteria($iProfileId);
  751. }
  752. protected function _getIcon ($sIcon)
  753. {
  754. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  755. }
  756. protected function _getLatestModuleTimestamp ()
  757. {
  758. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  759. if (empty($aModules))
  760. return 0;
  761. $aModuleNewest = array_pop($aModules);
  762. return $aModuleNewest['date'];
  763. }
  764. protected function _getProfilesModules($bForceActAsProfile = true)
  765. {
  766. $aRet = array();
  767. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  768. foreach($aModules as $aModule) {
  769. $oModule = BxDolModule::getInstance($aModule['name']);
  770. if(!($oModule instanceof iBxDolProfileService))
  771. continue;
  772. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  773. continue;
  774. $aRet[] = $aModule;
  775. }
  776. return $aRet;
  777. }
  778. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  779. {
  780. $sClass = 'BxTemplProfileSearchResult';
  781. bx_import($sClass);
  782. $o = new $sClass($sMode, $aParams);
  783. $o->setDesignBoxTemplateId($iDesignBox);
  784. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  785. $o->setAjaxPaginate($bAjaxPaginate);
  786. $o->setUnitParams(['context' => $sMode]);
  787. if ($o->isError)
  788. return '';
  789. if ($s = $o->processing()) {
  790. if(!bx_is_api())
  791. return $s;
  792. return [bx_api_get_block('browse', $s)];
  793. }
  794. else
  795. return '';
  796. }
  797. }
  798. /** @} */