Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once 'Mail/mime.php'; | |
4c6ce474 EM |
35 | |
36 | /** | |
37 | * Class CRM_Mailing_Event_BAO_Confirm | |
38 | */ | |
6a488035 TO |
39 | class CRM_Mailing_Event_BAO_Confirm extends CRM_Mailing_Event_DAO_Confirm { |
40 | ||
41 | /** | |
fe482240 | 42 | * Class constructor. |
6a488035 | 43 | */ |
00be9182 | 44 | public function __construct() { |
6a488035 TO |
45 | parent::__construct(); |
46 | } | |
47 | ||
48 | /** | |
fe482240 | 49 | * Confirm a pending subscription. |
6a488035 | 50 | * |
90c8230e TO |
51 | * @param int $contact_id |
52 | * The id of the contact. | |
53 | * @param int $subscribe_id | |
54 | * The id of the subscription event. | |
55 | * @param string $hash | |
56 | * The hash. | |
6a488035 | 57 | * |
795492f3 | 58 | * @return bool |
a6c01b45 | 59 | * True on success |
6a488035 TO |
60 | */ |
61 | public static function confirm($contact_id, $subscribe_id, $hash) { | |
62 | $se = &CRM_Mailing_Event_BAO_Subscribe::verify( | |
63 | $contact_id, | |
64 | $subscribe_id, | |
65 | $hash | |
66 | ); | |
67 | ||
68 | if (!$se) { | |
69 | return FALSE; | |
70 | } | |
71 | ||
72 | // before we proceed lets just check if this contact is already 'Added' | |
73 | // if so, we should ignore this request and hence avoid sending multiple | |
74 | // emails - CRM-11157 | |
75 | $details = CRM_Contact_BAO_GroupContact::getMembershipDetail($contact_id, $se->group_id); | |
76 | if ($details && $details->status == 'Added') { | |
77 | // This contact is already subscribed | |
f8d4979e DL |
78 | // lets return the group title |
79 | return CRM_Core_DAO::getFieldValue( | |
80 | 'CRM_Contact_DAO_Group', | |
81 | $se->group_id, | |
82 | 'title' | |
83 | ); | |
6a488035 TO |
84 | } |
85 | ||
86 | $transaction = new CRM_Core_Transaction(); | |
87 | ||
88 | $ce = new CRM_Mailing_Event_BAO_Confirm(); | |
89 | $ce->event_subscribe_id = $se->id; | |
90 | $ce->time_stamp = date('YmdHis'); | |
91 | $ce->save(); | |
92 | ||
c49a2977 CW |
93 | CRM_Contact_BAO_GroupContact::addContactsToGroup( |
94 | array($contact_id), | |
6a488035 TO |
95 | $se->group_id, |
96 | 'Email', | |
c49a2977 | 97 | 'Added', |
6a488035 TO |
98 | $ce->id |
99 | ); | |
100 | ||
101 | $transaction->commit(); | |
102 | ||
103 | $config = CRM_Core_Config::singleton(); | |
104 | ||
105 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
106 | list($domainEmailName, $_) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
107 | ||
108 | list($display_name, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($se->contact_id); | |
109 | ||
110 | $group = new CRM_Contact_DAO_Group(); | |
111 | $group->id = $se->group_id; | |
112 | $group->find(TRUE); | |
113 | ||
114 | $component = new CRM_Mailing_BAO_Component(); | |
115 | $component->is_default = 1; | |
116 | $component->is_active = 1; | |
117 | $component->component_type = 'Welcome'; | |
118 | ||
119 | $component->find(TRUE); | |
120 | ||
6a488035 TO |
121 | $html = $component->body_html; |
122 | ||
123 | if ($component->body_text) { | |
124 | $text = $component->body_text; | |
125 | } | |
126 | else { | |
127 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
128 | } | |
129 | ||
353ffa53 | 130 | $bao = new CRM_Mailing_BAO_Mailing(); |
6a488035 TO |
131 | $bao->body_text = $text; |
132 | $bao->body_html = $html; | |
353ffa53 | 133 | $tokens = $bao->getTokens(); |
6a488035 TO |
134 | |
135 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
136 | $html = CRM_Utils_Token::replaceWelcomeTokens($html, $group->title, TRUE); | |
137 | ||
138 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']); | |
139 | $text = CRM_Utils_Token::replaceWelcomeTokens($text, $group->title, FALSE); | |
140 | ||
141 | $mailParams = array( | |
35f7561f | 142 | 'groupName' => 'Mailing Event ' . $component->component_type, |
353ffa53 | 143 | 'subject' => $component->subject, |
576fcb9c | 144 | 'from' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain::getNoReplyEmailAddress() . '>', |
6a488035 TO |
145 | 'toEmail' => $email, |
146 | 'toName' => $display_name, | |
576fcb9c | 147 | 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), |
148 | 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), | |
6a488035 TO |
149 | 'html' => $html, |
150 | 'text' => $text, | |
151 | ); | |
152 | // send - ignore errors because the desired status change has already been successful | |
153 | $unused_result = CRM_Utils_Mail::send($mailParams); | |
154 | ||
155 | return $group->title; | |
156 | } | |
96025800 | 157 | |
6a488035 | 158 | } |