BxInvResponse.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. if(bx_is_api())
  94. $oAlert->aExtras['form_code_api'] = $this->_oModule->_oTemplate->getBlockRequestAPI($sCode);
  95. $oAlert->aExtras['form_object'] = false;
  96. }
  97. }
  98. protected function _processAccountAdded($oAlert)
  99. {
  100. $sKey = $this->_oModule->_oConfig->getUnsetKey();
  101. if($sKey === false)
  102. return;
  103. $this->_oModule->attachAccountIdToInvite($oAlert->iObject, $sKey);
  104. /**
  105. * Don't remove key if multi join invitation was used.
  106. */
  107. if(($aInvite = $this->_oModule->_oDb->getInvites(['type' => 'by_key', 'key' => $sKey])) && is_array($aInvite) && (int)$aInvite['multi'] != 0)
  108. return;
  109. $aKeysToRemove = [$sKey];
  110. if(($sKeysToRemove = $this->_oModule->_oDb->getInvites(['type' => 'invites_code_by_single', 'value' => $sKey])))
  111. $aKeysToRemove = explode(',', $sKeysToRemove);
  112. $oKeys = BxDolKey::getInstance();
  113. foreach($aKeysToRemove as $sKeyToRemove)
  114. if($oKeys->isKeyExists($sKeyToRemove))
  115. $oKeys->removeKey($sKeyToRemove);
  116. }
  117. protected function _processProfileAdd($oAlert)
  118. {
  119. $oProfile = BxDolProfile::getInstanceMagic($oAlert->iObject);
  120. if(!$oProfile || !$oProfile->isActAsProfile())
  121. return;
  122. $aInvite = $this->_oModule->_oDb->getInvites([
  123. 'type' => 'by_joined_account_id',
  124. 'account_id' => $oProfile->getAccountId()
  125. ]);
  126. $bInvite = !empty($aInvite) && is_array($aInvite);
  127. //--- Check 'invite_to_context'
  128. if($bInvite && !empty($aInvite['aj_action']) && $aInvite['aj_action'] == 'invite_to_context')
  129. $this->_oModule->processInviteToContext($oAlert->iObject, (int)$aInvite['aj_params']);
  130. //--- Check automatic befriending
  131. if(getParam('bx_invites_automatically_befriend') == 'on') {
  132. $bNeedToFriend = true;
  133. /**
  134. * @hooks
  135. * @hookdef hook-bx_invites-add_friend 'bx_invites', 'add_friend' - hook on add friend on new user registred by invitaion
  136. * - $unit_name - equals `add_friend`
  137. * - $action - equals `invite`
  138. * - $object_id - not used
  139. * - $sender_id - not used
  140. * - $extra_params - array of additional params with the following array keys:
  141. * - `profile_id` - [int] profile_id for user registred by invitaion
  142. * - `override_result` - [bool] by ref, if true friend will be added, can be overridden in hook processing
  143. * @hook @ref hook-bx_invites-add_friend
  144. */
  145. bx_alert($this->_sModule, 'add_friend', 0, 0, [
  146. 'profile_id' => $oAlert->iObject,
  147. 'override_result' => &$bNeedToFriend,
  148. ]);
  149. if($bNeedToFriend) {
  150. $iProfileInvitor = $this->_oModule->_oDb->getInvites([
  151. 'type' => 'profile_id_by_joined_account_id',
  152. 'value' => $oProfile->getAccountId()
  153. ]);
  154. if($iProfileInvitor && ($oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends')) !== false){
  155. $oConnection->addConnection($oAlert->iObject, $iProfileInvitor);
  156. $oConnection->addConnection($iProfileInvitor, $oAlert->iObject);
  157. }
  158. }
  159. }
  160. }
  161. protected function _processProfileDelete($oAlert)
  162. {
  163. $this->_oModule->_oDb->deleteInvites(array('profile_id' => $oAlert->iObject));
  164. }
  165. protected function _processAccountDelete($oAlert)
  166. {
  167. $this->_oModule->_oDb->deleteInvitesByAccount(array('joined_account_id' => $oAlert->iObject));
  168. }
  169. protected function _processBxAnalyticsGetModules($oAlert)
  170. {
  171. $oAlert->aExtras['list'][$this->_oModule->_aModule['name']] = $this->_oModule->_aModule['title'];
  172. }
  173. protected function _processBxAnalyticsGetReports($oAlert)
  174. {
  175. if ($this->_oModule->_aModule['name'] == $oAlert->aExtras['module']){
  176. $oAlert->aExtras['list'] = array();
  177. $oAlert->aExtras['list']['content_total_invited'] = _t('_bx_invites_reports_for_analytics_invited_total');
  178. $oAlert->aExtras['list']['content_speed_invited'] = _t('_bx_invites_reports_for_analytics_invited_speed_grows');
  179. $oAlert->aExtras['list']['content_total_invitation'] = _t('_bx_invites_reports_for_analytics_invitation_total');
  180. $oAlert->aExtras['list']['content_speed_invitation'] = _t('_bx_invites_reports_for_analytics_invitation_speed_grows');
  181. }
  182. }
  183. protected function _processBxAnalyticsGetChartDataLine($oAlert)
  184. {
  185. if ($this->_oModule->_aModule['name'] == $oAlert->aExtras['module']){
  186. $bIsInvited = false;
  187. if (substr_count($oAlert->aExtras['report_name'], '_invited') > 0)
  188. $bIsInvited = true;
  189. if (substr_count($oAlert->aExtras['report_name'], '_total_') > 0)
  190. $oAlert->aExtras['report_type'] = BX_ANALYTICS_CONTENT_TOTAL;
  191. $oAlert->aExtras['data'] = $this->_oModule->_oDb->getDataForCharts($oAlert->aExtras['date_from'], $oAlert->aExtras['date_to'], $bIsInvited);
  192. $oAlert->aExtras['prev_value'] = $this->_oModule->_oDb->getInitValueForCharts($oAlert->aExtras['date_from'], $bIsInvited);
  193. }
  194. }
  195. }
  196. /** @} */