BxBaseServiceProfiles.php 39 KB

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