BxBaseServiceProfiles.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 serviceProfileStats ($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. $oAcl = BxDolAcl::getInstance();
  41. $aAcl = $oAcl->getMemberMembershipInfo($iProfileId);
  42. $aAclInfo = $oAcl->getMembershipInfo($aAcl['id']);
  43. $aVars = array(
  44. 'profile_id' => $oProfile->id(),
  45. 'profile_url' => $oProfile->getUrl(),
  46. 'profile_edit_url' => $oProfile->getEditUrl(),
  47. 'profile_title' => $oProfile->getDisplayName(),
  48. 'profile_title_attr' => bx_html_attribute($oProfile->getDisplayName()),
  49. 'profile_ava_url' => $oProfile->getAvatar(),
  50. 'profile_unit' => $oProfile->getUnit(0, array('template' => array(
  51. 'name' => 'unit_wo_info',
  52. 'size' => 'ava'
  53. ))),
  54. 'profile_acl_title' => _t($aAclInfo['name']),
  55. 'profile_acl_icon' => $aAclInfo['icon'],
  56. 'menu' => BxDolMenu::getObjectInstance('sys_profile_stats')->getCode(),
  57. );
  58. return BxDolTemplate::getInstance()->parseHtmlByName('profile_stats.html', $aVars);
  59. }
  60. public function serviceGetMenuAddonProfileEdit($iProfileId = 0, $sCaption = '')
  61. {
  62. if(empty($sCaption))
  63. $sCaption = _t('_Edit');
  64. $oProfile = BxDolProfile::getInstance($iProfileId);
  65. if(!$oProfile)
  66. return '';
  67. $sModule = $oProfile->getModule();
  68. $sMethod = 'profile_edit_url';
  69. if(!BxDolRequest::serviceExists($sModule, $sMethod))
  70. return '';
  71. return BxDolTemplate::getInstance()->parseLink(BxDolService::call($sModule, $sMethod, array($oProfile->getContentId())), $sCaption);
  72. }
  73. public function serviceProfileMembership ($iProfileId = 0)
  74. {
  75. if (!$iProfileId)
  76. $iProfileId = bx_get_logged_profile_id();
  77. return BxDolAcl::getInstance()->getProfileMembership($iProfileId);
  78. }
  79. public function serviceProfileNotifications ($iProfileId = 0)
  80. {
  81. if (!$iProfileId)
  82. $iProfileId = bx_get_logged_profile_id();
  83. $oMenu = BxDolMenu::getObjectInstance('sys_account_notifications');
  84. if(!$oMenu)
  85. return 0;
  86. $iNum = 0;
  87. $aMenuItems = $oMenu->getMenuItems ();
  88. foreach ($aMenuItems as $r) {
  89. if (isset($r['bx_if:addon']) && $r['bx_if:addon']['condition'])
  90. $iNum += $r['bx_if:addon']['content']['addon'];
  91. }
  92. return $iNum;
  93. }
  94. public function serviceGetCountOnlineProfiles ()
  95. {
  96. $oProfilesQuery = BxDolProfileQuery::getInstance();
  97. return $oProfilesQuery->getOnlineCount();
  98. }
  99. public function serviceGetProfilesModules ()
  100. {
  101. if (getParam('sys_db_cache_enable')) { // get list of profiles modules from db cache, cache is invalidated when new module is installed
  102. $oDb = BxDolDb::getInstance();
  103. $oCache = $oDb->getDbCacheObject ();
  104. $sKey = $oDb->genDbCacheKey('profiles_modules_array');
  105. $sKeyTs = $oDb->genDbCacheKey('profiles_modules_ts');
  106. $mixedRetTs = $oCache->getData($sKeyTs);
  107. $mixedRet = $oCache->getData($sKey);
  108. $iNewestModuleTs = $this->_getLatestModuleTimestamp ();
  109. if ($mixedRetTs != null && $mixedRet !== null && $mixedRetTs == $iNewestModuleTs) {
  110. $aModulesArray = $mixedRet;
  111. } else {
  112. $aModulesArray = $this->_getProfilesModules ();
  113. $oCache->setData($sKey, $aModulesArray);
  114. $oCache->setData($sKeyTs, $iNewestModuleTs);
  115. }
  116. } else {
  117. $aModulesArray = $this->_getProfilesModules ();
  118. }
  119. return $aModulesArray;
  120. }
  121. public function serviceProfilesFriends ($iLimit = 20)
  122. {
  123. if (!($iProfileId = bx_get_logged_profile_id()))
  124. return array();
  125. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  126. if (!$oConnection)
  127. return array();
  128. if (!($a = $oConnection->getConnectedContent ($iProfileId, true, 0, $iLimit)))
  129. return array();
  130. $aRet = array();
  131. foreach ($a as $iId) {
  132. $oProfile = BxDolProfile::getInstance($iId);
  133. $aRet[] = array (
  134. 'label' => $oProfile->getDisplayName(),
  135. 'value' => $iId,
  136. 'url' => $oProfile->getUrl(),
  137. 'thumb' => $oProfile->getThumb(),
  138. 'unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  139. );
  140. }
  141. return $aRet;
  142. }
  143. public function serviceProfilesSearch ($sTerm, $iLimit = 20)
  144. {
  145. // display friends by default
  146. if (!$sTerm)
  147. return $this->serviceProfilesFriends($iLimit);
  148. // get list of "profiles" modules
  149. $aModules = $this->serviceGetProfilesModules();
  150. // search in each module
  151. $a = array();
  152. foreach ($aModules as $aModule) {
  153. if (!BxDolService::call($aModule['name'], 'act_as_profile'))
  154. continue;
  155. $a = array_merge($a, BxDolService::call($aModule['name'], 'profiles_search', array($sTerm, 10)));
  156. }
  157. // sort result
  158. usort($a, function($r1, $r2) {
  159. return strcmp($r1['label'], $r2['label']);
  160. });
  161. // return as array
  162. return array_slice($a, 0, $iLimit);
  163. }
  164. public function serviceProfilesList ($iAccountId = 0)
  165. {
  166. $oProfilesQuery = BxDolProfileQuery::getInstance();
  167. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  168. if (!$aProfiles)
  169. return false;
  170. $s = '';
  171. foreach ($aProfiles as $aProfile)
  172. if ($aProfile['type'] != 'system')
  173. $s .= BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id']));
  174. if (!$s)
  175. $s = MsgBox(_t('_sys_txt_empty'));
  176. return $s . '<div class="bx-clear"></div>';
  177. }
  178. public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
  179. {
  180. $oTemplate = BxDolTemplate::getInstance();
  181. BxDolInformer::getInstance($oTemplate)->setEnabled(false);
  182. $oProfilesQuery = BxDolProfileQuery::getInstance();
  183. $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
  184. if (!$aProfiles)
  185. return false;
  186. if (null === $iActiveProfileId)
  187. $iActiveProfileId = bx_get_logged_profile_id();
  188. $oModuleDb = BxDolModuleQuery::getInstance();
  189. $aVars = array (
  190. 'bx_repeat:row' => array(),
  191. );
  192. foreach ($aProfiles as $aProfile) {
  193. if (!$bShowAll && $iActiveProfileId == $aProfile['id'])
  194. continue;
  195. if(!$oModuleDb->isEnabledByName($aProfile['type']))
  196. continue;
  197. if (!BxDolService::call($aProfile['type'], 'act_as_profile'))
  198. continue;
  199. $aVars['bx_repeat:row'][] = array (
  200. 'class' => $iActiveProfileId == $aProfile['id'] ? '' : 'bx-def-color-bg-box',
  201. 'bx_if:active' => array (
  202. 'condition' => $iActiveProfileId == $aProfile['id'],
  203. 'content' => array('id' => $aProfile['id']),
  204. ),
  205. 'bx_if:inactive' => array (
  206. 'condition' => $iActiveProfileId != $aProfile['id'],
  207. 'content' => array(
  208. 'id' => $aProfile['id'],
  209. 'button_title' => $sButtonTitle ? $sButtonTitle : _t('_sys_txt_switch_profile_context'),
  210. 'url_switch' => $sUrlProfileAction ? str_replace('{profile_id}', $aProfile['id'], $sUrlProfileAction) : BxDolPermalinks::getInstance()->permalink('page.php?i=account-profile-switcher', array('switch_to_profile' => $aProfile['id'], 'redirect_back' => 1))
  211. ),
  212. ),
  213. 'unit' => BxDolService::call($aProfile['type'], 'profile_unit', array($aProfile['content_id'], array('template' => $sProfileTemplate))),
  214. );
  215. }
  216. $oTemplate->addCss('account.css');
  217. return array(
  218. 'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
  219. );
  220. }
  221. protected function _getLatestModuleTimestamp ()
  222. {
  223. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1, 'order_by' => '`date` ASC'));
  224. if (empty($aModules))
  225. return 0;
  226. $aModuleNewest = array_pop($aModules);
  227. return $aModuleNewest['date'];
  228. }
  229. protected function _getProfilesModules ()
  230. {
  231. $aRet = array();
  232. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  233. foreach ($aModules as $aModule) {
  234. $oModule = BxDolModule::getInstance($aModule['name']);
  235. if ($oModule instanceof iBxDolProfileService && $oModule->serviceActAsProfile())
  236. $aRet[] = $aModule;
  237. }
  238. return $aRet;
  239. }
  240. }
  241. /** @} */