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