BxBaseServiceProfiles.php 36 KB

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