1
0

BxContactModule.php 6.8 KB

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