BxContactModule.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  26. * @page service Service Calls
  27. * @section bx_contact Contact
  28. * @subsection bx_contact-page_blocks Page Blocks
  29. * @subsubsection bx_contact-get_block_form get_block_form
  30. *
  31. * @code bx_srv('bx_contact', 'get_block_form', [...]); @endcode
  32. *
  33. * Get page block with contact form.
  34. *
  35. * @return an array describing a block to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  36. *
  37. * @see BxContactModule::serviceGetBlockForm
  38. */
  39. /**
  40. * @ref bx_contact-get_block_form "get_block_form"
  41. */
  42. public function serviceGetBlockForm()
  43. {
  44. $aDefaultFields = array('name', 'email', 'subject', 'body', 'do_submit');
  45. $mixedAllowed = $this->isAllowedContact();
  46. if($mixedAllowed !== true)
  47. return array(
  48. 'content' => MsgBox($mixedAllowed)
  49. );
  50. $sResult = '';
  51. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_contact'), $this->_oConfig->getObject('form_display_contact_send'), $this->_oTemplate);
  52. $oForm->initChecker();
  53. if($oForm->isSubmittedAndValid()) {
  54. $iId = $oForm->insert(array(
  55. 'uri' => $oForm->generateUri(),
  56. 'date' => time()
  57. ));
  58. if($iId !== false) {
  59. $sCustomFields = '';
  60. $aCustomFields = array();
  61. foreach($oForm->aInputs as $aInput) {
  62. if(in_array($aInput['name'], $aDefaultFields))
  63. continue;
  64. $aCustomFields[$aInput['name']] = bx_process_output($oForm->getCleanValue($aInput['name']));
  65. $sCustomFields .= $aInput['caption'] . ': ' . $aCustomFields[$aInput['name']] . '<br />';
  66. }
  67. $aTemplateKeys = array(
  68. 'SenderName' => bx_process_output($oForm->getCleanValue('name')),
  69. 'SenderEmail' => bx_process_output($oForm->getCleanValue('email')),
  70. 'MessageSubject' => bx_process_output($oForm->getCleanValue('subject')),
  71. 'MessageBody' => bx_process_output(nl2br($oForm->getCleanValue('body')), BX_DATA_TEXT_MULTILINE),
  72. 'CustomFields' => $sCustomFields,
  73. );
  74. $aTemplateKeys = array_merge($aTemplateKeys, $aCustomFields);
  75. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_contact_contact_form_message', $aTemplateKeys);
  76. $sResult = '';
  77. $sRecipientEmail = $this->_oConfig->getEmail();
  78. $aCustomHeaders = array();
  79. if (getParam('bx_contact_add_reply_to')) {
  80. $aCustomHeaders = array(
  81. 'Reply-To' => $oForm->getCleanValue('name') . ' <' . bx_process_output($oForm->getCleanValue('email')) . '>',
  82. );
  83. }
  84. if(sendMail($sRecipientEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM, 'html', false, $aCustomHeaders)) {
  85. $this->onContact();
  86. foreach($oForm->aInputs as $iKey => $aInput)
  87. if(in_array($aInput['type'], array('text', 'textarea')))
  88. $oForm->aInputs[$iKey]['value'] = '';
  89. $sResult = '_ADM_PROFILE_SEND_MSG';
  90. } else
  91. $sResult = '_Email sent failed';
  92. $sResult = MsgBox(_t($sResult));
  93. }
  94. }
  95. return array(
  96. 'content' => $sResult . $oForm->getCode()
  97. );
  98. }
  99. /**
  100. * @page service Service Calls
  101. * @section bx_contact Contact
  102. * @subsection bx_contact-other Other
  103. * @subsubsection bx_contact-get_contact_page_url get_contact_page_url
  104. *
  105. * @code bx_srv('bx_contact', 'get_contact_page_url', [...]); @endcode
  106. *
  107. * Get string with contact page URL. Is used in system and inter-modular integration.
  108. *
  109. * @return string with contact page URL.
  110. *
  111. * @see BxContactModule::serviceGetContactPageUrl
  112. */
  113. /**
  114. * @ref bx_contact-get_block_form "get_block_form"
  115. */
  116. public function serviceGetContactPageUrl()
  117. {
  118. //if (true !== $this->isAllowedContact())
  119. // return false;
  120. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=contact');
  121. }
  122. /*
  123. * COMMON METHODS
  124. */
  125. public function getUserId()
  126. {
  127. return isLogged() ? bx_get_logged_profile_id() : 0;
  128. }
  129. protected function onContact()
  130. {
  131. $iUserId = $this->getUserId();
  132. $this->isAllowedContact(true);
  133. //--- Event -> Contact for Alerts Engine ---//
  134. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'contact', 0, $iUserId);
  135. $oAlert->alert();
  136. //--- Event -> Contact for Alerts Engine ---//
  137. }
  138. protected function isAllowedContact($bPerform = false)
  139. {
  140. $iUserId = $this->getUserId();
  141. $aCheckResult = checkActionModule($iUserId, 'contact', $this->getName(), $bPerform);
  142. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  143. }
  144. }
  145. /** @} */