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 { | |
4cf017d1 | 269 | throw new CRM_Core_Exception(ts('Petition Id and/or Activity Id is not of the type Positive.')); |
78dd2103 | 270 | } |
6a488035 TO |
271 | } |
272 | ||
273 | /** | |
fe482240 | 274 | * Get Petition Signature Total. |
6a488035 | 275 | * |
c490a46a | 276 | * @param int $surveyId |
77b97be7 EM |
277 | * |
278 | * @return array | |
6a488035 | 279 | */ |
00be9182 | 280 | public static function getPetitionSignatureTotalbyCountry($surveyId) { |
be2fb01f | 281 | $countries = []; |
6a488035 TO |
282 | $sql = " |
283 | SELECT count(civicrm_address.country_id) as total, | |
284 | IFNULL(country_id,'') as country_id,IFNULL(iso_code,'') as country_iso, IFNULL(civicrm_country.name,'') as country | |
9b8ec3a8 | 285 | FROM ( civicrm_activity a, civicrm_survey, civicrm_contact ) |
6a488035 TO |
286 | LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1 |
287 | LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id | |
9b8ec3a8 | 288 | LEFT JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id AND ac.record_type_id = %2 ) |
6a488035 | 289 | WHERE |
9b8ec3a8 | 290 | ac.contact_id = civicrm_contact.id AND |
6a488035 TO |
291 | a.activity_type_id = civicrm_survey.activity_type_id AND |
292 | civicrm_survey.id = %1 AND | |
293 | a.source_record_id = %1 "; | |
294 | ||
44f817d4 | 295 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
9b8ec3a8 | 296 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
be2fb01f CW |
297 | $params = [ |
298 | 1 => [$surveyId, 'Integer'], | |
299 | 2 => [$sourceID, 'Integer'], | |
300 | ]; | |
6a488035 | 301 | $sql .= " GROUP BY civicrm_address.country_id"; |
be2fb01f | 302 | $fields = ['total', 'country_id', 'country_iso', 'country']; |
6a488035 TO |
303 | |
304 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
305 | while ($dao->fetch()) { | |
be2fb01f | 306 | $row = []; |
6a488035 TO |
307 | foreach ($fields as $field) { |
308 | $row[$field] = $dao->$field; | |
309 | } | |
310 | $countries[] = $row; | |
311 | } | |
312 | return $countries; | |
313 | } | |
314 | ||
315 | /** | |
fe482240 | 316 | * Get Petition Signature Total. |
6a488035 | 317 | * |
c490a46a | 318 | * @param int $surveyId |
77b97be7 EM |
319 | * |
320 | * @return array | |
6a488035 | 321 | */ |
00be9182 | 322 | public static function getPetitionSignatureTotal($surveyId) { |
6a488035 TO |
323 | $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo((int) $surveyId); |
324 | //$activityTypeID = $surveyInfo['activity_type_id']; | |
6a488035 TO |
325 | $sql = " |
326 | SELECT | |
327 | status_id,count(id) as total | |
328 | FROM civicrm_activity | |
329 | WHERE | |
330 | source_record_id = " . (int) $surveyId . " AND activity_type_id = " . (int) $surveyInfo['activity_type_id'] . " GROUP BY status_id"; | |
331 | ||
be2fb01f | 332 | $statusTotal = []; |
abde1673 | 333 | $total = 0; |
334 | $dao = CRM_Core_DAO::executeQuery($sql); | |
6a488035 TO |
335 | while ($dao->fetch()) { |
336 | $total += $dao->total; | |
337 | $statusTotal['status'][$dao->status_id] = $dao->total; | |
338 | } | |
339 | $statusTotal['count'] = $total; | |
340 | return $statusTotal; | |
341 | } | |
342 | ||
30c4e065 | 343 | /** |
100fef9d | 344 | * @param int $surveyId |
30c4e065 EM |
345 | * |
346 | * @return array | |
347 | */ | |
43427a24 | 348 | public static function getSurveyInfo($surveyId = NULL) { |
be2fb01f | 349 | $surveyInfo = []; |
6a488035 TO |
350 | |
351 | $sql = " | |
352 | SELECT activity_type_id, | |
353 | campaign_id, | |
354 | s.title, | |
355 | ov.label AS activity_type | |
356 | FROM civicrm_survey s, civicrm_option_value ov, civicrm_option_group og | |
abde1673 | 357 | WHERE s.id = " . (int) $surveyId . " |
6a488035 TO |
358 | AND s.activity_type_id = ov.value |
359 | AND ov.option_group_id = og.id | |
360 | AND og.name = 'activity_type'"; | |
361 | ||
362 | $dao = CRM_Core_DAO::executeQuery($sql); | |
363 | while ($dao->fetch()) { | |
364 | //$survey['campaign_id'] = $dao->campaign_id; | |
365 | //$survey['campaign_name'] = $dao->campaign_name; | |
366 | $surveyInfo['activity_type'] = $dao->activity_type; | |
367 | $surveyInfo['activity_type_id'] = $dao->activity_type_id; | |
368 | $surveyInfo['title'] = $dao->title; | |
369 | } | |
370 | ||
371 | return $surveyInfo; | |
372 | } | |
373 | ||
374 | /** | |
fe482240 | 375 | * Get Petition Signature Details. |
6a488035 | 376 | * |
c490a46a CW |
377 | * @param int $surveyId |
378 | * @param int $status_id | |
77b97be7 EM |
379 | * |
380 | * @return array | |
6a488035 | 381 | */ |
00be9182 | 382 | public static function getPetitionSignature($surveyId, $status_id = NULL) { |
6a488035 TO |
383 | |
384 | // sql injection protection | |
abde1673 | 385 | $surveyId = (int) $surveyId; |
be2fb01f | 386 | $signature = []; |
6a488035 TO |
387 | |
388 | $sql = " | |
389 | SELECT a.id, | |
390 | a.source_record_id as survey_id, | |
391 | a.activity_date_time, | |
392 | a.status_id, | |
393 | civicrm_contact.id as contact_id, | |
394 | civicrm_contact.contact_type,civicrm_contact.contact_sub_type,image_URL, | |
395 | first_name,last_name,sort_name, | |
396 | employer_id,organization_name, | |
397 | household_name, | |
398 | IFNULL(gender_id,'') AS gender_id, | |
399 | IFNULL(state_province_id,'') AS state_province_id, | |
400 | IFNULL(country_id,'') as country_id,IFNULL(iso_code,'') as country_iso, IFNULL(civicrm_country.name,'') as country | |
ad674e50 | 401 | FROM (civicrm_activity a, civicrm_survey, civicrm_contact ) |
402 | LEFT JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id AND ac.record_type_id = %3 ) | |
6a488035 TO |
403 | LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1 |
404 | LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id | |
405 | WHERE | |
ad674e50 | 406 | ac.contact_id = civicrm_contact.id AND |
6a488035 TO |
407 | a.activity_type_id = civicrm_survey.activity_type_id AND |
408 | civicrm_survey.id = %1 AND | |
409 | a.source_record_id = %1 "; | |
410 | ||
be2fb01f | 411 | $params = [1 => [$surveyId, 'Integer']]; |
6a488035 TO |
412 | |
413 | if ($status_id) { | |
414 | $sql .= " AND status_id = %2"; | |
be2fb01f | 415 | $params[2] = [$status_id, 'Integer']; |
6a488035 TO |
416 | } |
417 | $sql .= " ORDER BY a.activity_date_time"; | |
418 | ||
44f817d4 | 419 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
ad674e50 | 420 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
be2fb01f | 421 | $params[3] = [$sourceID, 'Integer']; |
ad674e50 | 422 | |
be2fb01f | 423 | $fields = [ |
abde1673 | 424 | 'id', |
425 | 'survey_id', | |
426 | 'contact_id', | |
427 | 'activity_date_time', | |
428 | 'activity_type_id', | |
429 | 'status_id', | |
430 | 'first_name', | |
431 | 'last_name', | |
432 | 'sort_name', | |
433 | 'gender_id', | |
434 | 'country_id', | |
435 | 'state_province_id', | |
436 | 'country_iso', | |
437 | 'country', | |
be2fb01f | 438 | ]; |
6a488035 | 439 | |
6a488035 TO |
440 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
441 | while ($dao->fetch()) { | |
be2fb01f | 442 | $row = []; |
6a488035 TO |
443 | foreach ($fields as $field) { |
444 | $row[$field] = $dao->$field; | |
445 | } | |
446 | $signature[] = $row; | |
447 | } | |
448 | return $signature; | |
449 | } | |
450 | ||
451 | /** | |
fe482240 | 452 | * This function returns all entities assigned to a specific tag. |
6a488035 | 453 | * |
7aaf6db0 TO |
454 | * @param object $tag |
455 | * An object of a tag. | |
6a488035 | 456 | * |
a6c01b45 CW |
457 | * @return array |
458 | * array of contact ids | |
6a488035 | 459 | */ |
00be9182 | 460 | public function getEntitiesByTag($tag) { |
be2fb01f | 461 | $contactIds = []; |
6a488035 TO |
462 | $entityTagDAO = new CRM_Core_DAO_EntityTag(); |
463 | $entityTagDAO->tag_id = $tag['id']; | |
464 | $entityTagDAO->find(); | |
465 | ||
466 | while ($entityTagDAO->fetch()) { | |
467 | $contactIds[] = $entityTagDAO->entity_id; | |
468 | } | |
469 | return $contactIds; | |
470 | } | |
471 | ||
472 | /** | |
fe482240 | 473 | * Check if contact has signed this petition. |
6a488035 TO |
474 | * |
475 | * @param int $surveyId | |
476 | * @param int $contactId | |
77b97be7 EM |
477 | * |
478 | * @return array | |
6a488035 | 479 | */ |
00be9182 | 480 | public static function checkSignature($surveyId, $contactId) { |
6a488035 TO |
481 | |
482 | $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo($surveyId); | |
be2fb01f | 483 | $signature = []; |
44f817d4 | 484 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
3f815c3a | 485 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
6a488035 TO |
486 | |
487 | $sql = " | |
488 | SELECT a.id AS id, | |
489 | a.source_record_id AS source_record_id, | |
3f815c3a | 490 | ac.contact_id AS source_contact_id, |
6a488035 TO |
491 | a.activity_date_time AS activity_date_time, |
492 | a.activity_type_id AS activity_type_id, | |
493 | a.status_id AS status_id, | |
494 | %1 AS survey_title | |
495 | FROM civicrm_activity a | |
3f815c3a | 496 | INNER JOIN civicrm_activity_contact ac ON (ac.activity_id = a.id AND ac.record_type_id = %5) |
6a488035 TO |
497 | WHERE a.source_record_id = %2 |
498 | AND a.activity_type_id = %3 | |
3f815c3a | 499 | AND ac.contact_id = %4 |
6a488035 | 500 | "; |
be2fb01f CW |
501 | $params = [ |
502 | 1 => [$surveyInfo['title'], 'String'], | |
503 | 2 => [$surveyId, 'Integer'], | |
504 | 3 => [$surveyInfo['activity_type_id'], 'Integer'], | |
505 | 4 => [$contactId, 'Integer'], | |
506 | 5 => [$sourceID, 'Integer'], | |
507 | ]; | |
6a488035 TO |
508 | |
509 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
510 | while ($dao->fetch()) { | |
511 | $signature[$dao->id]['id'] = $dao->id; | |
512 | $signature[$dao->id]['source_record_id'] = $dao->source_record_id; | |
513 | $signature[$dao->id]['source_contact_id'] = CRM_Contact_BAO_Contact::displayName($dao->source_contact_id); | |
514 | $signature[$dao->id]['activity_date_time'] = $dao->activity_date_time; | |
515 | $signature[$dao->id]['activity_type_id'] = $dao->activity_type_id; | |
516 | $signature[$dao->id]['status_id'] = $dao->status_id; | |
517 | $signature[$dao->id]['survey_title'] = $dao->survey_title; | |
518 | $signature[$dao->id]['contactId'] = $dao->source_contact_id; | |
519 | } | |
520 | ||
521 | return $signature; | |
522 | } | |
523 | ||
524 | /** | |
fe482240 | 525 | * Takes an associative array and sends a thank you or email verification email. |
6a488035 | 526 | * |
7aaf6db0 TO |
527 | * @param array $params |
528 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 529 | * |
ce064e4f | 530 | * @param int $sendEmailMode |
2a6da8d7 EM |
531 | * |
532 | * @throws Exception | |
6a488035 | 533 | */ |
abde1673 | 534 | public static function sendEmail($params, $sendEmailMode) { |
535 | ||
536 | /* sendEmailMode | |
537 | * CRM_Campaign_Form_Petition_Signature::EMAIL_THANK | |
538 | * connected user via login/pwd - thank you | |
539 | * or dedupe contact matched who doesn't have a tag CIVICRM_TAG_UNCONFIRMED - thank you | |
540 | * or login using fb connect - thank you + click to add msg to fb wall | |
541 | * | |
542 | * CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM | |
543 | * send a confirmation request email | |
544 | */ | |
8ef12e64 | 545 | |
6a488035 | 546 | // check if the group defined by CIVICRM_PETITION_CONTACTS exists, else create it |
aaffa79f | 547 | $petitionGroupName = Civi::settings()->get('petition_contacts'); |
6a488035 TO |
548 | |
549 | $dao = new CRM_Contact_DAO_Group(); | |
550 | $dao->title = $petitionGroupName; | |
551 | if (!$dao->find(TRUE)) { | |
552 | $dao->is_active = 1; | |
8a012597 | 553 | $dao->visibility = 'User and User Admin Only'; |
6a488035 TO |
554 | $dao->save(); |
555 | } | |
556 | $group_id = $dao->id; | |
557 | ||
558 | // get petition info | |
559 | $petitionParams['id'] = $params['sid']; | |
be2fb01f | 560 | $petitionInfo = []; |
6a488035 TO |
561 | CRM_Campaign_BAO_Survey::retrieve($petitionParams, $petitionInfo); |
562 | if (empty($petitionInfo)) { | |
4cf017d1 | 563 | throw new CRM_Core_Exception('Petition doesn\'t exist.'); |
6a488035 TO |
564 | } |
565 | ||
566 | //get the default domain email address. | |
567 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
568 | ||
569 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
570 | ||
571 | $toName = CRM_Contact_BAO_Contact::displayName($params['contactId']); | |
572 | ||
576fcb9c | 573 | $replyTo = CRM_Core_BAO_Domain::getNoReplyEmailAddress(); |
6a488035 TO |
574 | |
575 | // set additional general message template params (custom tokens to use in email msg templates) | |
576 | // tokens then available in msg template as {$petition.title}, etc | |
577 | $petitionTokens['title'] = $petitionInfo['title']; | |
578 | $petitionTokens['petitionId'] = $params['sid']; | |
579 | $tplParams['petition'] = $petitionTokens; | |
580 | ||
581 | switch ($sendEmailMode) { | |
582 | case CRM_Campaign_Form_Petition_Signature::EMAIL_THANK: | |
583 | ||
584 | // add this contact to the CIVICRM_PETITION_CONTACTS group | |
585 | // Cannot pass parameter 1 by reference | |
be2fb01f | 586 | $p = [$params['contactId']]; |
6a488035 TO |
587 | CRM_Contact_BAO_GroupContact::addContactsToGroup($p, $group_id, 'API'); |
588 | ||
589 | if ($params['email-Primary']) { | |
c6327d7d | 590 | CRM_Core_BAO_MessageTemplate::sendTemplate( |
be2fb01f | 591 | [ |
6a488035 TO |
592 | 'groupName' => 'msg_tpl_workflow_petition', |
593 | 'valueName' => 'petition_sign', | |
594 | 'contactId' => $params['contactId'], | |
595 | 'tplParams' => $tplParams, | |
596 | 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", | |
597 | 'toName' => $toName, | |
598 | 'toEmail' => $params['email-Primary'], | |
599 | 'replyTo' => $replyTo, | |
600 | 'petitionId' => $params['sid'], | |
601 | 'petitionTitle' => $petitionInfo['title'], | |
be2fb01f | 602 | ] |
6a488035 TO |
603 | ); |
604 | } | |
605 | break; | |
606 | ||
607 | case CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM: | |
608 | // create mailing event subscription record for this contact | |
609 | // this will allow using a hash key to confirm email address by sending a url link | |
610 | $se = CRM_Mailing_Event_BAO_Subscribe::subscribe($group_id, | |
611 | $params['email-Primary'], | |
8a012597 NG |
612 | $params['contactId'], |
613 | 'profile' | |
6a488035 TO |
614 | ); |
615 | ||
616 | // require_once 'CRM/Core/BAO/Domain.php'; | |
617 | // $domain = CRM_Core_BAO_Domain::getDomain(); | |
618 | $config = CRM_Core_Config::singleton(); | |
619 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); | |
620 | ||
621 | $replyTo = implode($config->verpSeparator, | |
be2fb01f | 622 | [ |
abde1673 | 623 | $localpart . 'c', |
624 | $se->contact_id, | |
625 | $se->id, | |
626 | $se->hash, | |
be2fb01f | 627 | ] |
abde1673 | 628 | ) . "@$emailDomain"; |
6a488035 | 629 | |
6a488035 | 630 | $confirmUrl = CRM_Utils_System::url('civicrm/petition/confirm', |
18d62c87 | 631 | "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&pid={$params['sid']}", |
6a488035 TO |
632 | TRUE |
633 | ); | |
634 | $confirmUrlPlainText = CRM_Utils_System::url('civicrm/petition/confirm', | |
18d62c87 | 635 | "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&pid={$params['sid']}", |
6a488035 TO |
636 | TRUE, |
637 | NULL, | |
638 | FALSE | |
639 | ); | |
640 | ||
641 | // set email specific message template params and assign to tplParams | |
642 | $petitionTokens['confirmUrl'] = $confirmUrl; | |
643 | $petitionTokens['confirmUrlPlainText'] = $confirmUrlPlainText; | |
644 | $tplParams['petition'] = $petitionTokens; | |
645 | ||
646 | if ($params['email-Primary']) { | |
c6327d7d | 647 | CRM_Core_BAO_MessageTemplate::sendTemplate( |
be2fb01f | 648 | [ |
6a488035 TO |
649 | 'groupName' => 'msg_tpl_workflow_petition', |
650 | 'valueName' => 'petition_confirmation_needed', | |
651 | 'contactId' => $params['contactId'], | |
652 | 'tplParams' => $tplParams, | |
653 | 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", | |
654 | 'toName' => $toName, | |
655 | 'toEmail' => $params['email-Primary'], | |
656 | 'replyTo' => $replyTo, | |
657 | 'petitionId' => $params['sid'], | |
658 | 'petitionTitle' => $petitionInfo['title'], | |
659 | 'confirmUrl' => $confirmUrl, | |
be2fb01f | 660 | ] |
6a488035 TO |
661 | ); |
662 | } | |
663 | break; | |
664 | } | |
665 | } | |
96025800 | 666 | |
6a488035 | 667 | } |