BxContactModule.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 'From' => $oForm->getCleanValue('name') . ' <' . bx_process_output($oForm->getCleanValue('email')) . '>',
  63. 'Sender' => trim(getParam('bx_smtp_from_email')) ? trim(getParam('bx_smtp_from_email')) : getParam('site_email_notify'),
  64. );
  65. if(sendMail($sRecipientEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM, 'html', false, $aCustomHeaders)) {
  66. $this->onContact();
  67. foreach($oForm->aInputs as $iKey => $aInput)
  68. if(in_array($aInput['type'], array('text', 'textarea')))
  69. $oForm->aInputs[$iKey]['value'] = '';
  70. $sResult = '_ADM_PROFILE_SEND_MSG';
  71. } else
  72. $sResult = '_Email sent failed';
  73. $sResult = MsgBox(_t($sResult));
  74. }
  75. }
  76. return array(
  77. 'content' => $sResult . $oForm->getCode()
  78. );
  79. }
  80. public function serviceGetContactPageUrl()
  81. {
  82. //if (true !== $this->isAllowedContact())
  83. // return false;
  84. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=contact');
  85. }
  86. /*
  87. * COMMON METHODS
  88. */
  89. public function getUserId()
  90. {
  91. return isLogged() ? bx_get_logged_profile_id() : 0;
  92. }
  93. protected function onContact()
  94. {
  95. $iUserId = $this->getUserId();
  96. $this->isAllowedContact(true);
  97. //--- Event -> Contact for Alerts Engine ---//
  98. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'contact', 0, $iUserId);
  99. $oAlert->alert();
  100. //--- Event -> Contact for Alerts Engine ---//
  101. }
  102. protected function isAllowedContact($bPerform = false)
  103. {
  104. $iUserId = $this->getUserId();
  105. $aCheckResult = checkActionModule($iUserId, 'contact', $this->getName(), $bPerform);
  106. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  107. }
  108. }
  109. /** @} */