BxBaseServiceProfiles.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceGetProfileTypes()
  19. {
  20. $aTypes = array(
  21. '' => _t('_Select_one')
  22. );
  23. $aModules = $this->_getProfilesModules();
  24. foreach($aModules as $aModule) {
  25. if(empty($aModule) || !is_array($aModule))
  26. continue;
  27. $sTitleKey = '_' . $aModule['name'];
  28. $sTitle = _t($sTitleKey);
  29. $aTypes[$aModule['name']] = !empty($sTitle) && $sTitle != $sTitleKey ? $sTitle : $aModule['title'];
  30. }
  31. return $aTypes;
  32. }
  33. public function serviceProfileAvatar ($iProfileId = 0)
  34. {
  35. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  36. return '';
  37. $oProfile = BxDolProfile::getInstance($iProfileId);
  38. if(!$oProfile)
  39. return '';
  40. if(bx_is_api())
  41. return [
  42. bx_api_get_block ('profile_avatar', $oProfile->getData())
  43. ];
  44. $oTemplate = BxDolTemplate::getInstance();
  45. $sSwitcher = '';
  46. $aSwitcher = bx_srv('system', 'account_profile_switcher', array(false, null, '', true), 'TemplServiceProfiles');
  47. if($aSwitcher !== false)
  48. $sSwitcher = BxTemplFunctions::getInstance()->popupBox('bx-profile-switcher', _t('_sys_txt_switch_profiles'), $oTemplate->parseHtmlByName('profile_avatar_switcher.html', array(
  49. 'profile_switcher' => $aSwitcher['content'],
  50. 'bx_if:multiple_profiles_mode' => array(
  51. 'condition' => empty($aSwitcher['content']) || BxDolAccount::isAllowedCreateMultiple($oProfile->id()),
  52. 'content' => array(
  53. 'url_switch_profile' => BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher')
  54. )
  55. )
  56. )), true);
  57. $bSwitcher = !empty($sSwitcher);
  58. $sSwitcherUrl = $bSwitcher ? 'javascript:void(0)' : $sUrl;
  59. $sSwitcherOnclick = $bSwitcher ? "javascript:$('#bx-profile-switcher').dolPopup({});" : "";
  60. $sDisplayName = $oProfile->getDisplayName();
  61. $sUrl = $oProfile->getUrl();
  62. $oAcl = BxDolAcl::getInstance();
  63. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  64. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  65. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
  66. $aVars = array(
  67. 'profile_id' => $oProfile->id(),
  68. 'profile_url' => $sUrl,
  69. 'profile_edit_url' => $oProfile->getEditUrl(),
  70. 'profile_title' => $sDisplayName,
  71. 'profile_title_attr' => bx_html_attribute($sDisplayName),
  72. 'profile_ava_url' => $oProfile->getAvatar(),
  73. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  74. 'name' => 'unit_wo_info_links',
  75. 'size' => 'thumb'
  76. ))),
  77. 'profile_acl_title' => _t($aAclInfo['name']),
  78. 'bx_if:image' => array (
  79. 'condition' => (bool)$sIconUrl,
  80. 'content' => array('icon_url' => $sIconUrl),
  81. ),
  82. 'bx_if:image_inline' => array (
  83. 'condition' => false,
  84. 'content' => array('image' => ''),
  85. ),
  86. 'bx_if:icon' => array (
  87. 'condition' => (bool)$sIcon,
  88. 'content' => array('icon' => $sIcon),
  89. ),
  90. 'bx_if:icon-a' => array (
  91. 'condition' => (bool)$sIconA,
  92. 'content' => array('icon-a' => $sIconA),
  93. ),
  94. 'bx_if:icon-html' => array (
  95. 'condition' => (bool)$sIconHtml,
  96. 'content' => array('icon' => $sIconHtml),
  97. ),
  98. 'switcher_url' => $sSwitcherUrl,
  99. 'switcher_onclick' => $sSwitcherOnclick,
  100. 'bx_if:show_switcher_icon' => array(
  101. 'condition' => $bSwitcher,
  102. 'content' => array(
  103. 'switcher_url' => $sSwitcherUrl,
  104. 'switcher_onclick' => $bSwitcher ? "javascript:$('#bx-profile-switcher').dolPopup({});" : "",
  105. )
  106. ),
  107. 'switcher' => $sSwitcher
  108. );
  109. return $oTemplate->parseHtmlByName('profile_avatar.html', $aVars);
  110. }
  111. public function serviceProfileMenu ($iProfileId = 0)
  112. {
  113. if(($oMenu = BxDolMenu::getObjectInstance('sys_profile_stats')) !== false) {
  114. if(bx_is_api())
  115. return [
  116. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI())
  117. ];
  118. return $oMenu->getCode();
  119. }
  120. else
  121. return '';
  122. }
  123. public function serviceProfileFollowings ($iProfileId = 0)
  124. {
  125. if(($oMenu = BxDolMenu::getObjectInstance('sys_profile_followings')) !== false)
  126. return $oMenu->getCode();
  127. else
  128. return '';
  129. }
  130. public function serviceProfileStats ($iProfileId = 0)
  131. {
  132. if (!$iProfileId && !($iProfileId = bx_get_logged_profile_id()))
  133. return '';
  134. $oProfile = BxDolProfile::getInstance($iProfileId);
  135. if(!$oProfile)
  136. return '';
  137. $oMenu = BxDolMenu::getObjectInstance('sys_profile_stats');
  138. if(!$oMenu)
  139. return '';
  140. if(bx_is_api())
  141. return [
  142. bx_api_get_block ('profile_avatar', $oProfile->getData(), ['id' => 1]),
  143. bx_api_get_block ('profile_menu', $oMenu->getCodeAPI(), ['id' => 2])
  144. ];
  145. $oAcl = BxDolAcl::getInstance();
  146. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  147. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  148. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getIcon($aAclInfo['icon']);
  149. $aVars = array(
  150. 'profile_id' => $oProfile->id(),
  151. 'profile_url' => $oProfile->getUrl(),
  152. 'profile_edit_url' => $oProfile->getEditUrl(),
  153. 'profile_title' => $oProfile->getDisplayName(),
  154. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  155. 'profile_ava_url' => $oProfile->getAvatar(),
  156. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  157. 'name' => 'unit_wo_info',
  158. 'size' => 'ava'
  159. ))),
  160. 'profile_acl_title' => _t($aAclInfo['name']),
  161. 'menu' => $oMenu->getCode(),
  162. );
  163. $aVars['bx_if:image'] = array (
  164. 'condition' => (bool)$sIconUrl,
  165. 'content' => array('icon_url' => $sIconUrl),
  166. );
  167. $aVars['bx_if:image_inline'] = array (
  168. 'condition' => false,
  169. 'content' => array('image' => ''),
  170. );
  171. $aVars['bx_if:icon'] = array (
  172. 'condition' => (bool)$sIcon,
  173. 'content' => array('icon' => $sIcon),
  174. );
  175. $aVars['bx_if:icon-html'] = array (
  176. 'condition' => (bool)$sIconHtml,
  177. 'content' => array('icon' => $sIconHtml),
  178. );
  179. $aVars['bx_if:icon-a'] = array (
  180. 'condition' => (bool)$sIconA,
  181. 'content' => array('icon-a' => $sIconA),
  182. );
  183. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  184. }
  185. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  186. {
  187. /**
  188. * Disabled.
  189. */
  190. return '';
  191. if(empty($sCaption))
  192. $sCaption = _t('_Edit');
  193. $oProfile = BxDolProfile::getInstance($iProfileId);
  194. if(!$oProfile)
  195. return '';
  196. $sModule = $oProfile->getModule();
  197. $sMethod = 'profile_edit_url';
  198. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  199. return '';
  200. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  201. }
  202. /**
  203. * @page service Service Calls
  204. * @section bx_system_general System Services
  205. * @subsection bx_system_general-profiles Profiles
  206. * @subsubsection bx_system_general-profile_membership profile_membership
  207. *
  208. * @code bx_srv('system', 'profile_membership', [2], 'TemplServiceProfiles'); @endcode
  209. * @code {{~system:profile_membership:TemplServiceProfiles[2]~}} @endcode
  210. *
  211. * Get membership level for specified profile.
  212. * @param $iProfileId profile ID
  213. *
  214. * @see BxBaseServiceProfiles::serviceProfileMembership
  215. */
  216. /**
  217. * @ref bx_system_general-profile_membership "profile_membership"
  218. */
  219. public function serviceProfileMembership ($iProfileId = 0)
  220. {
  221. if (!$iProfileId)
  222. $iProfileId = bx_get_logged_profile_id();
  223. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  224. }
  225. public function serviceProfileMembershipStats ($iProfileId = 0)
  226. {
  227. if(!$iProfileId)
  228. $iProfileId = bx_get_logged_profile_id();
  229. $sTxtUnlimited = _t('_unlimited');
  230. $sTxtFiles = _t('_sys_storage_files');
  231. $aQuota = BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  232. $aTmplVarsStats = array();
  233. if(!empty($aQuota['quota_size'])) {
  234. $iWidth = (int)round(100 * $aQuota['current_size']/$aQuota['quota_size']);
  235. $sPercent = $iWidth . '%';
  236. }
  237. else {
  238. $iWidth = 0;
  239. $sPercent = $sTxtUnlimited;
  240. }
  241. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_size'), 'width' => $iWidth, 'value' => _t_format_size($aQuota['current_size']), 'percent' => $sPercent);
  242. if(!empty($aQuota['quota_number'])) {
  243. $iWidth = (int)round(100 * $aQuota['current_number']/$aQuota['quota_number']);
  244. $sPercent = $iWidth . '%';
  245. }
  246. else {
  247. $iWidth = 0;
  248. $sPercent = $sTxtUnlimited;
  249. }
  250. $aTmplVarsStats[] = array('title' => _t('_sys_profile_storage_quota_number'), 'width' => $iWidth, 'value' => $aQuota['current_number'] . ' ' . $sTxtFiles, 'percent' => $sPercent);
  251. return BxDolTemplate::getInstance()->parseHtmlByName('profile_membership_stats.html', array(
  252. 'membership' => BxDolAcl::getInstance()->getProfileMembership($iProfileId),
  253. 'bx_repeat:stat_items' => $aTmplVarsStats
  254. ));
  255. }
  256. /**
  257. * @page service Service Calls
  258. * @section bx_system_general System Services
  259. * @subsection bx_system_general-profiles Profiles
  260. * @subsubsection bx_system_general-profile_notifications profile_notifications
  261. *
  262. * @code bx_srv('system', 'profile_notifications', [], 'TemplServiceProfiles'); @endcode
  263. * @code {{~system:profile_notifications:TemplServiceProfiles~}} @endcode
  264. *
  265. * Get number of unread notifications for logged in profile
  266. *
  267. * @see BxBaseServiceProfiles::serviceProfileNotifications
  268. */
  269. /**
  270. * @ref bx_system_general-profile_notifications "profile_notifications"
  271. */
  272. public function serviceProfileNotifications ($iProfileId = 0)
  273. {
  274. if (!$iProfileId)
  275. $iProfileId = bx_get_logged_profile_id();
  276. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  277. if(!$oMenu)
  278. return 0;
  279. $iNum = 0;
  280. $aMenuItems = $oMenu->getMenuItems ();
  281. foreach ($aMenuItems as $r) {
  282. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  283. $iNum += $r['bx_if:addon']['content']['addon'];
  284. }
  285. return $iNum;
  286. }
  287. /**
  288. * @page service Service Calls
  289. * @section bx_system_general System Services
  290. * @subsection bx_system_general-profiles Profiles
  291. * @subsubsection bx_system_general-get_count_online_profiles get_count_online_profiles
  292. *
  293. * @code bx_srv('system', 'get_count_online_profiles', [], 'TemplServiceProfiles'); @endcode
  294. * @code {{~system:get_count_online_profiles:TemplServiceProfiles~}} @endcode
  295. *
  296. * Get number of online profiles
  297. *
  298. * @see BxBaseServiceProfiles::serviceGetCountOnlineProfiles
  299. */
  300. /**
  301. * @ref bx_system_general-get_count_online_profiles "get_count_online_profiles"
  302. */
  303. public function serviceGetCountOnlineProfiles ()
  304. {
  305. $oProfilesQuery = BxDolProfileQuery::getInstance();
  306. return $oProfilesQuery->getOnlineCount();
  307. }
  308. public function serviceGetProfilesModules ($bForceActAsProfile = true)
  309. {
  310. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  311. $oDb = BxDolDb::getInstance();
  312. $oCache = $oDb->getDbCacheObject ();
  313. $sKey = $oDb->genDbCacheKey('profiles_' . (!$bForceActAsProfile ? 'all_' : '') . 'modules_array');
  314. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  315. $mixedRetTs = $oCache->getData($sKeyTs);
  316. $mixedRet = $oCache->getData($sKey);
  317. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  318. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  319. $aModulesArray = $mixedRet;
  320. } else {
  321. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  322. $oCache->setData($sKey, $aModulesArray);
  323. $oCache->setData($sKeyTs, $iNewestModuleTs);
  324. }
  325. } else {
  326. $aModulesArray = $this->_getProfilesModules ($bForceActAsProfile);
  327. }
  328. return $aModulesArray;
  329. }
  330. public function serviceProfilesFriends ($iLimit = 20)
  331. {
  332. if (!($iProfileId = bx_get_logged_profile_id()))
  333. return array();
  334. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  335. if (!$oConnection)
  336. return array();
  337. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  338. return array();
  339. $aRet = array();
  340. foreach ($a as $iId) {
  341. $oProfile = BxDolProfile::getInstance($iId);
  342. $aRet[] = array (
  343. 'label' => $oProfile->getDisplayName(),
  344. 'value' => $iId,
  345. 'url' => $oProfile->getUrl(),
  346. 'thumb' => $oProfile->getThumb(),
  347. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  348. );
  349. }
  350. return $aRet;
  351. }
  352. public function serviceProfilesSearch ($sTerm, $mixedParems = [])
  353. {
  354. $iLimit = 20;
  355. if(is_int($mixedParems))
  356. $iLimit = (int)$mixedParems;
  357. else if(is_array($mixedParems) && isset($mixedParems['limit']))
  358. $iLimit = (int)$mixedParems['limit'];
  359. // display friends by default
  360. if (!$sTerm)
  361. return $this->serviceProfilesFriends($iLimit);
  362. // get list of "profiles" modules
  363. $aModules = $this->serviceGetProfilesModules();
  364. // search in each module
  365. $a = array();
  366. foreach ($aModules as $aModule) {
  367. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  368. continue;
  369. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, getParam('sys_per_page_search_keyword_single'))));
  370. }
  371. // sort result
  372. usort($a, function($r1, $r2) {
  373. return strcmp($r1['label'], $r2['label']);
  374. });
  375. bx_alert('system', 'profiles_search', 0, 0, array(
  376. 'module' => is_array($mixedParems) && isset($mixedParems['module']) ? $mixedParems['module'] : '',
  377. 'term' => $sTerm,
  378. 'result' => &$a
  379. ));
  380. // return as array
  381. return array_slice($a, 0, $iLimit);
  382. }
  383. public function serviceProfilesList ($iAccountId = 0)
  384. {
  385. $oProfilesQuery = BxDolProfileQuery::getInstance();
  386. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  387. if (!$aProfiles)
  388. return false;
  389. $s = '';
  390. foreach ($aProfiles as $aProfile)
  391. if ($aProfile['type'] != 'system')
  392. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  393. if (!$s)
  394. $s = MsgBox(_t('_sys_txt_empty'));
  395. return $s . '<div class="bx-clear"></div>';
  396. }
  397. public function serviceBrowseFriends ($iProfileId, $aParams = [])
  398. {
  399. if(is_string($aParams))
  400. $aParams = json_decode($aParams, true);
  401. $aParams = array_merge([
  402. 'params' => false,
  403. 'design_box' => BX_DB_PADDING_DEF,
  404. 'empty_message' => false,
  405. 'ajax_paginate' => true,
  406. ], $aParams);
  407. $aParamsBrowse = ['profile' => $iProfileId];
  408. if(!empty($aParams['params']) && is_array($aParams['params']))
  409. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  410. return $this->_serviceBrowseConnections('friends', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  411. }
  412. public function serviceBrowseSubscriptions ($iProfileId, $aParams = [])
  413. {
  414. if(is_string($aParams))
  415. $aParams = json_decode($aParams, true);
  416. $aParams = array_merge([
  417. 'params' => false,
  418. 'design_box' => BX_DB_PADDING_DEF,
  419. 'empty_message' => false,
  420. 'ajax_paginate' => true,
  421. ], $aParams);
  422. $aParamsBrowse = ['profile' => $iProfileId];
  423. if(!empty($aParams['params']) && is_array($aParams['params']))
  424. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  425. return $this->_serviceBrowseConnections('subscriptions', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  426. }
  427. public function serviceBrowseSubscribedMe ($iProfileId, $aParams = [])
  428. {
  429. if(is_string($aParams))
  430. $aParams = json_decode($aParams, true);
  431. $aParams = array_merge([
  432. 'params' => false,
  433. 'design_box' => BX_DB_PADDING_DEF,
  434. 'empty_message' => false,
  435. 'ajax_paginate' => true,
  436. ], $aParams);
  437. $aParamsBrowse = ['profile' => $iProfileId];
  438. if(!empty($aParams['params']) && is_array($aParams['params']))
  439. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  440. return $this->_serviceBrowseConnections('subscribed_me', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  441. }
  442. public function serviceBrowseMembers($iProfileId, $sObject, $aParams = [])
  443. {
  444. if(is_string($aParams))
  445. $aParams = json_decode($aParams, true);
  446. $aParams = array_merge([
  447. 'params' => false,
  448. 'design_box' => BX_DB_PADDING_DEF,
  449. 'empty_message' => false,
  450. 'ajax_paginate' => true,
  451. ], $aParams);
  452. $aParamsBrowse = [
  453. 'profile' => $iProfileId,
  454. 'object' => $sObject
  455. ];
  456. if(!empty($aParams['params']) && is_array($aParams['params']))
  457. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  458. return $this->_serviceBrowseConnections('members', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  459. }
  460. public function serviceBrowseConnections ($iProfileId, $aParams = [])
  461. {
  462. if(is_string($aParams))
  463. $aParams = json_decode($aParams, true);
  464. $aParams = array_merge([
  465. 'params' => false,
  466. 'design_box' => BX_DB_PADDING_DEF,
  467. 'empty_message' => false,
  468. 'ajax_paginate' => true,
  469. ], $aParams);
  470. $aParamsBrowse = [
  471. 'profile' => $iProfileId,
  472. 'object' => 'sys_profiles_friends',
  473. 'type' => 'content',
  474. 'mutual' => false,
  475. 'profile2' => 0
  476. ];
  477. if(!empty($aParams['params']) && is_array($aParams['params']))
  478. $aParamsBrowse = array_merge($aParamsBrowse, $aParams['params']);
  479. return $this->_serviceBrowseConnections('connections', $aParamsBrowse, $aParams['design_box'], $aParams['empty_message'], $aParams['ajax_paginate']);
  480. }
  481. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  482. {
  483. $oTemplate = BxDolTemplate::getInstance();
  484. $oProfilesQuery = BxDolProfileQuery::getInstance();
  485. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  486. if (!$aProfiles)
  487. return false;
  488. if (null === $iActiveProfileId)
  489. $iActiveProfileId = bx_get_logged_profile_id();
  490. $oModuleDb = BxDolModuleQuery::getInstance();
  491. $aVars = array (
  492. 'bx_repeat:row' => array(),
  493. );
  494. bx_alert('system', 'account_profile_switcher', 0, false, array(
  495. 'account_id' => $iAccountId,
  496. 'active_profile_id' => &$iActiveProfileId,
  497. 'url_profile_action' => &$sUrlProfileAction,
  498. 'show_all' => &$bShowAll,
  499. 'button_title' => &$sButtonTitle,
  500. 'profile_template' => &$sProfileTemplate,
  501. 'profiles' => &$aProfiles
  502. ));
  503. foreach ($aProfiles as $aProfile) {
  504. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  505. continue;
  506. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  507. continue;
  508. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  509. continue;
  510. $aVars['bx_repeat:row'][] = array (
  511. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  512. 'bx_if:active' => array (
  513. 'condition' => $iActiveProfileId == $aProfile['id'],
  514. 'content' => array('id' => $aProfile['id']),
  515. ),
  516. 'bx_if:inactive' => array (
  517. 'condition' => $iActiveProfileId != $aProfile['id'],
  518. 'content' => array(
  519. 'id' => $aProfile['id'],
  520. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  521. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolProfile::getSwitchToProfileRedirectUrl($aProfile['id'])
  522. ),
  523. ),
  524. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  525. );
  526. }
  527. $oTemplate->addCss('account.css');
  528. return array(
  529. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  530. );
  531. }
  532. public function serviceProfileSettingsCfilter($iProfileId = false)
  533. {
  534. if(!BxDolContentFilter::getInstance()->isEnabled()) {
  535. BxDolTemplate::getInstance()->displayPageNotFound();
  536. exit;
  537. }
  538. // set settings submenu
  539. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  540. if ($oMenuSubmenu) {
  541. $oMenuSubmenu->setObjectSubmenu('sys_account_settings_submenu', array (
  542. 'title' => _t('_sys_menu_item_title_account_settings'),
  543. 'link' => BX_DOL_URL_ROOT . 'member.php',
  544. 'icon' => '',
  545. ));
  546. }
  547. if($iProfileId === false)
  548. $iProfileId = bx_get_logged_profile_id();
  549. $oProfile = BxDolProfile::getInstance($iProfileId);
  550. $aProfileInfo = $oProfile ? $oProfile->getInfo() : false;
  551. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  552. return MsgBox(_t('_sys_txt_error_profile_is_not_defined'));
  553. $sForm = 'sys_profile';
  554. $sFormDisplay = 'sys_profile_cf_set';
  555. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  556. if(!$oForm)
  557. return MsgBox(_t('_sys_txt_error_occured'));
  558. $oForm->initChecker($aProfileInfo);
  559. if(!$oForm->isSubmittedAndValid())
  560. return $sCode = $oForm->getCode();
  561. if(!$oForm->update($iProfileId)) {
  562. if (!$oForm->isValid())
  563. return $oForm->getCode();
  564. else
  565. return MsgBox(_t('_sys_txt_error_profile_update'));
  566. }
  567. // display result message
  568. $sMsg = MsgBox(_t('_' . $sFormDisplay . '_successfully_submitted'));
  569. return $sMsg . $oForm->getCode();
  570. }
  571. public function serviceIsAllowedCfilter($sAction, $iValue, $aProfileInfo)
  572. {
  573. if(!in_array($sAction, ['watch', 'use']) || !isset($aProfileInfo['birthday']))
  574. return false;
  575. $iAge = 0;
  576. if(!empty($aProfileInfo['birthday']) && !in_array($aProfileInfo['birthday'], array('0000-00-00', '0000-00-00 00:00:00')))
  577. $iAge = bx_birthday2age($aProfileInfo['birthday']);
  578. $iResult = 0;
  579. switch($iValue) {
  580. case 1:
  581. $iResult = $iValue;
  582. break;
  583. case 2:
  584. if($iAge >= 6)
  585. $iResult = $iValue;
  586. break;
  587. case 3:
  588. if($iAge >= 13)
  589. $iResult = $iValue;
  590. break;
  591. case 4:
  592. if($iAge >= 17)
  593. $iResult = $iValue;
  594. break;
  595. case 5:
  596. if($iAge >= 21)
  597. $iResult = $iValue;
  598. break;
  599. }
  600. if($iResult != 0)
  601. $iResult = 1 << ($iResult - 1);
  602. return $iResult;
  603. }
  604. public function serviceIsEnabledCfilter()
  605. {
  606. return BxDolContentFilter::getInstance()->isEnabled();
  607. }
  608. protected function _getIcon ($sIcon)
  609. {
  610. return BxTemplFunctions::getInstance()->getIcon($sIcon);
  611. }
  612. protected function _getLatestModuleTimestamp ()
  613. {
  614. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  615. if (empty($aModules))
  616. return 0;
  617. $aModuleNewest = array_pop($aModules);
  618. return $aModuleNewest['date'];
  619. }
  620. protected function _getProfilesModules($bForceActAsProfile = true)
  621. {
  622. $aRet = array();
  623. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  624. foreach($aModules as $aModule) {
  625. $oModule = BxDolModule::getInstance($aModule['name']);
  626. if(!($oModule instanceof iBxDolProfileService))
  627. continue;
  628. if($bForceActAsProfile && !$oModule->serviceActAsProfile())
  629. continue;
  630. $aRet[] = $aModule;
  631. }
  632. return $aRet;
  633. }
  634. protected function _serviceBrowseConnections ($sMode, $aParams = [], $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
  635. {
  636. $sClass = 'BxTemplProfileSearchResult';
  637. bx_import($sClass);
  638. $o = new $sClass($sMode, $aParams);
  639. $o->setDesignBoxTemplateId($iDesignBox);
  640. $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
  641. $o->setAjaxPaginate($bAjaxPaginate);
  642. $o->setUnitParams(['context' => $sMode]);
  643. if ($o->isError)
  644. return '';
  645. if ($s = $o->processing()) {
  646. if(!bx_is_api())
  647. return $s;
  648. return [bx_api_get_block('browse', $s)];
  649. }
  650. else
  651. return '';
  652. }
  653. }
  654. /** @} */