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