BxBaseServiceProfiles.php 53 KB

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