BxInvModule.php 8.3 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. bx_import('FormCheckerHelper', $this->_aModule);
  56. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $this->_oConfig->getObject('form_display_invite_send'));
  57. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  58. $sResult = '';
  59. $oForm->initChecker();
  60. if($oForm->isSubmittedAndValid()) {
  61. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  62. $sText = bx_process_pass($oForm->getCleanValue('text'));
  63. $mixedResult = $this->invite($sEmails, $sText, $iInvites, $oForm);
  64. if($mixedResult !== false)
  65. $sResult = _t('_bx_invites_msg_invitation_sent', (int)$mixedResult);
  66. else
  67. $sResult = _t('_bx_invites_err_not_available');
  68. $sResult = MsgBox($sResult);
  69. }
  70. return array(
  71. 'content' => $sResult . $oForm->getCode()
  72. );
  73. }
  74. public function serviceGetBlockFormRequest()
  75. {
  76. if(!$this->_oConfig->isRequestInvite())
  77. return array(
  78. 'content' => MsgBox(_t('_bx_invites_err_not_available'))
  79. );
  80. $mixedAllowed = $this->isAllowedRequest(0);
  81. if($mixedAllowed !== true)
  82. return array(
  83. 'content' => MsgBox($mixedAllowed)
  84. );
  85. $sResult = '';
  86. bx_import('BxDolForm');
  87. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_request'), $this->_oConfig->getObject('form_display_request_send'));
  88. $oForm->initChecker();
  89. if($oForm->isSubmittedAndValid()) {
  90. $sIp = getVisitorIP();
  91. $iId = (int)$oForm->insert(array(
  92. 'nip' => ip2long($sIp),
  93. 'date' => time()
  94. ));
  95. if($iId !== false) {
  96. $sRequestsEmail = $this->_oConfig->getRequestsEmail();
  97. if(!empty($sRequestsEmail)) {
  98. bx_import('BxDolEmailTemplates');
  99. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_request_form_message', array(
  100. 'sender_name' => bx_process_output($oForm->getCleanValue('name')),
  101. 'sender_email' => bx_process_output($oForm->getCleanValue('email')),
  102. 'sender_ip' => $sIp
  103. ));
  104. sendMail($sRequestsEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  105. }
  106. $sResult = MsgBox(_t('_bx_invites_msg_request_sent'));
  107. }
  108. }
  109. return array(
  110. 'content' => $sResult . $oForm->getCode()
  111. );
  112. }
  113. public function serviceGetBlockManageRequests()
  114. {
  115. bx_import('BxDolGrid');
  116. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_requests'));
  117. if(!$oGrid)
  118. return '';
  119. return $oGrid->getCode();
  120. }
  121. public function serviceGetMenuAddonRequests()
  122. {
  123. return $this->_oDb->getRequests(array('type' => 'count_all'));
  124. }
  125. public function invite($sEmails, $sText, $iLimit = false, $oForm = null)
  126. {
  127. $iProfileId = $this->getProfileId();
  128. $iAccountId = $this->getAccountId($iProfileId);
  129. bx_import('BxDolKey');
  130. $oKeys = BxDolKey::getInstance();
  131. if(!$oKeys)
  132. return false;
  133. $sKeyCode = $this->_oConfig->getKeyCode();
  134. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  135. if(empty($oForm)) {
  136. bx_import('BxDolForm');
  137. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $this->_oConfig->getObject('form_display_invite_send'));
  138. }
  139. bx_import('BxDolPermalinks');
  140. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  141. bx_import('BxDolEmailTemplates');
  142. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_invite_form_message', array(
  143. 'text' => $sText
  144. ), $iAccountId, $iProfileId);
  145. $iSent = 0;
  146. $iDate = time();
  147. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  148. if(is_array($aEmails) && !empty($aEmails))
  149. foreach($aEmails as $sEmail) {
  150. if($iLimit !== false && (int)$iLimit <= 0)
  151. break;
  152. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  153. $sEmail = trim($sEmail);
  154. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey))), 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. /** @} */