BxInvModule.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 TridentModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. define('BX_INV_TYPE_FROM_MEMBER', 'from_member');
  13. define('BX_INV_TYPE_FROM_SYSTEM', 'from_system');
  14. class BxInvModule extends BxDolModule
  15. {
  16. /**
  17. * Constructor
  18. */
  19. function __construct($aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_oConfig->init($this->_oDb);
  23. }
  24. /**
  25. * SERVICE METHODS
  26. */
  27. public function serviceGetBlockInvite()
  28. {
  29. $iProfileId = $this->getProfileId();
  30. $iAccountId = $this->getAccountId($iProfileId);
  31. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  32. if($mixedAllowed !== true)
  33. return '';
  34. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  35. if($iInvited >= $this->_oConfig->getCountPerUser())
  36. return '';
  37. return array(
  38. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId)
  39. );
  40. }
  41. public function serviceGetBlockFormInvite()
  42. {
  43. $iProfileId = $this->getProfileId();
  44. $iAccountId = $this->getAccountId($iProfileId);
  45. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  46. if($mixedAllowed !== true)
  47. return array(
  48. 'content' => MsgBox($mixedAllowed)
  49. );
  50. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  51. $iInvites = $this->_oConfig->getCountPerUser() - $iInvited;
  52. if($iInvites <= 0)
  53. return array(
  54. 'content' => MsgBox(_t('_bx_invites_err_limit_reached'))
  55. );
  56. $oForm = $this->getFormObjectInvite();
  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(BX_INV_TYPE_FROM_MEMBER, $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. $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. $sManageUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=invites-requests');
  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. 'manage_url' => $sManageUrl
  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. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_requests'));
  116. if(!$oGrid)
  117. return '';
  118. $this->_oTemplate->addCss(array('main.css'));
  119. return $oGrid->getCode();
  120. }
  121. public function serviceGetMenuAddonRequests()
  122. {
  123. return $this->_oDb->getRequests(array('type' => 'count_all'));
  124. }
  125. public function invite($sType, $sEmails, $sText, $iLimit = false, $oForm = null)
  126. {
  127. $iProfileId = $this->getProfileId();
  128. $iAccountId = $this->getAccountId($iProfileId);
  129. $oKeys = BxDolKey::getInstance();
  130. if(!$oKeys || !in_array($sType, array(BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM)))
  131. return false;
  132. $sKeyCode = $this->_oConfig->getKeyCode();
  133. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  134. $sEmailTemplate = '';
  135. switch($sType) {
  136. case BX_INV_TYPE_FROM_MEMBER:
  137. $sEmailTemplate = 'bx_invites_invite_form_message';
  138. break;
  139. case BX_INV_TYPE_FROM_SYSTEM:
  140. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  141. break;
  142. }
  143. if(empty($oForm))
  144. $oForm = $this->getFormObjectInvite();
  145. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  146. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  147. 'text' => $sText
  148. ), $iAccountId, $iProfileId);
  149. $iSent = 0;
  150. $iDate = time();
  151. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  152. if(is_array($aEmails) && !empty($aEmails))
  153. foreach($aEmails as $sEmail) {
  154. if($iLimit !== false && (int)$iLimit <= 0)
  155. break;
  156. $sEmail = trim($sEmail);
  157. if(empty($sEmail))
  158. continue;
  159. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  160. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey))), BX_EMAIL_SYSTEM)) {
  161. $oForm->insert(array(
  162. 'account_id' => $iAccountId,
  163. 'profile_id' => $iProfileId,
  164. 'email' => $sEmail,
  165. 'date' => $iDate
  166. ));
  167. $this->onInvite($iAccountId, $iProfileId);
  168. $iSent += 1;
  169. if($iLimit !== false)
  170. $iLimit -= 1;
  171. }
  172. }
  173. return $iSent;
  174. }
  175. public function isAllowedInvite($iProfileId, $bPerform = false)
  176. {
  177. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  178. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  179. }
  180. public function isAllowedRequest($iProfileId, $bPerform = false)
  181. {
  182. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  183. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  184. }
  185. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  186. {
  187. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  188. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  189. }
  190. public function getProfileId()
  191. {
  192. return isLogged() ? bx_get_logged_profile_id() : 0;
  193. }
  194. public function getProfileObject($iProfileId = 0)
  195. {
  196. $oProfile = BxDolProfile::getInstance($iProfileId);
  197. if (!$oProfile)
  198. $oProfile = BxDolProfileUndefined::getInstance();
  199. return $oProfile;
  200. }
  201. public function getAccountId($iProfileId)
  202. {
  203. $oProfile = $this->getProfileObject($iProfileId);
  204. if($oProfile->id() == 0)
  205. return 0;
  206. return $oProfile->getAccountId();
  207. }
  208. protected function onInvite($iAccountId, $iProfileId)
  209. {
  210. $this->isAllowedInvite($iProfileId, true);
  211. //--- Event -> Invite for Alerts Engine ---//
  212. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  213. $oAlert->alert();
  214. //--- Event -> Invite for Alerts Engine ---//
  215. }
  216. protected function onRequest()
  217. {
  218. //--- Event -> Request for Alerts Engine ---//
  219. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'request');
  220. $oAlert->alert();
  221. //--- Event -> Request for Alerts Engine ---//
  222. }
  223. protected function getFormObjectInvite($sDisplay = '')
  224. {
  225. if(empty($sDisplay))
  226. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  227. bx_import('FormCheckerHelper', $this->_aModule);
  228. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  229. }
  230. }
  231. /** @} */