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