BxMassMailerModule.php 6.8 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 Mass mailer
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxMassMailerModule extends BxBaseModGeneralModule
  12. {
  13. private $aSegments = array();
  14. function __construct(&$aModule)
  15. {
  16. parent::__construct($aModule);
  17. $CNF = &$this->_oConfig->CNF;
  18. $aRv = array('lvl:0' => _t('_bx_massmailer_segments_all'));
  19. $oAccountQuery = BxDolAclQuery::getInstance();
  20. $aTmp = array();
  21. $oAccountQuery->getLevels(array('type' => 'all_active_pair'), $aTmp);
  22. foreach ($aTmp as $sKey => $sTmp){
  23. if ($sKey != MEMBERSHIP_ID_NON_MEMBER && $sKey != MEMBERSHIP_ID_ACCOUNT)
  24. $aRv['lvl:' . $sKey] = _t($sTmp);
  25. };
  26. $aTmp = $this->_oDb->getSegments();
  27. foreach ($aTmp as $sKey => $sTmp){
  28. $aRv['cst:' . $sKey] = _t($sTmp);
  29. };
  30. $this->aSegments = $aRv;
  31. }
  32. /**
  33. * ACTION METHODS
  34. */
  35. public function actionSetSeenMark($Code)
  36. {
  37. //header('Content-Type: image/png');
  38. if (isset($Code) && trim($Code) != "")
  39. $this->_oDb->updateDateSeenForLetter($Code);
  40. }
  41. /**
  42. * SERVICE METHODS
  43. */
  44. /**
  45. * @page service Service Calls
  46. * @section bx_massmailer Mass mailer
  47. * @subsection bx_massmailer-page_blocks Page Blocks
  48. * @subsubsection bx_massmailer-entity_view entity_view
  49. *
  50. * @code bx_srv('bx_massmailer', 'get_results_search_extended', [...]); @endcode
  51. *
  52. * Get page block with the campagn info
  53. *
  54. * @param $aParams an array with search params.
  55. * @return HTML string with block content to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  56. *
  57. * @see BxMassMailerModule::serviceEntityView
  58. */
  59. /**
  60. * @ref bx_massmailer-entity_view "entity_view"
  61. */
  62. public function serviceEntityView ($iContentId = 0, $sDisplay = false)
  63. {
  64. if(!$iContentId)
  65. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  66. if(!$iContentId)
  67. return false;
  68. $aData = $this->_oDb->getLettersByCampaignId($iContentId);
  69. if (count($aData) > 0)
  70. return $this->_oTemplate->getCampagnInfo($aData);
  71. return $iContentId;
  72. }
  73. public function getSegments($sKey = "")
  74. {
  75. if ($sKey == ""){
  76. return $this->aSegments;
  77. }
  78. else{
  79. if (isset($this->aSegments[$sKey]))
  80. return $this->aSegments[$sKey];
  81. return false;
  82. }
  83. }
  84. public function sendTest($sEmail, $iCampaignId)
  85. {
  86. $aTmp = $this->getDataForCampaign($iCampaignId);
  87. $aTemplate = $aTmp[0];
  88. $aCustomHeaders = $aTmp[1];
  89. if (!$aTemplate)
  90. return false;
  91. return sendMail($sEmail, $aTemplate['Subject'], $aTemplate['Body'], 0, array(), BX_EMAIL_SYSTEM, 'html', false, $aCustomHeaders, false);
  92. }
  93. public function sendAll($iCampaignId)
  94. {
  95. $aEmails = array();
  96. $aTmp = $this->getDataForCampaign($iCampaignId);
  97. $aTemplate = $aTmp[0];
  98. $aCustomHeaders = $aTmp[1];
  99. $aCampaign = $aTmp[2];
  100. if (!$aTemplate)
  101. return false;
  102. $aAccounts = $this->getEmailsBySegment($aCampaign['segments']);
  103. foreach ($aAccounts as $aAccount){
  104. array_push($aEmails, $aAccount['email']);
  105. $sCode = $this->_oDb->addLetter($iCampaignId, $aAccount['email']);
  106. $aAccount['email'] = 'user251@roma1.ru';
  107. sendMail($aAccount['email'], $aTemplate['Subject'], $aTemplate['Body'], 0, array('seen_image_url' => $this->getSeenImageUrl($sCode)), BX_EMAIL_MASS, 'html', false, $aCustomHeaders, true);
  108. }
  109. $this->_oDb->sendCampaign($iCampaignId, implode(',', $aEmails));
  110. return true;
  111. }
  112. public function getEmailCountInSegment($iCampaignId)
  113. {
  114. $aTmp = $this->getDataForCampaign($iCampaignId);
  115. $aCampaign = $aTmp[2];
  116. $aAccounts = $this->getEmailsBySegment($aCampaign['segments']);
  117. return count($aAccounts);
  118. }
  119. private function getEmailsBySegment($sSegment)
  120. {
  121. $aAccounts = array();
  122. if (substr_count($sSegment, 'lvl:') > 0){
  123. $sLvl = str_replace('lvl:', '', $sSegment);
  124. switch ($sLvl) {
  125. case 0:
  126. $aAccounts = $this->_oDb->getAccountsByTerms();
  127. break;
  128. case MEMBERSHIP_ID_UNCONFIRMED:
  129. $aAccounts = $this->_oDb->getAccountsByTerms(" AND `ta`.`email_confirmed` = 0 AND `ta`.`phone_confirmed` = 0 ");
  130. break;
  131. case MEMBERSHIP_ID_SUSPENDED:
  132. $aAccounts = $this->_oDb->getAccountsByTerms($this->_oDb->prepareAsString(" AND `tp`.`status`=?", 'suspended'));
  133. break;
  134. case MEMBERSHIP_ID_PENDING:
  135. $aAccounts = $this->_oDb->getAccountsByTerms($this->_oDb->prepareAsString(" AND `tp`.`status`=?", 'pending'));
  136. break;
  137. case MEMBERSHIP_ID_STANDARD:
  138. $aAccounts = $this->_oDb->getAccountsByTerms($this->_oDb->prepareAsString(" AND `tp`.`id` NOT IN (SELECT `IDMember` FROM `sys_acl_levels_members`) ", $sLvl));
  139. break;
  140. default:
  141. $aAccounts = $this->_oDb->getAccountsByTerms($this->_oDb->prepareAsString(" AND `tp`.`id` IN (SELECT `IDMember` FROM `sys_acl_levels_members` WHERE IDLevel = ?) ", $sLvl));
  142. break;
  143. }
  144. }
  145. return $aAccounts;
  146. }
  147. private function getDataForCampaign($iCampaignId)
  148. {
  149. $aCampaign = $this->_oDb->getCampaignInfoById($iCampaignId);
  150. $oEmailTemplates = BxDolEmailTemplates::getInstance();
  151. $aTemplate = $oEmailTemplates->parseTemplate('bx_massmailer_email', array('content' => $aCampaign['body']));
  152. $aTemplate['Subject'] = $aCampaign['subject'];
  153. $aCustomHeaders = array();
  154. if ($aCampaign['from_name'] != '')
  155. $aCustomHeaders['From'] = $aCampaign['from_name'];
  156. if ($aCampaign['reply_to'] != '')
  157. $aCustomHeaders['Reply-To'] = $aCampaign['reply_to'];
  158. return array($aTemplate, $aCustomHeaders, $aCampaign);
  159. }
  160. private function getSeenImageUrl($sKey)
  161. {
  162. return BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'SetSeenMark/' . $sKey . "/";
  163. }
  164. }
  165. /** @} */