BxBaseServiceProfiles.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 serviceBefriend ($iProfileId)
  133. {
  134. $oConnections = BxDolConnection::getObjectInstance('sys_profiles_friends');
  135. $oProfile = BxDolProfile::getInstance($iProfileId);
  136. if ($oProfile && $oConnections){
  137. $oConnections->addConnection((int)bx_get_logged_profile_id(), $iProfileId);
  138. $oConnections->addConnection($iProfileId, (int)bx_get_logged_profile_id());
  139. $oProfile = BxDolProfile::getInstance($iProfileId);
  140. return BxDolProfile::getData($iProfileId);
  141. }
  142. }
  143. public function serviceProfileStats ($iProfileId = 0)
  144. {
  145. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  146. return '';
  147. $oProfile = BxDolProfile::getInstance($iProfileId);
  148. if(!$oProfile)
  149. return '';
  150. $oMenu = BxDolMenu::getObjectInstance('sys_profile_stats');
  151. if(!$oMenu)
  152. return '';
  153. if(bx_is_api())
  154. return [
  155. bx_api_get_block ('profile_avatar', $oProfile->getData(), ['id' => 1]),
  156. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI(), ['id' => 2])
  157. ];
  158. $oAcl = BxDolAcl::getInstance();
  159. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  160. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  161. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
  162. $aVars = array(
  163. 'profile_id' => $oProfile->id(),
  164. 'profile_url' => $oProfile->getUrl(),
  165. 'profile_edit_url' => $oProfile->getEditUrl(),
  166. 'profile_title' => $oProfile->getDisplayName(),
  167. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  168. 'profile_ava_url' => $oProfile->getAvatar(),
  169. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  170. 'name' => 'unit_wo_info',
  171. 'size' => 'ava'
  172. ))),
  173. 'profile_acl_title' => _t($aAclInfo['name']),
  174. 'menu' => $oMenu->getCode(),
  175. );
  176. $aVars['bx_if:image'] = array (
  177. 'condition' => (bool)$sIconUrl,
  178. 'content' => array('icon_url' => $sIconUrl),
  179. );
  180. $aVars['bx_if:image_inline'] = array (
  181. 'condition' => false,
  182. 'content' => array('image' => ''),
  183. );
  184. $aVars['bx_if:icon'] = array (
  185. 'condition' => (bool)$sIcon,
  186. 'content' => array('icon' => $sIcon),
  187. );
  188. $aVars['bx_if:icon-html'] = array (
  189. 'condition' => (bool)$sIconHtml,
  190. 'content' => array('icon' => $sIconHtml),
  191. );
  192. $aVars['bx_if:icon-a'] = array (
  193. 'condition' => (bool)$sIconA,
  194. 'content' => array('icon-a' => $sIconA),
  195. );
  196. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  197. }
  198. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  199. {
  200. /**
  201. * Disabled.
  202. */
  203. return '';
  204. if(empty($sCaption))
  205. $sCaption = _t('_Edit');
  206. $oProfile = BxDolProfile::getInstance($iProfileId);
  207. if(!$oProfile)
  208. return '';
  209. $sModule = $oProfile->getModule();
  210. $sMethod = 'profile_edit_url';
  211. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  212. return '';
  213. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  214. }
  215. public function serviceGetMenuAddonProfileConnections($iProfileId = 0)
  216. {
  217. $oProfile = BxDolProfile::getInstance($iProfileId);
  218. if(!$oProfile)
  219. return '';
  220. $iCount = 0;
  221. //--- Add friend requests.
  222. $iCount += (int)bx_srv('system', 'get_unconfirmed_connections_num', ['sys_profiles_friends'], 'TemplServiceConnections');
  223. //--- Add subscriptions.
  224. $iCount += (int)bx_srv('system', 'get_connected_content_num', ['sys_profiles_subscriptions'], 'TemplServiceConnections');
  225. //--- Add subscribed me.
  226. $iCount += (int)bx_srv('system', 'get_connected_initiators_num', ['sys_profiles_subscriptions'], 'TemplServiceConnections');
  227. return $iCount > 0 ? $iCount : '';
  228. }
  229. /**
  230. * @page service Service Calls
  231. * @section bx_system_general System Services
  232. * @subsection bx_system_general-profiles Profiles
  233. * @subsubsection bx_system_general-profile_membership profile_membership
  234. *
  235. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  236. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  237. *
  238. * Get membership level for specified profile.
  239. * @param $iProfileId profile ID
  240. *
  241. * @see BxBaseServiceProfiles::serviceProfileMembership
  242. */
  243. /**
  244. * @ref bx_system_general-profile_membership "profile_membership"
  245. */
  246. public function serviceProfileMembership ($iProfileId = 0)
  247. {
  248. if (!$iProfileId)
  249. $iProfileId = bx_get_logged_profile_id();
  250. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  251. }
  252. public function serviceProfileMembershipStats ($iProfileId = 0)
  253. {
  254. if(!$iProfileId)
  255. $iProfileId = bx_get_logged_profile_id();
  256. $sTxtUnlimited = _t('_unlimited');
  257. $sTxtFiles = _t('_sys_storage_files');
  258. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  259. $aTmplVarsStats = array();
  260. if(!empty($aQuota['quota_size'])) {
  261. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  262. $sPercent = $iWidth . '%';
  263. }
  264. else {
  265. $iWidth = 0;
  266. $sPercent = $sTxtUnlimited;
  267. }
  268. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  269. if(!empty($aQuota['quota_number'])) {
  270. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  271. $sPercent = $iWidth . '%';
  272. }
  273. else {
  274. $iWidth = 0;
  275. $sPercent = $sTxtUnlimited;
  276. }
  277. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  278. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  279. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  280. 'bx_repeat:stat_items' => $aTmplVarsStats
  281. ));
  282. }
  283. /**
  284. * @page service Service Calls
  285. * @section bx_system_general System Services
  286. * @subsection bx_system_general-profiles Profiles
  287. * @subsubsection bx_system_general-profile_notifications profile_notifications
  288. *
  289. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  290. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  291. *
  292. * Get number of unread notifications for logged in profile
  293. *
  294. * @see BxBaseServiceProfiles::serviceProfileNotifications
  295. */
  296. /**
  297. * @ref bx_system_general-profile_notifications "profile_notifications"
  298. */
  299. public function serviceProfileNotifications ($iProfileId = 0)
  300. {
  301. if (!$iProfileId)
  302. $iProfileId = bx_get_logged_profile_id();
  303. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  304. if(!$oMenu)
  305. return 0;
  306. $iNum = 0;
  307. $aMenuItems = $oMenu->getMenuItems ();
  308. foreach ($aMenuItems as $r) {
  309. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  310. $iNum += $r['bx_if:addon']['content']['addon'];
  311. }
  312. return $iNum;
  313. }
  314. /**
  315. * @page service Service Calls
  316. * @section bx_system_general System Services
  317. * @subsection bx_system_general-profiles Profiles
  318. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  319. *
  320. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  321. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  322. *
  323. * Get number of online profiles
  324. *
  325. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  326. */
  327. /**
  328. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  329. */
  330. public function serviceGetCountOnlineProfiles ()
  331. {
  332. $oProfilesQuery = BxDolProfileQuery::getInstance();
  333. return $oProfilesQuery->getOnlineCount();
  334. }
  335. public function serviceGetProfilesModules ($bForceActAsProfile = true)
  336. {
  337. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  338. $oDb = BxDolDb::getInstance();
  339. $oCache = $oDb->getDbCacheObject ();
  340. $sKey = $oDb->genDbCacheKey('profiles_' . (!$bForceActAsProfile ? 'all_' : '') . 'modules_array');
  341. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  342. $mixedRetTs = $oCache->getData($sKeyTs);
  343. $mixedRet = $oCache->getData($sKey);
  344. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  345. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  346. $aModulesArray = $mixedRet;
  347. } else {
  348. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  349. $oCache->setData($sKey, $aModulesArray);
  350. $oCache->setData($sKeyTs, $iNewestModuleTs);
  351. }
  352. } else {
  353. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  354. }
  355. return $aModulesArray;
  356. }
  357. public function serviceProfilesFriends ($iLimit = 20)
  358. {
  359. if (!($iProfileId = bx_get_logged_profile_id()))
  360. return array();
  361. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  362. if (!$oConnection)
  363. return array();
  364. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  365. return array();
  366. $aRet = array();
  367. foreach ($a as $iId) {
  368. $oProfile = BxDolProfile::getInstance($iId);
  369. $aRet[] = array (
  370. 'label' => $oProfile->getDisplayName(),
  371. 'value' => $iId,
  372. 'url' => $oProfile->getUrl(),
  373. 'thumb' => $oProfile->getThumb(),
  374. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  375. );
  376. }
  377. return $aRet;
  378. }
  379. public function serviceProfilesSearch ($sTerm, $mixedParems = [])
  380. {
  381. $iLimit = (int)getParam('sys_profiles_search_limit');
  382. if(is_int($mixedParems))
  383. $iLimit = (int)$mixedParems;
  384. else if(is_array($mixedParems) && isset($mixedParems['limit']))
  385. $iLimit = (int)$mixedParems['limit'];
  386. // display friends by default
  387. if (!$sTerm)
  388. return $this->serviceProfilesFriends($iLimit);
  389. // get list of "profiles" modules
  390. $aModules = $this->serviceGetProfilesModules();
  391. // search in each module
  392. $a = array();
  393. foreach ($aModules as $aModule) {
  394. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  395. continue;
  396. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  397. }
  398. // sort result
  399. usort($a, function($r1, $r2) {
  400. return strcmp($r1['label'], $r2['label']);
  401. });
  402. bx_alert('system', 'profiles_search', 0, 0, array(
  403. 'module' => is_array($mixedParems) && isset($mixedParems['module']) ? $mixedParems['module'] : '',
  404. 'term' => $sTerm,
  405. 'result' => &$a
  406. ));
  407. // return as array
  408. return array_slice($a, 0, $iLimit);
  409. }
  410. public function serviceProfilesSearchByLocation ($aLocation, $iRadius, $mixedParems = [])
  411. {
  412. $sModule = !empty($mixedParems['module']) ? $mixedParems['module'] : '';
  413. $aModules = !empty($sModule) ? [BxDolModuleDb::getInstance()->getModuleByName($sModule)] : $this->serviceGetProfilesModules();
  414. $iStart = 0;
  415. $iLimit = 20;
  416. if(is_int($mixedParems))
  417. $iLimit = (int)$mixedParems;
  418. else if(is_array($mixedParems) && isset($mixedParems['limit']))
  419. $iLimit = (int)$mixedParems['limit'];
  420. $aLocation[] = $iRadius;
  421. $oProfileQuery = BxDolProfileQuery::getInstance();
  422. $aResult = [];
  423. foreach($aModules as $aModule) {
  424. if(!bx_srv($aModule['name'], 'act_as_profile'))
  425. continue;
  426. $aContentIds = bx_srv($aModule['name'], 'get_search_result_extended', [[
  427. 'location' => ['type' => 'location_radius', 'operator' => 'locate', 'value' => [
  428. 'array' => $aLocation
  429. ]]
  430. ], $iStart, $iLimit]);
  431. $aResult = array_merge($aResult, $oProfileQuery->getProfiles(['type' => 'id_by_module_content_id', 'module' => $aModule['name'], 'content_id' => $aContentIds]));
  432. }
  433. bx_alert('system', 'profiles_search_by_location', 0, 0, [
  434. 'module' => $sModule,
  435. 'location' => $aLocation,
  436. 'radius' => $iRadius,
  437. 'result' => &$aResult
  438. ]);
  439. return array_slice($aResult, 0, $iLimit);
  440. }
  441. public function serviceProfilesList ($iAccountId = 0)
  442. {
  443. $oProfilesQuery = BxDolProfileQuery::getInstance();
  444. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  445. if (!$aProfiles)
  446. return false;
  447. $s = '';
  448. foreach ($aProfiles as $aProfile)
  449. if ($aProfile['type'] != 'system')
  450. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  451. if (!$s)
  452. $s = MsgBox(_t('_sys_txt_empty'));
  453. return $s . '<div class="bx-clear"></div>';
  454. }
  455. public function serviceBrowseFriends ($iProfileId = 0, $aParams = [])
  456. {
  457. if($this->_bIsApi)
  458. $aParams = bx_api_get_browse_params($aParams);
  459. if(!$iProfileId)
  460. $iProfileId = bx_get_logged_profile_id();
  461. if(!$iProfileId)
  462. return '';
  463. $aParams = array_merge([
  464. 'params' => false,
  465. 'design_box' => BX_DB_PADDING_DEF,
  466. 'empty_message' => false,
  467. 'ajax_paginate' => true,
  468. ], $aParams);
  469. $aParamsBrowse = ['profile' => $iProfileId];
  470. if(!empty($aParams['params']) && is_array($aParams['params']))
  471. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  472. return $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  473. }
  474. public function serviceProfileFriendsRequestsCount ($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->getConnectedInitiatorsCount($iProfileId, false) - $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  484. }
  485. public function serviceProfileSentFriendsRequestsCount ($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->getConnectedContentCount($iProfileId, false) - $oConnection->getConnectedContentCount($iProfileId, true);
  495. }
  496. public function serviceProfileFriendsCount ($iProfileId = 0)
  497. {
  498. if(!$iProfileId)
  499. $iProfileId = bx_get_logged_profile_id();
  500. if(!$iProfileId)
  501. return '';
  502. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  503. if(!$oConnection)
  504. return false;
  505. return $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  506. }
  507. public function serviceProfileFollowersCount ($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->getConnectedInitiatorsCount($iProfileId);
  517. }
  518. public function serviceProfileFollowingCount ($iProfileId = 0)
  519. {
  520. if(!$iProfileId)
  521. $iProfileId = bx_get_logged_profile_id();
  522. if(!$iProfileId)
  523. return '';
  524. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  525. if(!$oConnection)
  526. return false;
  527. return $oConnection->getConnectedContentCount($iProfileId);
  528. }
  529. public function serviceProfileRecommendationFollowingCount ($iProfileId = 0, $aParams = [])
  530. {
  531. if(!$iProfileId)
  532. $iProfileId = bx_get_logged_profile_id();
  533. if(!$iProfileId)
  534. return '';
  535. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  536. if(!$oRecommendation)
  537. return false;
  538. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  539. return $oRecommendation->getCount($iProfileId, array_merge([
  540. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  541. ], $aParams));
  542. }
  543. public function serviceProfileRecommendationFriendsCount ($iProfileId = 0, $aParams = [])
  544. {
  545. if(!$iProfileId)
  546. $iProfileId = bx_get_logged_profile_id();
  547. if(!$iProfileId)
  548. return '';
  549. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  550. if(!$oRecommendation)
  551. return false;
  552. return $oRecommendation->getCount($iProfileId, $aParams);
  553. }
  554. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  555. {
  556. if($this->_bIsApi)
  557. $aParams = bx_api_get_browse_params($aParams);
  558. if(!$iProfileId)
  559. $iProfileId = bx_get_logged_profile_id();
  560. if(!$iProfileId)
  561. return '';
  562. $aParams = array_merge([
  563. 'params' => false,
  564. 'design_box' => BX_DB_PADDING_DEF,
  565. 'empty_message' => false,
  566. 'ajax_paginate' => true,
  567. ], $aParams);
  568. $aParamsBrowse = ['profile' => $iProfileId];
  569. if(!empty($aParams['params']) && is_array($aParams['params']))
  570. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  571. return $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  572. }
  573. public function serviceBrowseFriendRequested ($iProfileId = 0, $aParams = [])
  574. {
  575. if($this->_bIsApi)
  576. $aParams = bx_api_get_browse_params($aParams);
  577. if(!$iProfileId)
  578. $iProfileId = bx_get_logged_profile_id();
  579. if(!$iProfileId)
  580. return '';
  581. $aParams = array_merge([
  582. 'params' => false,
  583. 'design_box' => BX_DB_PADDING_DEF,
  584. 'empty_message' => false,
  585. 'ajax_paginate' => true,
  586. ], $aParams);
  587. $aParamsBrowse = ['profile' => $iProfileId];
  588. if(!empty($aParams['params']) && is_array($aParams['params']))
  589. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  590. return $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  591. }
  592. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  593. {
  594. if($this->_bIsApi)
  595. $aParams = bx_api_get_browse_params($aParams);
  596. if(!$iProfileId)
  597. $iProfileId = bx_get_logged_profile_id();
  598. if(!$iProfileId)
  599. return '';
  600. $aParams = array_merge([
  601. 'params' => false,
  602. 'design_box' => BX_DB_PADDING_DEF,
  603. 'empty_message' => false,
  604. 'ajax_paginate' => true,
  605. ], $aParams);
  606. $aParamsBrowse = ['profile' => $iProfileId];
  607. if(!empty($aParams['params']) && is_array($aParams['params']))
  608. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  609. return $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  610. }
  611. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  612. {
  613. if($this->_bIsApi)
  614. $aParams = bx_api_get_browse_params($aParams);
  615. if(!$iProfileId)
  616. $iProfileId = bx_get_logged_profile_id();
  617. if(!$iProfileId)
  618. return '';
  619. $aParams = array_merge([
  620. 'params' => false,
  621. 'design_box' => BX_DB_PADDING_DEF,
  622. 'empty_message' => false,
  623. 'ajax_paginate' => true,
  624. ], $aParams);
  625. $aParamsBrowse = ['profile' => $iProfileId];
  626. if(!empty($aParams['params']) && is_array($aParams['params']))
  627. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  628. return $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  629. }
  630. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  631. {
  632. if($this->_bIsApi)
  633. $aParams = bx_api_get_browse_params($aParams);
  634. if(!$iProfileId)
  635. $iProfileId = bx_get_logged_profile_id();
  636. if(!$iProfileId)
  637. return '';
  638. $aParams = array_merge([
  639. 'params' => false,
  640. 'design_box' => BX_DB_PADDING_DEF,
  641. 'empty_message' => false,
  642. 'ajax_paginate' => true,
  643. ], $aParams);
  644. $aParamsBrowse = [
  645. 'profile' => $iProfileId,
  646. 'object' => $sObject
  647. ];
  648. if(!empty($aParams['params']) && is_array($aParams['params']))
  649. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  650. return $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  651. }
  652. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  653. {
  654. if($this->_bIsApi)
  655. $aParams = bx_api_get_browse_params($aParams);
  656. if(!$iProfileId)
  657. $iProfileId = bx_get_logged_profile_id();
  658. if(!$iProfileId)
  659. return '';
  660. $aParams = array_merge([
  661. 'params' => false,
  662. 'design_box' => BX_DB_PADDING_DEF,
  663. 'empty_message' => false,
  664. 'ajax_paginate' => true,
  665. ], $aParams);
  666. $aParamsBrowse = [
  667. 'profile' => $iProfileId,
  668. 'object' => 'sys_profiles_friends',
  669. 'type' => 'content',
  670. 'mutual' => false,
  671. 'profile2' => 0
  672. ];
  673. if(!empty($aParams['params']) && is_array($aParams['params']))
  674. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  675. return $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  676. }
  677. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  678. {
  679. if($this->_bIsApi)
  680. $aParams = bx_api_get_browse_params($aParams, true);
  681. if(!$iProfileId)
  682. $iProfileId = bx_get_logged_profile_id();
  683. if(!$iProfileId)
  684. return '';
  685. $aParams = array_merge([
  686. 'empty_message' => false,
  687. 'start' => 0,
  688. 'per_page' => 0
  689. ], $aParams);
  690. if(($iStartGet = bx_get('start')) !== false)
  691. $aParams['start'] = (int)$iStartGet;
  692. if(($iPerPageGet = bx_get('per_page')) !== false)
  693. $aParams['per_page'] = (int)$iPerPageGet;
  694. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  695. if(!$oRecommendation)
  696. return false;
  697. if(bx_is_api()) {
  698. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams);
  699. $aData = array_merge($aData, [
  700. 'module' => 'system',
  701. 'unit' => 'mixed',
  702. 'request_url' => '/api.php?r=system/browse_recommendations_friends/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  703. ]);
  704. return [bx_api_get_block('browse', $aData)];
  705. }
  706. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  707. if(!$sCode && $aParams['empty_message'])
  708. $sCode = MsgBox(_t('_Empty'));
  709. return $sCode;
  710. }
  711. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  712. {
  713. if($this->_bIsApi)
  714. $aParams = bx_api_get_browse_params($aParams, true);
  715. if(!$iProfileId)
  716. $iProfileId = bx_get_logged_profile_id();
  717. if(!$iProfileId)
  718. return '';
  719. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  720. $aParams = array_merge([
  721. 'empty_message' => false,
  722. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  723. 'start' => 0,
  724. 'per_page' => 0
  725. ], $aParams);
  726. if(($iStartGet = bx_get('start')) !== false)
  727. $aParams['start'] = (int)$iStartGet;
  728. if(($iPerPageGet = bx_get('per_page')) !== false)
  729. $aParams['per_page'] = (int)$iPerPageGet;
  730. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  731. if(!$oRecommendation)
  732. return false;
  733. if(bx_is_api()) {
  734. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams);
  735. $aData = array_merge($aData, [
  736. 'module' => 'system',
  737. 'unit' => 'mixed',
  738. 'request_url' => '/api.php?r=system/browse_recommendations_subscriptions/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  739. ]);
  740. return [bx_api_get_block('browse', $aData)];
  741. }
  742. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  743. if(!$sCode && $aParams['empty_message'])
  744. $sCode = MsgBox(_t('_Empty'));
  745. return $sCode;
  746. }
  747. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  748. {
  749. $oTemplate = BxDolTemplate::getInstance();
  750. $oProfilesQuery = BxDolProfileQuery::getInstance();
  751. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  752. if (!$aProfiles)
  753. return false;
  754. if (null === $iActiveProfileId)
  755. $iActiveProfileId = bx_get_logged_profile_id();
  756. $oModuleDb = BxDolModuleQuery::getInstance();
  757. $aVars = array (
  758. 'bx_repeat:row' => array(),
  759. );
  760. bx_alert('system', 'account_profile_switcher', 0, false, array(
  761. 'account_id' => $iAccountId,
  762. 'active_profile_id' => &$iActiveProfileId,
  763. 'url_profile_action' => &$sUrlProfileAction,
  764. 'show_all' => &$bShowAll,
  765. 'button_title' => &$sButtonTitle,
  766. 'profile_template' => &$sProfileTemplate,
  767. 'profiles' => &$aProfiles
  768. ));
  769. foreach ($aProfiles as $aProfile) {
  770. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  771. continue;
  772. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  773. continue;
  774. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  775. continue;
  776. if($this->_bIsApi){
  777. $o = BxDolProfile::getInstance($aProfile['id']);
  778. $aProfilesData[] = [
  779. 'id' => $o->id(),
  780. 'display_name' => $o->getDisplayName(),
  781. 'url' => bx_api_get_relative_url($o->getUrl()),
  782. 'avatar' => $o->getAvatar()
  783. ];
  784. }
  785. $aVars['bx_repeat:row'][] = array (
  786. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  787. 'bx_if:active' => array (
  788. 'condition' => $iActiveProfileId == $aProfile['id'],
  789. 'content' => array('id' => $aProfile['id']),
  790. ),
  791. 'bx_if:inactive' => array (
  792. 'condition' => $iActiveProfileId != $aProfile['id'],
  793. 'content' => array(
  794. 'id' => $aProfile['id'],
  795. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  796. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  797. ),
  798. ),
  799. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  800. );
  801. }
  802. if($this->_bIsApi){
  803. return [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
  804. }
  805. $oTemplate->addCss('account.css');
  806. return array(
  807. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  808. );
  809. }
  810. public function serviceAccountProfileSwitcherAll ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = true, $sButtonTitle = '', $sProfileTemplate = '')
  811. {
  812. return $this->serviceAccountProfileSwitcher($iAccountId, $iActiveProfileId, $sUrlProfileAction, $bShowAll, $sButtonTitle, $sProfileTemplate);
  813. }
  814. public function serviceProfileSettingsCfilter($iProfileId = false)
  815. {
  816. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  817. BxDolTemplate::getInstance()->displayPageNotFound();
  818. exit;
  819. }
  820. // set settings submenu
  821. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  822. if ($oMenuSubmenu) {
  823. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  824. 'title' => _t('_sys_menu_item_title_account_settings'),
  825. 'link' => BX_DOL_URL_ROOT . 'member.php',
  826. 'icon' => '',
  827. ));
  828. }
  829. if($iProfileId === false)
  830. $iProfileId = bx_get_logged_profile_id();
  831. $oProfile = BxDolProfile::getInstance($iProfileId);
  832. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  833. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  834. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  835. $sForm = 'sys_profile';
  836. $sFormDisplay = 'sys_profile_cf_set';
  837. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  838. if(!$oForm)
  839. return MsgBox(_t('_sys_txt_error_occured'));
  840. $oForm->initChecker($aProfileInfo);
  841. if(!$oForm->isSubmittedAndValid())
  842. return $sCode = $oForm->getCode();
  843. if(!$oForm->update($iProfileId)) {
  844. if (!$oForm->isValid())
  845. return $oForm->getCode();
  846. else
  847. return MsgBox(_t('_sys_txt_error_profile_update'));
  848. }
  849. // display result message
  850. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  851. return $sMsg . $oForm->getCode();
  852. }
  853. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  854. {
  855. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  856. return false;
  857. $iAge = 0;
  858. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  859. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  860. $iResult = 0;
  861. switch($iValue) {
  862. case 1:
  863. $iResult = $iValue;
  864. break;
  865. case 2:
  866. if($iAge >= 6)
  867. $iResult = $iValue;
  868. break;
  869. case 3:
  870. if($iAge >= 13)
  871. $iResult = $iValue;
  872. break;
  873. case 4:
  874. if($iAge >= 17)
  875. $iResult = $iValue;
  876. break;
  877. case 5:
  878. if($iAge >= 21)
  879. $iResult = $iValue;
  880. break;
  881. }
  882. if($iResult != 0)
  883. $iResult = 1 << ($iResult - 1);
  884. return $iResult;
  885. }
  886. public function serviceIsEnabledCfilter()
  887. {
  888. return BxDolContentFilter::getInstance()->isEnabled();
  889. }
  890. public function serviceUpdateSettings($iProfileId = false, $sSettings = '')
  891. {
  892. $oProfile = BxDolProfile::getInstance($iProfileId);
  893. if(!$oProfile)
  894. return '';
  895. $sModule = $oProfile->getModule();
  896. $sMethod = 'profile_settings_set';
  897. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  898. return '';
  899. BxDolService::call($sModule, $sMethod, [$oProfile->getContentId(), json_decode($sSettings)]);
  900. }
  901. protected function _getIcon ($sIcon)
  902. {
  903. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  904. }
  905. protected function _getLatestModuleTimestamp ()
  906. {
  907. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  908. if (empty($aModules))
  909. return 0;
  910. $aModuleNewest = array_pop($aModules);
  911. return $aModuleNewest['date'];
  912. }
  913. protected function _getProfilesModules($bForceActAsProfile = true)
  914. {
  915. $aRet = array();
  916. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  917. foreach($aModules as $aModule) {
  918. $oModule = BxDolModule::getInstance($aModule['name']);
  919. if(!($oModule instanceof iBxDolProfileService))
  920. continue;
  921. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  922. continue;
  923. $aRet[] = $aModule;
  924. }
  925. return $aRet;
  926. }
  927. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  928. {
  929. $sClass = 'BxTemplProfileSearchResult';
  930. bx_import($sClass);
  931. $o = new $sClass($sMode, $aParams);
  932. $o->setDesignBoxTemplateId($iDesignBox);
  933. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  934. $o->setAjaxPaginate($bAjaxPaginate);
  935. $o->setUnitParams(['context' => $sMode]);
  936. if ($o->isError)
  937. return '';
  938. if ($s = $o->processing()) {
  939. if(!bx_is_api())
  940. return $s;
  941. return [bx_api_get_block('browse', $s)];
  942. }
  943. else
  944. return '';
  945. }
  946. }
  947. /** @} */