BxDolEmailTemplates.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * @page objects
  11. * @section email_templates Email Templates
  12. * @ref BxDolEmailTemplates
  13. */
  14. /**
  15. * Email templates are used to send preformated messages.
  16. *
  17. * Email templates are multilingual.
  18. *
  19. * User is sent email in language which is defined in their account, if account language is not set - then default site language is used.
  20. *
  21. * Email templates use template system, so it is possible to customize header/footer accross all templates, by default header/footer files are:
  22. * - templates/base/_email_header.html
  23. * - templates/base/_email_footer.html
  24. *
  25. * 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).
  26. * Please note: make sure that {unsubscribe} marker is not removed, or unsubscribe link will not be added.
  27. *
  28. *
  29. * @section example Example of usage
  30. *
  31. * Send an email using email templates:
  32. *
  33. * @code
  34. *
  35. * // define custom template variables
  36. * $aPlus = array();
  37. * $aPlus['email'] = 'ktoto@example.com';
  38. * $aPlus['conf_code'] = '123456';
  39. * $aPlus['conf_link'] = BX_DOL_URL_ROOT . 'page.php?i=confirm-email&code=123456';
  40. * $aPlus['conf_form_link'] = BX_DOL_URL_ROOT . 'page.php?i=confirm-email';
  41. *
  42. * $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_Confirmation', $aPlus); // get class instance and parse template
  43. *
  44. * if ($aTemplate && sendMail('ktoto@example.com', $aTemplate['Subject'], $aTemplate['Body'])) // send email if template exists
  45. * echo 'email was successfully sent';
  46. * else
  47. * echo 'email send failed';
  48. *
  49. * @endcode
  50. *
  51. */
  52. class BxDolEmailTemplates extends BxDol implements iBxDolSingleton
  53. {
  54. protected $_oEmailTemplatesQuery;
  55. protected $iDefaultLangId;
  56. protected $iFallbackLangId;
  57. protected $aDefaultKeys;
  58. /**
  59. * Class constructor.
  60. */
  61. protected function __construct()
  62. {
  63. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  64. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  65. parent::__construct();
  66. $oLang = BxDolLanguages::getInstance();
  67. $this->iDefaultLangId = $oLang->getCurrentLangId();
  68. $this->iFallbackLangId = $oLang->getLangId('en');
  69. $sAboutUsLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=about');
  70. $this->aDefaultKeys = array(
  71. 'site_url' => BX_DOL_URL_ROOT,
  72. 'site_name' => getParam('site_title'),
  73. 'about_us' => '<a href="' . $sAboutUsLink . '">' . _t('_sys_et_txt_about_us') . '</a>',
  74. );
  75. $this->_oEmailTemplatesQuery = BxDolEmailTemplatesQuery::getInstance();
  76. }
  77. /**
  78. * Prevent cloning the instance
  79. */
  80. public function __clone()
  81. {
  82. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  83. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  84. }
  85. /**
  86. * Get singleton instance of the class
  87. */
  88. static function getInstance()
  89. {
  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. {
  103. $iUseLang = $this->iDefaultLangId;
  104. if ($iAccountId) {
  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. {
  124. $aTemplate = $this->getTemplate($sTemplateName, $iAccountId);
  125. if (!$aTemplate)
  126. return false;
  127. return array(
  128. 'Subject' => $this->parseContent($aTemplate['Subject'], $aTemplateKeys, $iAccountId, $iProfileId),
  129. 'Body' => $this->parseContent($aTemplate['Body'], $aTemplateKeys, $iAccountId, $iProfileId)
  130. );
  131. }
  132. function parseContent($sContent, $aKeys, $iAccountId = 0, $iProfileId = 0)
  133. {
  134. $aResultKeys = $this->aDefaultKeys;
  135. if ($iAccountId) {
  136. $oAccount = BxDolAccount::getInstance($iAccountId);
  137. if ($oAccount && ($aAccountInfo = $oAccount->getInfo())) {
  138. $aResultKeys = array_merge($aResultKeys, array(
  139. 'account_id' => $aAccountInfo['id'],
  140. 'account_name' => $aAccountInfo['name'],
  141. 'account_email' => $aAccountInfo['email'],
  142. ));
  143. }
  144. }
  145. if ($iProfileId) {
  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. return BxDolTemplate::getInstance()->parseHtmlByContent($sContent, $aResultKeys, array('{', '}'));
  163. }
  164. }
  165. /** @} */