Merge pull request #23184 from totten/master-bd-drush
[civicrm-core.git] / CRM / Mailing / Event / BAO / Confirm.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18require_once 'Mail/mime.php';
4c6ce474
EM
19
20/**
21 * Class CRM_Mailing_Event_BAO_Confirm
22 */
6a488035
TO
23class CRM_Mailing_Event_BAO_Confirm extends CRM_Mailing_Event_DAO_Confirm {
24
6a488035 25 /**
fe482240 26 * Confirm a pending subscription.
6a488035 27 *
90c8230e
TO
28 * @param int $contact_id
29 * The id of the contact.
30 * @param int $subscribe_id
31 * The id of the subscription event.
32 * @param string $hash
33 * The hash.
6a488035 34 *
795492f3 35 * @return bool
a6c01b45 36 * True on success
6a488035
TO
37 */
38 public static function confirm($contact_id, $subscribe_id, $hash) {
39 $se = &CRM_Mailing_Event_BAO_Subscribe::verify(
40 $contact_id,
41 $subscribe_id,
42 $hash
43 );
44
45 if (!$se) {
46 return FALSE;
47 }
48
49 // before we proceed lets just check if this contact is already 'Added'
50 // if so, we should ignore this request and hence avoid sending multiple
51 // emails - CRM-11157
52 $details = CRM_Contact_BAO_GroupContact::getMembershipDetail($contact_id, $se->group_id);
53 if ($details && $details->status == 'Added') {
54 // This contact is already subscribed
f8d4979e
DL
55 // lets return the group title
56 return CRM_Core_DAO::getFieldValue(
57 'CRM_Contact_DAO_Group',
58 $se->group_id,
59 'title'
60 );
6a488035
TO
61 }
62
63 $transaction = new CRM_Core_Transaction();
64
65 $ce = new CRM_Mailing_Event_BAO_Confirm();
66 $ce->event_subscribe_id = $se->id;
67 $ce->time_stamp = date('YmdHis');
68 $ce->save();
69
c49a2977 70 CRM_Contact_BAO_GroupContact::addContactsToGroup(
be2fb01f 71 [$contact_id],
6a488035
TO
72 $se->group_id,
73 'Email',
c49a2977 74 'Added',
6a488035
TO
75 $ce->id
76 );
77
78 $transaction->commit();
79
80 $config = CRM_Core_Config::singleton();
81
82 $domain = CRM_Core_BAO_Domain::getDomain();
a5c03029 83 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
6a488035
TO
84
85 list($display_name, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($se->contact_id);
86
87 $group = new CRM_Contact_DAO_Group();
88 $group->id = $se->group_id;
89 $group->find(TRUE);
90
4825de4a 91 $component = new CRM_Mailing_BAO_MailingComponent();
6a488035
TO
92 $component->is_default = 1;
93 $component->is_active = 1;
94 $component->component_type = 'Welcome';
95
3959be91
KJ
96 // we should return early if welcome email temaplate is disabled
97 // this means confirmation email will not be sent
98 if (!$component->find(TRUE)) {
99 return $group->title;
100 }
6a488035 101
6a488035
TO
102 $html = $component->body_html;
103
104 if ($component->body_text) {
105 $text = $component->body_text;
106 }
107 else {
108 $text = CRM_Utils_String::htmlToText($component->body_html);
109 }
110
353ffa53 111 $bao = new CRM_Mailing_BAO_Mailing();
6a488035
TO
112 $bao->body_text = $text;
113 $bao->body_html = $html;
353ffa53 114 $tokens = $bao->getTokens();
6a488035
TO
115
116 $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
117 $html = CRM_Utils_Token::replaceWelcomeTokens($html, $group->title, TRUE);
118
119 $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
120 $text = CRM_Utils_Token::replaceWelcomeTokens($text, $group->title, FALSE);
121
be2fb01f 122 $mailParams = [
35f7561f 123 'groupName' => 'Mailing Event ' . $component->component_type,
353ffa53 124 'subject' => $component->subject,
a5c03029 125 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
6a488035
TO
126 'toEmail' => $email,
127 'toName' => $display_name,
576fcb9c 128 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
129 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
6a488035
TO
130 'html' => $html,
131 'text' => $text,
be2fb01f 132 ];
6a488035
TO
133 // send - ignore errors because the desired status change has already been successful
134 $unused_result = CRM_Utils_Mail::send($mailParams);
135
136 return $group->title;
137 }
96025800 138
6a488035 139}