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