BxInvResponse.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Invites Invites
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxInvResponse extends BxDolAlertsResponse
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->_sModule = 'bx_invites';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. }
  21. /**
  22. * Overwtire the method of parent class.
  23. *
  24. * @param BxDolAlerts $oAlert an instance of alert.
  25. */
  26. public function response($oAlert)
  27. {
  28. $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  29. if(!method_exists($this, $sMethod))
  30. return;
  31. return $this->$sMethod($oAlert);
  32. }
  33. protected function _processAccountAddFormCheck($oAlert)
  34. {
  35. $sKey = $this->_oModule->_oConfig->getKey();
  36. if($sKey === false)
  37. return;
  38. $aInvite = $this->_oModule->_oDb->getInvites(['type' => 'by_key', 'key' => $sKey]);
  39. if(!empty($aInvite) && is_array($aInvite) && (int)$aInvite['email_use'] != 0) {
  40. $oForm = &$oAlert->aExtras['form_object'];
  41. if($oForm->isSubmittedAndValid() && $oForm->aInputs['email']['value'] != $aInvite['email']) {
  42. $oForm->aInputs['email']['error'] = _t('_bx_invites_err_wrong_email');
  43. $oForm->setValid(false);
  44. }
  45. }
  46. }
  47. protected function _processAccountAddForm($oAlert)
  48. {
  49. if(!($sCode = $this->_oModule->serviceAccountAddFormCheck())) {
  50. $aInvite = $this->_oModule->_oDb->getInvites([
  51. 'type' => 'by_key',
  52. 'key' => $this->_oModule->_oConfig->getKey()
  53. ]);
  54. if(!empty($aInvite) && is_array($aInvite)) {
  55. $bGetCode = false;
  56. if(!empty($aInvite['email_use'])) {
  57. $oAlert->aExtras['form_object']->aInputs['email']['value'] = $aInvite['email'];
  58. if(!is_array($oAlert->aExtras['form_object']->aInputs['email']['attrs']))
  59. $oAlert->aExtras['form_object']->aInputs['email']['attrs'] = [];
  60. $oAlert->aExtras['form_object']->aInputs['email']['attrs']['readonly'] = 'readonly';
  61. $bGetCode = true;
  62. }
  63. if(!empty($aInvite['aj_action']) && in_array($aInvite['aj_action'], ['redirect', 'invite_to_context'])) {
  64. $sRedirectUrl = '';
  65. switch($aInvite['aj_action']) {
  66. case 'redirect':
  67. if(!empty($aInvite['aj_params']))
  68. $sRedirectUrl = BxDolPermalinks::getInstance()->permalink($aInvite['aj_params']);
  69. break;
  70. case 'invite_to_context':
  71. if(($iContextPid = (int)$aInvite['aj_params']) && ($oContext = BxDolProfile::getInstanceMagic($iContextPid)))
  72. $sRedirectUrl = $oContext->getUrl();
  73. break;
  74. }
  75. if($sRedirectUrl) {
  76. $sRedirectUrl = bx_absolute_url($sRedirectUrl);
  77. if(bx_is_api())
  78. $sRedirectUrl = bx_api_get_relative_url($sRedirectUrl);
  79. $oAlert->aExtras['form_object']->aInputs['relocate'] = [
  80. 'name' => 'relocate',
  81. 'type' => 'hidden',
  82. 'value' => $sRedirectUrl
  83. ];
  84. }
  85. $bGetCode = true;
  86. }
  87. if($bGetCode)
  88. $oAlert->aExtras['form_code'] = $oAlert->aExtras['form_object']->getCode();
  89. }
  90. }
  91. else {
  92. $oAlert->aExtras['form_code'] = $sCode;
  93. $oAlert->aExtras['form_object'] = false;
  94. }
  95. }
  96. protected function _processAccountAdded($oAlert)
  97. {
  98. $sKey = $this->_oModule->_oConfig->getUnsetKey();
  99. if($sKey === false)
  100. return;
  101. $this->_oModule->attachAccountIdToInvite($oAlert->iObject, $sKey);
  102. /**
  103. * Don't remove key if multi join invitation was used.
  104. */
  105. if(($aInvite = $this->_oModule->_oDb->getInvites(['type' => 'by_key', 'key' => $sKey])) && is_array($aInvite) && (int)$aInvite['multi'] != 0)
  106. return;
  107. $aKeysToRemove = [$sKey];
  108. if(($sKeysToRemove = $this->_oModule->_oDb->getInvites(['type' => 'invites_code_by_single', 'value' => $sKey])))
  109. $aKeysToRemove = explode(',', $sKeysToRemove);
  110. $oKeys = BxDolKey::getInstance();
  111. foreach($aKeysToRemove as $sKeyToRemove)
  112. if($oKeys->isKeyExists($sKeyToRemove))
  113. $oKeys->removeKey($sKeyToRemove);
  114. }
  115. protected function _processProfileAdd($oAlert)
  116. {
  117. $oProfile = BxDolProfile::getInstanceMagic($oAlert->iObject);
  118. if(!$oProfile || !$oProfile->isActAsProfile())
  119. return;
  120. $aInvite = $this->_oModule->_oDb->getInvites([
  121. 'type' => 'by_joined_account_id',
  122. 'account_id' => $oProfile->getAccountId()
  123. ]);
  124. $bInvite = !empty($aInvite) && is_array($aInvite);
  125. //--- Check 'invite_to_context'
  126. if($bInvite && !empty($aInvite['aj_action']) && $aInvite['aj_action'] == 'invite_to_context')
  127. $this->_oModule->processInviteToContext($oAlert->iObject, (int)$aInvite['aj_params']);
  128. //--- Check automatic befriending
  129. if(getParam('bx_invites_automatically_befriend') == 'on') {
  130. $bNeedToFriend = true;
  131. /**
  132. * @hooks
  133. * @hookdef hook-bx_invites-add_friend 'bx_invites', 'add_friend' - hook on add friend on new user registred by invitaion
  134. * - $unit_name - equals `add_friend`
  135. * - $action - equals `invite`
  136. * - $object_id - not used
  137. * - $sender_id - not used
  138. * - $extra_params - array of additional params with the following array keys:
  139. * - `profile_id` - [int] profile_id for user registred by invitaion
  140. * - `override_result` - [bool] by ref, if true friend will be added, can be overridden in hook processing
  141. * @hook @ref hook-bx_invites-add_friend
  142. */
  143. bx_alert($this->_sModule, 'add_friend', 0, 0, [
  144. 'profile_id' => $oAlert->iObject,
  145. 'override_result' => &$bNeedToFriend,
  146. ]);
  147. if($bNeedToFriend) {
  148. $iProfileInvitor = $this->_oModule->_oDb->getInvites([
  149. 'type' => 'profile_id_by_joined_account_id',
  150. 'value' => $oProfile->getAccountId()
  151. ]);
  152. if($iProfileInvitor && ($oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends')) !== false){
  153. $oConnection->addConnection($oAlert->iObject, $iProfileInvitor);
  154. $oConnection->addConnection($iProfileInvitor, $oAlert->iObject);
  155. }
  156. }
  157. }
  158. }
  159. protected function _processProfileDelete($oAlert)
  160. {
  161. $this->_oModule->_oDb->deleteInvites(array('profile_id' => $oAlert->iObject));
  162. }
  163. protected function _processAccountDelete($oAlert)
  164. {
  165. $this->_oModule->_oDb->deleteInvitesByAccount(array('joined_account_id' => $oAlert->iObject));
  166. }
  167. protected function _processBxAnalyticsGetModules($oAlert)
  168. {
  169. $oAlert->aExtras['list'][$this->_oModule->_aModule['name']] = $this->_oModule->_aModule['title'];
  170. }
  171. protected function _processBxAnalyticsGetReports($oAlert)
  172. {
  173. if ($this->_oModule->_aModule['name'] == $oAlert->aExtras['module']){
  174. $oAlert->aExtras['list'] = array();
  175. $oAlert->aExtras['list']['content_total_invited'] = _t('_bx_invites_reports_for_analytics_invited_total');
  176. $oAlert->aExtras['list']['content_speed_invited'] = _t('_bx_invites_reports_for_analytics_invited_speed_grows');
  177. $oAlert->aExtras['list']['content_total_invitation'] = _t('_bx_invites_reports_for_analytics_invitation_total');
  178. $oAlert->aExtras['list']['content_speed_invitation'] = _t('_bx_invites_reports_for_analytics_invitation_speed_grows');
  179. }
  180. }
  181. protected function _processBxAnalyticsGetChartDataLine($oAlert)
  182. {
  183. if ($this->_oModule->_aModule['name'] == $oAlert->aExtras['module']){
  184. $bIsInvited = false;
  185. if (substr_count($oAlert->aExtras['report_name'], '_invited') > 0)
  186. $bIsInvited = true;
  187. if (substr_count($oAlert->aExtras['report_name'], '_total_') > 0)
  188. $oAlert->aExtras['report_type'] = BX_ANALYTICS_CONTENT_TOTAL;
  189. $oAlert->aExtras['data'] = $this->_oModule->_oDb->getDataForCharts($oAlert->aExtras['date_from'], $oAlert->aExtras['date_to'], $bIsInvited);
  190. $oAlert->aExtras['prev_value'] = $this->_oModule->_oDb->getInitValueForCharts($oAlert->aExtras['date_from'], $bIsInvited);
  191. }
  192. }
  193. }
  194. /** @} */