BxBaseServiceProfiles.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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(bx_is_api()){
  116. $oMenu = BxDolMenu::getObjectInstance('sys_site');
  117. return [
  118. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI())
  119. ];
  120. }
  121. if(($oMenu = BxDolMenu::getObjectInstance('sys_profile_stats')) !== false) {
  122. return $oMenu->getCode();
  123. }
  124. else
  125. return '';
  126. }
  127. public function serviceProfileFollowings ($iProfileId = 0)
  128. {
  129. if(($oMenu = BxDolMenu::getObjectInstance('sys_profile_followings')) !== false)
  130. return $oMenu->getCode();
  131. else
  132. return '';
  133. }
  134. public function serviceBefriend ($sParams)
  135. {
  136. $aParams = json_decode($sParams, true);
  137. $iProfileId = $aParams['profile_id'];
  138. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  139. $oProfile = BxDolProfile::getInstance($iProfileId);
  140. if ($aParams['action'] == 'info'){
  141. return ['profile' => BxDolProfile::getData($iProfileId), 'result' => $oConnection->isConnected((int)bx_get_logged_profile_id(), $iProfileId, true)];
  142. }
  143. if ($oProfile && $oConnection){
  144. if ($oConnection->isConnected((int)bx_get_logged_profile_id(), $iProfileId, true)){
  145. return ['profile' => BxDolProfile::getData($iProfileId), 'result' => false];
  146. }
  147. else{
  148. $oConnection->addConnection((int)bx_get_logged_profile_id(), $iProfileId);
  149. $oConnection->addConnection($iProfileId, (int)bx_get_logged_profile_id());
  150. $oProfile = BxDolProfile::getInstance($iProfileId);
  151. return ['profile' => BxDolProfile::getData($iProfileId), 'result' => true];
  152. }
  153. }
  154. }
  155. public function serviceProfileStats ($iProfileId = 0)
  156. {
  157. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  158. return '';
  159. $oProfile = BxDolProfile::getInstance($iProfileId);
  160. if(!$oProfile)
  161. return '';
  162. $oMenu = BxDolMenu::getObjectInstance('sys_profile_stats');
  163. if(!$oMenu)
  164. return '';
  165. if(bx_is_api())
  166. return [
  167. bx_api_get_block ('profile_avatar', $oProfile->getData(), ['id' => 1]),
  168. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI(), ['id' => 2])
  169. ];
  170. $oAcl = BxDolAcl::getInstance();
  171. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  172. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  173. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
  174. $aVars = array(
  175. 'profile_id' => $oProfile->id(),
  176. 'profile_url' => $oProfile->getUrl(),
  177. 'profile_edit_url' => $oProfile->getEditUrl(),
  178. 'profile_title' => $oProfile->getDisplayName(),
  179. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  180. 'profile_ava_url' => $oProfile->getAvatar(),
  181. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  182. 'name' => 'unit_wo_info',
  183. 'size' => 'ava'
  184. ))),
  185. 'profile_acl_title' => _t($aAclInfo['name']),
  186. 'menu' => $oMenu->getCode(),
  187. );
  188. $aVars['bx_if:image'] = array (
  189. 'condition' => (bool)$sIconUrl,
  190. 'content' => array('icon_url' => $sIconUrl),
  191. );
  192. $aVars['bx_if:image_inline'] = array (
  193. 'condition' => false,
  194. 'content' => array('image' => ''),
  195. );
  196. $aVars['bx_if:icon'] = array (
  197. 'condition' => (bool)$sIcon,
  198. 'content' => array('icon' => $sIcon),
  199. );
  200. $aVars['bx_if:icon-html'] = array (
  201. 'condition' => (bool)$sIconHtml,
  202. 'content' => array('icon' => $sIconHtml),
  203. );
  204. $aVars['bx_if:icon-a'] = array (
  205. 'condition' => (bool)$sIconA,
  206. 'content' => array('icon-a' => $sIconA),
  207. );
  208. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  209. }
  210. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  211. {
  212. /**
  213. * Disabled.
  214. */
  215. return '';
  216. if(empty($sCaption))
  217. $sCaption = _t('_Edit');
  218. $oProfile = BxDolProfile::getInstance($iProfileId);
  219. if(!$oProfile)
  220. return '';
  221. $sModule = $oProfile->getModule();
  222. $sMethod = 'profile_edit_url';
  223. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  224. return '';
  225. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  226. }
  227. public function serviceGetMenuAddonProfileConnections($iProfileId = 0)
  228. {
  229. $oProfile = BxDolProfile::getInstance($iProfileId);
  230. if(!$oProfile)
  231. return '';
  232. $iCount = 0;
  233. //--- Add friend requests.
  234. $iCount += (int)bx_srv('system', 'get_unconfirmed_connections_num', ['sys_profiles_friends'], 'TemplServiceConnections');
  235. //--- Add subscriptions.
  236. $iCount += (int)bx_srv('system', 'get_connected_content_num', ['sys_profiles_subscriptions'], 'TemplServiceConnections');
  237. //--- Add subscribed me.
  238. $iCount += (int)bx_srv('system', 'get_connected_initiators_num', ['sys_profiles_subscriptions'], 'TemplServiceConnections');
  239. return $iCount > 0 ? $iCount : '';
  240. }
  241. /**
  242. * @page service Service Calls
  243. * @section bx_system_general System Services
  244. * @subsection bx_system_general-profiles Profiles
  245. * @subsubsection bx_system_general-profile_membership profile_membership
  246. *
  247. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  248. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  249. *
  250. * Get membership level for specified profile.
  251. * @param $iProfileId profile ID
  252. *
  253. * @see BxBaseServiceProfiles::serviceProfileMembership
  254. */
  255. /**
  256. * @ref bx_system_general-profile_membership "profile_membership"
  257. */
  258. public function serviceProfileMembership ($iProfileId = 0)
  259. {
  260. if (!$iProfileId)
  261. $iProfileId = bx_get_logged_profile_id();
  262. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  263. }
  264. public function serviceProfileMembershipStats ($iProfileId = 0)
  265. {
  266. if(!$iProfileId)
  267. $iProfileId = bx_get_logged_profile_id();
  268. $sTxtUnlimited = _t('_unlimited');
  269. $sTxtFiles = _t('_sys_storage_files');
  270. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  271. $aTmplVarsStats = array();
  272. if(!empty($aQuota['quota_size'])) {
  273. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  274. $sPercent = $iWidth . '%';
  275. }
  276. else {
  277. $iWidth = 0;
  278. $sPercent = $sTxtUnlimited;
  279. }
  280. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  281. if(!empty($aQuota['quota_number'])) {
  282. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  283. $sPercent = $iWidth . '%';
  284. }
  285. else {
  286. $iWidth = 0;
  287. $sPercent = $sTxtUnlimited;
  288. }
  289. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  290. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  291. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  292. 'bx_repeat:stat_items' => $aTmplVarsStats
  293. ));
  294. }
  295. /**
  296. * @page service Service Calls
  297. * @section bx_system_general System Services
  298. * @subsection bx_system_general-profiles Profiles
  299. * @subsubsection bx_system_general-profile_notifications profile_notifications
  300. *
  301. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  302. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  303. *
  304. * Get number of unread notifications for logged in profile
  305. *
  306. * @see BxBaseServiceProfiles::serviceProfileNotifications
  307. */
  308. /**
  309. * @ref bx_system_general-profile_notifications "profile_notifications"
  310. */
  311. public function serviceProfileNotifications ($iProfileId = 0)
  312. {
  313. if (!$iProfileId)
  314. $iProfileId = bx_get_logged_profile_id();
  315. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  316. if(!$oMenu)
  317. return 0;
  318. $iNum = 0;
  319. $aMenuItems = $oMenu->getMenuItems ();
  320. foreach ($aMenuItems as $r) {
  321. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  322. $iNum += $r['bx_if:addon']['content']['addon'];
  323. }
  324. return $iNum;
  325. }
  326. /**
  327. * @page service Service Calls
  328. * @section bx_system_general System Services
  329. * @subsection bx_system_general-profiles Profiles
  330. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  331. *
  332. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  333. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  334. *
  335. * Get number of online profiles
  336. *
  337. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  338. */
  339. /**
  340. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  341. */
  342. public function serviceGetCountOnlineProfiles ()
  343. {
  344. $oProfilesQuery = BxDolProfileQuery::getInstance();
  345. return $oProfilesQuery->getOnlineCount();
  346. }
  347. public function serviceGetProfilesModules ($bForceActAsProfile = true)
  348. {
  349. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  350. $oDb = BxDolDb::getInstance();
  351. $oCache = $oDb->getDbCacheObject ();
  352. $sKey = $oDb->genDbCacheKey('profiles_' . (!$bForceActAsProfile ? 'all_' : '') . 'modules_array');
  353. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  354. $mixedRetTs = $oCache->getData($sKeyTs);
  355. $mixedRet = $oCache->getData($sKey);
  356. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  357. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  358. $aModulesArray = $mixedRet;
  359. } else {
  360. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  361. $oCache->setData($sKey, $aModulesArray);
  362. $oCache->setData($sKeyTs, $iNewestModuleTs);
  363. }
  364. } else {
  365. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  366. }
  367. return $aModulesArray;
  368. }
  369. public function serviceProfilesFriends ($iLimit = 20)
  370. {
  371. if (!($iProfileId = bx_get_logged_profile_id()))
  372. return array();
  373. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  374. if (!$oConnection)
  375. return array();
  376. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  377. return array();
  378. $aRet = array();
  379. foreach ($a as $iId) {
  380. $oProfile = BxDolProfile::getInstance($iId);
  381. $aRet[] = array (
  382. 'label' => $oProfile->getDisplayName(),
  383. 'value' => $iId,
  384. 'url' => $oProfile->getUrl(),
  385. 'thumb' => $oProfile->getThumb(),
  386. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  387. );
  388. }
  389. return $aRet;
  390. }
  391. public function serviceProfilesSearch ($sTerm, $mixedParams = [])
  392. {
  393. $bParams = is_array($mixedParams);
  394. $iLimit = 0;
  395. if((is_numeric($mixedParams) && ($_iLimit = (int)$mixedParams)) || ($bParams && isset($mixedParams['limit']) && ($_iLimit = (int)$mixedParams['limit'])))
  396. $iLimit = $_iLimit;
  397. else
  398. $iLimit = (int)getParam('sys_profiles_search_limit');
  399. // display friends by default
  400. if (!$sTerm)
  401. return $this->serviceProfilesFriends($iLimit);
  402. // get list of "profiles" modules
  403. $aModules = $this->serviceGetProfilesModules();
  404. $mixedSearchParams = (int)getParam('sys_per_page_search_keyword_single');
  405. if($bParams && !empty($mixedParams['search_params']) && is_array($mixedParams['search_params']))
  406. $mixedSearchParams = [
  407. 'search_params' => $mixedParams['search_params'],
  408. 'limit' => $mixedSearchParams
  409. ];
  410. // search in each module
  411. $a = [];
  412. foreach ($aModules as $aModule) {
  413. if(!bx_srv($aModule['name'], 'act_as_profile'))
  414. continue;
  415. $a = array_merge($a, bx_srv($aModule['name'], 'profiles_search', [$sTerm, $mixedSearchParams]));
  416. }
  417. // sort result
  418. $sOrderField = $mixedParams['order_by'] ?? 'label';
  419. usort($a, function($r1, $r2) use ($sOrderField) {
  420. if($sOrderField == 'weight')
  421. return -((int)$r1[$sOrderField] - (int)$r2[$sOrderField]);
  422. else
  423. return strcmp($r1[$sOrderField], $r2[$sOrderField]);
  424. });
  425. /**
  426. * @hooks
  427. * @hookdef hook-system-profiles_search_by_location 'system', 'profiles_search_by_location' - hook on serach profiles by location
  428. * - $unit_name - equals `system`
  429. * - $action - equals `profiles_search_by_location`
  430. * - $object_id - not used
  431. * - $sender_id - not used
  432. * - $extra_params - array of additional params with the following array keys:
  433. * - `module` - [string] module name
  434. * - `term` - [string] term for searching
  435. * - `result` - [array] by ref, array of results, can be overridden in hook processing
  436. * @hook @ref hook-account-profiles_search_by_location
  437. */
  438. bx_alert('system', 'profiles_search', 0, 0, [
  439. 'module' => $bParams && isset($mixedParams['module']) ? $mixedParams['module'] : '',
  440. 'term' => $sTerm,
  441. 'result' => &$a
  442. ]);
  443. // return as array
  444. return array_slice($a, 0, $iLimit);
  445. }
  446. public function serviceProfilesSearchByLocation ($aLocation, $iRadius, $mixedParams = [])
  447. {
  448. $sModule = !empty($mixedParams['module']) ? $mixedParams['module'] : '';
  449. $aModules = !empty($sModule) ? [BxDolModuleDb::getInstance()->getModuleByName($sModule)] : $this->serviceGetProfilesModules();
  450. $iStart = 0;
  451. $iLimit = 20;
  452. if(is_int($mixedParams))
  453. $iLimit = (int)$mixedParams;
  454. else if(is_array($mixedParams) && isset($mixedParams['limit']))
  455. $iLimit = (int)$mixedParams['limit'];
  456. $aLocation[] = $iRadius;
  457. $oProfileQuery = BxDolProfileQuery::getInstance();
  458. $aResult = [];
  459. foreach($aModules as $aModule) {
  460. if(!bx_srv($aModule['name'], 'act_as_profile'))
  461. continue;
  462. $aContentIds = bx_srv($aModule['name'], 'get_search_result_extended', [[
  463. 'location' => ['type' => 'location_radius', 'operator' => 'locate', 'value' => [
  464. 'array' => $aLocation
  465. ]]
  466. ], $iStart, $iLimit]);
  467. $aResult = array_merge($aResult, $oProfileQuery->getProfiles(['type' => 'id_by_module_content_id', 'module' => $aModule['name'], 'content_id' => $aContentIds]));
  468. }
  469. /**
  470. * @hooks
  471. * @hookdef hook-system-profiles_search_by_location 'system', 'profiles_search_by_location' - hook on serach profiles by location
  472. * - $unit_name - equals `system`
  473. * - $action - equals `profiles_search_by_location`
  474. * - $object_id - not used
  475. * - $sender_id - not used
  476. * - $extra_params - array of additional params with the following array keys:
  477. * - `module` - [string] module name
  478. * - `location` - [array] location's info
  479. * - `radius` - [int] radius for search
  480. * - `result` - [array] by ref, array of results, can be overridden in hook processing
  481. * @hook @ref hook-account-profiles_search_by_location
  482. */
  483. bx_alert('system', 'profiles_search_by_location', 0, 0, [
  484. 'module' => $sModule,
  485. 'location' => $aLocation,
  486. 'radius' => $iRadius,
  487. 'result' => &$aResult
  488. ]);
  489. return array_slice($aResult, 0, $iLimit);
  490. }
  491. public function serviceProfilesList ($iAccountId = 0)
  492. {
  493. $oProfilesQuery = BxDolProfileQuery::getInstance();
  494. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  495. if (!$aProfiles)
  496. return false;
  497. $s = '';
  498. foreach ($aProfiles as $aProfile)
  499. if ($aProfile['type'] != 'system')
  500. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  501. if (!$s)
  502. $s = MsgBox(_t('_sys_txt_empty'));
  503. return $s . '<div class="bx-clear"></div>';
  504. }
  505. public function serviceBrowseFriends ($iProfileId = 0, $aParams = [])
  506. {
  507. if($this->_bIsApi)
  508. $aParams = bx_api_get_browse_params($aParams);
  509. if(!$iProfileId)
  510. $iProfileId = bx_get_logged_profile_id();
  511. if(!$iProfileId)
  512. return '';
  513. $aParams = array_merge([
  514. 'params' => false,
  515. 'design_box' => BX_DB_PADDING_DEF,
  516. 'empty_message' => false,
  517. 'ajax_paginate' => true,
  518. ], $aParams);
  519. $aParamsBrowse = ['profile' => $iProfileId];
  520. if(!empty($aParams['params']) && is_array($aParams['params']))
  521. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  522. return $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  523. }
  524. public function serviceProfileFriendsRequestsCount ($iProfileId = 0)
  525. {
  526. if(!$iProfileId)
  527. $iProfileId = bx_get_logged_profile_id();
  528. if(!$iProfileId)
  529. return '';
  530. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  531. if(!$oConnection)
  532. return false;
  533. return $oConnection->getConnectedInitiatorsCount($iProfileId, false) - $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  534. }
  535. public function serviceProfileSentFriendsRequestsCount ($iProfileId = 0)
  536. {
  537. if(!$iProfileId)
  538. $iProfileId = bx_get_logged_profile_id();
  539. if(!$iProfileId)
  540. return '';
  541. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  542. if(!$oConnection)
  543. return false;
  544. return $oConnection->getConnectedContentCount($iProfileId, false) - $oConnection->getConnectedContentCount($iProfileId, true);
  545. }
  546. public function serviceProfileFriendsCount ($iProfileId = 0)
  547. {
  548. if(!$iProfileId)
  549. $iProfileId = bx_get_logged_profile_id();
  550. if(!$iProfileId)
  551. return '';
  552. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  553. if(!$oConnection)
  554. return false;
  555. return $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  556. }
  557. public function serviceProfileFollowersCount ($iProfileId = 0)
  558. {
  559. if(!$iProfileId)
  560. $iProfileId = bx_get_logged_profile_id();
  561. if(!$iProfileId)
  562. return '';
  563. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  564. if(!$oConnection)
  565. return false;
  566. return $oConnection->getConnectedInitiatorsCount($iProfileId);
  567. }
  568. public function serviceProfileFollowingCount ($iProfileId = 0, $bProfilesOnly = false)
  569. {
  570. if(!$iProfileId)
  571. $iProfileId = bx_get_logged_profile_id();
  572. if(!$iProfileId)
  573. return '';
  574. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  575. if(!$oConnection)
  576. return false;
  577. if(!$bProfilesOnly)
  578. return $oConnection->getConnectedContentCount($iProfileId);
  579. $aProfileModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  580. $aProfileModulesNames = array_map(function($item) {
  581. return $item['name'];
  582. }, $aProfileModules);
  583. return $oConnection->getConnectedContentCountExt($iProfileId, false, ['by_type' => $aProfileModulesNames]);
  584. }
  585. public function serviceProfileRecommendationFollowingCount ($iProfileId = 0, $aParams = [])
  586. {
  587. if(!$iProfileId)
  588. $iProfileId = bx_get_logged_profile_id();
  589. if(!$iProfileId)
  590. return '';
  591. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  592. if(!$oRecommendation)
  593. return false;
  594. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  595. return $oRecommendation->getCount($iProfileId, array_merge([
  596. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  597. ], $aParams));
  598. }
  599. public function serviceProfileRecommendationFriendsCount ($iProfileId = 0, $aParams = [])
  600. {
  601. if(!$iProfileId)
  602. $iProfileId = bx_get_logged_profile_id();
  603. if(!$iProfileId)
  604. return '';
  605. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  606. if(!$oRecommendation)
  607. return false;
  608. return $oRecommendation->getCount($iProfileId, $aParams);
  609. }
  610. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  611. {
  612. if($this->_bIsApi)
  613. $aParams = bx_api_get_browse_params($aParams);
  614. if(!$iProfileId)
  615. $iProfileId = bx_get_logged_profile_id();
  616. if(!$iProfileId)
  617. return '';
  618. $aParams = array_merge([
  619. 'params' => false,
  620. 'design_box' => BX_DB_PADDING_DEF,
  621. 'empty_message' => false,
  622. 'ajax_paginate' => true,
  623. ], $aParams);
  624. $aParamsBrowse = ['profile' => $iProfileId];
  625. if(!empty($aParams['params']) && is_array($aParams['params']))
  626. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  627. BxDolProfileQuery::getInstance()->updateProfileTrack($iProfileId, 'view_friend_requests');
  628. return $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  629. }
  630. public function serviceBrowseFriendRequested ($iProfileId = 0, $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 = ['profile' => $iProfileId];
  645. if(!empty($aParams['params']) && is_array($aParams['params']))
  646. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  647. return $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  648. }
  649. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  650. {
  651. if($this->_bIsApi)
  652. $aParams = bx_api_get_browse_params($aParams);
  653. if(!$iProfileId)
  654. $iProfileId = bx_get_logged_profile_id();
  655. if(!$iProfileId)
  656. return '';
  657. $aParams = array_merge([
  658. 'params' => false,
  659. 'design_box' => BX_DB_PADDING_DEF,
  660. 'empty_message' => false,
  661. 'ajax_paginate' => true,
  662. ], $aParams);
  663. $aParamsBrowse = ['profile' => $iProfileId];
  664. if(!empty($aParams['params']) && is_array($aParams['params']))
  665. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  666. return $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  667. }
  668. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  669. {
  670. if($this->_bIsApi)
  671. $aParams = bx_api_get_browse_params($aParams);
  672. if(!$iProfileId)
  673. $iProfileId = bx_get_logged_profile_id();
  674. if(!$iProfileId)
  675. return '';
  676. $aParams = array_merge([
  677. 'params' => false,
  678. 'design_box' => BX_DB_PADDING_DEF,
  679. 'empty_message' => false,
  680. 'ajax_paginate' => true,
  681. ], $aParams);
  682. $aParamsBrowse = ['profile' => $iProfileId];
  683. if(!empty($aParams['params']) && is_array($aParams['params']))
  684. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  685. return $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  686. }
  687. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  688. {
  689. if($this->_bIsApi)
  690. $aParams = bx_api_get_browse_params($aParams);
  691. if(!$iProfileId)
  692. $iProfileId = bx_get_logged_profile_id();
  693. if(!$iProfileId)
  694. return '';
  695. $aParams = array_merge([
  696. 'params' => false,
  697. 'design_box' => BX_DB_PADDING_DEF,
  698. 'empty_message' => false,
  699. 'ajax_paginate' => true,
  700. ], $aParams);
  701. $aParamsBrowse = [
  702. 'profile' => $iProfileId,
  703. 'object' => $sObject
  704. ];
  705. if(!empty($aParams['params']) && is_array($aParams['params']))
  706. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  707. return $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  708. }
  709. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  710. {
  711. if($this->_bIsApi)
  712. $aParams = bx_api_get_browse_params($aParams);
  713. if(!$iProfileId)
  714. $iProfileId = bx_get_logged_profile_id();
  715. if(!$iProfileId)
  716. return '';
  717. $aParams = array_merge([
  718. 'params' => false,
  719. 'design_box' => BX_DB_PADDING_DEF,
  720. 'empty_message' => false,
  721. 'ajax_paginate' => true,
  722. ], $aParams);
  723. $aParamsBrowse = [
  724. 'profile' => $iProfileId,
  725. 'object' => 'sys_profiles_friends',
  726. 'type' => 'content',
  727. 'mutual' => false,
  728. 'profile2' => 0
  729. ];
  730. if(!empty($aParams['params']) && is_array($aParams['params']))
  731. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  732. return $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  733. }
  734. public function serviceBrowseInvitations ($iProfileId = 0, $aParams = [])
  735. {
  736. $sMode = 'invitations';
  737. if($this->_bIsApi)
  738. $aParams = bx_api_get_browse_params($aParams);
  739. $iLoggedId = bx_get_logged_profile_id();
  740. if(!$iProfileId)
  741. $iProfileId = $iLoggedId;
  742. if(!$iProfileId || $iProfileId != $iLoggedId)
  743. return '';
  744. $aParams = array_merge([
  745. 'params' => false,
  746. 'design_box' => BX_DB_PADDING_DEF,
  747. 'empty_message' => false,
  748. 'ajax_paginate' => true,
  749. ], $aParams);
  750. $aParamsBrowse = [
  751. 'profile' => $iProfileId,
  752. ];
  753. if(!empty($aParams['params']) && is_array($aParams['params']))
  754. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  755. $aModules = [];
  756. if(empty($aParamsBrowse['modules']) || !is_array($aParamsBrowse['modules'])) {
  757. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  758. if(!empty($aModules) && is_array($aModules))
  759. $aModules = array_keys($aModules);
  760. }
  761. else
  762. $aModules = $aParamsBrowse['modules'];
  763. $sApiContentType = 'browse';
  764. $bApiBrowseSimple = $bApiGetData = false;
  765. if($this->_bIsApi) {
  766. $aBlock = BxDolPage::getBlockProcessing();
  767. $sApiContentType = 'browse';
  768. if(($sK = 'config_api') && $aBlock[$sK] && is_array($aBlock[$sK]))
  769. $sApiContentType = $aBlock[$sK]['content_type'] ?? $sApiContentType;
  770. $bApiBrowseSimple = $sApiContentType == 'browse_simple';
  771. $bApiGetData = !defined('BX_API_PAGE') || $bApiBrowseSimple;
  772. }
  773. $aData = $this->_bIsApi ? ['queries' => [], 'limit' => ''] : [];
  774. foreach($aModules as $sModule) {
  775. $o = new BxTemplProfileSearchResult($sMode, array_merge($aParamsBrowse, ['module' => $sModule]));
  776. $o->setDesignBoxTemplateId($aParams['design_box']);
  777. $o->setDisplayEmptyMsg($aParams['empty_message']);
  778. $o->setAjaxPaginate($aParams['ajax_paginate']);
  779. $o->setUnitParams(['context' => $sMode]);
  780. if($o->isError)
  781. continue;
  782. if($this->_bIsApi && $bApiGetData) {
  783. $aQuery = $o->getSearchQuery($sModule);
  784. if(!empty($aQuery['query']))
  785. $aData['queries'][] = $aQuery['query'];
  786. if(!empty($aQuery['limit']) && empty($aData['limit']))
  787. $aData['limit'] = $aQuery['limit'];
  788. }
  789. else
  790. $aData = array_merge($aData, $o->getSearchData());
  791. }
  792. if($this->_bIsApi) {
  793. $aDataApi = [];
  794. if($bApiGetData && $aData && $aData['queries']) {
  795. $aItems = BxDolDb::getInstance()->getAll('(' . implode(') UNION (', $aData['queries']) . ') ORDER BY `added` DESC ' . $aData['limit']);
  796. $oProfileQuery = BxDolProfileQuery::getInstance();
  797. foreach($aItems as $aItem)
  798. if(($iContextPid = (int)$aItem['id']) && ($aContextProfileInfo = $oProfileQuery->getInfoById($iContextPid))) {
  799. $sContext = $aContextProfileInfo['type'];
  800. $oContext = BxDolModule::getInstance($sContext);
  801. if(!$oContext)
  802. continue;
  803. $CNF_CONTEXT = &$oContext->_oConfig->CNF;
  804. $iContextId = (int)$aContextProfileInfo['content_id'];
  805. $aContentInfo = $oContext->serviceGetInfo($iContextId, false);
  806. $sKey = $oContext->serviceGetInvitedKey($iContextPid, $iProfileId);
  807. $sCallback = '/api.php?r=' . $sContext . '/process_invite/&params[]=' . $sKey . '&params[]=' . $iContextPid . '&params[]=';
  808. $aDataAdd = [
  809. 'callback_accept' => $sCallback . '1',
  810. 'callback_decline' => $sCallback . '0',
  811. ];
  812. $aDataAddView = [
  813. 'redirect_url' => $oContext->serviceGetLink($iContextId),
  814. 'redirect_title' => _t('_View_and_Join')
  815. ];
  816. if($oContext->serviceIsPaidJoinAvaliable($iContextPid)) {
  817. $aDataAdd = $aDataAddView;
  818. }
  819. else if(($iPtContextPid = $aContentInfo[$CNF_CONTEXT['FIELD_ALLOW_VIEW_TO']]) < 0) {
  820. $iPtContextPid = abs($iPtContextPid);
  821. $aPtContextProfileInfo = $oProfileQuery->getInfoById($iPtContextPid);
  822. if($aPtContextProfileInfo && bx_srv($aPtContextProfileInfo['type'], 'is_paid_join_avaliable', [$iPtContextPid]))
  823. $aDataAdd = $aDataAddView;
  824. }
  825. $aDataApi[] = array_merge(bx_srv($aContextProfileInfo['type'], 'get_search_result_unit', [$iContextId]), $aDataAdd);
  826. }
  827. }
  828. if(!$bApiBrowseSimple || $aDataApi)
  829. $aDataApi = [bx_api_get_block($sApiContentType, [
  830. 'module' => 'system',
  831. 'unit' => 'invitations',
  832. 'request_url' => '/api.php?r=system/browse_invitations/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]=',
  833. 'data' => $aDataApi,
  834. 'params' => [
  835. 'start' => 0,
  836. 'per_page' => 999
  837. ]
  838. ])];
  839. return $aDataApi;
  840. }
  841. else {
  842. if(!$aData && $aParams['empty_message'])
  843. return MsgBox(_t('_Empty'));
  844. $aUnitParams = [
  845. 'template' => [
  846. 'name' => 'unit_with_cover'
  847. ]
  848. ];
  849. if(isset($aParams['unit_params']))
  850. $aUnitParams = array_merge($aUnitParams, $aParams['unit_params']);
  851. return $this->serviceBrowseQuick($aData, 0, 0, [
  852. 'unit_params' => $aUnitParams
  853. ]);
  854. }
  855. }
  856. public function serviceBrowseQuick($aProfiles, $iStart = 0, $iLimit = 0, $aParams = [])
  857. {
  858. $sCode = $sPaginate = '';
  859. if($iLimit) {
  860. $aAdditionalParams = isset($aParams['additional_params']) ? $aParams['additional_params'] : [];
  861. $oPaginate = new BxTemplPaginate([
  862. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aAdditionalParams)) . ");",
  863. 'num' => count($aProfiles),
  864. 'per_page' => $iLimit,
  865. 'start' => $iStart,
  866. ]);
  867. /*
  868. * Remove last item from connection array, because we've got one more item for pagination calculations only
  869. */
  870. if(count($aProfiles) > $iLimit)
  871. array_pop($aProfiles);
  872. if($iStart || $oPaginate->getNum() > $iLimit)
  873. $sPaginate = $oPaginate->getSimplePaginate();
  874. }
  875. $bUnitParams = !empty($aParams['unit_params']) && is_array($aParams['unit_params']);
  876. foreach ($aProfiles as $mixedProfile) {
  877. $bProfile = is_array($mixedProfile);
  878. $oProfile = BxDolProfile::getInstance($bProfile ? (int)$mixedProfile['id'] : (int)$mixedProfile);
  879. if(!$oProfile)
  880. continue;
  881. $aUnitParams = ['template' => ['name' => 'unit', 'size' => 'thumb']];
  882. if(BxDolModule::getInstance($oProfile->getModule()) instanceof BxBaseModGroupsModule)
  883. $aUnitParams['template']['name'] = 'unit_wo_cover';
  884. if($bProfile && !empty($mixedProfile['info']) && is_array($mixedProfile['info']))
  885. $aUnitParams['template']['vars'] = $mixedProfile['info'];
  886. if($bUnitParams)
  887. $aUnitParams = array_merge($aUnitParams, $aParams['unit_params']);
  888. $sCode .= $oProfile->getUnit(0, $aUnitParams);
  889. }
  890. return BxDolTemplate::getInstance()->parseHtmlByName('profiles_browse_quick.html', [
  891. 'code' => $sCode,
  892. 'bx_if:show_paginate' => [
  893. 'condition' => !empty($sPaginate),
  894. 'content' => [
  895. 'paginate' => $sPaginate
  896. ]
  897. ]
  898. ]);
  899. }
  900. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  901. {
  902. if($this->_bIsApi)
  903. $aParams = bx_api_get_browse_params($aParams, true);
  904. return $this->_serviceBrowseRecommendations($iProfileId, array_merge([
  905. 'object' => 'sys_friends',
  906. 'uri' => 'browse_recommendations_friends',
  907. ], $aParams));
  908. }
  909. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  910. {
  911. if($this->_bIsApi)
  912. $aParams = bx_api_get_browse_params($aParams, true);
  913. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  914. return $this->_serviceBrowseRecommendations($iProfileId, array_merge([
  915. 'object' => 'sys_subscriptions',
  916. 'uri' => 'browse_recommendations_subscriptions',
  917. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  918. ], $aParams));
  919. }
  920. protected function _serviceBrowseRecommendations($iProfileId = 0, $aParams = [])
  921. {
  922. if(!$iProfileId)
  923. $iProfileId = bx_get_logged_profile_id();
  924. if(!$iProfileId)
  925. return '';
  926. $sObject = '';
  927. if(isset($aParams['object'])) {
  928. $sObject = $aParams['object'];
  929. unset($aParams['object']);
  930. }
  931. $sUri = '';
  932. if(isset($aParams['uri'])) {
  933. $sUri = $aParams['uri'];
  934. unset($aParams['uri']);
  935. }
  936. $aParams = array_merge([
  937. 'empty_message' => false,
  938. 'start' => 0,
  939. 'per_page' => 0
  940. ], $aParams);
  941. if(($iStartGet = bx_get('start')) !== false)
  942. $aParams['start'] = (int)$iStartGet;
  943. if(($iPerPageGet = bx_get('per_page')) !== false)
  944. $aParams['per_page'] = (int)$iPerPageGet;
  945. $oRecommendation = BxDolRecommendation::getObjectInstance($sObject);
  946. if(!$oRecommendation)
  947. return false;
  948. if($this->_bIsApi) {
  949. $aBlock = BxDolPage::getBlockProcessing();
  950. $sContentType = 'browse';
  951. if(($sK = 'config_api') && $aBlock[$sK] && is_array($aBlock[$sK]))
  952. $sContentType = $aBlock[$sK]['content_type'] ?? $sContentType;
  953. $bBrowseSimple = $sContentType == 'browse_simple';
  954. $aData = $oRecommendation->getCodeAPI($iProfileId, array_merge($aParams, ['force_get_data' => $bBrowseSimple]));
  955. if(!$bBrowseSimple || $aData['data'])
  956. $aData = [bx_api_get_block($sContentType, array_merge($aData, [
  957. 'module' => 'system',
  958. 'unit' => 'mixed',
  959. 'request_url' => '/api.php?r=system/' . $sUri . '/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  960. ]))];
  961. return $aData;
  962. }
  963. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  964. if(!$sCode && $aParams['empty_message'])
  965. $sCode = MsgBox(_t('_Empty'));
  966. return $sCode;
  967. }
  968. public function serviceSetMembership($mixedProfileId, $iAclLevelId, $iAclLevelDuration = 0)
  969. {
  970. if(!is_array($mixedProfileId))
  971. $mixedProfileId = array($mixedProfileId);
  972. $iPerformerId = bx_get_logged_profile_id();
  973. $aCheck = checkActionModule($iPerformerId, 'set acl level', 'system', false);
  974. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  975. return true;//array('code' => 1, 'msg' => $aCheck[CHECK_ACTION_MESSAGE]);
  976. $iSet = 0;
  977. $oAcl = BxDolAcl::getInstance();
  978. foreach($mixedProfileId as $iProfileId) {
  979. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, $iAclLevelDuration, true))
  980. continue;
  981. $iSet += 1;
  982. checkActionModule($iPerformerId, 'set acl level', 'system', true); // perform action
  983. }
  984. if(count($mixedProfileId) != $iSet)
  985. return true;//array('code' => 2, 'msg' => _t('_error occured'));
  986. return true;//$aResult;
  987. }
  988. public function serviceResetMembership($mixedProfileId)
  989. {
  990. if(!is_array($mixedProfileId))
  991. $mixedProfileId = [$mixedProfileId];
  992. $oAcl = BxDolAcl::getInstance();
  993. $iAclLevelId = MEMBERSHIP_ID_STANDARD;
  994. $iReset = 0;
  995. foreach($mixedProfileId as $iProfileId) {
  996. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, 0, true))
  997. continue;
  998. $iReset += 1;
  999. }
  1000. return $iReset != 0;
  1001. }
  1002. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  1003. {
  1004. if (!$iAccountId)
  1005. $iAccountId = getLoggedId();
  1006. $oProfilesQuery = BxDolProfileQuery::getInstance();
  1007. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId);
  1008. if (!$aProfiles)
  1009. return false;
  1010. if (null === $iActiveProfileId)
  1011. $iActiveProfileId = bx_get_logged_profile_id();
  1012. $oTemplate = null;
  1013. if (!$this->_bIsApi) {
  1014. $oTemplate = BxDolTemplate::getInstance();
  1015. $oTemplate->addCss('account.css');
  1016. }
  1017. $sCacheKey = "profile_switcher_{$iAccountId}_" . ($this->_bIsApi ? 1 : 0);
  1018. if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') { // cache is enabled only for default params (profile switcher in account popup)
  1019. $mixedCache = bx_content_cache_get($sCacheKey);
  1020. if ($mixedCache !== null)
  1021. return $mixedCache;
  1022. }
  1023. $oModuleDb = BxDolModuleQuery::getInstance();
  1024. $aVars = array (
  1025. 'bx_repeat:row' => array(),
  1026. );
  1027. /**
  1028. * @hooks
  1029. * @hookdef hook-system-account_profile_switcher 'system', 'account_profile_switcher' - hook to get account profile switcher
  1030. * - $unit_name - equals `system`
  1031. * - $action - equals `account_profile_switcher`
  1032. * - $object_id - not used
  1033. * - $sender_id - not used
  1034. * - $extra_params - array of additional params with the following array keys:
  1035. * - `account_id` - [int] account id
  1036. * - `active_profile_id` - [int] by ref, current profile id, can be overridden in hook processing
  1037. * - `url_profile_action` - [string] url profile action
  1038. * - `show_all` - [bool] by ref, show all profiles or not, can be overridden in hook processing
  1039. * - `button_title` - [string] by ref, title for button, can be overridden in hook processing
  1040. * - `profile_template` - [string] by ref, profile template, can be overridden in hook processing
  1041. * - `profiles` - [array] by ref, array of profiles info , can be overridden in hook processing
  1042. * @hook @ref hook-system-account_profile_switcher
  1043. */
  1044. bx_alert('system', 'account_profile_switcher', 0, false, array(
  1045. 'account_id' => $iAccountId,
  1046. 'active_profile_id' => &$iActiveProfileId,
  1047. 'url_profile_action' => &$sUrlProfileAction,
  1048. 'show_all' => &$bShowAll,
  1049. 'button_title' => &$sButtonTitle,
  1050. 'profile_template' => &$sProfileTemplate,
  1051. 'profiles' => &$aProfiles,
  1052. 'active_profile_id_ref' => &$iActiveProfileId,
  1053. 'url_profile_action_ref' => &$sUrlProfileAction,
  1054. 'show_all_ref' => &$bShowAll,
  1055. 'button_title_ref' => &$sButtonTitle,
  1056. 'profile_template_ref' => &$sProfileTemplate,
  1057. 'profiles_ref' => &$aProfiles,
  1058. ));
  1059. $aProfilesData = [];
  1060. foreach ($aProfiles as $aProfile) {
  1061. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  1062. continue;
  1063. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  1064. continue;
  1065. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  1066. continue;
  1067. if($this->_bIsApi){
  1068. $o = BxDolProfile::getInstance($aProfile['id']);
  1069. $aProfilesData[] = [
  1070. 'id' => $o->id(),
  1071. 'display_name' => $o->getDisplayName(),
  1072. 'url' => bx_api_get_relative_url($o->getUrl()),
  1073. 'avatar' => $o->getAvatar()
  1074. ];
  1075. }
  1076. $aVars['bx_repeat:row'][] = array (
  1077. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  1078. 'bx_if:active' => array (
  1079. 'condition' => $iActiveProfileId == $aProfile['id'],
  1080. 'content' => array('id' => $aProfile['id']),
  1081. ),
  1082. 'bx_if:inactive' => array (
  1083. 'condition' => $iActiveProfileId != $aProfile['id'],
  1084. 'content' => array(
  1085. 'id' => $aProfile['id'],
  1086. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  1087. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  1088. ),
  1089. ),
  1090. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  1091. );
  1092. }
  1093. if($this->_bIsApi){
  1094. $a = [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
  1095. if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
  1096. bx_content_cache_set($sCacheKey, $a);
  1097. }
  1098. return $a;
  1099. }
  1100. $a = array(
  1101. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  1102. );
  1103. if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
  1104. bx_content_cache_set($sCacheKey, $a);
  1105. }
  1106. return $a;
  1107. }
  1108. public function serviceAccountProfileSwitcherAll ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = true, $sButtonTitle = '', $sProfileTemplate = '')
  1109. {
  1110. return $this->serviceAccountProfileSwitcher($iAccountId, $iActiveProfileId, $sUrlProfileAction, $bShowAll, $sButtonTitle, $sProfileTemplate);
  1111. }
  1112. public function serviceProfileSettingsCfilter($iProfileId = false)
  1113. {
  1114. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  1115. BxDolTemplate::getInstance()->displayPageNotFound();
  1116. exit;
  1117. }
  1118. // set settings submenu
  1119. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  1120. if ($oMenuSubmenu) {
  1121. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  1122. 'title' => _t('_sys_menu_item_title_account_settings'),
  1123. 'link' => BX_DOL_URL_ROOT . 'member.php',
  1124. 'icon' => '',
  1125. ));
  1126. }
  1127. if($iProfileId === false)
  1128. $iProfileId = bx_get_logged_profile_id();
  1129. $oProfile = BxDolProfile::getInstance($iProfileId);
  1130. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  1131. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  1132. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  1133. $sForm = 'sys_profile';
  1134. $sFormDisplay = 'sys_profile_cf_set';
  1135. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  1136. if(!$oForm)
  1137. return MsgBox(_t('_sys_txt_error_occured'));
  1138. $oForm->initChecker($aProfileInfo);
  1139. if(!$oForm->isSubmittedAndValid())
  1140. return $sCode = $oForm->getCode();
  1141. if(!$oForm->update($iProfileId)) {
  1142. if (!$oForm->isValid())
  1143. return $oForm->getCode();
  1144. else
  1145. return MsgBox(_t('_sys_txt_error_profile_update'));
  1146. }
  1147. // display result message
  1148. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  1149. return $sMsg . $oForm->getCode();
  1150. }
  1151. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  1152. {
  1153. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  1154. return false;
  1155. $iAge = 0;
  1156. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  1157. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  1158. $iResult = 0;
  1159. switch($iValue) {
  1160. case 1:
  1161. $iResult = $iValue;
  1162. break;
  1163. case 2:
  1164. if($iAge >= 6)
  1165. $iResult = $iValue;
  1166. break;
  1167. case 3:
  1168. if($iAge >= 13)
  1169. $iResult = $iValue;
  1170. break;
  1171. case 4:
  1172. if($iAge >= 17)
  1173. $iResult = $iValue;
  1174. break;
  1175. case 5:
  1176. if($iAge >= 21)
  1177. $iResult = $iValue;
  1178. break;
  1179. }
  1180. if($iResult != 0)
  1181. $iResult = 1 << ($iResult - 1);
  1182. return $iResult;
  1183. }
  1184. public function serviceIsEnabledCfilter()
  1185. {
  1186. return BxDolContentFilter::getInstance()->isEnabled();
  1187. }
  1188. public function serviceProfileCounters($bInit = false)
  1189. {
  1190. if($this->_bIsApi && ($oLiveUpdates = BxDolLiveUpdates::getInstance()) !== false)
  1191. return $oLiveUpdates->performApi($bInit);
  1192. }
  1193. public function serviceProfileInfo()
  1194. {
  1195. if (isLogged()) {
  1196. $o = BxDolProfile::getInstance();
  1197. return BxDolProfile::getDataForPage($o);
  1198. }
  1199. }
  1200. public function serviceUpdateSettings($iProfileId = false, $sSettings = '')
  1201. {
  1202. $oProfile = BxDolProfile::getInstance($iProfileId);
  1203. if(!$oProfile)
  1204. return '';
  1205. $sModule = $oProfile->getModule();
  1206. $sMethod = 'profile_settings_set';
  1207. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  1208. return '';
  1209. BxDolService::call($sModule, $sMethod, [$oProfile->getContentId(), json_decode($sSettings)]);
  1210. }
  1211. protected function _getIcon ($sIcon)
  1212. {
  1213. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  1214. }
  1215. protected function _getLatestModuleTimestamp ()
  1216. {
  1217. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  1218. if (empty($aModules))
  1219. return 0;
  1220. $aModuleNewest = array_pop($aModules);
  1221. return $aModuleNewest['date'];
  1222. }
  1223. protected function _getProfilesModules($bForceActAsProfile = true)
  1224. {
  1225. $aRet = array();
  1226. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  1227. foreach($aModules as $aModule) {
  1228. $oModule = BxDolModule::getInstance($aModule['name']);
  1229. if(!($oModule instanceof iBxDolProfileService))
  1230. continue;
  1231. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  1232. continue;
  1233. $aRet[] = $aModule;
  1234. }
  1235. return $aRet;
  1236. }
  1237. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  1238. {
  1239. $sClass = 'BxTemplProfileSearchResult';
  1240. bx_import($sClass);
  1241. $o = new $sClass($sMode, $aParams);
  1242. $o->setDesignBoxTemplateId($iDesignBox);
  1243. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  1244. $o->setAjaxPaginate($bAjaxPaginate);
  1245. $o->setUnitParams(['context' => $sMode]);
  1246. if ($o->isError)
  1247. return '';
  1248. if ($s = $o->processing()) {
  1249. if(!bx_is_api())
  1250. return $s;
  1251. return [bx_api_get_block('browse', $s)];
  1252. }
  1253. else
  1254. return '';
  1255. }
  1256. }
  1257. /** @} */