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