| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | */ |
| 33 | class CRM_Campaign_BAO_Petition extends CRM_Campaign_BAO_Survey { |
| 34 | /** |
| 35 | * Class constructor. |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | parent::__construct(); |
| 39 | // expire cookie in one day |
| 40 | $this->cookieExpire = (1 * 60 * 60 * 24); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get Petition Details for dashboard. |
| 45 | * |
| 46 | * @param array $params |
| 47 | * @param bool $onlyCount |
| 48 | * |
| 49 | * @return array|int |
| 50 | */ |
| 51 | public static function getPetitionSummary($params = array(), $onlyCount = FALSE) { |
| 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) { |
| 62 | if (!empty($params[$name])) { |
| 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 = " |
| 78 | LEFT JOIN civicrm_option_value activity_type ON ( activity_type.value = petition.activity_type_id |
| 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 | } |
| 102 | if (!empty($params['title'])) { |
| 103 | $where[] = "( petition.title LIKE %2 )"; |
| 104 | $queryParams[2] = array('%' . trim($params['title']) . '%', 'String'); |
| 105 | } |
| 106 | if (!empty($params['campaign_id'])) { |
| 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) { |
| 132 | return (int) CRM_Core_DAO::singleValueQuery($query, $queryParams); |
| 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 | * |
| 159 | */ |
| 160 | public static function getPetitionCount() { |
| 161 | $whereClause = 'WHERE ( 1 )'; |
| 162 | $queryParams = array(); |
| 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 | |
| 170 | return (int) CRM_Core_DAO::singleValueQuery($query, $queryParams); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Takes an associative array and creates a petition signature activity. |
| 175 | * |
| 176 | * @param array $params |
| 177 | * (reference ) an assoc array of name/value pairs. |
| 178 | * |
| 179 | * @return CRM_Campaign_BAO_Petition |
| 180 | */ |
| 181 | public function createSignature(&$params) { |
| 182 | if (empty($params)) { |
| 183 | return NULL; |
| 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 |
| 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'], |
| 209 | 'activity_campaign_id' => $params['activity_campaign_id'], |
| 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 |
| 217 | if (!empty($params['custom']) && |
| 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 | |
| 230 | /** |
| 231 | * @param int $activity_id |
| 232 | * @param int $contact_id |
| 233 | * @param int $petition_id |
| 234 | * |
| 235 | * @return bool |
| 236 | */ |
| 237 | public function confirmSignature($activity_id, $contact_id, $petition_id) { |
| 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] |
| 240 | $sql = 'UPDATE civicrm_activity SET status_id = 2 WHERE id = %1'; |
| 241 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 242 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 243 | $params = array( |
| 244 | 1 => array($activity_id, 'Integer'), |
| 245 | 2 => array($contact_id, 'Integer'), |
| 246 | 3 => array($sourceID, 'Integer'), |
| 247 | ); |
| 248 | CRM_Core_DAO::executeQuery($sql, $params); |
| 249 | |
| 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); |
| 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 = " |
| 260 | DELETE FROM civicrm_entity_tag |
| 261 | WHERE entity_table = 'civicrm_contact' |
| 262 | AND entity_id = %1 |
| 263 | AND tag_id = ( SELECT id FROM civicrm_tag WHERE name = %2 )"; |
| 264 | $params = array( |
| 265 | 1 => array($contact_id, 'Integer'), |
| 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 | /** |
| 281 | * Get Petition Signature Total. |
| 282 | * |
| 283 | * @param int $surveyId |
| 284 | * |
| 285 | * @return array |
| 286 | */ |
| 287 | public static function getPetitionSignatureTotalbyCountry($surveyId) { |
| 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 |
| 292 | FROM ( civicrm_activity a, civicrm_survey, civicrm_contact ) |
| 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 |
| 295 | LEFT JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id AND ac.record_type_id = %2 ) |
| 296 | WHERE |
| 297 | ac.contact_id = civicrm_contact.id AND |
| 298 | a.activity_type_id = civicrm_survey.activity_type_id AND |
| 299 | civicrm_survey.id = %1 AND |
| 300 | a.source_record_id = %1 "; |
| 301 | |
| 302 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 303 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 304 | $params = array( |
| 305 | 1 => array($surveyId, 'Integer'), |
| 306 | 2 => array($sourceID, 'Integer'), |
| 307 | ); |
| 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 | /** |
| 323 | * Get Petition Signature Total. |
| 324 | * |
| 325 | * @param int $surveyId |
| 326 | * |
| 327 | * @return array |
| 328 | */ |
| 329 | public static function getPetitionSignatureTotal($surveyId) { |
| 330 | $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo((int) $surveyId); |
| 331 | //$activityTypeID = $surveyInfo['activity_type_id']; |
| 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(); |
| 340 | $total = 0; |
| 341 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 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 | |
| 351 | /** |
| 352 | * @param int $surveyId |
| 353 | * |
| 354 | * @return array |
| 355 | */ |
| 356 | public static function getSurveyInfo($surveyId = NULL) { |
| 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 |
| 365 | WHERE s.id = " . (int) $surveyId . " |
| 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 | /** |
| 383 | * Get Petition Signature Details. |
| 384 | * |
| 385 | * @param int $surveyId |
| 386 | * @param int $status_id |
| 387 | * |
| 388 | * @return array |
| 389 | */ |
| 390 | public static function getPetitionSignature($surveyId, $status_id = NULL) { |
| 391 | |
| 392 | // sql injection protection |
| 393 | $surveyId = (int) $surveyId; |
| 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 |
| 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 ) |
| 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 |
| 414 | ac.contact_id = civicrm_contact.id AND |
| 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 | |
| 427 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 428 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 429 | $params[3] = array($sourceID, 'Integer'); |
| 430 | |
| 431 | $fields = array( |
| 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', |
| 446 | ); |
| 447 | |
| 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 | /** |
| 460 | * This function returns all entities assigned to a specific tag. |
| 461 | * |
| 462 | * @param object $tag |
| 463 | * An object of a tag. |
| 464 | * |
| 465 | * @return array |
| 466 | * array of contact ids |
| 467 | */ |
| 468 | public function getEntitiesByTag($tag) { |
| 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 | /** |
| 481 | * Check if contact has signed this petition. |
| 482 | * |
| 483 | * @param int $surveyId |
| 484 | * @param int $contactId |
| 485 | * |
| 486 | * @return array |
| 487 | */ |
| 488 | public static function checkSignature($surveyId, $contactId) { |
| 489 | |
| 490 | $surveyInfo = CRM_Campaign_BAO_Petition::getSurveyInfo($surveyId); |
| 491 | $signature = array(); |
| 492 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 493 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 494 | |
| 495 | $sql = " |
| 496 | SELECT a.id AS id, |
| 497 | a.source_record_id AS source_record_id, |
| 498 | ac.contact_id AS source_contact_id, |
| 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 |
| 504 | INNER JOIN civicrm_activity_contact ac ON (ac.activity_id = a.id AND ac.record_type_id = %5) |
| 505 | WHERE a.source_record_id = %2 |
| 506 | AND a.activity_type_id = %3 |
| 507 | AND ac.contact_id = %4 |
| 508 | "; |
| 509 | $params = array( |
| 510 | 1 => array($surveyInfo['title'], 'String'), |
| 511 | 2 => array($surveyId, 'Integer'), |
| 512 | 3 => array($surveyInfo['activity_type_id'], 'Integer'), |
| 513 | 4 => array($contactId, 'Integer'), |
| 514 | 5 => array($sourceID, 'Integer'), |
| 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 | /** |
| 533 | * Takes an associative array and sends a thank you or email verification email. |
| 534 | * |
| 535 | * @param array $params |
| 536 | * (reference ) an assoc array of name/value pairs. |
| 537 | * |
| 538 | * @param int $sendEmailMode |
| 539 | * |
| 540 | * @throws Exception |
| 541 | */ |
| 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 | */ |
| 553 | |
| 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; |
| 565 | $dao->visibility = 'User and User Admin Only'; |
| 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']) { |
| 602 | CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 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'], |
| 624 | $params['contactId'], |
| 625 | 'profile' |
| 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, |
| 634 | array( |
| 635 | $localpart . 'c', |
| 636 | $se->contact_id, |
| 637 | $se->id, |
| 638 | $se->hash, |
| 639 | ) |
| 640 | ) . "@$emailDomain"; |
| 641 | |
| 642 | $confirmUrl = CRM_Utils_System::url('civicrm/petition/confirm', |
| 643 | "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&pid={$params['sid']}", |
| 644 | TRUE |
| 645 | ); |
| 646 | $confirmUrlPlainText = CRM_Utils_System::url('civicrm/petition/confirm', |
| 647 | "reset=1&cid={$se->contact_id}&sid={$se->id}&h={$se->hash}&a={$params['activityId']}&pid={$params['sid']}", |
| 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']) { |
| 659 | CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 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 | } |
| 678 | |
| 679 | } |