BxContactModule.php 6.5 KB

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