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