/** * This function handles all petition signature processing. * * @activity_id integer The activity id of the signature activity * @profile_fields array An array of fields submitted by the user, which * may include the custom subject and custom message values. */ function petitionemail_process_signature($activity_id, $profile_fields = NULL) { $petition_id = petitionemail_get_petition_id_for_activity($activity_id); if (empty($petition_id)) { $log = "Failed to find petition id for activity id: {$activity_id}"; CRM_Core_Error::debug_log_message($log); return FALSE; } $petition_vars = petitionemail_get_petition_details($petition_id); if (!$petition_vars) { // Nothing to process, this isn't an email target enabled petition return; } $default_message = $petition_vars['default_message']; $default_subject = $petition_vars['subject']; $message_field = $petition_vars['message_field']; $subject_field = $petition_vars['subject_field']; $activity = civicrm_api3("Activity", "getsingle", array('id' => $activity_id)); $contact_id = $activity['source_contact_id']; $contact = civicrm_api3("Contact", "getsingle", array('id' => $contact_id)); // Figure out whether to use the user-supplied message/subject or the default // message/subject. $petition_message = NULL; $subject = NULL; // If the petition has specified a message field if (!empty($message_field)) { // Check for a custom message field value in the passed in profile fields. // This field will be populated if we are operating on a new activity via // the post hook. if (is_array($profile_fields) && !empty($profile_fields[$message_field])) { $petition_message = $profile_fields[$message_field]; } else { // Retrieve the value of the field for this activity (this may happen // if we are operating on a confirmation click from pageRun hook). $params = array('id' => $activity_id, 'return' => $message_field); $result = civicrm_api3('Activity', 'getsingle', $params); if (!empty($result[$message_field])) { $petition_message = $result[$message_field]; } } } if (is_null($petition_message)) { $petition_message = $default_message; } // CiviCRM seems to htmlentitize everything submitted, but we are // preventing any html tags in our validation and we want to avoid // weird htmlentites being added to text messages. $petition_message = html_entity_decode($petition_message); // Add the sending contacts address info $address_block = petitionemail_get_address_block($contact_id); if ($address_block) { $petition_message = strip_tags($address_block) . "\n\n" . $petition_message; } // If the petition has specified a subject field if (!empty($subject_field)) { // Check for a custom subject field value in the passed in profile fields. // This field will be populated if we are operating on a new activity via // the post hook. if (is_array($profile_fields) && !empty($profile_fields[$subject_field])) { $subject = $profile_fields[$subject_field]; } else { // Retrieve the value of the field for this activity (this may happen // if we are operating on a confirmation click from pageRun hook). $params = array('id' => $activity_id, 'return' => $subject_field); $result = civicrm_api3('Activity', 'getsingle', $params); if (!empty($result[$subject_field])) { $subject = $result[$subject_field]; } } } // No user supplied message/subject, use the default if (is_null($subject)) { $subject = $default_subject; } // CiviCRM seems to htmlentitize everything submitted, but we don't // want 3 > 1 in a subject line get converted to 3 > 1 $subject = html_entity_decode($subject); $from = NULL; if (empty($contact['email'])) { $domain = civicrm_api3("Domain", "get", array()); if ($domain['is_error'] != 0 || !is_array($domain['values'])) { // Can't send email without a from address. $msg = "petition_email: Failed to send petition email because from\n address not sent."; CRM_Core_Error::debug_log_message($msg); return; } $contact['email'] = $domain['values']['from_email']; } $from = $contact['display_name'] . ' <' . $contact['email'] . '>'; // Setup email message (except to address) $email_params = array('from' => $from, 'toName' => NULL, 'toEmail' => NULL, 'subject' => $subject, 'text' => $petition_message, 'html' => NULL); // Get array of recipients $recipients = petitionemail_get_recipients($contact_id, $petition_id); // Keep track of the targets we actually send the message to so we can // email the petition signer to let them now. $message_sent_to = array(); while (list(, $recipient) = each($recipients)) { if (!empty($recipient['email'])) { $log = "petition email: contact id ({$contact_id}) sending to email (" . $recipient['email'] . ")"; CRM_Core_Error::debug_log_message($log); if (!empty($recipient['contact_id'])) { // Since we're sending to a recipient in the database, create an // email activity. Note: we are not using the built-in // function to create (and send) and email activity because it won't // send if the contact has DoNotEmail they won't get it. However, it's // normal to have DoNotEmail for your targets, but you still want them // to get the petition. $log = "petition email: recording email as activity against " . "target contact id: " . $recipient['contact_id']; CRM_Core_Error::debug_log_message($log); $contactDetails = array(0 => $recipient); // We are sending a text message, so ensure it's the preferred one $contactDetails[0]['preferred_mail_format'] = 'Text'; $subject = $email_params['subject']; $text = $email_params['text']; $html = $email_params['html']; $emailAddress = $recipient['email']; $userID = $contact_id; $from = NULL; // This will be pulled from $contact_id, $attachments = NULL; $cc = NULL; $bcc = NULL; $contactIds = array($recipient['contact_id']); // Create the activity first, then we will send the email. $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'); $activity_type_id = array_search('Email', $activityTypes); $activity_status = CRM_Core_PseudoConstant::activityStatus(); $status_id = array_search('Completed', $activity_status); $params = array('activity_type_id' => $activity_type_id, 'subject' => $subject, 'details' => $text, 'source_contact_id' => $contact_id, 'target_contact_id' => $recipient['contact_id'], 'status_id' => $status_id); $activity_id = NULL; try { $ret = civicrm_api3('Activity', 'create', $params); $value = array_pop($ret['values']); $activity_id = $value['id']; } catch (CiviCRM_API3_Exception $e) { $log = "petition email: email activity not created"; $log .= $e->getMessage(); CRM_Core_Error::debug_log_message($log); return FALSE; } if ($activity_id) { // Update the activity with the petition id so we can properly // report on the email messages sent as a result of this petition. $params = array('activity_id' => $activity_id, 'source_record_id' => $petition_id); $result = civicrm_api3('Activity', 'update', $params); if ($result['is_error'] != 0) { $log = "civicrm petition: failed to update activity with " . "source_record_id"; CRM_Core_Error::debug_log_message($log); } } } // Now send all email. // Handle targets not in the database. $email_params['toName'] = $recipient['name']; $email_params['toEmail'] = $recipient['email']; $to = $email_params['toName'] . ' <' . $email_params['toEmail'] . '>'; $log = "petition_email: sending petition to '{$to}' via mail function."; CRM_Core_Error::debug_log_message($log); $success = CRM_Utils_Mail::send($email_params); if ($success == 1) { CRM_Core_Session::setStatus(ts('Message sent successfully to: ') . htmlentities($to), '', 'success'); $log = "petition_email: message sent."; $message_sent_to[] = $to; } else { $log = "petition_email: message was not sent."; } CRM_Core_Error::debug_log_message($log); } } }
function alterDisplay(&$rows) { $petition_id = $this->_params['petition_id_value']; foreach ($rows as $rowNum => $row) { if (array_key_exists('civicrm_contact_contacts_matched', $row)) { $recipients = petitionemail_get_recipients($row['civicrm_contact_id'], $petition_id); $contacts_matched = array(); while (list(, $recipient) = each($recipients)) { if (!empty($recipient['contact_id'])) { $contacts_matched[] = $this->convert_contact_to_link($recipient['name'], $recipient['contact_id']); } } $rows[$rowNum]['civicrm_contact_contacts_matched'] = implode($contacts_matched, ','); } if (array_key_exists('civicrm_contact_emails_sent', $row)) { $emails_sent = $this->get_emails_sent_for_contact($row['civicrm_contact_id'], $petition_id); $rows[$rowNum]['civicrm_contact_emails_sent'] = implode($emails_sent, ','); } if (array_key_exists('civicrm_contact_display_name', $row)) { $rows[$rowNum]['civicrm_contact_display_name'] = $this->convert_contact_to_link($row['civicrm_contact_display_name'], $row['civicrm_contact_id']); } } }