BxContactModule.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Contact Contact
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolModule');
  12. class BxContactModule extends BxDolModule
  13. {
  14. /**
  15. * Constructor
  16. */
  17. function __construct($aModule)
  18. {
  19. parent::BxDolModule($aModule);
  20. $this->_oConfig->init($this->_oDb);
  21. }
  22. /**
  23. * SERVICE METHODS
  24. */
  25. public function serviceGetBlockForm()
  26. {
  27. $aDefaultFields = array('name', 'email', 'subject', 'body', 'do_submit');
  28. $mixedAllowed = $this->isAllowedContact();
  29. if($mixedAllowed !== true)
  30. return array(
  31. 'content' => MsgBox($mixedAllowed)
  32. );
  33. $sResult = '';
  34. bx_import('BxDolForm');
  35. $oForm = BxDolForm::getObjectInstance('mod_cnt_contact', 'mod_cnt_contact_send');
  36. $oForm->initChecker();
  37. if($oForm->isSubmittedAndValid()) {
  38. $iId = $oForm->insert(array(
  39. 'uri' => $oForm->generateUri(),
  40. 'date' => time()
  41. ));
  42. if($iId !== false) {
  43. $sCustomFields = '';
  44. $aCustomFields = array();
  45. foreach($oForm->aInputs as $aInput) {
  46. if(in_array($aInput['name'], $aDefaultFields))
  47. continue;
  48. $aCustomFields[$aInput['name']] = bx_process_output($oForm->getCleanValue($aInput['name']));
  49. $sCustomFields .= $aInput['caption'] . ': ' . $aCustomFields[$aInput['name']] . '<br />';
  50. }
  51. $aTemplateKeys = array(
  52. 'SenderName' => bx_process_output($oForm->getCleanValue('name')),
  53. 'SenderEmail' => bx_process_output($oForm->getCleanValue('email')),
  54. 'MessageSubject' => bx_process_output($oForm->getCleanValue('subject')),
  55. 'MessageBody' => bx_process_output(nl2br($oForm->getCleanValue('body')), BX_DATA_TEXT_MULTILINE),
  56. 'CustomFields' => $sCustomFields,
  57. );
  58. $aTemplateKeys = array_merge($aTemplateKeys, $aCustomFields);
  59. bx_import('BxDolEmailTemplates');
  60. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_contact_contact_form_message', $aTemplateKeys);
  61. $sResult = '';
  62. $sRecipientEmail = $this->_oConfig->getEmail();
  63. if(sendMail($sRecipientEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM)) {
  64. $this->onContact();
  65. $sResult = '_ADM_PROFILE_SEND_MSG';
  66. }
  67. else
  68. $sResult = '_Email sent failed';
  69. $sResult = MsgBox(_t($sResult));
  70. }
  71. }
  72. return array(
  73. 'content' => $sResult . $oForm->getCode()
  74. );
  75. }
  76. /*
  77. * COMMON METHODS
  78. */
  79. public function getUserId()
  80. {
  81. return isLogged() ? bx_get_logged_profile_id() : 0;
  82. }
  83. protected function onContact()
  84. {
  85. $iUserId = $this->getUserId();
  86. $this->isAllowedContact(true);
  87. //--- Event -> Contact for Alerts Engine ---//
  88. bx_import('BxDolAlerts');
  89. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'contact', 0, $iUserId);
  90. $oAlert->alert();
  91. //--- Event -> Contact for Alerts Engine ---//
  92. }
  93. protected function isAllowedContact($bPerform = false)
  94. {
  95. $iUserId = $this->getUserId();
  96. $aCheckResult = checkActionModule($iUserId, 'contact', $this->getName(), $bPerform);
  97. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  98. }
  99. }
  100. /** @} */