BxBaseServiceProfiles.php 9.7 KB

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