BxDolEmailTemplates.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. {
  57. protected $_oEmailTemplatesQuery;
  58. protected $iDefaultLangId;
  59. protected $iFallbackLangId;
  60. protected $aDefaultKeys;
  61. /**
  62. * Class constructor.
  63. */
  64. protected function __construct()
  65. {
  66. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  67. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  68. parent::__construct();
  69. $oLang = BxDolLanguages::getInstance();
  70. $this->iDefaultLangId = $oLang->getCurrentLangId();
  71. $this->iFallbackLangId = $oLang->getLangId('en');
  72. bx_import('BxDolPermalinks');
  73. $sAboutUsLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=about');
  74. $this->aDefaultKeys = array(
  75. 'site_url' => BX_DOL_URL_ROOT,
  76. 'site_name' => getParam('site_title'),
  77. 'about_us' => '<a href="' . $sAboutUsLink . '">' . _t('_sys_et_txt_about_us') . '</a>',
  78. );
  79. $this->_oEmailTemplatesQuery = BxDolEmailTemplatesQuery::getInstance();
  80. }
  81. /**
  82. * Prevent cloning the instance
  83. */
  84. public function __clone()
  85. {
  86. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  87. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  88. }
  89. /**
  90. * Get singleton instance of the class
  91. */
  92. static function getInstance()
  93. {
  94. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  95. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolEmailTemplates();
  96. return $GLOBALS['bxDolClasses'][__CLASS__];
  97. }
  98. /**
  99. * Function will return array of needed template ;
  100. *
  101. * @param string $sTemplateName - name of necessary template.
  102. * @param integer $iAccountId - account ID of registered member.
  103. * @return array with template 'Subject' and 'Body'.
  104. */
  105. function getTemplate($sTemplateName, $iAccountId = 0 )
  106. {
  107. $iUseLang = $this->iDefaultLangId;
  108. if ($iAccountId) {
  109. bx_import('BxDolAccount');
  110. $oAccount = BxDolAccount::getInstance($iAccountId);
  111. if ($oAccount) {
  112. $aAccountInfo = $oAccount->getInfo();
  113. $iUseLang = $aAccountInfo['lang_id'] ? $aAccountInfo['lang_id'] : $this->iDefaultLangId;
  114. }
  115. }
  116. return $this->_oEmailTemplatesQuery->getTemplate ($sTemplateName, $iUseLang, $this->iDefaultLangId, $this->iFallbackLangId);
  117. }
  118. /**
  119. * Function will return array of needed template with neccessary markers replaced ;
  120. *
  121. * @param string $sTemplateName - name of necessary template.
  122. * @param string $aTemplatekeys - key ane value pairs to replace in subject and body.
  123. * @param integer $iAccountId - account ID of registered member.
  124. * @param integer $iProfileId - profile ID of registered member.
  125. * @return array with template 'Subject' and 'Body'.
  126. */
  127. function parseTemplate($sTemplateName, $aTemplateKeys, $iAccountId = 0, $iProfileId = 0)
  128. {
  129. $aTemplate = $this->getTemplate($sTemplateName, $iAccountId);
  130. if (!$aTemplate)
  131. return false;
  132. return array(
  133. 'Subject' => $this->parseContent($aTemplate['Subject'], $aTemplateKeys, $iAccountId, $iProfileId),
  134. 'Body' => $this->parseContent($aTemplate['Body'], $aTemplateKeys, $iAccountId, $iProfileId)
  135. );
  136. }
  137. function parseContent($sContent, $aKeys, $iAccountId = 0, $iProfileId = 0)
  138. {
  139. $aResultKeys = $this->aDefaultKeys;
  140. if ($iAccountId) {
  141. bx_import('BxDolAccount');
  142. $oAccount = BxDolAccount::getInstance($iAccountId);
  143. if ($oAccount && ($aAccountInfo = $oAccount->getInfo())) {
  144. $aResultKeys = array_merge($aResultKeys, array(
  145. 'account_id' => $aAccountInfo['id'],
  146. 'account_name' => $aAccountInfo['name'],
  147. 'account_email' => $aAccountInfo['email'],
  148. ));
  149. }
  150. }
  151. if ($iProfileId) {
  152. bx_import('BxDolProfile');
  153. $oProfile = BxDolProfile::getInstance($iProfileId);
  154. if ($oProfile && ($aProfileInfo = $oProfile->getInfo())) {
  155. $aResultKeys = array_merge($aResultKeys, array(
  156. 'profile_name' => $oProfile->getDisplayName(),
  157. 'profile_url' => $oProfile->getUrl(),
  158. 'profile_thumb' => $oProfile->getThumb(),
  159. 'profile_icon' => $oProfile->getIcon(),
  160. 'profile_id' => $aProfileInfo['id'],
  161. 'profile_status' => $aProfileInfo['status'],
  162. 'profile_type' => $aProfileInfo['type'],
  163. 'profile_content_id' => $aProfileInfo['content_id'],
  164. ));
  165. }
  166. }
  167. if (is_array($aKeys))
  168. $aResultKeys = array_merge($aResultKeys, $aKeys);
  169. bx_import('BxDolTemplate');
  170. return BxDolTemplate::getInstance()->parseHtmlByContent($sContent, $aResultKeys, array('{', '}'));
  171. }
  172. }
  173. /** @} */