3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 require_once 'Mail/mime.php';
38 class CRM_Mailing_Event_BAO_Subscribe
extends CRM_Mailing_Event_DAO_Subscribe
{
43 function __construct() {
44 parent
::__construct();
48 * Register a subscription event. Create a new contact if one does not
51 * @param int $group_id The group id to subscribe to
52 * @param string $email The email address of the (new) contact
53 * @params int $contactId Currently used during event registration/contribution.
54 * Specifically to avoid linking group to wrong duplicate contact
55 * during event registration.
57 * @return int|null $se_id The id of the subscription event, null on failure
61 public static function &subscribe($group_id, $email, $contactId = NULL, $context = NULL) {
62 // CRM-1797 - allow subscription only to public groups
63 $params = array('id' => (int) $group_id);
68 $bao = CRM_Contact_BAO_Group
::retrieve($params, $defaults);
69 if ($bao && substr($bao->visibility
, 0, 6) != 'Public' && $context != 'profile') {
73 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
74 $email = $strtolower($email);
76 // process the query only if no contactId
78 $contact_id = $contactId;
81 /* First, find out if the contact already exists */
84 SELECT DISTINCT contact_a.id as contact_id
85 FROM civicrm_contact contact_a
86 LEFT JOIN civicrm_email ON contact_a.id = civicrm_email.contact_id
87 WHERE civicrm_email.email = %1 AND contact_a.is_deleted = 0";
89 $params = array(1 => array($email, 'String'));
90 $dao = CRM_Core_DAO
::executeQuery($query, $params);
92 // lets just use the first contact id we got
94 $contact_id = $dao->contact_id
;
99 $transaction = new CRM_Core_Transaction();
102 require_once 'CRM/Utils/DeprecatedUtils.php';
104 /* If the contact does not exist, create one. */
107 'contact_type' => 'Individual',
110 $locationType = CRM_Core_BAO_LocationType
::getDefault();
113 'location_type_id' => $locationType->id
,
115 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
117 $formatted['onDuplicate'] = CRM_Import_Parser
::DUPLICATE_SKIP
;
118 $formatted['fixAddress'] = TRUE;
119 require_once 'api/api.php';
120 $contact = civicrm_api('contact', 'create', $formatted);
121 if (civicrm_error($contact)) {
124 $contact_id = $contact['id'];
126 elseif (!is_numeric($contact_id) &&
127 (int ) $contact_id > 0
129 // make sure contact_id is numeric
134 /* Get the primary email id from the contact to use as a hash input */
136 $dao = new CRM_Core_DAO();
139 SELECT civicrm_email.id as email_id
141 WHERE civicrm_email.email = %1
142 AND civicrm_email.contact_id = %2";
143 $params = array(1 => array($email, 'String'),
144 2 => array($contact_id, 'Integer'),
146 $dao = CRM_Core_DAO
::executeQuery($query, $params);
148 if (!$dao->fetch()) {
149 CRM_Core_Error
::fatal('Please file an issue with the backtrace');
153 $se = new CRM_Mailing_Event_BAO_Subscribe();
154 $se->group_id
= $group_id;
155 $se->contact_id
= $contact_id;
156 $se->time_stamp
= date('YmdHis');
157 $se->hash
= substr(sha1("{$group_id}:{$contact_id}:{$dao->email_id}:" . time()),
162 $contacts = array($contact_id);
163 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id,
164 'Email', 'Pending', $se->id
167 $transaction->commit();
172 * Verify the hash of a subscription event
174 * @param int $contact_id ID of the contact
175 * @param int $subscribe_id ID of the subscription event
176 * @param string $hash Hash to verify
178 * @return object|null The subscribe event object, or null on failure
182 public static function &verify($contact_id, $subscribe_id, $hash) {
184 $se = new CRM_Mailing_Event_BAO_Subscribe();
185 $se->contact_id
= $contact_id;
186 $se->id
= $subscribe_id;
188 if ($se->find(TRUE)) {
195 * Ask a contact for subscription confirmation (opt-in)
197 * @param string $email The email address
202 public function send_confirm_request($email) {
203 $config = CRM_Core_Config
::singleton();
205 $domain = CRM_Core_BAO_Domain
::getDomain();
207 //get the default domain email address.
208 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
210 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
211 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
213 $confirm = implode($config->verpSeparator
,
222 $group = new CRM_Contact_BAO_Group();
223 $group->id
= $this->group_id
;
226 $component = new CRM_Mailing_BAO_Component();
227 $component->is_default
= 1;
228 $component->is_active
= 1;
229 $component->component_type
= 'Subscribe';
231 $component->find(TRUE);
234 'Subject' => $component->subject
,
235 'From' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
237 'Reply-To' => $confirm,
238 'Return-Path' => "do-not-reply@$emailDomain",
241 $url = CRM_Utils_System
::url('civicrm/mailing/confirm',
242 "reset=1&cid={$this->contact_id}&sid={$this->id}&h={$this->hash}",
246 $html = $component->body_html
;
248 if ($component->body_text
) {
249 $text = $component->body_text
;
252 $text = CRM_Utils_String
::htmlToText($component->body_html
);
255 $bao = new CRM_Mailing_BAO_Mailing();
256 $bao->body_text
= $text;
257 $bao->body_html
= $html;
258 $tokens = $bao->getTokens();
260 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
261 $html = CRM_Utils_Token
::replaceSubscribeTokens($html,
266 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
267 $text = CRM_Utils_Token
::replaceSubscribeTokens($text,
271 // render the & entities in text mode, so that the links work
272 $text = str_replace('&', '&', $text);
274 $message = new Mail_mime("\n");
276 $message->setHTMLBody($html);
277 $message->setTxtBody($text);
278 $b = CRM_Utils_Mail
::setMimeParams($message);
279 $h = $message->headers($headers);
280 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 's',
285 $mailer = $config->getMailer();
287 if (is_object($mailer)) {
288 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
289 $mailer->send($email, $h, $b);
295 * Get the domain object given a subscribe event
297 * @param int $subscribe_id ID of the subscribe event
299 * @return object $domain The domain owning the event
303 public static function &getDomain($subscribe_id) {
304 return CRM_Core_BAO_Domain
::getDomain();
308 * Get the group details to which given email belongs
310 * @param string $email email of the contact
311 * @param int $contactID contactID if we want an exact match
313 * @return array $groups array of group ids
316 public static function getContactGroups($email, $contactID = NULL) {
319 SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title
320 FROM civicrm_group_contact group_a
321 LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id
322 LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id )
323 WHERE civicrm_contact.id = %1";
325 $params = array(1 => array($contactID, 'Integer'));
328 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
329 $email = $strtolower($email);
332 SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title
333 FROM civicrm_group_contact group_a
334 LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id
335 LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id ) AND civicrm_contact.is_deleted = 0
336 LEFT JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id
337 WHERE civicrm_email.email = %1";
339 $params = array(1 => array($email, 'String'));
342 $dao = CRM_Core_DAO
::executeQuery($query, $params);
344 while ($dao->fetch()) {
345 $groups[$dao->group_id
] = array(
346 'id' => $dao->group_id
,
347 'title' => $dao->title
,
348 'status' => $dao->status
,
357 * Function to send subscribe mail
359 * @params array $groups the list of group ids for subscribe
360 * @params array $params the list of email
361 * @params int $contactId Currently used during event registration/contribution.
362 * Specifically to avoid linking group to wrong duplicate contact
363 * during event registration.
370 public static function commonSubscribe(&$groups, &$params, $contactId = NULL, $context = NULL) {
371 $contactGroups = CRM_Mailing_Event_BAO_Subscribe
::getContactGroups($params['email'], $contactId);
374 foreach ($groups as $groupID) {
375 $title = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $groupID, 'title');
376 if (array_key_exists($groupID, $contactGroups) && $contactGroups[$groupID]['status'] != 'Removed') {
377 $group[$groupID]['title'] = $contactGroups[$groupID]['title'];
379 $group[$groupID]['status'] = $contactGroups[$groupID]['status'];
380 $status = ts('You are already subscribed in %1, your subscription is %2.', array(1 => $group[$groupID]['title'], 2 => $group[$groupID]['status']));
381 CRM_Utils_System
::setUFMessage($status);
385 $se = self
::subscribe($groupID,
386 $params['email'], $contactId, $context
390 $groupAdded[] = $title;
392 /* Ask the contact for confirmation */
394 $se->send_confirm_request($params['email']);
398 $groupFailed[] = $title;
402 $groupTitle = implode(', ', $groupAdded);
403 CRM_Utils_System
::setUFMessage(ts('Your subscription request has been submitted for %1. Check your inbox shortly for the confirmation email(s). If you do not see a confirmation email, please check your spam/junk mail folder.', array(1 => $groupTitle)));
405 elseif ($success === FALSE) {
406 $groupTitle = implode(',', $groupFailed);
407 CRM_Utils_System
::setUFMessage(ts('We had a problem processing your subscription request for %1. You have tried to subscribe to a private group and/or we encountered a database error. Please contact the site administrator.', array(1 => $groupTitle)));