BxContactModule.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Contact Contact
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. class BxContactModule 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 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. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_contact'), $this->_oConfig->getObject('form_display_contact_send'), $this->_oTemplate);
  35. $oForm->initChecker();
  36. if($oForm->isSubmittedAndValid()) {
  37. $iId = $oForm->insert(array(
  38. 'uri' => $oForm->generateUri(),
  39. 'date' => time()
  40. ));
  41. if($iId !== false) {
  42. $sCustomFields = '';
  43. $aCustomFields = array();
  44. foreach($oForm->aInputs as $aInput) {
  45. if(in_array($aInput['name'], $aDefaultFields))
  46. continue;
  47. $aCustomFields[$aInput['name']] = bx_process_output($oForm->getCleanValue($aInput['name']));
  48. $sCustomFields .= $aInput['caption'] . ': ' . $aCustomFields[$aInput['name']] . '<br />';
  49. }
  50. $aTemplateKeys = array(
  51. 'SenderName' => bx_process_output($oForm->getCleanValue('name')),
  52. 'SenderEmail' => bx_process_output($oForm->getCleanValue('email')),
  53. 'MessageSubject' => bx_process_output($oForm->getCleanValue('subject')),
  54. 'MessageBody' => bx_process_output(nl2br($oForm->getCleanValue('body')), BX_DATA_TEXT_MULTILINE),
  55. 'CustomFields' => $sCustomFields,
  56. );
  57. $aTemplateKeys = array_merge($aTemplateKeys, $aCustomFields);
  58. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_contact_contact_form_message', $aTemplateKeys);
  59. $sResult = '';
  60. $sRecipientEmail = $this->_oConfig->getEmail();
  61. $aCustomHeaders = array();
  62. if (getParam('bx_contact_add_reply_to')) {
  63. $aCustomHeaders = array(
  64. 'Reply-To' => $oForm->getCleanValue('name') . ' <' . bx_process_output($oForm->getCleanValue('email')) . '>',
  65. );
  66. }
  67. if(sendMail($sRecipientEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM, 'html', false, $aCustomHeaders)) {
  68. $this->onContact();
  69. foreach($oForm->aInputs as $iKey => $aInput)
  70. if(in_array($aInput['type'], array('text', 'textarea')))
  71. $oForm->aInputs[$iKey]['value'] = '';
  72. $sResult = '_ADM_PROFILE_SEND_MSG';
  73. } else
  74. $sResult = '_Email sent failed';
  75. $sResult = MsgBox(_t($sResult));
  76. }
  77. }
  78. return array(
  79. 'content' => $sResult . $oForm->getCode()
  80. );
  81. }
  82. public function serviceGetContactPageUrl()
  83. {
  84. //if (true !== $this->isAllowedContact())
  85. // return false;
  86. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=contact');
  87. }
  88. /*
  89. * COMMON METHODS
  90. */
  91. public function getUserId()
  92. {
  93. return isLogged() ? bx_get_logged_profile_id() : 0;
  94. }
  95. protected function onContact()
  96. {
  97. $iUserId = $this->getUserId();
  98. $this->isAllowedContact(true);
  99. //--- Event -> Contact for Alerts Engine ---//
  100. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'contact', 0, $iUserId);
  101. $oAlert->alert();
  102. //--- Event -> Contact for Alerts Engine ---//
  103. }
  104. protected function isAllowedContact($bPerform = false)
  105. {
  106. $iUserId = $this->getUserId();
  107. $aCheckResult = checkActionModule($iUserId, 'contact', $this->getName(), $bPerform);
  108. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  109. }
  110. }
  111. /** @} */