BxBaseServiceProfiles.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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. usort($a, function($r1, $r2) {
  419. return strcmp($r1['label'], $r2['label']);
  420. });
  421. /**
  422. * @hooks
  423. * @hookdef hook-system-profiles_search_by_location 'system', 'profiles_search_by_location' - hook on serach profiles by location
  424. * - $unit_name - equals `system`
  425. * - $action - equals `profiles_search_by_location`
  426. * - $object_id - not used
  427. * - $sender_id - not used
  428. * - $extra_params - array of additional params with the following array keys:
  429. * - `module` - [string] module name
  430. * - `term` - [string] term for searching
  431. * - `result` - [array] by ref, array of results, can be overridden in hook processing
  432. * @hook @ref hook-account-profiles_search_by_location
  433. */
  434. bx_alert('system', 'profiles_search', 0, 0, [
  435. 'module' => $bParams && isset($mixedParams['module']) ? $mixedParams['module'] : '',
  436. 'term' => $sTerm,
  437. 'result' => &$a
  438. ]);
  439. // return as array
  440. return array_slice($a, 0, $iLimit);
  441. }
  442. public function serviceProfilesSearchByLocation ($aLocation, $iRadius, $mixedParams = [])
  443. {
  444. $sModule = !empty($mixedParams['module']) ? $mixedParams['module'] : '';
  445. $aModules = !empty($sModule) ? [BxDolModuleDb::getInstance()->getModuleByName($sModule)] : $this->serviceGetProfilesModules();
  446. $iStart = 0;
  447. $iLimit = 20;
  448. if(is_int($mixedParams))
  449. $iLimit = (int)$mixedParams;
  450. else if(is_array($mixedParams) && isset($mixedParams['limit']))
  451. $iLimit = (int)$mixedParams['limit'];
  452. $aLocation[] = $iRadius;
  453. $oProfileQuery = BxDolProfileQuery::getInstance();
  454. $aResult = [];
  455. foreach($aModules as $aModule) {
  456. if(!bx_srv($aModule['name'], 'act_as_profile'))
  457. continue;
  458. $aContentIds = bx_srv($aModule['name'], 'get_search_result_extended', [[
  459. 'location' => ['type' => 'location_radius', 'operator' => 'locate', 'value' => [
  460. 'array' => $aLocation
  461. ]]
  462. ], $iStart, $iLimit]);
  463. $aResult = array_merge($aResult, $oProfileQuery->getProfiles(['type' => 'id_by_module_content_id', 'module' => $aModule['name'], 'content_id' => $aContentIds]));
  464. }
  465. /**
  466. * @hooks
  467. * @hookdef hook-system-profiles_search_by_location 'system', 'profiles_search_by_location' - hook on serach profiles by location
  468. * - $unit_name - equals `system`
  469. * - $action - equals `profiles_search_by_location`
  470. * - $object_id - not used
  471. * - $sender_id - not used
  472. * - $extra_params - array of additional params with the following array keys:
  473. * - `module` - [string] module name
  474. * - `location` - [array] location's info
  475. * - `radius` - [int] radius for search
  476. * - `result` - [array] by ref, array of results, can be overridden in hook processing
  477. * @hook @ref hook-account-profiles_search_by_location
  478. */
  479. bx_alert('system', 'profiles_search_by_location', 0, 0, [
  480. 'module' => $sModule,
  481. 'location' => $aLocation,
  482. 'radius' => $iRadius,
  483. 'result' => &$aResult
  484. ]);
  485. return array_slice($aResult, 0, $iLimit);
  486. }
  487. public function serviceProfilesList ($iAccountId = 0)
  488. {
  489. $oProfilesQuery = BxDolProfileQuery::getInstance();
  490. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  491. if (!$aProfiles)
  492. return false;
  493. $s = '';
  494. foreach ($aProfiles as $aProfile)
  495. if ($aProfile['type'] != 'system')
  496. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  497. if (!$s)
  498. $s = MsgBox(_t('_sys_txt_empty'));
  499. return $s . '<div class="bx-clear"></div>';
  500. }
  501. public function serviceBrowseFriends ($iProfileId = 0, $aParams = [])
  502. {
  503. if($this->_bIsApi)
  504. $aParams = bx_api_get_browse_params($aParams);
  505. if(!$iProfileId)
  506. $iProfileId = bx_get_logged_profile_id();
  507. if(!$iProfileId)
  508. return '';
  509. $aParams = array_merge([
  510. 'params' => false,
  511. 'design_box' => BX_DB_PADDING_DEF,
  512. 'empty_message' => false,
  513. 'ajax_paginate' => true,
  514. ], $aParams);
  515. $aParamsBrowse = ['profile' => $iProfileId];
  516. if(!empty($aParams['params']) && is_array($aParams['params']))
  517. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  518. return $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  519. }
  520. public function serviceProfileFriendsRequestsCount ($iProfileId = 0)
  521. {
  522. if(!$iProfileId)
  523. $iProfileId = bx_get_logged_profile_id();
  524. if(!$iProfileId)
  525. return '';
  526. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  527. if(!$oConnection)
  528. return false;
  529. return $oConnection->getConnectedInitiatorsCount($iProfileId, false) - $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  530. }
  531. public function serviceProfileSentFriendsRequestsCount ($iProfileId = 0)
  532. {
  533. if(!$iProfileId)
  534. $iProfileId = bx_get_logged_profile_id();
  535. if(!$iProfileId)
  536. return '';
  537. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  538. if(!$oConnection)
  539. return false;
  540. return $oConnection->getConnectedContentCount($iProfileId, false) - $oConnection->getConnectedContentCount($iProfileId, true);
  541. }
  542. public function serviceProfileFriendsCount ($iProfileId = 0)
  543. {
  544. if(!$iProfileId)
  545. $iProfileId = bx_get_logged_profile_id();
  546. if(!$iProfileId)
  547. return '';
  548. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  549. if(!$oConnection)
  550. return false;
  551. return $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  552. }
  553. public function serviceProfileFollowersCount ($iProfileId = 0)
  554. {
  555. if(!$iProfileId)
  556. $iProfileId = bx_get_logged_profile_id();
  557. if(!$iProfileId)
  558. return '';
  559. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  560. if(!$oConnection)
  561. return false;
  562. return $oConnection->getConnectedInitiatorsCount($iProfileId);
  563. }
  564. public function serviceProfileFollowingCount ($iProfileId = 0, $bProfilesOnly = false)
  565. {
  566. if(!$iProfileId)
  567. $iProfileId = bx_get_logged_profile_id();
  568. if(!$iProfileId)
  569. return '';
  570. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  571. if(!$oConnection)
  572. return false;
  573. if(!$bProfilesOnly)
  574. return $oConnection->getConnectedContentCount($iProfileId);
  575. $aProfileModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  576. $aProfileModulesNames = array_map(function($item) {
  577. return $item['name'];
  578. }, $aProfileModules);
  579. return $oConnection->getConnectedContentCountExt($iProfileId, false, ['by_type' => $aProfileModulesNames]);
  580. }
  581. public function serviceProfileRecommendationFollowingCount ($iProfileId = 0, $aParams = [])
  582. {
  583. if(!$iProfileId)
  584. $iProfileId = bx_get_logged_profile_id();
  585. if(!$iProfileId)
  586. return '';
  587. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions');
  588. if(!$oRecommendation)
  589. return false;
  590. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  591. return $oRecommendation->getCount($iProfileId, array_merge([
  592. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  593. ], $aParams));
  594. }
  595. public function serviceProfileRecommendationFriendsCount ($iProfileId = 0, $aParams = [])
  596. {
  597. if(!$iProfileId)
  598. $iProfileId = bx_get_logged_profile_id();
  599. if(!$iProfileId)
  600. return '';
  601. $oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends');
  602. if(!$oRecommendation)
  603. return false;
  604. return $oRecommendation->getCount($iProfileId, $aParams);
  605. }
  606. public function serviceBrowseFriendRequests ($iProfileId = 0, $aParams = [])
  607. {
  608. if($this->_bIsApi)
  609. $aParams = bx_api_get_browse_params($aParams);
  610. if(!$iProfileId)
  611. $iProfileId = bx_get_logged_profile_id();
  612. if(!$iProfileId)
  613. return '';
  614. $aParams = array_merge([
  615. 'params' => false,
  616. 'design_box' => BX_DB_PADDING_DEF,
  617. 'empty_message' => false,
  618. 'ajax_paginate' => true,
  619. ], $aParams);
  620. $aParamsBrowse = ['profile' => $iProfileId];
  621. if(!empty($aParams['params']) && is_array($aParams['params']))
  622. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  623. BxDolProfileQuery::getInstance()->updateProfileTrack($iProfileId, 'view_friend_requests');
  624. return $this->_serviceBrowseConnections('friend_requests', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  625. }
  626. public function serviceBrowseFriendRequested ($iProfileId = 0, $aParams = [])
  627. {
  628. if($this->_bIsApi)
  629. $aParams = bx_api_get_browse_params($aParams);
  630. if(!$iProfileId)
  631. $iProfileId = bx_get_logged_profile_id();
  632. if(!$iProfileId)
  633. return '';
  634. $aParams = array_merge([
  635. 'params' => false,
  636. 'design_box' => BX_DB_PADDING_DEF,
  637. 'empty_message' => false,
  638. 'ajax_paginate' => true,
  639. ], $aParams);
  640. $aParamsBrowse = ['profile' => $iProfileId];
  641. if(!empty($aParams['params']) && is_array($aParams['params']))
  642. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  643. return $this->_serviceBrowseConnections('friend_requested', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  644. }
  645. public function serviceBrowseSubscriptions ($iProfileId = 0, $aParams = [])
  646. {
  647. if($this->_bIsApi)
  648. $aParams = bx_api_get_browse_params($aParams);
  649. if(!$iProfileId)
  650. $iProfileId = bx_get_logged_profile_id();
  651. if(!$iProfileId)
  652. return '';
  653. $aParams = array_merge([
  654. 'params' => false,
  655. 'design_box' => BX_DB_PADDING_DEF,
  656. 'empty_message' => false,
  657. 'ajax_paginate' => true,
  658. ], $aParams);
  659. $aParamsBrowse = ['profile' => $iProfileId];
  660. if(!empty($aParams['params']) && is_array($aParams['params']))
  661. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  662. return $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  663. }
  664. public function serviceBrowseSubscribedMe ($iProfileId = 0, $aParams = [])
  665. {
  666. if($this->_bIsApi)
  667. $aParams = bx_api_get_browse_params($aParams);
  668. if(!$iProfileId)
  669. $iProfileId = bx_get_logged_profile_id();
  670. if(!$iProfileId)
  671. return '';
  672. $aParams = array_merge([
  673. 'params' => false,
  674. 'design_box' => BX_DB_PADDING_DEF,
  675. 'empty_message' => false,
  676. 'ajax_paginate' => true,
  677. ], $aParams);
  678. $aParamsBrowse = ['profile' => $iProfileId];
  679. if(!empty($aParams['params']) && is_array($aParams['params']))
  680. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  681. return $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  682. }
  683. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  684. {
  685. if($this->_bIsApi)
  686. $aParams = bx_api_get_browse_params($aParams);
  687. if(!$iProfileId)
  688. $iProfileId = bx_get_logged_profile_id();
  689. if(!$iProfileId)
  690. return '';
  691. $aParams = array_merge([
  692. 'params' => false,
  693. 'design_box' => BX_DB_PADDING_DEF,
  694. 'empty_message' => false,
  695. 'ajax_paginate' => true,
  696. ], $aParams);
  697. $aParamsBrowse = [
  698. 'profile' => $iProfileId,
  699. 'object' => $sObject
  700. ];
  701. if(!empty($aParams['params']) && is_array($aParams['params']))
  702. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  703. return $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  704. }
  705. public function serviceBrowseConnections ($iProfileId = 0, $aParams = [])
  706. {
  707. if($this->_bIsApi)
  708. $aParams = bx_api_get_browse_params($aParams);
  709. if(!$iProfileId)
  710. $iProfileId = bx_get_logged_profile_id();
  711. if(!$iProfileId)
  712. return '';
  713. $aParams = array_merge([
  714. 'params' => false,
  715. 'design_box' => BX_DB_PADDING_DEF,
  716. 'empty_message' => false,
  717. 'ajax_paginate' => true,
  718. ], $aParams);
  719. $aParamsBrowse = [
  720. 'profile' => $iProfileId,
  721. 'object' => 'sys_profiles_friends',
  722. 'type' => 'content',
  723. 'mutual' => false,
  724. 'profile2' => 0
  725. ];
  726. if(!empty($aParams['params']) && is_array($aParams['params']))
  727. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  728. return $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  729. }
  730. public function serviceBrowseInvitations ($iProfileId = 0, $aParams = [])
  731. {
  732. $sMode = 'invitations';
  733. if($this->_bIsApi)
  734. $aParams = bx_api_get_browse_params($aParams);
  735. $iLoggedId = bx_get_logged_profile_id();
  736. if(!$iProfileId)
  737. $iProfileId = $iLoggedId;
  738. if(!$iProfileId || $iProfileId != $iLoggedId)
  739. return '';
  740. $aParams = array_merge([
  741. 'params' => false,
  742. 'design_box' => BX_DB_PADDING_DEF,
  743. 'empty_message' => false,
  744. 'ajax_paginate' => true,
  745. ], $aParams);
  746. $aParamsBrowse = [
  747. 'profile' => $iProfileId,
  748. ];
  749. if(!empty($aParams['params']) && is_array($aParams['params']))
  750. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  751. $aModules = [];
  752. if(empty($aParamsBrowse['modules']) || !is_array($aParamsBrowse['modules'])) {
  753. $aModules = bx_srv('system', 'get_modules_by_type', ['context', ['name_as_key' => true]]);
  754. if(!empty($aModules) && is_array($aModules))
  755. $aModules = array_keys($aModules);
  756. }
  757. else
  758. $aModules = $aParamsBrowse['modules'];
  759. $sApiContentType = 'browse';
  760. $bApiBrowseSimple = $bApiGetData = false;
  761. if($this->_bIsApi) {
  762. $aBlock = BxDolPage::getBlockProcessing();
  763. $sApiContentType = 'browse';
  764. if(($sK = 'config_api') && $aBlock[$sK] && is_array($aBlock[$sK]))
  765. $sApiContentType = $aBlock[$sK]['content_type'] ?? $sApiContentType;
  766. $bApiBrowseSimple = $sApiContentType == 'browse_simple';
  767. $bApiGetData = !defined('BX_API_PAGE') || $bApiBrowseSimple;
  768. }
  769. $aData = $this->_bIsApi ? ['queries' => [], 'limit' => ''] : [];
  770. foreach($aModules as $sModule) {
  771. $o = new BxTemplProfileSearchResult($sMode, array_merge($aParamsBrowse, ['module' => $sModule]));
  772. $o->setDesignBoxTemplateId($aParams['design_box']);
  773. $o->setDisplayEmptyMsg($aParams['empty_message']);
  774. $o->setAjaxPaginate($aParams['ajax_paginate']);
  775. $o->setUnitParams(['context' => $sMode]);
  776. if($o->isError)
  777. continue;
  778. if($this->_bIsApi && $bApiGetData) {
  779. $aQuery = $o->getSearchQuery($sModule);
  780. if(!empty($aQuery['query']))
  781. $aData['queries'][] = $aQuery['query'];
  782. if(!empty($aQuery['limit']) && empty($aData['limit']))
  783. $aData['limit'] = $aQuery['limit'];
  784. }
  785. else
  786. $aData = array_merge($aData, $o->getSearchData());
  787. }
  788. if($this->_bIsApi) {
  789. $aDataApi = [];
  790. if($bApiGetData) {
  791. $aItems = BxDolDb::getInstance()->getAll('(' . implode(') UNION (', $aData['queries']) . ') ORDER BY `added` DESC ' . $aData['limit']);
  792. $oProfileQuery = BxDolProfileQuery::getInstance();
  793. foreach($aItems as $aItem)
  794. if(($iContextPid = (int)$aItem['id']) && ($aContextProfileInfo = $oProfileQuery->getInfoById($iContextPid))) {
  795. $sContext = $aContextProfileInfo['type'];
  796. $oContext = BxDolModule::getInstance($sContext);
  797. if(!$oContext)
  798. continue;
  799. $CNF_CONTEXT = &$oContext->_oConfig->CNF;
  800. $iContextId = (int)$aContextProfileInfo['content_id'];
  801. $aContentInfo = $oContext->serviceGetInfo($iContextId, false);
  802. $sKey = $oContext->serviceGetInvitedKey($iContextPid, $iProfileId);
  803. $sCallback = '/api.php?r=' . $sContext . '/process_invite/&params[]=' . $sKey . '&params[]=' . $iContextPid . '&params[]=';
  804. $aDataAdd = [
  805. 'callback_accept' => $sCallback . '1',
  806. 'callback_decline' => $sCallback . '0',
  807. ];
  808. $aDataAddView = [
  809. 'redirect_url' => $oContext->serviceGetLink($iContextId),
  810. 'redirect_title' => _t('_View_and_Join')
  811. ];
  812. if($oContext->serviceIsPaidJoinAvaliable($iContextPid)) {
  813. $aDataAdd = $aDataAddView;
  814. }
  815. else if(($iPtContextPid = $aContentInfo[$CNF_CONTEXT['FIELD_ALLOW_VIEW_TO']]) < 0) {
  816. $iPtContextPid = abs($iPtContextPid);
  817. $aPtContextProfileInfo = $oProfileQuery->getInfoById($iPtContextPid);
  818. if($aPtContextProfileInfo && bx_srv($aPtContextProfileInfo['type'], 'is_paid_join_avaliable', [$iPtContextPid]))
  819. $aDataAdd = $aDataAddView;
  820. }
  821. $aDataApi[] = array_merge(bx_srv($aContextProfileInfo['type'], 'get_search_result_unit', [$iContextId]), $aDataAdd);
  822. }
  823. }
  824. if(!$bApiBrowseSimple || $aDataApi)
  825. $aDataApi = [bx_api_get_block($sApiContentType, [
  826. 'module' => 'system',
  827. 'unit' => 'invitations',
  828. 'request_url' => '/api.php?r=system/browse_invitations/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]=',
  829. 'data' => $aDataApi,
  830. 'params' => [
  831. 'start' => 0,
  832. 'per_page' => 999
  833. ]
  834. ])];
  835. return $aDataApi;
  836. }
  837. else {
  838. if(!$aData && $aParams['empty_message'])
  839. return MsgBox(_t('_Empty'));
  840. $aUnitParams = [
  841. 'template' => [
  842. 'name' => 'unit_with_cover'
  843. ]
  844. ];
  845. if(isset($aParams['unit_params']))
  846. $aUnitParams = array_merge($aUnitParams, $aParams['unit_params']);
  847. return $this->serviceBrowseQuick($aData, 0, 0, [
  848. 'unit_params' => $aUnitParams
  849. ]);
  850. }
  851. }
  852. public function serviceBrowseQuick($aProfiles, $iStart = 0, $iLimit = 0, $aParams = [])
  853. {
  854. $sCode = $sPaginate = '';
  855. if($iLimit) {
  856. $aAdditionalParams = isset($aParams['additional_params']) ? $aParams['additional_params'] : [];
  857. $oPaginate = new BxTemplPaginate([
  858. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aAdditionalParams)) . ");",
  859. 'num' => count($aProfiles),
  860. 'per_page' => $iLimit,
  861. 'start' => $iStart,
  862. ]);
  863. /*
  864. * Remove last item from connection array, because we've got one more item for pagination calculations only
  865. */
  866. if(count($aProfiles) > $iLimit)
  867. array_pop($aProfiles);
  868. if($iStart || $oPaginate->getNum() > $iLimit)
  869. $sPaginate = $oPaginate->getSimplePaginate();
  870. }
  871. $bUnitParams = !empty($aParams['unit_params']) && is_array($aParams['unit_params']);
  872. foreach ($aProfiles as $mixedProfile) {
  873. $bProfile = is_array($mixedProfile);
  874. $oProfile = BxDolProfile::getInstance($bProfile ? (int)$mixedProfile['id'] : (int)$mixedProfile);
  875. if(!$oProfile)
  876. continue;
  877. $aUnitParams = ['template' => ['name' => 'unit', 'size' => 'thumb']];
  878. if(BxDolModule::getInstance($oProfile->getModule()) instanceof BxBaseModGroupsModule)
  879. $aUnitParams['template']['name'] = 'unit_wo_cover';
  880. if($bProfile && !empty($mixedProfile['info']) && is_array($mixedProfile['info']))
  881. $aUnitParams['template']['vars'] = $mixedProfile['info'];
  882. if($bUnitParams)
  883. $aUnitParams = array_merge($aUnitParams, $aParams['unit_params']);
  884. $sCode .= $oProfile->getUnit(0, $aUnitParams);
  885. }
  886. return BxDolTemplate::getInstance()->parseHtmlByName('profiles_browse_quick.html', [
  887. 'code' => $sCode,
  888. 'bx_if:show_paginate' => [
  889. 'condition' => !empty($sPaginate),
  890. 'content' => [
  891. 'paginate' => $sPaginate
  892. ]
  893. ]
  894. ]);
  895. }
  896. public function serviceBrowseRecommendationsFriends ($iProfileId = 0, $aParams = [])
  897. {
  898. if($this->_bIsApi)
  899. $aParams = bx_api_get_browse_params($aParams, true);
  900. return $this->_serviceBrowseRecommendations($iProfileId, array_merge([
  901. 'object' => 'sys_friends',
  902. 'uri' => 'browse_recommendations_friends',
  903. ], $aParams));
  904. }
  905. public function serviceBrowseRecommendationsSubscriptions ($iProfileId = 0, $aParams = [])
  906. {
  907. if($this->_bIsApi)
  908. $aParams = bx_api_get_browse_params($aParams, true);
  909. $aModules = bx_srv('system', 'get_modules_by_type', ['profile', ['name_as_key' => true]]);
  910. return $this->_serviceBrowseRecommendations($iProfileId, array_merge([
  911. 'object' => 'sys_subscriptions',
  912. 'uri' => 'browse_recommendations_subscriptions',
  913. 'type' => !empty($aModules) && is_array($aModules) ? array_keys($aModules) : '',
  914. ], $aParams));
  915. }
  916. protected function _serviceBrowseRecommendations($iProfileId = 0, $aParams = [])
  917. {
  918. if(!$iProfileId)
  919. $iProfileId = bx_get_logged_profile_id();
  920. if(!$iProfileId)
  921. return '';
  922. $sObject = '';
  923. if(isset($aParams['object'])) {
  924. $sObject = $aParams['object'];
  925. unset($aParams['object']);
  926. }
  927. $sUri = '';
  928. if(isset($aParams['uri'])) {
  929. $sUri = $aParams['uri'];
  930. unset($aParams['uri']);
  931. }
  932. $aParams = array_merge([
  933. 'empty_message' => false,
  934. 'start' => 0,
  935. 'per_page' => 0
  936. ], $aParams);
  937. if(($iStartGet = bx_get('start')) !== false)
  938. $aParams['start'] = (int)$iStartGet;
  939. if(($iPerPageGet = bx_get('per_page')) !== false)
  940. $aParams['per_page'] = (int)$iPerPageGet;
  941. $oRecommendation = BxDolRecommendation::getObjectInstance($sObject);
  942. if(!$oRecommendation)
  943. return false;
  944. if($this->_bIsApi) {
  945. $aBlock = BxDolPage::getBlockProcessing();
  946. $sContentType = 'browse';
  947. if(($sK = 'config_api') && $aBlock[$sK] && is_array($aBlock[$sK]))
  948. $sContentType = $aBlock[$sK]['content_type'] ?? $sContentType;
  949. $bBrowseSimple = $sContentType == 'browse_simple';
  950. $aData = $oRecommendation->getCodeAPI($iProfileId, array_merge($aParams, ['force_get_data' => $bBrowseSimple]));
  951. if(!$bBrowseSimple || $aData['data'])
  952. $aData = [bx_api_get_block($sContentType, array_merge($aData, [
  953. 'module' => 'system',
  954. 'unit' => 'mixed',
  955. 'request_url' => '/api.php?r=system/' . $sUri . '/TemplServiceProfiles&params[]=' . $iProfileId . '&params[]='
  956. ]))];
  957. return $aData;
  958. }
  959. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  960. if(!$sCode && $aParams['empty_message'])
  961. $sCode = MsgBox(_t('_Empty'));
  962. return $sCode;
  963. }
  964. public function serviceSetMembership($mixedProfileId, $iAclLevelId, $iAclLevelDuration = 0)
  965. {
  966. if(!is_array($mixedProfileId))
  967. $mixedProfileId = array($mixedProfileId);
  968. $iPerformerId = bx_get_logged_profile_id();
  969. $aCheck = checkActionModule($iPerformerId, 'set acl level', 'system', false);
  970. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  971. return true;//array('code' => 1, 'msg' => $aCheck[CHECK_ACTION_MESSAGE]);
  972. $iSet = 0;
  973. $oAcl = BxDolAcl::getInstance();
  974. foreach($mixedProfileId as $iProfileId) {
  975. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, $iAclLevelDuration, true))
  976. continue;
  977. $iSet += 1;
  978. checkActionModule($iPerformerId, 'set acl level', 'system', true); // perform action
  979. }
  980. if(count($mixedProfileId) != $iSet)
  981. return true;//array('code' => 2, 'msg' => _t('_error occured'));
  982. return true;//$aResult;
  983. }
  984. public function serviceResetMembership($mixedProfileId)
  985. {
  986. if(!is_array($mixedProfileId))
  987. $mixedProfileId = [$mixedProfileId];
  988. $oAcl = BxDolAcl::getInstance();
  989. $iAclLevelId = MEMBERSHIP_ID_STANDARD;
  990. $iReset = 0;
  991. foreach($mixedProfileId as $iProfileId) {
  992. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, 0, true))
  993. continue;
  994. $iReset += 1;
  995. }
  996. return $iReset != 0;
  997. }
  998. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  999. {
  1000. if (!$iAccountId)
  1001. $iAccountId = getLoggedId();
  1002. $oProfilesQuery = BxDolProfileQuery::getInstance();
  1003. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId);
  1004. if (!$aProfiles)
  1005. return false;
  1006. if (null === $iActiveProfileId)
  1007. $iActiveProfileId = bx_get_logged_profile_id();
  1008. $oTemplate = null;
  1009. if (!$this->_bIsApi) {
  1010. $oTemplate = BxDolTemplate::getInstance();
  1011. $oTemplate->addCss('account.css');
  1012. }
  1013. $sCacheKey = "profile_switcher_{$iAccountId}_" . ($this->_bIsApi ? 1 : 0);
  1014. if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') { // cache is enabled only for default params (profile switcher in account popup)
  1015. $mixedCache = bx_content_cache_get($sCacheKey);
  1016. if ($mixedCache !== null)
  1017. return $mixedCache;
  1018. }
  1019. $oModuleDb = BxDolModuleQuery::getInstance();
  1020. $aVars = array (
  1021. 'bx_repeat:row' => array(),
  1022. );
  1023. /**
  1024. * @hooks
  1025. * @hookdef hook-system-account_profile_switcher 'system', 'account_profile_switcher' - hook to get account profile switcher
  1026. * - $unit_name - equals `system`
  1027. * - $action - equals `account_profile_switcher`
  1028. * - $object_id - not used
  1029. * - $sender_id - not used
  1030. * - $extra_params - array of additional params with the following array keys:
  1031. * - `account_id` - [int] account id
  1032. * - `active_profile_id` - [int] by ref, current profile id, can be overridden in hook processing
  1033. * - `url_profile_action` - [string] url profile action
  1034. * - `show_all` - [bool] by ref, show all profiles or not, can be overridden in hook processing
  1035. * - `button_title` - [string] by ref, title for button, can be overridden in hook processing
  1036. * - `profile_template` - [string] by ref, profile template, can be overridden in hook processing
  1037. * - `profiles` - [array] by ref, array of profiles info , can be overridden in hook processing
  1038. * @hook @ref hook-system-account_profile_switcher
  1039. */
  1040. bx_alert('system', 'account_profile_switcher', 0, false, array(
  1041. 'account_id' => $iAccountId,
  1042. 'active_profile_id' => &$iActiveProfileId,
  1043. 'url_profile_action' => &$sUrlProfileAction,
  1044. 'show_all' => &$bShowAll,
  1045. 'button_title' => &$sButtonTitle,
  1046. 'profile_template' => &$sProfileTemplate,
  1047. 'profiles' => &$aProfiles,
  1048. 'active_profile_id_ref' => &$iActiveProfileId,
  1049. 'url_profile_action_ref' => &$sUrlProfileAction,
  1050. 'show_all_ref' => &$bShowAll,
  1051. 'button_title_ref' => &$sButtonTitle,
  1052. 'profile_template_ref' => &$sProfileTemplate,
  1053. 'profiles_ref' => &$aProfiles,
  1054. ));
  1055. $aProfilesData = [];
  1056. foreach ($aProfiles as $aProfile) {
  1057. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  1058. continue;
  1059. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  1060. continue;
  1061. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  1062. continue;
  1063. if($this->_bIsApi){
  1064. $o = BxDolProfile::getInstance($aProfile['id']);
  1065. $aProfilesData[] = [
  1066. 'id' => $o->id(),
  1067. 'display_name' => $o->getDisplayName(),
  1068. 'url' => bx_api_get_relative_url($o->getUrl()),
  1069. 'avatar' => $o->getAvatar()
  1070. ];
  1071. }
  1072. $aVars['bx_repeat:row'][] = array (
  1073. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  1074. 'bx_if:active' => array (
  1075. 'condition' => $iActiveProfileId == $aProfile['id'],
  1076. 'content' => array('id' => $aProfile['id']),
  1077. ),
  1078. 'bx_if:inactive' => array (
  1079. 'condition' => $iActiveProfileId != $aProfile['id'],
  1080. 'content' => array(
  1081. 'id' => $aProfile['id'],
  1082. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  1083. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  1084. ),
  1085. ),
  1086. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  1087. );
  1088. }
  1089. if($this->_bIsApi){
  1090. $a = [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
  1091. if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
  1092. bx_content_cache_set($sCacheKey, $a);
  1093. }
  1094. return $a;
  1095. }
  1096. $a = array(
  1097. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  1098. );
  1099. if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
  1100. bx_content_cache_set($sCacheKey, $a);
  1101. }
  1102. return $a;
  1103. }
  1104. public function serviceAccountProfileSwitcherAll ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = true, $sButtonTitle = '', $sProfileTemplate = '')
  1105. {
  1106. return $this->serviceAccountProfileSwitcher($iAccountId, $iActiveProfileId, $sUrlProfileAction, $bShowAll, $sButtonTitle, $sProfileTemplate);
  1107. }
  1108. public function serviceProfileSettingsCfilter($iProfileId = false)
  1109. {
  1110. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  1111. BxDolTemplate::getInstance()->displayPageNotFound();
  1112. exit;
  1113. }
  1114. // set settings submenu
  1115. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  1116. if ($oMenuSubmenu) {
  1117. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  1118. 'title' => _t('_sys_menu_item_title_account_settings'),
  1119. 'link' => BX_DOL_URL_ROOT . 'member.php',
  1120. 'icon' => '',
  1121. ));
  1122. }
  1123. if($iProfileId === false)
  1124. $iProfileId = bx_get_logged_profile_id();
  1125. $oProfile = BxDolProfile::getInstance($iProfileId);
  1126. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  1127. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  1128. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  1129. $sForm = 'sys_profile';
  1130. $sFormDisplay = 'sys_profile_cf_set';
  1131. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  1132. if(!$oForm)
  1133. return MsgBox(_t('_sys_txt_error_occured'));
  1134. $oForm->initChecker($aProfileInfo);
  1135. if(!$oForm->isSubmittedAndValid())
  1136. return $sCode = $oForm->getCode();
  1137. if(!$oForm->update($iProfileId)) {
  1138. if (!$oForm->isValid())
  1139. return $oForm->getCode();
  1140. else
  1141. return MsgBox(_t('_sys_txt_error_profile_update'));
  1142. }
  1143. // display result message
  1144. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  1145. return $sMsg . $oForm->getCode();
  1146. }
  1147. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  1148. {
  1149. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  1150. return false;
  1151. $iAge = 0;
  1152. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  1153. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  1154. $iResult = 0;
  1155. switch($iValue) {
  1156. case 1:
  1157. $iResult = $iValue;
  1158. break;
  1159. case 2:
  1160. if($iAge >= 6)
  1161. $iResult = $iValue;
  1162. break;
  1163. case 3:
  1164. if($iAge >= 13)
  1165. $iResult = $iValue;
  1166. break;
  1167. case 4:
  1168. if($iAge >= 17)
  1169. $iResult = $iValue;
  1170. break;
  1171. case 5:
  1172. if($iAge >= 21)
  1173. $iResult = $iValue;
  1174. break;
  1175. }
  1176. if($iResult != 0)
  1177. $iResult = 1 << ($iResult - 1);
  1178. return $iResult;
  1179. }
  1180. public function serviceIsEnabledCfilter()
  1181. {
  1182. return BxDolContentFilter::getInstance()->isEnabled();
  1183. }
  1184. public function serviceProfileCounters()
  1185. {
  1186. if(bx_is_api() && ($oLiveUpdates = BxDolLiveUpdates::getInstance()) !== false)
  1187. return $oLiveUpdates->performApi();
  1188. }
  1189. public function serviceProfileInfo()
  1190. {
  1191. if (isLogged()) {
  1192. $o = BxDolProfile::getInstance();
  1193. return BxDolProfile::getDataForPage($o);
  1194. }
  1195. }
  1196. public function serviceUpdateSettings($iProfileId = false, $sSettings = '')
  1197. {
  1198. $oProfile = BxDolProfile::getInstance($iProfileId);
  1199. if(!$oProfile)
  1200. return '';
  1201. $sModule = $oProfile->getModule();
  1202. $sMethod = 'profile_settings_set';
  1203. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  1204. return '';
  1205. BxDolService::call($sModule, $sMethod, [$oProfile->getContentId(), json_decode($sSettings)]);
  1206. }
  1207. protected function _getIcon ($sIcon)
  1208. {
  1209. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  1210. }
  1211. protected function _getLatestModuleTimestamp ()
  1212. {
  1213. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  1214. if (empty($aModules))
  1215. return 0;
  1216. $aModuleNewest = array_pop($aModules);
  1217. return $aModuleNewest['date'];
  1218. }
  1219. protected function _getProfilesModules($bForceActAsProfile = true)
  1220. {
  1221. $aRet = array();
  1222. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  1223. foreach($aModules as $aModule) {
  1224. $oModule = BxDolModule::getInstance($aModule['name']);
  1225. if(!($oModule instanceof iBxDolProfileService))
  1226. continue;
  1227. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  1228. continue;
  1229. $aRet[] = $aModule;
  1230. }
  1231. return $aRet;
  1232. }
  1233. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  1234. {
  1235. $sClass = 'BxTemplProfileSearchResult';
  1236. bx_import($sClass);
  1237. $o = new $sClass($sMode, $aParams);
  1238. $o->setDesignBoxTemplateId($iDesignBox);
  1239. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  1240. $o->setAjaxPaginate($bAjaxPaginate);
  1241. $o->setUnitParams(['context' => $sMode]);
  1242. if ($o->isError)
  1243. return '';
  1244. if ($s = $o->processing()) {
  1245. if(!bx_is_api())
  1246. return $s;
  1247. return [bx_api_get_block('browse', $s)];
  1248. }
  1249. else
  1250. return '';
  1251. }
  1252. }
  1253. /** @} */