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