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