Merge pull request #22850 from ixiam/dev_Issue#3080
[civicrm-core.git] / CRM / Mailing / Event / BAO / Confirm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 require_once 'Mail/mime.php';
19
20 /**
21 * Class CRM_Mailing_Event_BAO_Confirm
22 */
23 class CRM_Mailing_Event_BAO_Confirm extends CRM_Mailing_Event_DAO_Confirm {
24
25 /**
26 * Confirm a pending subscription.
27 *
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.
34 *
35 * @return bool
36 * True on success
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
55 // lets return the group title
56 return CRM_Core_DAO::getFieldValue(
57 'CRM_Contact_DAO_Group',
58 $se->group_id,
59 'title'
60 );
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
70 CRM_Contact_BAO_GroupContact::addContactsToGroup(
71 [$contact_id],
72 $se->group_id,
73 'Email',
74 'Added',
75 $ce->id
76 );
77
78 $transaction->commit();
79
80 $config = CRM_Core_Config::singleton();
81
82 $domain = CRM_Core_BAO_Domain::getDomain();
83 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
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
91 $component = new CRM_Mailing_BAO_MailingComponent();
92 $component->is_default = 1;
93 $component->is_active = 1;
94 $component->component_type = 'Welcome';
95
96 $component->find(TRUE);
97
98 $html = $component->body_html;
99
100 if ($component->body_text) {
101 $text = $component->body_text;
102 }
103 else {
104 $text = CRM_Utils_String::htmlToText($component->body_html);
105 }
106
107 $bao = new CRM_Mailing_BAO_Mailing();
108 $bao->body_text = $text;
109 $bao->body_html = $html;
110 $tokens = $bao->getTokens();
111
112 $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
113 $html = CRM_Utils_Token::replaceWelcomeTokens($html, $group->title, TRUE);
114
115 $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
116 $text = CRM_Utils_Token::replaceWelcomeTokens($text, $group->title, FALSE);
117
118 $mailParams = [
119 'groupName' => 'Mailing Event ' . $component->component_type,
120 'subject' => $component->subject,
121 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
122 'toEmail' => $email,
123 'toName' => $display_name,
124 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
125 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
126 'html' => $html,
127 'text' => $text,
128 ];
129 // send - ignore errors because the desired status change has already been successful
130 $unused_result = CRM_Utils_Mail::send($mailParams);
131
132 return $group->title;
133 }
134
135 }