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