BxInvModule.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Invites Invites
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolModule');
  12. class BxInvModule extends BxDolModule
  13. {
  14. /**
  15. * Constructor
  16. */
  17. function __construct($aModule)
  18. {
  19. parent::__construct($aModule);
  20. $this->_oConfig->init($this->_oDb);
  21. }
  22. /**
  23. * SERVICE METHODS
  24. */
  25. public function serviceGetBlockInvite()
  26. {
  27. $iProfileId = $this->getProfileId();
  28. $iAccountId = $this->getAccountId($iProfileId);
  29. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  30. if($mixedAllowed !== true)
  31. return '';
  32. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  33. if($iInvited >= $this->_oConfig->getCountPerUser())
  34. return '';
  35. return array(
  36. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId)
  37. );
  38. }
  39. public function serviceGetBlockFormInvite()
  40. {
  41. $iProfileId = $this->getProfileId();
  42. $iAccountId = $this->getAccountId($iProfileId);
  43. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  44. if($mixedAllowed !== true)
  45. return array(
  46. 'content' => MsgBox($mixedAllowed)
  47. );
  48. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  49. $iInvites = $this->_oConfig->getCountPerUser() - $iInvited;
  50. if($iInvites <= 0)
  51. return array(
  52. 'content' => MsgBox(_t('_bx_invites_err_limit_reached'))
  53. );
  54. bx_import('BxDolForm');
  55. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $this->_oConfig->getObject('form_display_invite_send'));
  56. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  57. $sResult = '';
  58. $oForm->initChecker();
  59. if($oForm->isSubmittedAndValid()) {
  60. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  61. $sText = bx_process_pass($oForm->getCleanValue('text'));
  62. $mixedResult = $this->invite($sEmails, $sText, $iInvites, $oForm);
  63. if($mixedResult !== false)
  64. $sResult = _t('_bx_invites_msg_invitation_sent', (int)$mixedResult);
  65. else
  66. $sResult = _t('_bx_invites_err_not_available');
  67. $sResult = MsgBox($sResult);
  68. }
  69. return array(
  70. 'content' => $sResult . $oForm->getCode()
  71. );
  72. }
  73. public function serviceGetBlockFormRequest()
  74. {
  75. if(!$this->_oConfig->isRequestInvite())
  76. return array(
  77. 'content' => MsgBox(_t('_bx_invites_err_not_available'))
  78. );
  79. $mixedAllowed = $this->isAllowedRequest(0);
  80. if($mixedAllowed !== true)
  81. return array(
  82. 'content' => MsgBox($mixedAllowed)
  83. );
  84. $sResult = '';
  85. bx_import('BxDolForm');
  86. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_request'), $this->_oConfig->getObject('form_display_request_send'));
  87. $oForm->initChecker();
  88. if($oForm->isSubmittedAndValid()) {
  89. $sIp = getVisitorIP();
  90. $iId = (int)$oForm->insert(array(
  91. 'nip' => ip2long($sIp),
  92. 'date' => time()
  93. ));
  94. if($iId !== false) {
  95. $sRequestsEmail = $this->_oConfig->getRequestsEmail();
  96. if(!empty($sRequestsEmail)) {
  97. bx_import('BxDolEmailTemplates');
  98. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_request_form_message', array(
  99. 'sender_name' => bx_process_output($oForm->getCleanValue('name')),
  100. 'sender_email' => bx_process_output($oForm->getCleanValue('email')),
  101. 'sender_ip' => $sIp
  102. ));
  103. sendMail($sRequestsEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  104. }
  105. $sResult = MsgBox(_t('_bx_invites_msg_request_sent'));
  106. }
  107. }
  108. return array(
  109. 'content' => $sResult . $oForm->getCode()
  110. );
  111. }
  112. public function serviceGetBlockManageRequests()
  113. {
  114. bx_import('BxDolGrid');
  115. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_requests'));
  116. if(!$oGrid)
  117. return '';
  118. return $oGrid->getCode();
  119. }
  120. public function serviceGetMenuAddonRequests()
  121. {
  122. return $this->_oDb->getRequests(array('type' => 'count_all'));
  123. }
  124. public function invite($sEmails, $sText, $iLimit = false, $oForm = null)
  125. {
  126. $iProfileId = $this->getProfileId();
  127. $iAccountId = $this->getAccountId($iProfileId);
  128. bx_import('BxDolKey');
  129. $oKeys = BxDolKey::getInstance();
  130. if(!$oKeys)
  131. return false;
  132. $sKeyCode = $this->_oConfig->getKeyCode();
  133. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  134. if(empty($oForm)) {
  135. bx_import('BxDolForm');
  136. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $this->_oConfig->getObject('form_display_invite_send'));
  137. }
  138. bx_import('BxDolPermalinks');
  139. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  140. bx_import('BxDolEmailTemplates');
  141. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_invite_form_message', array(
  142. 'text' => $sText
  143. ), $iAccountId, $iProfileId);
  144. $iSent = 0;
  145. $iDate = time();
  146. $aEmails = explode(';', $sEmails);
  147. if(is_array($aEmails) && !empty($aEmails))
  148. foreach($aEmails as $sEmail) {
  149. if($iLimit !== false && (int)$iLimit <= 0)
  150. break;
  151. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  152. $sJoinUrl .= '&' . $sKeyCode . '=' . $sKey;
  153. $sEmail = trim($sEmail);
  154. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => $sJoinUrl), BX_EMAIL_SYSTEM)) {
  155. $oForm->insert(array(
  156. 'account_id' => $iAccountId,
  157. 'profile_id' => $iProfileId,
  158. 'email' => $sEmail,
  159. 'date' => $iDate
  160. ));
  161. $this->onInvite($iAccountId, $iProfileId);
  162. $iSent += 1;
  163. if($iLimit !== false)
  164. $iLimit -= 1;
  165. }
  166. }
  167. return $iSent;
  168. }
  169. public function isAllowedInvite($iProfileId, $bPerform = false)
  170. {
  171. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  172. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  173. }
  174. public function isAllowedRequest($iProfileId, $bPerform = false)
  175. {
  176. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  177. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  178. }
  179. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  180. {
  181. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  182. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  183. }
  184. public function getProfileId()
  185. {
  186. return isLogged() ? bx_get_logged_profile_id() : 0;
  187. }
  188. public function getProfileObject($iProfileId = 0)
  189. {
  190. bx_import('BxDolProfile');
  191. $oProfile = BxDolProfile::getInstance($iProfileId);
  192. if (!$oProfile) {
  193. bx_import('BxDolProfileUndefined');
  194. $oProfile = BxDolProfileUndefined::getInstance();
  195. }
  196. return $oProfile;
  197. }
  198. public function getAccountId($iProfileId)
  199. {
  200. $oProfile = $this->getProfileObject($iProfileId);
  201. if($oProfile->id() == 0)
  202. return 0;
  203. return $oProfile->getAccountId();
  204. }
  205. protected function onInvite($iAccountId, $iProfileId)
  206. {
  207. $this->isAllowedInvite($iProfileId, true);
  208. //--- Event -> Invite for Alerts Engine ---//
  209. bx_import('BxDolAlerts');
  210. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  211. $oAlert->alert();
  212. //--- Event -> Invite for Alerts Engine ---//
  213. }
  214. protected function onRequest()
  215. {
  216. //--- Event -> Request for Alerts Engine ---//
  217. bx_import('BxDolAlerts');
  218. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'request');
  219. $oAlert->alert();
  220. //--- Event -> Request for Alerts Engine ---//
  221. }
  222. }
  223. /** @} */