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