Merge pull request #2744 from pratik-joshi/CRM-13992_temp_fix
[civicrm-core.git] / CRM / Campaign / BAO / Petition.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 */
43427a24 35class CRM_Campaign_BAO_Petition extends CRM_Campaign_BAO_Survey {
6a488035
TO
36 function __construct() {
37 parent::__construct();
38 // expire cookie in one day
39 $this->cookieExpire = (1 * 60 * 60 * 24);
40 }
41
42 /**
43 * Function to get Petition Details for dashboard.
44 *
45 * @static
46 */
abde1673 47 static function getPetitionSummary($params = array(), $onlyCount = FALSE) {
6a488035
TO
48 //build the limit and order clause.
49 $limitClause = $orderByClause = $lookupTableJoins = NULL;
50 if (!$onlyCount) {
51 $sortParams = array(
52 'sort' => 'created_date',
53 'offset' => 0,
54 'rowCount' => 10,
55 'sortOrder' => 'desc',
56 );
57 foreach ($sortParams as $name => $default) {
a7488080 58 if (!empty($params[$name])) {
6a488035
TO
59 $sortParams[$name] = $params[$name];
60 }
61 }
62
63 //need to lookup tables.
64 $orderOnPetitionTable = TRUE;
65 if ($sortParams['sort'] == 'campaign') {
66 $orderOnPetitionTable = FALSE;
67 $lookupTableJoins = '
68 LEFT JOIN civicrm_campaign campaign ON ( campaign.id = petition.campaign_id )';
69 $orderByClause = "ORDER BY campaign.title {$sortParams['sortOrder']}";
70 }
71 elseif ($sortParams['sort'] == 'activity_type') {
72 $orderOnPetitionTable = FALSE;
73 $lookupTableJoins = "
8ef12e64 74 LEFT JOIN civicrm_option_value activity_type ON ( activity_type.value = petition.activity_type_id
6a488035
TO
75 OR petition.activity_type_id IS NULL )
76INNER JOIN civicrm_option_group grp ON ( activity_type.option_group_id = grp.id AND grp.name = 'activity_type' )";
77 $orderByClause = "ORDER BY activity_type.label {$sortParams['sortOrder']}";
78 }
79 elseif ($sortParams['sort'] == 'isActive') {
80 $sortParams['sort'] = 'is_active';
81 }
82 if ($orderOnPetitionTable) {
83 $orderByClause = "ORDER BY petition.{$sortParams['sort']} {$sortParams['sortOrder']}";
84 }
85 $limitClause = "LIMIT {$sortParams['offset']}, {$sortParams['rowCount']}";
86 }
87
88 //build the where clause.
89 $queryParams = $where = array();
90
91 //we only have activity type as a
92 //difference between survey and petition.
93 $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name');
94 if ($petitionTypeID) {
95 $where[] = "( petition.activity_type_id = %1 )";
96 $queryParams[1] = array($petitionTypeID, 'Positive');
97 }
a7488080 98 if (!empty($params['title'])) {
6a488035
TO
99 $where[] = "( petition.title LIKE %2 )";
100 $queryParams[2] = array('%' . trim($params['title']) . '%', 'String');
101 }
a7488080 102 if (!empty($params['campaign_id'])) {
6a488035
TO
103 $where[] = '( petition.campaign_id = %3 )';
104 $queryParams[3] = array($params['campaign_id'], 'Positive');
105 }
106 $whereClause = NULL;
107 if (!empty($where)) {
108 $whereClause = ' WHERE ' . implode(" \nAND ", $where);
109 }
110
111 $selectClause = '
112SELECT petition.id as id,
113 petition.title as title,
114 petition.is_active as is_active,
115 petition.result_id as result_id,
116 petition.is_default as is_default,
117 petition.campaign_id as campaign_id,
118 petition.activity_type_id as activity_type_id';
119
120 if ($onlyCount) {
121 $selectClause = 'SELECT COUNT(*)';
122 }
123 $fromClause = 'FROM civicrm_survey petition';
124
125 $query = "{$selectClause} {$fromClause} {$whereClause} {$orderByClause} {$limitClause}";
126
127 if ($onlyCount) {
abde1673 128 return (int) CRM_Core_DAO::singleValueQuery($query, $queryParams);
6a488035
TO
129 }
130
131 $petitions = array();
132 $properties = array(
133 'id',
134 'title',
135 'campaign_id',
136 'is_active',
137 'is_default',
138 'result_id',
139 'activity_type_id',
140 );
141
142 $petition = CRM_Core_DAO::executeQuery($query, $queryParams);
143 while ($petition->fetch()) {
144 foreach ($properties as $property) {
145 $petitions[$petition->id][$property] = $petition->$property;
146 }
147 }
148
149 return $petitions;
150 }
151
152 /**
153 * Get the petition count.
154 *
155 * @static
156 */
157 static function getPetitionCount() {
abde1673 158 $whereClause = 'WHERE ( 1 )';
159 $queryParams = array();
6a488035
TO
160 $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name');
161 if ($petitionTypeID) {
162 $whereClause = "WHERE ( petition.activity_type_id = %1 )";
163 $queryParams[1] = array($petitionTypeID, 'Positive');
164 }
165 $query = "SELECT COUNT(*) FROM civicrm_survey petition {$whereClause}";
166
abde1673 167 return (int) CRM_Core_DAO::singleValueQuery($query, $queryParams);
6a488035
TO
168 }
169
170 /**
171 * takes an associative array and creates a petition signature activity
172 *
abde1673 173 * @param array $params (reference ) an assoc array of name/value pairs
6a488035
TO
174 *
175 * @return object CRM_Campaign_BAO_Petition
176 * @access public
177 * @static
178 */
179 function createSignature(&$params) {
180 if (empty($params)) {
181 return;
182 }
183
184 if (!isset($params['sid'])) {
185 $statusMsg = ts('No survey sid parameter. Cannot process signature.');
186 CRM_Core_Session::setStatus($statusMsg, ts('Sorry'), 'error');
187 return;
188 }
189
190 if (isset($params['contactId'])) {
191
192 // add signature as activity with survey id as source id
193 // get the activity type id associated with this survey
194 $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo($params['sid']);
195
196 // create activity
6a488035
TO
197 // 1-Schedule, 2-Completed
198
199 $activityParams = array(
200 'source_contact_id' => $params['contactId'],
201 'target_contact_id' => $params['contactId'],
202 'source_record_id' => $params['sid'],
203 'subject' => $surveyInfo['title'],
204 'activity_type_id' => $surveyInfo['activity_type_id'],
205 'activity_date_time' => date("YmdHis"),
206 'status_id' => $params['statusId'],
03158688 207 'activity_campaign_id' => $params['activity_campaign_id'],
6a488035
TO
208 );
209
210 //activity creation
211 // *** check for activity using source id - if already signed
212 $activity = CRM_Activity_BAO_Activity::create($activityParams);
213
214 // save activity custom data
a7488080 215 if (!empty($params['custom']) &&
6a488035
TO
216 is_array($params['custom'])
217 ) {
218 CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $activity->id);
219 }
220
221 // set permanent cookie to indicate this petition already signed on the computer
222 setcookie('signed_' . $params['sid'], $activity->id, time() + $this->cookieExpire, '/');
223 }
224
225 return $activity;
226 }
227
228 function confirmSignature($activity_id, $contact_id, $petition_id) {
229 // change activity status to completed (status_id = 2)
230 // I wonder why do we need contact_id when we have activity_id anyway? [chastell]
3f815c3a 231 $sql = 'UPDATE civicrm_activity SET status_id = 2 WHERE id = %1';
afe0f8cf 232 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3f815c3a 233 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
234 $params = array(
8ef12e64 235 1 => array($activity_id, 'Integer'),
236 2 => array($contact_id, 'Integer'),
3f815c3a 237 3 => array($sourceID, 'Integer')
238 );
6a488035
TO
239 CRM_Core_DAO::executeQuery($sql, $params);
240
3f815c3a 241 $sql = 'UPDATE civicrm_activity_contact SET contact_id = %2 WHERE activity_id = %1 AND record_type_id = %3';
242 CRM_Core_DAO::executeQuery($sql, $params);
6a488035
TO
243 // remove 'Unconfirmed' tag for this contact
244 $tag_name = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
245 'tag_unconfirmed',
246 NULL,
247 'Unconfirmed'
248 );
249
250 $sql = "
8ef12e64 251DELETE FROM civicrm_entity_tag
252WHERE entity_table = 'civicrm_contact'
253AND entity_id = %1
6a488035 254AND tag_id = ( SELECT id FROM civicrm_tag WHERE name = %2 )";
abde1673 255 $params = array(
256 1 => array($contact_id, 'Integer'),
6a488035
TO
257 2 => array($tag_name, 'String'),
258 );
259 CRM_Core_DAO::executeQuery($sql, $params);
260
261 // set permanent cookie to indicate this users email address now confirmed
262 setcookie("confirmed_{$petition_id}",
263 $activity_id,
264 time() + $this->cookieExpire,
265 '/'
266 );
267
268 return TRUE;
269 }
270
271 /**
272 * Function to get Petition Signature Total
273 *
274 * @param boolean $all
275 * @param int $id
276 * @static
277 */
278 static function getPetitionSignatureTotalbyCountry($surveyId) {
279 $countries = array();
280 $sql = "
281 SELECT count(civicrm_address.country_id) as total,
282 IFNULL(country_id,'') as country_id,IFNULL(iso_code,'') as country_iso, IFNULL(civicrm_country.name,'') as country
9b8ec3a8 283 FROM ( civicrm_activity a, civicrm_survey, civicrm_contact )
6a488035
TO
284 LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1
285 LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
9b8ec3a8 286 LEFT JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id AND ac.record_type_id = %2 )
6a488035 287 WHERE
9b8ec3a8 288 ac.contact_id = civicrm_contact.id AND
6a488035
TO
289 a.activity_type_id = civicrm_survey.activity_type_id AND
290 civicrm_survey.id = %1 AND
291 a.source_record_id = %1 ";
292
afe0f8cf 293 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
9b8ec3a8 294 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
295 $params = array(
296 1 => array($surveyId, 'Integer'),
abde1673 297 2 => array($sourceID, 'Integer')
298 );
6a488035
TO
299 $sql .= " GROUP BY civicrm_address.country_id";
300 $fields = array('total', 'country_id', 'country_iso', 'country');
301
302 $dao = CRM_Core_DAO::executeQuery($sql, $params);
303 while ($dao->fetch()) {
304 $row = array();
305 foreach ($fields as $field) {
306 $row[$field] = $dao->$field;
307 }
308 $countries[] = $row;
309 }
310 return $countries;
311 }
312
313 /**
314 * Function to get Petition Signature Total
315 *
316 * @param boolean $all
317 * @param int $id
318 * @static
319 */
320 static function getPetitionSignatureTotal($surveyId) {
321 $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo((int) $surveyId);
322 //$activityTypeID = $surveyInfo['activity_type_id'];
6a488035
TO
323 $sql = "
324 SELECT
325 status_id,count(id) as total
326 FROM civicrm_activity
327 WHERE
328 source_record_id = " . (int) $surveyId . " AND activity_type_id = " . (int) $surveyInfo['activity_type_id'] . " GROUP BY status_id";
329
330 $statusTotal = array();
abde1673 331 $total = 0;
332 $dao = CRM_Core_DAO::executeQuery($sql);
6a488035
TO
333 while ($dao->fetch()) {
334 $total += $dao->total;
335 $statusTotal['status'][$dao->status_id] = $dao->total;
336 }
337 $statusTotal['count'] = $total;
338 return $statusTotal;
339 }
340
341
43427a24 342 public static function getSurveyInfo($surveyId = NULL) {
6a488035
TO
343 $surveyInfo = array();
344
345 $sql = "
346 SELECT activity_type_id,
347 campaign_id,
348 s.title,
349 ov.label AS activity_type
350 FROM civicrm_survey s, civicrm_option_value ov, civicrm_option_group og
abde1673 351 WHERE s.id = " . (int) $surveyId . "
6a488035
TO
352 AND s.activity_type_id = ov.value
353 AND ov.option_group_id = og.id
354 AND og.name = 'activity_type'";
355
356 $dao = CRM_Core_DAO::executeQuery($sql);
357 while ($dao->fetch()) {
358 //$survey['campaign_id'] = $dao->campaign_id;
359 //$survey['campaign_name'] = $dao->campaign_name;
360 $surveyInfo['activity_type'] = $dao->activity_type;
361 $surveyInfo['activity_type_id'] = $dao->activity_type_id;
362 $surveyInfo['title'] = $dao->title;
363 }
364
365 return $surveyInfo;
366 }
367
368 /**
369 * Function to get Petition Signature Details
370 *
371 * @param boolean $all
372 * @param int $id
373 * @static
374 */
375 static function getPetitionSignature($surveyId, $status_id = NULL) {
376
377 // sql injection protection
abde1673 378 $surveyId = (int) $surveyId;
6a488035
TO
379 $signature = array();
380
381 $sql = "
382 SELECT a.id,
383 a.source_record_id as survey_id,
384 a.activity_date_time,
385 a.status_id,
386 civicrm_contact.id as contact_id,
387 civicrm_contact.contact_type,civicrm_contact.contact_sub_type,image_URL,
388 first_name,last_name,sort_name,
389 employer_id,organization_name,
390 household_name,
391 IFNULL(gender_id,'') AS gender_id,
392 IFNULL(state_province_id,'') AS state_province_id,
393 IFNULL(country_id,'') as country_id,IFNULL(iso_code,'') as country_iso, IFNULL(civicrm_country.name,'') as country
ad674e50 394 FROM (civicrm_activity a, civicrm_survey, civicrm_contact )
395 LEFT JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id AND ac.record_type_id = %3 )
6a488035
TO
396 LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1
397 LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
398 WHERE
ad674e50 399 ac.contact_id = civicrm_contact.id AND
6a488035
TO
400 a.activity_type_id = civicrm_survey.activity_type_id AND
401 civicrm_survey.id = %1 AND
402 a.source_record_id = %1 ";
403
404 $params = array(1 => array($surveyId, 'Integer'));
405
406 if ($status_id) {
407 $sql .= " AND status_id = %2";
408 $params[2] = array($status_id, 'Integer');
409 }
410 $sql .= " ORDER BY a.activity_date_time";
411
afe0f8cf 412 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
ad674e50 413 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
414 $params[3] = array($sourceID, 'Integer');
415
6a488035 416 $fields = array(
abde1673 417 'id',
418 'survey_id',
419 'contact_id',
420 'activity_date_time',
421 'activity_type_id',
422 'status_id',
423 'first_name',
424 'last_name',
425 'sort_name',
426 'gender_id',
427 'country_id',
428 'state_province_id',
429 'country_iso',
430 'country',
6a488035
TO
431 );
432
6a488035
TO
433 $dao = CRM_Core_DAO::executeQuery($sql, $params);
434 while ($dao->fetch()) {
435 $row = array();
436 foreach ($fields as $field) {
437 $row[$field] = $dao->$field;
438 }
439 $signature[] = $row;
440 }
441 return $signature;
442 }
443
444 /**
445 * This function returns all entities assigned to a specific tag
446 *
abde1673 447 * @param object $tag an object of a tag.
6a488035
TO
448 *
449 * @return array $contactIds array of contact ids
450 * @access public
451 */
452 function getEntitiesByTag($tag) {
453 $contactIds = array();
454 $entityTagDAO = new CRM_Core_DAO_EntityTag();
455 $entityTagDAO->tag_id = $tag['id'];
456 $entityTagDAO->find();
457
458 while ($entityTagDAO->fetch()) {
459 $contactIds[] = $entityTagDAO->entity_id;
460 }
461 return $contactIds;
462 }
463
464 /**
465 * Function to check if contact has signed this petition
466 *
467 * @param int $surveyId
468 * @param int $contactId
469 * @static
470 */
471 static function checkSignature($surveyId, $contactId) {
472
473 $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo($surveyId);
474 $signature = array();
afe0f8cf 475 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
3f815c3a 476 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035
TO
477
478 $sql = "
479 SELECT a.id AS id,
480 a.source_record_id AS source_record_id,
3f815c3a 481 ac.contact_id AS source_contact_id,
6a488035
TO
482 a.activity_date_time AS activity_date_time,
483 a.activity_type_id AS activity_type_id,
484 a.status_id AS status_id,
485 %1 AS survey_title
486 FROM civicrm_activity a
3f815c3a 487 INNER JOIN civicrm_activity_contact ac ON (ac.activity_id = a.id AND ac.record_type_id = %5)
6a488035
TO
488 WHERE a.source_record_id = %2
489 AND a.activity_type_id = %3
3f815c3a 490 AND ac.contact_id = %4
6a488035 491";
abde1673 492 $params = array(
493 1 => array($surveyInfo['title'], 'String'),
6a488035
TO
494 2 => array($surveyId, 'Integer'),
495 3 => array($surveyInfo['activity_type_id'], 'Integer'),
496 4 => array($contactId, 'Integer'),
3f815c3a 497 5 => array($sourceID, 'Integer')
6a488035
TO
498 );
499
500 $dao = CRM_Core_DAO::executeQuery($sql, $params);
501 while ($dao->fetch()) {
502 $signature[$dao->id]['id'] = $dao->id;
503 $signature[$dao->id]['source_record_id'] = $dao->source_record_id;
504 $signature[$dao->id]['source_contact_id'] = CRM_Contact_BAO_Contact::displayName($dao->source_contact_id);
505 $signature[$dao->id]['activity_date_time'] = $dao->activity_date_time;
506 $signature[$dao->id]['activity_type_id'] = $dao->activity_type_id;
507 $signature[$dao->id]['status_id'] = $dao->status_id;
508 $signature[$dao->id]['survey_title'] = $dao->survey_title;
509 $signature[$dao->id]['contactId'] = $dao->source_contact_id;
510 }
511
512 return $signature;
513 }
514
515 /**
516 * takes an associative array and sends a thank you or email verification email
517 *
abde1673 518 * @param array $params (reference ) an assoc array of name/value pairs
6a488035
TO
519 *
520 * @return
521 * @access public
522 * @static
523 */
abde1673 524 public static function sendEmail($params, $sendEmailMode) {
525
526 /* sendEmailMode
527 * CRM_Campaign_Form_Petition_Signature::EMAIL_THANK
528 * connected user via login/pwd - thank you
529 * or dedupe contact matched who doesn't have a tag CIVICRM_TAG_UNCONFIRMED - thank you
530 * or login using fb connect - thank you + click to add msg to fb wall
531 *
532 * CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM
533 * send a confirmation request email
534 */
8ef12e64 535
6a488035
TO
536 // check if the group defined by CIVICRM_PETITION_CONTACTS exists, else create it
537 $petitionGroupName = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
538 'petition_contacts',
539 NULL,
540 'Petition Contacts'
541 );
542
543 $dao = new CRM_Contact_DAO_Group();
544 $dao->title = $petitionGroupName;
545 if (!$dao->find(TRUE)) {
546 $dao->is_active = 1;
8a012597 547 $dao->visibility = 'User and User Admin Only';
6a488035
TO
548 $dao->save();
549 }
550 $group_id = $dao->id;
551
552 // get petition info
553 $petitionParams['id'] = $params['sid'];
554 $petitionInfo = array();
555 CRM_Campaign_BAO_Survey::retrieve($petitionParams, $petitionInfo);
556 if (empty($petitionInfo)) {
557 CRM_Core_Error::fatal('Petition doesn\'t exist.');
558 }
559
560 //get the default domain email address.
561 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
562
563 $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
564
565 $toName = CRM_Contact_BAO_Contact::displayName($params['contactId']);
566
567 $replyTo = "do-not-reply@$emailDomain";
568
569 // set additional general message template params (custom tokens to use in email msg templates)
570 // tokens then available in msg template as {$petition.title}, etc
571 $petitionTokens['title'] = $petitionInfo['title'];
572 $petitionTokens['petitionId'] = $params['sid'];
573 $tplParams['petition'] = $petitionTokens;
574
575 switch ($sendEmailMode) {
576 case CRM_Campaign_Form_Petition_Signature::EMAIL_THANK:
577
578 // add this contact to the CIVICRM_PETITION_CONTACTS group
579 // Cannot pass parameter 1 by reference
580 $p = array($params['contactId']);
581 CRM_Contact_BAO_GroupContact::addContactsToGroup($p, $group_id, 'API');
582
583 if ($params['email-Primary']) {
c6327d7d 584 CRM_Core_BAO_MessageTemplate::sendTemplate(
6a488035
TO
585 array(
586 'groupName' => 'msg_tpl_workflow_petition',
587 'valueName' => 'petition_sign',
588 'contactId' => $params['contactId'],
589 'tplParams' => $tplParams,
590 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
591 'toName' => $toName,
592 'toEmail' => $params['email-Primary'],
593 'replyTo' => $replyTo,
594 'petitionId' => $params['sid'],
595 'petitionTitle' => $petitionInfo['title'],
596 )
597 );
598 }
599 break;
600
601 case CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM:
602 // create mailing event subscription record for this contact
603 // this will allow using a hash key to confirm email address by sending a url link
604 $se = CRM_Mailing_Event_BAO_Subscribe::subscribe($group_id,
605 $params['email-Primary'],
8a012597
NG
606 $params['contactId'],
607 'profile'
6a488035
TO
608 );
609
610 // require_once 'CRM/Core/BAO/Domain.php';
611 // $domain = CRM_Core_BAO_Domain::getDomain();
612 $config = CRM_Core_Config::singleton();
613 $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
614
615 $replyTo = implode($config->verpSeparator,
abde1673 616 array(
617 $localpart . 'c',
618 $se->contact_id,
619 $se->id,
620 $se->hash,
621 )
622 ) . "@$emailDomain";
6a488035
TO
623
624
625 $confirmUrl = CRM_Utils_System::url('civicrm/petition/confirm',
626 "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&p={$params['sid']}",
627 TRUE
628 );
629 $confirmUrlPlainText = CRM_Utils_System::url('civicrm/petition/confirm',
630 "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&p={$params['sid']}",
631 TRUE,
632 NULL,
633 FALSE
634 );
635
636 // set email specific message template params and assign to tplParams
637 $petitionTokens['confirmUrl'] = $confirmUrl;
638 $petitionTokens['confirmUrlPlainText'] = $confirmUrlPlainText;
639 $tplParams['petition'] = $petitionTokens;
640
641 if ($params['email-Primary']) {
c6327d7d 642 CRM_Core_BAO_MessageTemplate::sendTemplate(
6a488035
TO
643 array(
644 'groupName' => 'msg_tpl_workflow_petition',
645 'valueName' => 'petition_confirmation_needed',
646 'contactId' => $params['contactId'],
647 'tplParams' => $tplParams,
648 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
649 'toName' => $toName,
650 'toEmail' => $params['email-Primary'],
651 'replyTo' => $replyTo,
652 'petitionId' => $params['sid'],
653 'petitionTitle' => $petitionInfo['title'],
654 'confirmUrl' => $confirmUrl,
655 )
656 );
657 }
658 break;
659 }
660 }
661}
662