PageRenderTime 55ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Mailing/Event/BAO/Resubscribe.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 290 lines | 183 code | 36 blank | 71 comment | 28 complexity | 4f974fab30c238df992ff9b8c4e769bd MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. require_once 'Mail/mime.php';
  33. /**
  34. * Class CRM_Mailing_Event_BAO_Resubscribe
  35. */
  36. class CRM_Mailing_Event_BAO_Resubscribe {
  37. /**
  38. * Resubscribe a contact to the groups, he/she was unsubscribed from.
  39. *
  40. * @param int $job_id
  41. * The job ID.
  42. * @param int $queue_id
  43. * The Queue Event ID of the recipient.
  44. * @param string $hash
  45. * The hash.
  46. *
  47. * @return array|null
  48. * $groups Array of all groups to which the contact was added, or null if the queue event could not be found.
  49. */
  50. public static function &resub_to_mailing($job_id, $queue_id, $hash) {
  51. // First make sure there's a matching queue event.
  52. $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
  53. $success = NULL;
  54. if (!$q) {
  55. return $success;
  56. }
  57. // check if this queue_id was actually unsubscribed
  58. $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
  59. $ue->event_queue_id = $queue_id;
  60. $ue->org_unsubscribe = 0;
  61. if (!$ue->find(TRUE)) {
  62. return $success;
  63. }
  64. $contact_id = $q->contact_id;
  65. $transaction = new CRM_Core_Transaction();
  66. $do = new CRM_Core_DAO();
  67. $mg = CRM_Mailing_DAO_MailingGroup::getTableName();
  68. $job = CRM_Mailing_BAO_MailingJob::getTableName();
  69. $mailing = CRM_Mailing_BAO_Mailing::getTableName();
  70. $group = CRM_Contact_BAO_Group::getTableName();
  71. $gc = CRM_Contact_BAO_GroupContact::getTableName();
  72. // We Need the mailing Id for the hook...
  73. $do->query("SELECT $job.mailing_id as mailing_id
  74. FROM $job
  75. WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
  76. $do->fetch();
  77. $mailing_id = $do->mailing_id;
  78. $do->query("
  79. SELECT $mg.entity_table as entity_table,
  80. $mg.entity_id as entity_id
  81. FROM $mg
  82. INNER JOIN $job
  83. ON $job.mailing_id = $mg.mailing_id
  84. INNER JOIN $group
  85. ON $mg.entity_id = $group.id
  86. WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "
  87. AND $mg.group_type IN ( 'Include', 'Base' )
  88. AND $group.is_hidden = 0"
  89. );
  90. // Make a list of groups and a list of prior mailings that received
  91. // this mailing.
  92. $groups = [];
  93. $mailings = [];
  94. while ($do->fetch()) {
  95. if ($do->entity_table == $group) {
  96. $groups[$do->entity_id] = NULL;
  97. }
  98. elseif ($do->entity_table == $mailing) {
  99. $mailings[] = $do->entity_id;
  100. }
  101. }
  102. // As long as we have prior mailings, find their groups and add to the
  103. // list.
  104. while (!empty($mailings)) {
  105. $do->query("
  106. SELECT $mg.entity_table as entity_table,
  107. $mg.entity_id as entity_id
  108. FROM $mg
  109. WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
  110. AND $mg.group_type = 'Include'");
  111. $mailings = [];
  112. while ($do->fetch()) {
  113. if ($do->entity_table == $group) {
  114. $groups[$do->entity_id] = TRUE;
  115. }
  116. elseif ($do->entity_table == $mailing) {
  117. $mailings[] = $do->entity_id;
  118. }
  119. }
  120. }
  121. $group_ids = array_keys($groups);
  122. $base_groups = NULL;
  123. CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
  124. // Now we have a complete list of recipient groups. Filter out all
  125. // those except smart groups and those that the contact belongs to.
  126. $do->query("
  127. SELECT $group.id as group_id,
  128. $group.title as title
  129. FROM $group
  130. LEFT JOIN $gc
  131. ON $gc.group_id = $group.id
  132. WHERE $group.id IN (" . implode(', ', $group_ids) . ")
  133. AND ($group.saved_search_id is not null
  134. OR ($gc.contact_id = $contact_id
  135. AND $gc.status = 'Removed')
  136. )");
  137. while ($do->fetch()) {
  138. $groups[$do->group_id] = $do->title;
  139. }
  140. $contacts = [$contact_id];
  141. foreach ($groups as $group_id => $group_name) {
  142. $notadded = 0;
  143. if ($group_name) {
  144. list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email');
  145. }
  146. if ($notadded) {
  147. unset($groups[$group_id]);
  148. }
  149. }
  150. // remove entry from Unsubscribe table.
  151. $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
  152. $ue->event_queue_id = $queue_id;
  153. $ue->org_resubscribe = 0;
  154. if ($ue->find(TRUE)) {
  155. $ue->delete();
  156. }
  157. $transaction->commit();
  158. return $groups;
  159. }
  160. /**
  161. * Send a response email informing the contact of the groups to which he/she
  162. * has been resubscribed.
  163. *
  164. * @param string $queue_id
  165. * The queue event ID.
  166. * @param array $groups
  167. * List of group IDs.
  168. * @param bool $is_domain
  169. * Is this domain-level?.
  170. * @param int $job
  171. * The job ID.
  172. */
  173. public static function send_resub_response($queue_id, $groups, $is_domain = FALSE, $job) {
  174. // param is_domain is not supported as of now.
  175. $config = CRM_Core_Config::singleton();
  176. $domain = CRM_Core_BAO_Domain::getDomain();
  177. $jobTable = CRM_Mailing_BAO_MailingJob::getTableName();
  178. $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
  179. $contacts = CRM_Contact_DAO_Contact::getTableName();
  180. $email = CRM_Core_DAO_Email::getTableName();
  181. $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
  182. //get the default domain email address.
  183. list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
  184. $dao = new CRM_Mailing_BAO_Mailing();
  185. $dao->query(" SELECT * FROM $mailingTable
  186. INNER JOIN $jobTable ON
  187. $jobTable.mailing_id = $mailingTable.id
  188. WHERE $jobTable.id = $job");
  189. $dao->fetch();
  190. $component = new CRM_Mailing_BAO_MailingComponent();
  191. $component->id = $dao->resubscribe_id;
  192. $component->find(TRUE);
  193. $html = $component->body_html;
  194. if ($component->body_text) {
  195. $text = $component->body_text;
  196. }
  197. else {
  198. $text = CRM_Utils_String::htmlToText($component->body_html);
  199. }
  200. $eq = new CRM_Core_DAO();
  201. $eq->query(
  202. "SELECT $contacts.preferred_mail_format as format,
  203. $contacts.id as contact_id,
  204. $email.email as email,
  205. $queue.hash as hash
  206. FROM $contacts
  207. INNER JOIN $queue ON $queue.contact_id = $contacts.id
  208. INNER JOIN $email ON $queue.email_id = $email.id
  209. WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer')
  210. );
  211. $eq->fetch();
  212. foreach ($groups as $key => $value) {
  213. if (!$value) {
  214. unset($groups[$key]);
  215. }
  216. }
  217. $message = new Mail_mime("\n");
  218. list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email);
  219. $bao = new CRM_Mailing_BAO_Mailing();
  220. $bao->body_text = $text;
  221. $bao->body_html = $html;
  222. $tokens = $bao->getTokens();
  223. if ($eq->format == 'HTML' || $eq->format == 'Both') {
  224. $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
  225. $html = CRM_Utils_Token::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id, $eq->hash);
  226. $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
  227. $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
  228. $message->setHTMLBody($html);
  229. }
  230. if (!$html || $eq->format == 'Text' || $eq->format == 'Both') {
  231. $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, TRUE, $tokens['text']);
  232. $text = CRM_Utils_Token::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id, $eq->hash);
  233. $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
  234. $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
  235. $message->setTxtBody($text);
  236. }
  237. $headers = [
  238. 'Subject' => $component->subject,
  239. 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>',
  240. 'To' => $eq->email,
  241. 'Reply-To' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
  242. 'Return-Path' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
  243. ];
  244. CRM_Mailing_BAO_Mailing::addMessageIdHeader($headers, 'e', $job, $queue_id, $eq->hash);
  245. $b = CRM_Utils_Mail::setMimeParams($message);
  246. $h = $message->headers($headers);
  247. $mailer = \Civi::service('pear_mail');
  248. if (is_object($mailer)) {
  249. $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
  250. $mailer->send($eq->email, $h, $b);
  251. unset($errorScope);
  252. }
  253. }
  254. }