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';
40 * Class CRM_Mailing_Event_BAO_Subscribe
42 class CRM_Mailing_Event_BAO_Subscribe
extends CRM_Mailing_Event_DAO_Subscribe
{
47 function __construct() {
48 parent
::__construct();
52 * Register a subscription event. Create a new contact if one does not
55 * @param int $group_id The group id to subscribe to
56 * @param string $email The email address of the (new) contact
57 * @param null $contactId
58 * @param null $context
59 * @params int $contactId Currently used during event registration/contribution.
60 * Specifically to avoid linking group to wrong duplicate contact
61 * during event registration.
63 * @return int|null $se_id The id of the subscription event, null on failure
67 public static function &subscribe($group_id, $email, $contactId = NULL, $context = NULL) {
68 // CRM-1797 - allow subscription only to public groups
69 $params = array('id' => (int) $group_id);
74 $bao = CRM_Contact_BAO_Group
::retrieve($params, $defaults);
75 if ($bao && substr($bao->visibility
, 0, 6) != 'Public' && $context != 'profile') {
79 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
80 $email = $strtolower($email);
82 // process the query only if no contactId
84 $contact_id = $contactId;
87 /* First, find out if the contact already exists */
90 SELECT DISTINCT contact_a.id as contact_id
91 FROM civicrm_contact contact_a
92 LEFT JOIN civicrm_email ON contact_a.id = civicrm_email.contact_id
93 WHERE civicrm_email.email = %1 AND contact_a.is_deleted = 0";
95 $params = array(1 => array($email, 'String'));
96 $dao = CRM_Core_DAO
::executeQuery($query, $params);
98 // lets just use the first contact id we got
100 $contact_id = $dao->contact_id
;
105 $transaction = new CRM_Core_Transaction();
108 require_once 'CRM/Utils/DeprecatedUtils.php';
110 /* If the contact does not exist, create one. */
113 'contact_type' => 'Individual',
116 $locationType = CRM_Core_BAO_LocationType
::getDefault();
119 'location_type_id' => $locationType->id
,
121 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
123 $formatted['onDuplicate'] = CRM_Import_Parser
::DUPLICATE_SKIP
;
124 $formatted['fixAddress'] = TRUE;
125 require_once 'api/api.php';
126 $contact = civicrm_api('contact', 'create', $formatted);
127 if (civicrm_error($contact)) {
130 $contact_id = $contact['id'];
132 elseif (!is_numeric($contact_id) &&
133 (int ) $contact_id > 0
135 // make sure contact_id is numeric
140 /* Get the primary email id from the contact to use as a hash input */
142 $dao = new CRM_Core_DAO();
145 SELECT civicrm_email.id as email_id
147 WHERE civicrm_email.email = %1
148 AND civicrm_email.contact_id = %2";
149 $params = array(1 => array($email, 'String'),
150 2 => array($contact_id, 'Integer'),
152 $dao = CRM_Core_DAO
::executeQuery($query, $params);
154 if (!$dao->fetch()) {
155 CRM_Core_Error
::fatal('Please file an issue with the backtrace');
159 $se = new CRM_Mailing_Event_BAO_Subscribe();
160 $se->group_id
= $group_id;
161 $se->contact_id
= $contact_id;
162 $se->time_stamp
= date('YmdHis');
163 $se->hash
= substr(sha1("{$group_id}:{$contact_id}:{$dao->email_id}:" . time()),
168 $contacts = array($contact_id);
169 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id,
170 'Email', 'Pending', $se->id
173 $transaction->commit();
178 * Verify the hash of a subscription event
180 * @param int $contact_id ID of the contact
181 * @param int $subscribe_id ID of the subscription event
182 * @param string $hash Hash to verify
184 * @return object|null The subscribe event object, or null on failure
188 public static function &verify($contact_id, $subscribe_id, $hash) {
190 $se = new CRM_Mailing_Event_BAO_Subscribe();
191 $se->contact_id
= $contact_id;
192 $se->id
= $subscribe_id;
194 if ($se->find(TRUE)) {
201 * Ask a contact for subscription confirmation (opt-in)
203 * @param string $email The email address
208 public function send_confirm_request($email) {
209 $config = CRM_Core_Config
::singleton();
211 $domain = CRM_Core_BAO_Domain
::getDomain();
213 //get the default domain email address.
214 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
216 $localpart = CRM_Core_BAO_MailSettings
::defaultLocalpart();
217 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
219 $confirm = implode($config->verpSeparator
,
228 $group = new CRM_Contact_BAO_Group();
229 $group->id
= $this->group_id
;
232 $component = new CRM_Mailing_BAO_Component();
233 $component->is_default
= 1;
234 $component->is_active
= 1;
235 $component->component_type
= 'Subscribe';
237 $component->find(TRUE);
240 'Subject' => $component->subject
,
241 'From' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
243 'Reply-To' => $confirm,
244 'Return-Path' => "do-not-reply@$emailDomain",
247 $url = CRM_Utils_System
::url('civicrm/mailing/confirm',
248 "reset=1&cid={$this->contact_id}&sid={$this->id}&h={$this->hash}",
252 $html = $component->body_html
;
254 if ($component->body_text
) {
255 $text = $component->body_text
;
258 $text = CRM_Utils_String
::htmlToText($component->body_html
);
261 $bao = new CRM_Mailing_BAO_Mailing();
262 $bao->body_text
= $text;
263 $bao->body_html
= $html;
264 $tokens = $bao->getTokens();
266 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
267 $html = CRM_Utils_Token
::replaceSubscribeTokens($html,
272 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
273 $text = CRM_Utils_Token
::replaceSubscribeTokens($text,
277 // render the & entities in text mode, so that the links work
278 $text = str_replace('&', '&', $text);
280 $message = new Mail_mime("\n");
282 $message->setHTMLBody($html);
283 $message->setTxtBody($text);
284 $b = CRM_Utils_Mail
::setMimeParams($message);
285 $h = $message->headers($headers);
286 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($h, 's',
291 $mailer = $config->getMailer();
293 if (is_object($mailer)) {
294 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
295 $mailer->send($email, $h, $b);
301 * Get the domain object given a subscribe event
303 * @param int $subscribe_id ID of the subscribe event
305 * @return object $domain The domain owning the event
309 public static function &getDomain($subscribe_id) {
310 return CRM_Core_BAO_Domain
::getDomain();
314 * Get the group details to which given email belongs
316 * @param string $email email of the contact
317 * @param int $contactID contactID if we want an exact match
319 * @return array $groups array of group ids
322 public static function getContactGroups($email, $contactID = NULL) {
325 SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title
326 FROM civicrm_group_contact group_a
327 LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id
328 LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id )
329 WHERE civicrm_contact.id = %1";
331 $params = array(1 => array($contactID, 'Integer'));
334 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
335 $email = $strtolower($email);
338 SELECT DISTINCT group_a.group_id, group_a.status, civicrm_group.title
339 FROM civicrm_group_contact group_a
340 LEFT JOIN civicrm_group ON civicrm_group.id = group_a.group_id
341 LEFT JOIN civicrm_contact ON ( group_a.contact_id = civicrm_contact.id ) AND civicrm_contact.is_deleted = 0
342 LEFT JOIN civicrm_email ON civicrm_contact.id = civicrm_email.contact_id
343 WHERE civicrm_email.email = %1";
345 $params = array(1 => array($email, 'String'));
348 $dao = CRM_Core_DAO
::executeQuery($query, $params);
350 while ($dao->fetch()) {
351 $groups[$dao->group_id
] = array(
352 'id' => $dao->group_id
,
353 'title' => $dao->title
,
354 'status' => $dao->status
,
363 * Function to send subscribe mail
365 * @params array $groups the list of group ids for subscribe
366 * @params array $params the list of email
367 * @params int $contactId Currently used during event registration/contribution.
368 * Specifically to avoid linking group to wrong duplicate contact
369 * during event registration.
375 * @param null $contactId
376 * @param null $context
382 public static function commonSubscribe(&$groups, &$params, $contactId = NULL, $context = NULL) {
383 $contactGroups = CRM_Mailing_Event_BAO_Subscribe
::getContactGroups($params['email'], $contactId);
386 foreach ($groups as $groupID) {
387 $title = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $groupID, 'title');
388 if (array_key_exists($groupID, $contactGroups) && $contactGroups[$groupID]['status'] != 'Removed') {
389 $group[$groupID]['title'] = $contactGroups[$groupID]['title'];
391 $group[$groupID]['status'] = $contactGroups[$groupID]['status'];
392 $status = ts('You are already subscribed in %1, your subscription is %2.', array(1 => $group[$groupID]['title'], 2 => $group[$groupID]['status']));
393 CRM_Utils_System
::setUFMessage($status);
397 $se = self
::subscribe($groupID,
398 $params['email'], $contactId, $context
402 $groupAdded[] = $title;
404 /* Ask the contact for confirmation */
406 $se->send_confirm_request($params['email']);
410 $groupFailed[] = $title;
414 $groupTitle = implode(', ', $groupAdded);
415 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)));
417 elseif ($success === FALSE) {
418 $groupTitle = implode(',', $groupFailed);
419 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)));