forked from vanderbilt-redcap/email-alerts-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreviewRecordForm.php
76 lines (58 loc) · 3.17 KB
/
previewRecordForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Vanderbilt\EmailTriggerExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
require_once __DIR__.'/vendor/autoload.php';
$project_id = $_GET['pid'];
$index = $_REQUEST['index_modal_record_preview'];
$record = $_REQUEST['preview_record_id'];
#get data from the DB
$form_name = empty($module->getProjectSetting('form-name'))?array():$module->getProjectSetting('form-name')[$index];
$form_name_event = empty($module->getProjectSetting('form-name-event'))?array():$module->getProjectSetting('form-name-event')[$index];
$email_from = empty($module->getProjectSetting('email-from'))?array():$module->getProjectSetting('email-from')[$index];
$email_subject = empty($module->getProjectSetting('email-subject'))?array():$module->getProjectSetting('email-subject')[$index];
$email_text = empty($module->getProjectSetting('email-text'))?array():$module->getProjectSetting('email-text')[$index];
$datapipe_var = $module->getProjectSetting("datapipe_var", $project_id);
$data = \REDCap::getData($project_id);
if(empty($form_name_event)){
if(array_key_exists('repeat_instances',$data[$record])){
foreach ($data[$record]['repeat_instances'] as $event_id=>$value){
$form_name_event = $event_id;
break;
}
}else{
foreach ($data[$record] as $event_id=>$value){
$form_name_event = $event_id;
break;
}
}
}
$mail = new \PHPMailer;
#Email Addresses
$mail = $module->setEmailAddresses($mail, $project_id, $record, $form_name_event, $form_name, 1, $data, $index,\REDCap::isLongitudinal());
$email_to = "";
foreach ($mail->getToAddresses() as $address){
$email_to .= $address[0].", ";
}
$email_cc = "";
foreach ($mail->getCcAddresses() as $address){
$email_cc .= $address[0].", ";
}
$email_bcc = "";
foreach ($mail->getBccAddresses() as $address){
$email_bcc .= $address[0].", ";
}
$preview = "<table style='margin:0 auto;width:100%'><tr><td>From:</td><td>".preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '<a href="mailto:$1">$1</a>', $email_from)."</td></tr>";
$preview .= "<tr><td>To:</td><td>".str_replace(',',', ',preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '<a href="mailto:$1">$1</a>', rtrim($email_to,', ')))."</td></tr>";
if($email_cc != ''){
$preview = "<tr><td>CC:</td><td>".str_replace(',',', ',preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '<a href="mailto:$1">$1</a>', rtrim($email_cc,', ')))."</td></tr>";
}
if($email_bcc != ''){
$preview = "<tr><td>BCC:</td><td>".str_replace(',',', ',preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '<a href="mailto:$1">$1</a>', rtrim($email_bcc,', ')))."</td></tr>";
}
$email_text = $module->setDataPiping($datapipe_var, $email_text, $project_id, $data, $record, $form_name_event, $form_name, 1,\REDCap::isLongitudinal());
$email_subject = $module->setDataPiping($datapipe_var, $email_subject, $project_id, $data, $record, $form_name_event, $form_name, 1,\REDCap::isLongitudinal());
$preview .= "<tr><td>Subject:</td><td>".$email_subject."</td></tr>";
$preview .= "<tr><td>Message:</td><td>".$email_text."</td></tr></table>";
echo $preview;
?>