BxBaseServiceProfiles.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 = (int)getParam('sys_profiles_search_limit');
  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 serviceProfileFriendsRequestsCount ($iProfileId = 0)
  464. {
  465. if(!$iProfileId)
  466. $iProfileId = bx_get_logged_profile_id();
  467. if(!$iProfileId)
  468. return '';
  469. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  470. if(!$oConnection)
  471. return false;
  472. return $oConnection->getConnectedInitiatorsCount($iProfileId, false) - $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  473. }
  474. public function serviceProfileSentFriendsRequestsCount ($iProfileId = 0)
  475. {
  476. if(!$iProfileId)
  477. $iProfileId = bx_get_logged_profile_id();
  478. if(!$iProfileId)
  479. return '';
  480. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  481. if(!$oConnection)
  482. return false;
  483. return $oConnection->getConnectedContentCount($iProfileId, false) - $oConnection->getConnectedContentCount($iProfileId, true);
  484. }
  485. public function serviceProfileFriendsCount ($iProfileId = 0)
  486. {
  487. if(!$iProfileId)
  488. $iProfileId = bx_get_logged_profile_id();
  489. if(!$iProfileId)
  490. return '';
  491. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  492. if(!$oConnection)
  493. return false;
  494. return $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  495. }
  496. public function serviceProfileFollowersCount ($iProfileId = 0)
  497. {
  498. if(!$iProfileId)
  499. $iProfileId = bx_get_logged_profile_id();
  500. if(!$iProfileId)
  501. return '';
  502. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  503. if(!$oConnection)
  504. return false;
  505. return $oConnection->getConnectedInitiatorsCount($iProfileId);
  506. }
  507. public function serviceProfileFollowingCount ($iProfileId = 0)
  508. {
  509. if(!$iProfileId)
  510. $iProfileId = bx_get_logged_profile_id();
  511. if(!$iProfileId)
  512. return '';
  513. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  514. if(!$oConnection)
  515. return false;
  516. return $oConnection->getConnectedContentCount($iProfileId);
  517. }
  518. public function serviceProfileRecommendationFollowingCount ($iProfileId = 0, $aParams = [])
  519. {
  520. if(!$iProfileId)
  521. $iProfileId = bx_get_logged_profile_id();
  522. if(!$iProfileId)
  523. return '';
  524. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  525. if(!$oRecommendation)
  526. return false;
  527. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  528. return $oRecommendation->getCount($iProfileId, array_merge([
  529. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  530. ], $aParams));
  531. }
  532. public function serviceProfileRecommendationFriendsCount ($iProfileId = 0, $aParams = [])
  533. {
  534. if(!$iProfileId)
  535. $iProfileId = bx_get_logged_profile_id();
  536. if(!$iProfileId)
  537. return '';
  538. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  539. if(!$oRecommendation)
  540. return false;
  541. return $oRecommendation->getCount($iProfileId, $aParams);
  542. }
  543. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  544. {
  545. if($this->_bIsApi)
  546. $aParams = bx_api_get_browse_params($aParams);
  547. if(!$iProfileId)
  548. $iProfileId = bx_get_logged_profile_id();
  549. if(!$iProfileId)
  550. return '';
  551. $aParams = array_merge([
  552. 'params' => false,
  553. 'design_box' => BX_DB_PADDING_DEF,
  554. 'empty_message' => false,
  555. 'ajax_paginate' => true,
  556. ], $aParams);
  557. $aParamsBrowse = ['profile' => $iProfileId];
  558. if(!empty($aParams['params']) && is_array($aParams['params']))
  559. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  560. return $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  561. }
  562. public function serviceBrowseFriendRequested ($iProfileId = 0, $aParams = [])
  563. {
  564. if($this->_bIsApi)
  565. $aParams = bx_api_get_browse_params($aParams);
  566. if(!$iProfileId)
  567. $iProfileId = bx_get_logged_profile_id();
  568. if(!$iProfileId)
  569. return '';
  570. $aParams = array_merge([
  571. 'params' => false,
  572. 'design_box' => BX_DB_PADDING_DEF,
  573. 'empty_message' => false,
  574. 'ajax_paginate' => true,
  575. ], $aParams);
  576. $aParamsBrowse = ['profile' => $iProfileId];
  577. if(!empty($aParams['params']) && is_array($aParams['params']))
  578. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  579. return $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  580. }
  581. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  582. {
  583. if($this->_bIsApi)
  584. $aParams = bx_api_get_browse_params($aParams);
  585. if(!$iProfileId)
  586. $iProfileId = bx_get_logged_profile_id();
  587. if(!$iProfileId)
  588. return '';
  589. $aParams = array_merge([
  590. 'params' => false,
  591. 'design_box' => BX_DB_PADDING_DEF,
  592. 'empty_message' => false,
  593. 'ajax_paginate' => true,
  594. ], $aParams);
  595. $aParamsBrowse = ['profile' => $iProfileId];
  596. if(!empty($aParams['params']) && is_array($aParams['params']))
  597. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  598. return $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  599. }
  600. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  601. {
  602. if($this->_bIsApi)
  603. $aParams = bx_api_get_browse_params($aParams);
  604. if(!$iProfileId)
  605. $iProfileId = bx_get_logged_profile_id();
  606. if(!$iProfileId)
  607. return '';
  608. $aParams = array_merge([
  609. 'params' => false,
  610. 'design_box' => BX_DB_PADDING_DEF,
  611. 'empty_message' => false,
  612. 'ajax_paginate' => true,
  613. ], $aParams);
  614. $aParamsBrowse = ['profile' => $iProfileId];
  615. if(!empty($aParams['params']) && is_array($aParams['params']))
  616. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  617. return $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  618. }
  619. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  620. {
  621. if($this->_bIsApi)
  622. $aParams = bx_api_get_browse_params($aParams);
  623. if(!$iProfileId)
  624. $iProfileId = bx_get_logged_profile_id();
  625. if(!$iProfileId)
  626. return '';
  627. $aParams = array_merge([
  628. 'params' => false,
  629. 'design_box' => BX_DB_PADDING_DEF,
  630. 'empty_message' => false,
  631. 'ajax_paginate' => true,
  632. ], $aParams);
  633. $aParamsBrowse = [
  634. 'profile' => $iProfileId,
  635. 'object' => $sObject
  636. ];
  637. if(!empty($aParams['params']) && is_array($aParams['params']))
  638. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  639. return $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  640. }
  641. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  642. {
  643. if($this->_bIsApi)
  644. $aParams = bx_api_get_browse_params($aParams);
  645. if(!$iProfileId)
  646. $iProfileId = bx_get_logged_profile_id();
  647. if(!$iProfileId)
  648. return '';
  649. $aParams = array_merge([
  650. 'params' => false,
  651. 'design_box' => BX_DB_PADDING_DEF,
  652. 'empty_message' => false,
  653. 'ajax_paginate' => true,
  654. ], $aParams);
  655. $aParamsBrowse = [
  656. 'profile' => $iProfileId,
  657. 'object' => 'sys_profiles_friends',
  658. 'type' => 'content',
  659. 'mutual' => false,
  660. 'profile2' => 0
  661. ];
  662. if(!empty($aParams['params']) && is_array($aParams['params']))
  663. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  664. return $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  665. }
  666. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  667. {
  668. if($this->_bIsApi)
  669. $aParams = bx_api_get_browse_params($aParams, true);
  670. if(!$iProfileId)
  671. $iProfileId = bx_get_logged_profile_id();
  672. if(!$iProfileId)
  673. return '';
  674. $aParams = array_merge([
  675. 'empty_message' => false,
  676. 'start' => 0,
  677. 'per_page' => 0
  678. ], $aParams);
  679. if(($iStartGet = bx_get('start')) !== false)
  680. $aParams['start'] = (int)$iStartGet;
  681. if(($iPerPageGet = bx_get('per_page')) !== false)
  682. $aParams['per_page'] = (int)$iPerPageGet;
  683. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  684. if(!$oRecommendation)
  685. return false;
  686. if(bx_is_api()) {
  687. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams);
  688. $aData = array_merge($aData, [
  689. 'module' => 'system',
  690. 'unit' => 'mixed',
  691. 'request_url' => '/api.php?r=system/browse_recommendations_friends/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  692. ]);
  693. return [bx_api_get_block('browse', $aData)];
  694. }
  695. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  696. if(!$sCode && $aParams['empty_message'])
  697. $sCode = MsgBox(_t('_Empty'));
  698. return $sCode;
  699. }
  700. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  701. {
  702. if($this->_bIsApi)
  703. $aParams = bx_api_get_browse_params($aParams, true);
  704. if(!$iProfileId)
  705. $iProfileId = bx_get_logged_profile_id();
  706. if(!$iProfileId)
  707. return '';
  708. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  709. $aParams = array_merge([
  710. 'empty_message' => false,
  711. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  712. 'start' => 0,
  713. 'per_page' => 0
  714. ], $aParams);
  715. if(($iStartGet = bx_get('start')) !== false)
  716. $aParams['start'] = (int)$iStartGet;
  717. if(($iPerPageGet = bx_get('per_page')) !== false)
  718. $aParams['per_page'] = (int)$iPerPageGet;
  719. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  720. if(!$oRecommendation)
  721. return false;
  722. if(bx_is_api()) {
  723. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams);
  724. $aData = array_merge($aData, [
  725. 'module' => 'system',
  726. 'unit' => 'mixed',
  727. 'request_url' => '/api.php?r=system/browse_recommendations_subscriptions/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  728. ]);
  729. return [bx_api_get_block('browse', $aData)];
  730. }
  731. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  732. if(!$sCode && $aParams['empty_message'])
  733. $sCode = MsgBox(_t('_Empty'));
  734. return $sCode;
  735. }
  736. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  737. {
  738. $oTemplate = BxDolTemplate::getInstance();
  739. $oProfilesQuery = BxDolProfileQuery::getInstance();
  740. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  741. if (!$aProfiles)
  742. return false;
  743. if (null === $iActiveProfileId)
  744. $iActiveProfileId = bx_get_logged_profile_id();
  745. $oModuleDb = BxDolModuleQuery::getInstance();
  746. $aVars = array (
  747. 'bx_repeat:row' => array(),
  748. );
  749. bx_alert('system', 'account_profile_switcher', 0, false, array(
  750. 'account_id' => $iAccountId,
  751. 'active_profile_id' => &$iActiveProfileId,
  752. 'url_profile_action' => &$sUrlProfileAction,
  753. 'show_all' => &$bShowAll,
  754. 'button_title' => &$sButtonTitle,
  755. 'profile_template' => &$sProfileTemplate,
  756. 'profiles' => &$aProfiles
  757. ));
  758. foreach ($aProfiles as $aProfile) {
  759. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  760. continue;
  761. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  762. continue;
  763. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  764. continue;
  765. if($this->_bIsApi){
  766. $o = BxDolProfile::getInstance($aProfile['id']);
  767. $aProfilesData[] = [
  768. 'id' => $o->id(),
  769. 'display_name' => $o->getDisplayName(),
  770. 'url' => bx_api_get_relative_url($o->getUrl()),
  771. 'avatar' => $o->getAvatar()
  772. ];
  773. }
  774. $aVars['bx_repeat:row'][] = array (
  775. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  776. 'bx_if:active' => array (
  777. 'condition' => $iActiveProfileId == $aProfile['id'],
  778. 'content' => array('id' => $aProfile['id']),
  779. ),
  780. 'bx_if:inactive' => array (
  781. 'condition' => $iActiveProfileId != $aProfile['id'],
  782. 'content' => array(
  783. 'id' => $aProfile['id'],
  784. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  785. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  786. ),
  787. ),
  788. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  789. );
  790. }
  791. if($this->_bIsApi){
  792. return [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
  793. }
  794. $oTemplate->addCss('account.css');
  795. return array(
  796. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  797. );
  798. }
  799. public function serviceAccountProfileSwitcherAll ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = true, $sButtonTitle = '', $sProfileTemplate = '')
  800. {
  801. return $this->serviceAccountProfileSwitcher($iAccountId, $iActiveProfileId, $sUrlProfileAction, $bShowAll, $sButtonTitle, $sProfileTemplate);
  802. }
  803. public function serviceProfileSettingsCfilter($iProfileId = false)
  804. {
  805. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  806. BxDolTemplate::getInstance()->displayPageNotFound();
  807. exit;
  808. }
  809. // set settings submenu
  810. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  811. if ($oMenuSubmenu) {
  812. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  813. 'title' => _t('_sys_menu_item_title_account_settings'),
  814. 'link' => BX_DOL_URL_ROOT . 'member.php',
  815. 'icon' => '',
  816. ));
  817. }
  818. if($iProfileId === false)
  819. $iProfileId = bx_get_logged_profile_id();
  820. $oProfile = BxDolProfile::getInstance($iProfileId);
  821. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  822. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  823. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  824. $sForm = 'sys_profile';
  825. $sFormDisplay = 'sys_profile_cf_set';
  826. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  827. if(!$oForm)
  828. return MsgBox(_t('_sys_txt_error_occured'));
  829. $oForm->initChecker($aProfileInfo);
  830. if(!$oForm->isSubmittedAndValid())
  831. return $sCode = $oForm->getCode();
  832. if(!$oForm->update($iProfileId)) {
  833. if (!$oForm->isValid())
  834. return $oForm->getCode();
  835. else
  836. return MsgBox(_t('_sys_txt_error_profile_update'));
  837. }
  838. // display result message
  839. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  840. return $sMsg . $oForm->getCode();
  841. }
  842. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  843. {
  844. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  845. return false;
  846. $iAge = 0;
  847. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  848. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  849. $iResult = 0;
  850. switch($iValue) {
  851. case 1:
  852. $iResult = $iValue;
  853. break;
  854. case 2:
  855. if($iAge >= 6)
  856. $iResult = $iValue;
  857. break;
  858. case 3:
  859. if($iAge >= 13)
  860. $iResult = $iValue;
  861. break;
  862. case 4:
  863. if($iAge >= 17)
  864. $iResult = $iValue;
  865. break;
  866. case 5:
  867. if($iAge >= 21)
  868. $iResult = $iValue;
  869. break;
  870. }
  871. if($iResult != 0)
  872. $iResult = 1 << ($iResult - 1);
  873. return $iResult;
  874. }
  875. public function serviceIsEnabledCfilter()
  876. {
  877. return BxDolContentFilter::getInstance()->isEnabled();
  878. }
  879. protected function _getIcon ($sIcon)
  880. {
  881. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  882. }
  883. protected function _getLatestModuleTimestamp ()
  884. {
  885. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  886. if (empty($aModules))
  887. return 0;
  888. $aModuleNewest = array_pop($aModules);
  889. return $aModuleNewest['date'];
  890. }
  891. protected function _getProfilesModules($bForceActAsProfile = true)
  892. {
  893. $aRet = array();
  894. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  895. foreach($aModules as $aModule) {
  896. $oModule = BxDolModule::getInstance($aModule['name']);
  897. if(!($oModule instanceof iBxDolProfileService))
  898. continue;
  899. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  900. continue;
  901. $aRet[] = $aModule;
  902. }
  903. return $aRet;
  904. }
  905. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  906. {
  907. $sClass = 'BxTemplProfileSearchResult';
  908. bx_import($sClass);
  909. $o = new $sClass($sMode, $aParams);
  910. $o->setDesignBoxTemplateId($iDesignBox);
  911. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  912. $o->setAjaxPaginate($bAjaxPaginate);
  913. $o->setUnitParams(['context' => $sMode]);
  914. if ($o->isError)
  915. return '';
  916. if ($s = $o->processing()) {
  917. if(!bx_is_api())
  918. return $s;
  919. return [bx_api_get_block('browse', $s)];
  920. }
  921. else
  922. return '';
  923. }
  924. }
  925. /** @} */