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