BxDolPush.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolPush extends BxDolFactoryObject
  10. {
  11. protected $_sApiUrlRootPush;
  12. protected $_sApiUrlRootEmail;
  13. protected function __construct($aObject, $oTemplate = null, $sDbClassName = 'BxDolPushQuery')
  14. {
  15. parent::__construct($aObject, $oTemplate, $sDbClassName);
  16. $this->_sApiUrlRootEmail = $this->_getUrlRoot('email');
  17. $this->_sApiUrlRootPush = $this->_getUrlRoot('push');
  18. }
  19. /**
  20. * Get Push Notification provider object instance by object name
  21. * @param $sObject object name
  22. * @return object instance or false on error
  23. */
  24. static public function getObjectInstance($sObject = false, $oTemplate = false)
  25. {
  26. if(!$sObject)
  27. $sObject = getParam('sys_push_default');
  28. if(!$sObject)
  29. return false;
  30. return parent::getObjectInstanceByClassNames($sObject, $oTemplate, 'BxDolPush', 'BxDolPushQuery');
  31. }
  32. /**
  33. * Get tags to send to PUSH server
  34. * @param $iProfileId - profile ID
  35. * @return array of tags
  36. */
  37. public static function getTags($iProfileId = false)
  38. {
  39. if (false === $iProfileId)
  40. $iProfileId = bx_get_logged_profile_id();
  41. $oProfile = BxDolProfile::getInstance($iProfileId);
  42. $oAccount = $oProfile ? $oProfile->getAccountObject() : null;
  43. if (!$oProfile || !$oAccount)
  44. return false;
  45. $sEmail = $oAccount->getEmail();
  46. $a = array (
  47. 'user_hash' => encryptUserId($iProfileId),
  48. 'real_name' => $oProfile->getDisplayName(),
  49. 'email' => $sEmail,
  50. 'email_hash' => $sEmail ? hash_hmac('sha256', $sEmail, getParam('sys_push_app_id')) : '',
  51. );
  52. /**
  53. * @hooks
  54. * @hookdef hook-system-is_confirmed 'system', 'push_tags' - hook on get tags to send to PUSH server
  55. * - $unit_name - equals `system`
  56. * - $action - equals `push_tags`
  57. * - $object_id - profile_id from current user
  58. * - $sender_id - profile_id from current user
  59. * - $extra_params - array of additional params with the following array keys:
  60. * - `tags` - [array] by ref, array of tags, can be overridden in hook processing
  61. * @hook @ref hook-system-push_tags
  62. */
  63. bx_alert('system', 'push_tags', $iProfileId, $iProfileId, array('tags' => &$a, 'tags_ref' => &$a));
  64. return $a;
  65. }
  66. /**
  67. * @param $a - array to fill with notification counter per module
  68. * @return total number of notifications
  69. */
  70. public static function getNotificationsCount($iProfileId = 0, &$aBubbles = null)
  71. {
  72. if ('' != trim(getParam('sys_api_url_root_push'))) {
  73. return bx_srv('bx_notifications', 'get_unread_notifications_num', [$iProfileId]);
  74. }
  75. $iMemberIdCookie = null;
  76. $bLoggedMemberGlobals = null;
  77. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  78. if (getLoggedId())
  79. $iMemberIdCookie = getLoggedId();
  80. if (!empty($GLOBALS['logged']['member']))
  81. $bLoggedMemberGlobals = $GLOBALS['logged']['member'];
  82. $oProfile = BxDolProfile::getInstance($iProfileId);
  83. $_COOKIE['memberID'] = $oProfile ? $oProfile->getAccountId() : 0;
  84. $GLOBALS['logged']['member'] = $oProfile ? true : false;
  85. }
  86. $aMenusObjects = array('sys_account_notifications', 'sys_toolbar_member');
  87. foreach ($aMenusObjects as $sMenuObject) {
  88. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  89. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  90. $oMenu = BxDolMenu::getObjectInstance($sMenuObject);
  91. if ($iProfileId && $iProfileId != bx_get_logged_profile_id())
  92. unset($GLOBALS['bxDolClasses']['BxDolMenu!sys_account_notifications']);
  93. $bSave = $oMenu->setDisplayAddons(true);
  94. $a = $oMenu->getMenuItems();
  95. $iBubbles = 0;
  96. foreach ($a as $r) {
  97. if (!$r['bx_if:addon']['condition'])
  98. continue;
  99. if (null !== $aBubbles)
  100. $aBubbles[$r['name']] = $r['bx_if:addon']['content']['addon'];
  101. $iBubbles += $r['bx_if:addon']['content']['addon'];
  102. }
  103. }
  104. if ($iProfileId && $iProfileId != bx_get_logged_profile_id()) {
  105. if (null === $iMemberIdCookie)
  106. unset($_COOKIE['memberID']);
  107. else
  108. $_COOKIE['memberID'] = $iMemberIdCookie;
  109. if (null === $bLoggedMemberGlobals)
  110. unset($GLOBALS['logged']['member']);
  111. else
  112. $GLOBALS['logged']['member'] = $bLoggedMemberGlobals;
  113. }
  114. return $iBubbles;
  115. }
  116. public function getCode($sFor = '')
  117. {
  118. if($sFor && ($sMethod = '_getCode' . bx_gen_method_name($sFor)) && method_exists($this, $sMethod))
  119. return $this->$sMethod();
  120. return '';
  121. }
  122. public function send($iProfileId, $aMessage, $bAddToQueue = false) {}
  123. protected function _getUrlRoot($sType)
  124. {
  125. $sRootUrl = getParam('sys_api_url_root_' . $sType);
  126. if(!$sRootUrl)
  127. return false;
  128. if(substr(BX_DOL_URL_ROOT, -1) == '/' && substr($sRootUrl, -1) != '/')
  129. $sRootUrl .= '/';
  130. return $sRootUrl;
  131. }
  132. }
  133. /** @} */