BxDolEmailTemplates.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolLanguagesQuery');
  10. bx_import('BxDolEmailTemplatesQuery');
  11. /**
  12. * @page objects
  13. * @section email_templates Email Templates
  14. * @ref BxDolEmailTemplates
  15. */
  16. /**
  17. * Email templates are used to send preformated messages.
  18. *
  19. * Email templates are multilingual.
  20. *
  21. * User is sent email in language which is defined in their account, if account language is not set - then default site language is used.
  22. *
  23. * Email templates use template system, so it is possible to customize header/footer accross all templates, by default header/footer files are:
  24. * - templates/base/_email_header.html
  25. * - templates/base/_email_footer.html
  26. *
  27. * 1-click unsubscribe link is added automatically to every email (except a few system emails, which is not supposed to unsubscribe from, like forgot password email).
  28. * Please note: make sure that {unsubscribe} marker is not removed, or unsubscribe link will not be added.
  29. *
  30. *
  31. * @section example Example of usage
  32. *
  33. * Send an email using email templates:
  34. *
  35. * @code
  36. *
  37. * // define custom template variables
  38. * $aPlus = array();
  39. * $aPlus['email'] = 'ktoto@example.com';
  40. * $aPlus['conf_code'] = '123456';
  41. * $aPlus['conf_link'] = BX_DOL_URL_ROOT . 'page.php?i=confirm-email&code=123456';
  42. * $aPlus['conf_form_link'] = BX_DOL_URL_ROOT . 'page.php?i=confirm-email';
  43. *
  44. * bx_import('BxDolEmailTemplates'); // import class
  45. * $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Confirmation', $aPlus); // get class instance and parse template
  46. *
  47. * if ($aTemplate && sendMail('ktoto@example.com', $aTemplate['Subject'], $aTemplate['Body'])) // send email if template exists
  48. * echo 'email was successfully sent';
  49. * else
  50. * echo 'email send failed';
  51. *
  52. * @endcode
  53. *
  54. */
  55. class BxDolEmailTemplates extends BxDol implements iBxDolSingleton {
  56. protected $_oEmailTemplatesQuery;
  57. protected $iDefaultLangId;
  58. protected $iFallbackLangId;
  59. protected $aDefaultKeys;
  60. /**
  61. * Class constructor.
  62. */
  63. protected function __construct() {
  64. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  65. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  66. parent::__construct();
  67. $oLang = BxDolLanguages::getInstance();
  68. $this->iDefaultLangId = $oLang->getCurrentLangId();
  69. $this->iFallbackLangId = $oLang->getLangId('en');
  70. bx_import('BxDolPermalinks');
  71. $sAboutUsLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=about');
  72. $this->aDefaultKeys = array(
  73. 'site_url' => BX_DOL_URL_ROOT,
  74. 'site_name' => getParam('site_title'),
  75. 'about_us' => '<a href="' . $sAboutUsLink . '">' . _t('_sys_et_txt_about_us') . '</a>',
  76. );
  77. $this->_oEmailTemplatesQuery = BxDolEmailTemplatesQuery::getInstance();
  78. }
  79. /**
  80. * Prevent cloning the instance
  81. */
  82. public function __clone() {
  83. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  84. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  85. }
  86. /**
  87. * Get singleton instance of the class
  88. */
  89. static function getInstance() {
  90. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  91. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolEmailTemplates();
  92. return $GLOBALS['bxDolClasses'][__CLASS__];
  93. }
  94. /**
  95. * Function will return array of needed template ;
  96. *
  97. * @param string $sTemplateName - name of necessary template.
  98. * @param integer $iAccountId - account ID of registered member.
  99. * @return array with template 'Subject' and 'Body'.
  100. */
  101. function getTemplate($sTemplateName, $iAccountId = 0 ) {
  102. $iUseLang = $this->iDefaultLangId;
  103. if ($iAccountId) {
  104. bx_import('BxDolAccount');
  105. $oAccount = BxDolAccount::getInstance($iAccountId);
  106. if ($oAccount) {
  107. $aAccountInfo = $oAccount->getInfo();
  108. $iUseLang = $aAccountInfo['lang_id'] ? $aAccountInfo['lang_id'] : $this->iDefaultLangId;
  109. }
  110. }
  111. return $this->_oEmailTemplatesQuery->getTemplate ($sTemplateName, $iUseLang, $this->iDefaultLangId, $this->iFallbackLangId);
  112. }
  113. /**
  114. * Function will return array of needed template with neccessary markers replaced ;
  115. *
  116. * @param string $sTemplateName - name of necessary template.
  117. * @param string $aTemplatekeys - key ane value pairs to replace in subject and body.
  118. * @param integer $iAccountId - account ID of registered member.
  119. * @param integer $iProfileId - profile ID of registered member.
  120. * @return array with template 'Subject' and 'Body'.
  121. */
  122. function parseTemplate($sTemplateName, $aTemplateKeys, $iAccountId = 0, $iProfileId = 0) {
  123. $aTemplate = $this->getTemplate($sTemplateName, $iAccountId);
  124. if (!$aTemplate)
  125. return false;
  126. return array(
  127. 'Subject' => $this->parseContent($aTemplate['Subject'], $aTemplateKeys, $iAccountId, $iProfileId),
  128. 'Body' => $this->parseContent($aTemplate['Body'], $aTemplateKeys, $iAccountId, $iProfileId)
  129. );
  130. }
  131. function parseContent($sContent, $aKeys, $iAccountId = 0, $iProfileId = 0) {
  132. $aResultKeys = $this->aDefaultKeys;
  133. if ($iAccountId) {
  134. bx_import('BxDolAccount');
  135. $oAccount = BxDolAccount::getInstance($iAccountId);
  136. if ($oAccount && ($aAccountInfo = $oAccount->getInfo())) {
  137. $aResultKeys = array_merge($aResultKeys, array(
  138. 'account_id' => $aAccountInfo['id'],
  139. 'account_name' => $aAccountInfo['name'],
  140. 'account_email' => $aAccountInfo['email'],
  141. ));
  142. }
  143. }
  144. if ($iProfileId) {
  145. bx_import('BxDolProfile');
  146. $oProfile = BxDolProfile::getInstance($iProfileId);
  147. if ($oProfile && ($aProfileInfo = $oProfile->getInfo())) {
  148. $aResultKeys = array_merge($aResultKeys, array(
  149. 'profile_name' => $oProfile->getDisplayName(),
  150. 'profile_url' => $oProfile->getUrl(),
  151. 'profile_thumb' => $oProfile->getThumb(),
  152. 'profile_icon' => $oProfile->getIcon(),
  153. 'profile_id' => $aProfileInfo['id'],
  154. 'profile_status' => $aProfileInfo['status'],
  155. 'profile_type' => $aProfileInfo['type'],
  156. 'profile_content_id' => $aProfileInfo['content_id'],
  157. ));
  158. }
  159. }
  160. if (is_array($aKeys))
  161. $aResultKeys = array_merge($aResultKeys, $aKeys);
  162. bx_import('BxDolTemplate');
  163. return BxDolTemplate::getInstance()->parseHtmlByContent($sContent, $aResultKeys, array('{', '}'));
  164. }
  165. }
  166. /** @} */