-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathREDCapREST.php
More file actions
1353 lines (1217 loc) · 66.9 KB
/
Copy pathREDCapREST.php
File metadata and controls
1353 lines (1217 loc) · 66.9 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* REDCap External Module: REDCap REST
* Send API calls when saving particular instruments when a trigger condition is met.
* @author Luke Stevens, Murdoch Children's Research Institute
*/
namespace MCRI\REDCapREST;
use ExternalModules\AbstractExternalModule;
require_once 'OAuth2.php';
require_once 'OAuth2ClientCredentials.php';
require_once 'Instruction.php';
require_once 'ModuleSettingsManager.php';
class REDCapREST extends AbstractExternalModule {
const MODULE_TITLE = "REDCap REST";
protected const DISPLAY_MAX_FIELD_MAP = 5;
protected const IMPORT_ACTION = 'import-instructions';
protected $configArray;
protected $Proj;
protected $record;
protected $event_id;
protected $instrument;
protected $instance;
protected $destURL;
protected $token;
protected $tokenRef;
protected $curlOpts;
protected $title;
function redcap_save_record($project_id, $record, $instrument, $event_id, $group_id, $survey_hash, $response_id, $repeat_instance=1) {
global $Proj;
$this->Proj = $Proj;
$this->title = self::MODULE_TITLE." external module";
$settings = $this->getSubSettings('message-config');
foreach($settings as $i => $instruction) {
if (!$instruction['message-enabled']) continue;
if (array_search($instrument, $instruction['trigger-form'])===false) continue;
if (!empty($instruction['trigger-logic']) && true!==\REDCap::evaluateLogic($instruction['trigger-logic'], $project_id, $record, $event_id, $repeat_instance)) continue;
$this->record = $record;
$this->event_id = $event_id;
$this->instrument = $instrument;
$this->instance = $repeat_instance;
$this->destURL = $this->pipe($instruction['dest-url']);
$this->token = '';
$this->tokenRef = '';
$method = $instruction['http-method'];
$contentType = $this->makeContentType($instruction['content-type']);
$curlHeaders = $this->makeCurlHeadersArray($instruction['curl-headers']);
$curlOptions = $this->makeCurlOptionsArray($instruction['curl-options']);
$resultField = $instruction['result-field'];
$resultCodeField = $instruction['result-http-code'];
$resultMap = $instruction['map-to-field'];
$resultMap = (is_array($resultMap)) ? $resultMap : array();
foreach ($resultMap as $i => $pair) {
// remove any incomplete field mappings
if (array_key_exists('prop-ref', $pair) && empty($pair['prop-ref'])) {
unset($resultMap[$i]);
} else if (array_key_exists('dest-field', $pair) && empty($pair['dest-field'])) {
unset($resultMap[$i]);
} else if (array_key_exists('dest-field', $pair) && !array_key_exists($pair['dest-field'], $this->Proj->metadata)) {
unset($resultMap[$i]);
}
}
reset($resultMap);
$payloadForLog = $instruction['payload'];
try {
$payload = $this->formatPayload($instruction['payload'], $contentType);
$payloadForLog = (empty($this->token)) ? $payload : str_replace($this->token, '|||Token '.$this->tokenRef.' removed|||', $payload);
} catch (\JsonException $je) {
\REDCap::logEvent($this->title, 'Error parsing payload JSON string: '.$je->getMessage().PHP_EOL.$payloadForLog, '', $this->record, $this->event_id);
return;
} catch (\Throwable $th) {
\REDCap::logEvent($this->title, $th->getMessage().PHP_EOL.$payloadForLog, '', $this->record, $this->event_id);
return;
}
switch ($instruction['oauth2-option']) {
case 'client-credentials':
// oauth2 connection required
try {
$oauth2 = new OAuth2ClientCredentials($this, $instruction, $i);
$oauth2->oauth2Call($method, $this->destURL, $contentType, $curlHeaders, $curlOptions, $payload);
$response = $oauth2->getResponse();
$info = $oauth2->getInfo();
} catch (\Throwable $th) {
$response = $th->getMessage();
$info = array('http_code'=>500);
}
break;
default:
// Make the call using simple curl
list($response, $info) = $this->curlCall($method, $this->destURL, $contentType, $curlHeaders, $curlOptions, $payload);
break;
}
\REDCap::logEvent($this->title, "Sent $method to {$this->destURL}:\n".$payloadForLog."\nResponse: ".$info['http_code']."\n".$response, '', $this->record, $this->event_id);
if (!empty($resultField) || !empty($resultCodeField) || count($resultMap)>0) {
if (!empty($resultField)) {
$this->saveValueToField($resultField, $response);
}
if (!empty($resultCodeField)) {
$this->saveValueToField($resultCodeField, $info['http_code']);
}
if (!empty($resultMap)) {
$responseArray = \json_decode($response, true);
if (!empty($responseArray)) {
foreach ($resultMap as $i => $pair) {
$responseValue = $this->extractResultFromResponse($responseArray, $pair['prop-ref']);
if ($responseValue!==null) $this->saveValueToField($pair['dest-field'], $responseValue);
}
}
}
}
}
}
/**
* pipe
* Perform piping on a string in the current record context
* @param string $string
* @return string
*/
protected function pipe($string, $contentType='') {
try {
$string = $this->pipeApiToken($string);
} catch (\Throwable $th) {
\REDCap::logEvent(self::MODULE_TITLE." external module", 'Error retrieving system token for API call: '.$th->getMessage(), '', $this->record, $this->event_id);
}
if ($contentType=='application/x-www-form-urlencoded') {
// need to urlencode piped strings for x-www-form-urlencoded
$encodedString = '';
$kvpairs = \explode('&', $string);
foreach ($kvpairs as $kvpair) {
list($k, $v) = \explode('=', $kvpair, 2);
$v = \urlencode($this->pipe($v));
$v = str_replace('%7B%7B', '{{', str_replace('%7D%7D', '}}', $v)); // reverse encoding of %7B%7B0%7D%7D => {{0}} (placeholders)
$encodedString .= "&$k=$v";
}
$pipedString = substr($encodedString, 1);
} else if ($contentType=='application/json') {
$pipedString = $string;
try {
$pipedString = $this->pipe($string);
$pipedString = str_replace('"______"','""',$pipedString); // empty string value e.g. { "a": "______" } -> { "a": "" }
$pipedString = preg_replace('/:\s*______/',':null',$pipedString); // empty non-string value e.g. { "b": ______ } -> { "b":null }
// difficult to guarantee valid json :
// - instruction payload config can be invalid if piping numeric data e.g. {"x":[someint]}
// and/or
// - string after piping can be invalid due to unescaped " characters
// e.g. { "a": "text "containing" quotes" } -> { "a": "text \"containing\" quotes" }
// ignoring for now...
$jsonDecodedPipedString = \json_decode($pipedString, null, 512, JSON_THROW_ON_ERROR);
$pipedString = \json_encode($jsonDecodedPipedString);
} catch (\JsonException $je) {
\REDCap::logEvent(self::MODULE_TITLE." external module", 'Error parsing payload JSON string: '.$je->getMessage().PHP_EOL.$pipedString, '', $this->record, $this->event_id);
}
} else {
$pipedString = \Piping::replaceVariablesInLabel(
$string, // $label='',
$this->record, // $record=null,
$this->event_id, // $event_id=null,
$this->instance, // $instance=1,
array(), // $record_data=array(),
true, // $replaceWithUnderlineIfMissing=true,
null, // $project_id=null,
false // $wrapValueInSpan=true
);
}
return $pipedString;
}
/**
* pipeApiToken
* Substiute reference to api token in form [token-ref:xyz] with real token according to system-level module settings
* (Helps avoid user api tokens being visible in project module settings)
* @param string
* @return string
*/
public function pipeApiToken($string) {
$found = false;
$matches = array();
$pattern = "/\[token-ref:([-\w]+)\]/";
if (!preg_match($pattern, $string, $matches)) return $string;
$systemTokens = $this->getSubSettings('token-management');
foreach ($systemTokens as $i => $systemToken) {
if ( array_key_exists(1, $matches) && $matches[1]==$systemToken['token-ref'] &&
starts_with($this->destURL, $systemToken['token-url']) ) {
$found = true;
break;
}
}
if (!$found) throw new \Exception('Token with reference "'.$matches[1].'" for destination URL "'.$this->destURL.'" not found in system-level token management.');
if ($systemToken['token-lookup-option']==='lookup') {
$sql = "select api_token from redcap_user_rights where project_id=? and username=? limit 1";
$q = $this->query($sql, [$systemToken['token-project'], $systemToken['token-username']]);
$r = db_fetch_assoc($q);
$this->token = $this->escape($r["api_token"]);
} else if ($systemToken['token-lookup-option']==='specify') {
$this->token = $this->escape($systemToken['token-specified']);
}
if (empty($this->token)) throw new \Exception('Could not read token with reference "'.$matches[1].'" in system-level token management.');
$this->tokenRef = $matches[1];
return str_replace($matches[0], $this->token, $string);
}
/**
* formatPayload
* @param string
* @return string
*/
protected function formatPayload($rawPayload='', $contentType='application/json') {
if ($rawPayload==='') return $rawPayload;
$payload = $rawPayload;
// find and replace sections to skip piping replacement and/or urlencoding e.g. filter logic expressions for REDCap record exports
$escapePiping = array();
$escape = preg_match_all('/({{[^}]+}})/', $payload, $escapePiping);
if ($escape && count($escapePiping) && count($escapePiping[0])) {
foreach($escapePiping[0] as $idx => $skip) {
$pos = strpos($payload, $skip);
$len = strlen($skip);
$payload = substr_replace($payload, '{{'.$idx.'}}', $pos, $len); // not str_replace() in case multiple of same match
}
}
$payload = $this->pipe($payload, $contentType);
// reinstate skipped content minus the {{ }} delimiters
if ($escape && count($escapePiping) && count($escapePiping[0])) {
foreach($escapePiping[0] as $idx => $skip) {
$payload = preg_replace("/\{\{$idx\}\}/", trim($skip, '{}'), $payload, 1); // not str_replace() in case multiple of same match
}
}
return $payload;
}
protected function findCurlOptInt($key) {
if (!isset($this->curlConstants)) {
// from https://stackoverflow.com/a/59650799/2286209
$constants = get_defined_constants(true);
$curlOptKeys = preg_grep('/^CURLOPT_/', array_keys($constants['curl']));
$this->curlOpts = array_intersect_key($constants['curl'], array_flip($curlOptKeys));
}
return (array_key_exists($key, $this->curlOpts))
? $this->curlOpts[$key] : false;
}
protected function makeContentType($instructionContentType='application/json') {
if (empty(trim($instructionContentType))) $instructionContentType = 'application/json';
return $instructionContentType;
}
protected function makeCurlHeadersArray($instructionCurlHeaders = '')
{
// Build a sanitized header list from the EM config, skipping hop-by-hop/problematic headers.
if (empty(trim($instructionCurlHeaders)))
return array();
$headers = array();
$lines = explode("\n", $instructionCurlHeaders);
// Never forward these explicitly; cURL/Apache will handle them.
$blocked = array('content-length', 'connection', 'transfer-encoding', 'expect');
foreach ($lines as $line) {
$line = trim($line);
if ($line === '')
continue;
// Piping (RedCap variables)
$line = $this->pipe($line);
// Normalize spaces and remove CR/LF
$line = str_replace(array("\r", "\n"), ' ', $line);
$line = preg_replace('/\s+/', ' ', $line);
// Split "Name: Value"
$parts = explode(':', $line, 2);
if (count($parts) !== 2)
continue;
$name = trim($parts[0]);
$value = trim($parts[1]);
if ($name === '')
continue;
if (in_array(strtolower($name), $blocked, true))
continue;
$headers[] = $name . ': ' . $value;
}
return $headers;
}
protected function makeCurlOptionsArray($instructionCurlOptions='') {
if (empty(trim($instructionCurlOptions))) return array();
$optionsArray = array();
$optionLines = explode('\n', $instructionCurlOptions);
foreach ($optionLines as $line) {
list($opt, $val) = explode('=', $line, 2);
$optIntVal = $this->findCurlOptInt($opt);
if ($optIntVal) $optionsArray[] = [$optIntVal, $this->pipe($val)];
}
return $optionsArray;
}
/**
* makeSaveArrayElement($response, $ref)
* Search response for an element with key matching $ref
* Return the (first) corresponding value
* @param string $field
* @param string $value
* @return string
*/
protected function makeSaveArrayElement($field, $value) {
$elem = array();
$elem[$this->Proj->table_pk] = $this->record;
if (\REDCap::isLongitudinal()) {
$elem['redcap_event_name'] = \REDCap::getEventNames(true, false, $this->event_id);
}
if ($this->Proj->isRepeatingEvent($this->event_id)) {
$elem['redcap_repeat_instrument'] = '';
$elem['redcap_repeat_instance'] = $this->instance;
} else if ($this->Proj->isRepeatingForm($this->event_id, $this->Proj->metadata[$field]['form_name'])) { // note this is the form of the field we're saving to, not the triggering form
$elem['redcap_repeat_instrument'] = $this->Proj->metadata[$field]['form_name'];
$elem['redcap_repeat_instance'] = $this->instance;
}
$elem[$field] = $value;
return $elem;
}
/**
* extractResultFromResponse($array, $ref)
* Search multidimensional for an element with key matching $ref
* Return the (last) value with the specified key
* @param array $array
* @param string $ref
* @return string
*/
protected function extractResultFromResponse($responseArray, $ref) {
$ref = (string)$ref;
$result= array($ref => null);
if (is_array($responseArray)) {
array_walk_recursive($responseArray,
function ($item, $key) use (&$result) {
if (array_key_exists($key, $result)) $result[$key] = (string)$item;
}
);
}
return $result[$ref];
}
protected function saveValueToField($field, $value) {
$saveArray = array($this->makeSaveArrayElement($field, $value));
$saveResult = \REDCap::saveData('json-array', $saveArray, 'overwrite'); // json_encode() not required for 'json-array' format
if (empty($saveResult['errors']) ) {
//\REDCap::logEvent($this->title, "Results saved \n".print_r($saveResult, true)."\nData:\n".print_r($saveArray, true), '', $this->record, $this->event_id);
} else {
\REDCap::logEvent($this->title, "Results save failed \n".print_r($saveResult, true)."\nData:\n".print_r($saveArray, true), '', $this->record, $this->event_id);
}
}
/**
* example
* Return the result requested
* @return string
*/
public function example() {
if (empty($_POST)) {
$content = file_get_contents("php://input");
$data = \json_decode($content, true); // not sure why $_POST is empty!
} else {
$data = $_POST;
}
\REDCap::logEvent(self::MODULE_TITLE." external module", "Test request:\n".print_r($this->escape($data), true));
$result = '';
if (is_array($data)) {
$result = (array_key_exists('result', $data)) ? $data['result'] : '';
} else if (is_object($data)) {
$result = (isset($data->result)) ? $data->result : '';
} else if (isset($data)) {
$result = $data;
}
return \REDCap::filterHtml(htmlspecialchars_decode($result));
}
/**
* call()
* Send request using curl
*/
public function curlCall(string $method, string $url, string $contentType, array $headers, array $curlOptions, string $payload) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, self::MODULE_TITLE);
if ($GLOBALS['is_development_server']) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
foreach ($curlOptions as $opt) {
curl_setopt($ch, $opt[0], $opt[1]);
}
switch ($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
break;
case 'PUT':
case 'PATCH':
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
break;
default: // GET
break;
}
$headers[] = "Content-Type: $contentType";
$headers[] = "Content-Length: ".strlen($payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$this->log('cURL info: '.json_encode($info)); // log response info useful for debugging responses
return array($response, $info);
}
/*
* redcap_module_configuration_settings()
* Triggered when the system or project configuration dialog is displayed for a given module.
* Allows dynamically modify and return the settings that will be displayed - here include a link to the instruction summary page
* @param string $project_id, $settings
*/
public function redcap_module_configuration_settings($project_id, $settings) {
if (!empty($project_id)) {
foreach ($settings as $si => $sarray) {
if ($sarray['key']=='summary-page') {
$url = $this->getUrl('summary.php',false,false);
$settings[$si]['name'] = str_replace('href="#"', 'href="'.$url.'"', $settings[$si]['name']);
break;
}
}
}
return $settings;
}
protected function getConfigArray() {
if (!isset($this->configArray)) {
$this->configArray = $this->getConfig();
}
return $this->configArray;
}
protected function getChoicesAsArray(array $settingsArray, $findSetting) {
$return = null;
foreach ($settingsArray as $setting => $settingAttrs) {
if (is_array($settingAttrs) && array_key_exists('key', $settingAttrs) && $settingAttrs['key']==$findSetting) {
if (array_key_exists('choices', $settingAttrs)) {
$return = array();
foreach ($settingAttrs['choices'] as $choice) {
$return[$choice['value']] = $choice['name'];
}
}
break;
} else if (is_array($settingAttrs)) {
$return = $this->getChoicesAsArray($settingAttrs, $findSetting);
}
if (is_array($return)) break;
}
return $return;
}
protected function getLabelForConfigChoice($setting, $value) {
$label = $value;
$choices = $this->getChoicesAsArray($this->getConfigArray(), $setting);
if (is_array($choices) && array_key_exists($value, $choices)) {
$label = $choices[$value];
}
return $label;
}
/**
* summaryPage()
* Content for summary page showing table of copy instruction configurations
*/
public function summaryPage() {
global $project_id;
// make the dropdown list of version history for export
$msm = new ModuleSettingsManager($this);
$instructionKeys = $this->getInstructionSettingKeys();
try {
$history = $msm->getFilteredSettingsHistory($instructionKeys);
} catch (\Throwable $th) {
//$this->log('Failed to read module settings history: '.$th->getMessage());
$history = array();
}
$versions = array();
if (empty($history)) {
$versions[0] = 'Current';
$cc = $this->getProjectSetting('message-config');
if (!is_null($cc)) $msm->saveCurrentSettingsToHistory(); // log current when this is the first time viewed after upgrade to module v1.6.0+
} else {
foreach ($history as $version) {
$id = (empty($versions)) ? 0 : $version['log_id']; // use id=0 for current version of settings
$lbl = substr($version['timestamp'], 0, 16).' ('.$version['username'].')';
$versions[$id] = $lbl;
}
}
$versionDropdown = \RCView::select(array('id'=>'module-version','name'=>'module-version'), $versions);
$instructions = $this->getSubSettings('message-config');
$columns = array(
array('title'=>'#','tdclass'=>'text-center','getter'=>function(array $instruction){ return '<span class="module-seq"></span>'; }),
array('title'=>'Description','tdclass'=>'text-center','getter'=>function(array $instruction){
$desc = (isset($instruction['instruction-description'])) ? $instruction['instruction-description'] : ''; // old configs may be missing instruction-description
if (is_null($desc) || trim($desc=='')) {
return '<i class="fa-solid fa-minus text-muted"></i>';
} else {
$desc = $this->escape(trim($desc));
$descDisplay = '<span class="m-0 text-left module-two-line-text" style="font-size:75%; max-width: 20ch;">'.str_replace("\n",' ',$desc).'</span>';
return '<span class="module-hidden">'.str_replace("\n",'<br>',$desc).'</span><button class="module-btn-show btn btn-xs btn-outline-primary" title="View full description">'.$descDisplay.'</button>';
}
}),
array('title'=>'Enabled','tdclass'=>'text-center','getter'=>function(array $instruction){
$enabledDesc = '<i class="fa-solid '.(($instruction['message-enabled']) ? 'fa-check text-success' : 'fa-times text-danger').'"></i>';
$messageInstruction = new Instruction($instruction);
$configErrors = $messageInstruction->getConfigErrors();
if (count($configErrors)) {
$errMsg = '<ul><li>'.implode('</li><li>', $configErrors).'</li></ul>';
$enabledDesc .= '<span class="module-hidden">'.$this->escape($errMsg).'</span><button class="module-btn-show btn btn-xs btn-outline-danger ml-2" title="Instruction Configuration Errors"><i class="fa-solid fa-exclamation-triangle mx-2"></i></button>';
}
$configWarnings = $messageInstruction->getConfigWarnings();
if (count($configWarnings)) {
$warnMsg = '<ul><li>'.implode('</li><li>', $configWarnings).'</li></ul>';
$enabledDesc .= '<span class="module-hidden">'.$this->escape($warnMsg).'</span><button class="module-btn-show btn btn-xs btn-outline-warning ml-2" title="Instruction Configuration Warnings"><i class="fa-solid fa-exclamation-triangle mx-2"></i></button>';
}
return $enabledDesc;
}),
array('title'=>'Trigger Form(s)','tdclass'=>'text-center','getter'=>function(array $instruction){
$formList = array();
foreach ($instruction['trigger-form'] as $form) {
$formList[] = "<span class='badge bg-primary'>$form</span>";
}
return implode('<br>', $formList);
}),
array('title'=>'Trigger Logic','tdclass'=>'text-center','getter'=>function(array $instruction){
$logic = $instruction['trigger-logic'];
if (is_null($logic) || trim($logic=='')) {
return '<i class="fa-solid fa-minus text-muted"></i>';
} else {
return '<span class="module-hidden"><pre>'.\htmlspecialchars($logic,ENT_QUOTES).'</pre></span><button class="module-btn-show btn btn-xs btn-outline-primary" title="View Trigger Logic"><i class="fa-solid fa-bolt mx-2"></i></button>';
}
}),
array('title'=>'Destination URL','tdclass'=>'text-center','getter'=>function(array $instruction){
$dest_url = \htmlspecialchars($instruction['dest-url'], ENT_QUOTES);
if (empty($dest_url)) {
return '<i class="fa-solid fa-minus text-danger"></i>';
} else {
list($path, $query) = explode('?', $dest_url);
$query = (is_null($query) || $query=='') ? '' : '<div class="text-left pl-2">?'.str_replace('&','<br> &',$query).'</div>';
return "<span class='badge bg-secondary'>$path $query</span>";
}
}),
array('title'=>'HTTP Method','tdclass'=>'text-center','getter'=>function(array $instruction){
if (empty($instruction['http-method'])) {
return '<i class="fa-solid fa-minus text-muted"></i>';
} else {
$val = $instruction['http-method'];
return '<span class="badge bg-primary">'.$this->getLabelForConfigChoice('http-method', $val).'</span>';
}
}),
array('title'=>'Message Payload','tdclass'=>'text-center','getter'=>function(array $instruction){
$payload = (isset($instruction['payload'])) ? $instruction['payload'] : '';
if (is_null($payload) || trim($payload=='')) {
return '<i class="fa-solid fa-minus text-muted"></i>';
} else {
$payload = $this->escape(trim($payload));
$payloadDisplay = '<span class="m-0 text-left module-two-line-text" style="font-size:75%; max-width: 20ch;">'.str_replace("\n",' ',$payload).'</span>';
$payloadDialogText = '';
$lines = explode("\n", $payload);
foreach ($lines as $l => $line) {
$payloadDialogText .= substr(" ".($l+1).". ", -5);
$chunks = str_split(htmlspecialchars_decode($line), 100);
$payloadDialogText .= htmlspecialchars(implode('<br> ↳ ', $chunks), ENT_QUOTES).'<br>';
}
return '<span class="module-hidden"><pre>'.rtrim($payloadDialogText).'</pre></span><button class="module-btn-show btn btn-xs btn-outline-primary" title="View full payload">'.$payloadDisplay.'</button>';
}
}),
array('title'=>'Content Type','tdclass'=>'text-center','getter'=>function(array $instruction){
$val = htmlspecialchars($instruction['content-type'], ENT_QUOTES);
if (empty($val)) {
$val = 'application/json (default)';
}
return "<span class='badge bg-primary'>$val</span>";
}),
array('title'=>'Additional Headers','tdclass'=>'text-center','getter'=>function(array $instruction){
$val = $instruction['curl-headers'];
if (empty($val)) {
return '<i class="fa-solid fa-minus text-muted"></i>';
}
$outTxt = '';
$vals = explode("\n", $val);
foreach ($vals as $hdr) {
$outTxt .= "<div class='badge bg-primary'>".\htmlspecialchars($hdr, ENT_QUOTES)."</div>";
}
return $outTxt;
}),
array('title'=>'cURL Options','tdclass'=>'text-center','getter'=>function(array $instruction){
$val = $instruction['curl-options'];
if (empty($val)) {
return '<i class="fa-solid fa-minus text-muted"></i>';
}
$outTxt = '';
$vals = explode("\n", $val);
foreach ($vals as $opt) {
$outTxt .= "<div class='badge bg-primary'>".\htmlspecialchars($opt, ENT_QUOTES)."</div>";
}
return $outTxt;
}),
array('title'=>'OAuth2 Option','tdclass'=>'text-center','getter'=>function(array $instruction){
if (empty($instruction['oauth2-option'])) {
return '<i class="fa-solid fa-minus text-muted"></i>';
} else {
$val = $instruction['oauth2-option'];
return '<span class="badge bg-secondary">'.$this->getLabelForConfigChoice('oauth2-option', $val).'</span>';
}
}),
array('title'=>'OAuth2 Configuration','tdclass'=>'text-center','getter'=>function(array $instruction){
$oauth2config = (isset($instruction['oauth2-config'])) ? $instruction['oauth2-config'] : ''; // old configs may be missing oauth2-config
if (is_null($oauth2config) || trim($oauth2config=='')) {
return '<i class="fa-solid fa-minus text-muted"></i>';
} else {
$oauth2config = $this->escape(trim($oauth2config));
$oauth2configDisplay = '<span class="m-0 text-left module-two-line-text" style="font-size:75%; max-width: 20ch;">'.str_replace("\n",' ',$oauth2config).'</span>';
$dialogText = '';
$lines = explode("\n", $oauth2config);
foreach ($lines as $l => $line) {
$dialogText .= substr(" ".($l+1).". ", -5);
$chunks = str_split(htmlspecialchars_decode($line), 100);
$dialogText .= htmlspecialchars(implode('<br> ↳ ', $chunks), ENT_QUOTES).'<br>';
}
return '<span class="module-hidden"><pre>'.rtrim($dialogText).'</pre></span><button class="module-btn-show btn btn-xs btn-outline-secondary" title="View full payload">'.$oauth2configDisplay.'</button>';
}
}),
array('title'=>'Save Response To','tdclass'=>'text-center','getter'=>function(array $instruction){
$val = \htmlspecialchars($instruction['result-field'], ENT_QUOTES);
if (empty($val)) {
return '<i class="fa-solid fa-minus text-muted"></i>';
}
return "<span class='badge bg-primary'>$val</span>";
}),
array('title'=>'Save Response Code To','tdclass'=>'text-center','getter'=>function(array $instruction){
$val = \htmlspecialchars($instruction['result-http-code'], ENT_QUOTES);
if (empty($val)) {
return '<i class="fa-solid fa-minus text-muted"></i>';
}
return "<span class='badge bg-primary'>$val</span>";
}),
array('title'=>'Response Data Mapping','tdclass'=>'module-field-map-col','getter'=>function(array $instruction){
$return = '';
$fieldList = array();
if (empty($instruction['map-to-field']) || (count($instruction['map-to-field'])===1 && is_null($instruction['map-to-field'][0]['prop-ref']))) {
return '<i class="fa-solid fa-minus text-muted"></i>';
}
foreach ($instruction['map-to-field'] as $pair) {
if (is_array($pair)) {
$p = "<span class='badge bg-secondary'>".\htmlspecialchars($pair['prop-ref'], ENT_QUOTES)."</span>";
$d = "<span class='badge bg-primary'>".\htmlspecialchars($pair['dest-field'], ENT_QUOTES)."</span>";
$fieldList[] = "<div class='nowrap my-1' style='display:flex;align-items:center;gap:2px;'>$p<i class='fa-solid fa-arrow-right-long text-muted'></i>$d</div>";
}
}
if (count($fieldList) > self::DISPLAY_MAX_FIELD_MAP) {
$firstN = array_slice($fieldList, 0, self::DISPLAY_MAX_FIELD_MAP);
$return = '<span class="module-hidden"><div class="module-field-map-dialog-content">';
for ($i=0; $i < count($fieldList); $i++) {
$return .= '<div class="my-1" style="display:flex;align-items:center;"><span class="module-field-map-index">'.($i+1).'.</span>'.$fieldList[$i].'</div>';
}
$return .= '</div></span>';
$return .= implode('', $firstN);
$return .= '<button type="button" class="module-btn-show btn btn-xs btn-outline-success mt-1" title="View All Field Mappings"><i class="fa-solid fa-eye mr-1"></i>+'.(count($fieldList)-self::DISPLAY_MAX_FIELD_MAP).'</button>';
} else {
$return = implode('', $fieldList);
}
return $return;
})
);
$lastExportType = \UIState::getUIStateValue($project_id, 'module-export', 'repeat-separator') ?? 'line';
switch ($lastExportType) {
case 'space': $spaceActive = 'active" aria-current="true"'; $lineActive = $pipeActive = ''; break;
case 'pipe': $pipeActive = 'active" aria-current="true"'; $lineActive = $spaceActive = ''; break;
default: $lineActive = 'active" aria-current="true"'; $spaceActive = $pipeActive = ''; break;
}
?>
<div class="projhdr"><i class="fa-solid fa-file-export mr-1"></i>REDCap REST: Summary of Instructions</div>
<div style="max-width: 1000px;">
<div id="module-intro-info">
<h6>Table of Instructions</h6>
<p>The table below shows the current configuration settings of copy instructions set up in this project.</p>
<p>You may download or upload instructions in CSV format using the "Export" and "Import" buttons on this page. A new version is included in the dropdown list each time the module settings are saved or altered settings or imported.</p>
</div>
<div id="module-import-export">
<h6>Export & Import <button id="module-btn-import-info" class="btn btn-xs btn-default fs18" type="button"><i class="fa-solid fa-circle-info"></i></button></h6>
<div id="module-import-export-controls">
<?= $versionDropdown ?>
<div class="btn-group">
<button name="module-btn-export-<?= $lastExportType ?>" class="module-btn-export btn btn-xs btn-primaryrc" type="button">
<i class="fa-solid fa-file-export"></i> <?= \RCView::tt('global_71') ?>
</button>
<button type="button" class="btn btn-xs btn-primaryrc dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><span class="dropdown-item-text">Repeating value separator</span></li>
<li><button name="module-btn-export-line" class="module-btn-export dropdown-item <?= $lineActive ?>" type="button"><i class="fa-solid fa-file-export"></i> <?= \RCView::tt('global_71') ?> (line)</button></li>
<li><button name="module-btn-export-space" class="module-btn-export dropdown-item <?= $spaceActive ?>" type="button"><i class="fa-solid fa-file-export"></i> <?= \RCView::tt('global_71') ?> (space)</button></li>
<li><button name="module-btn-export-pipe" class="module-btn-export dropdown-item <?= $pipeActive ?>" type="button"><i class="fa-solid fa-file-export"></i> <?= \RCView::tt('global_71') ?> (pipe)</button></li>
</ul>
</div>
<button id="module-btn-import" class="btn btn-xs btn-success mx-2"><i class="fa-solid fa-file-import"></i> <?= \RCView::tt('global_72') ?></button>
<button id="module-btn-edit" class="btn btn-xs btn-dark"><i class="fa-solid fa-pencil"></i> <?= \RCView::tt('econsent_43') //"Edit settings"?></button>
</div>
</div>
</div>
<div id="module-summary-table-container">
<table id="module-summary-table"><thead><tr>
<?php
foreach ($columns as $col) {
$class = (empty($col['tdclass'])) ? '' : ' class="'.$this->escape($col['tdclass']).'"';
echo "<th$class>".\REDCap::filterHtml($col['title']).'</th>';
}
echo '</tr></thead><tbody>';
foreach ($instructions as $instruction) {
echo '<tr>';
foreach ($columns as $col) {
$class = (empty($col['tdclass'])) ? '' : ' class="'.$col['tdclass'].'"';
$contentGetterFunction = $col['getter'];
$cellContent = call_user_func($contentGetterFunction, $instruction);
echo "<td $class>".\REDCap::filterHtml($cellContent).'</td>';
}
echo '</tr>';
}
echo '</tbody></table></div>';
$url = $this->getUrl('export_import.php', false, false);
$this->initializeJavascriptModuleObject();
?>
<div id="module-import-dialog-text" class="module-hidden">You may import REDCap REST instructions using a CSV file formatted congruently with how the instructions are exported from this page.</div>
<div id="module-import-file-container" class="module-hidden"></div>
<div id="module-import-info" class="module-hidden">
<p class="mt-0">Instructions may be exported and imported. Use the CSV delimiter character from your REDCap profile when importing.</p>
<p>Select the timestamp of the settings version to export. The first entry (default) is the latest version; the current settings.</p>
<p>Import files require the following <strong>sixteen columns</strong> to be present (although you may alter the title text). Values are required for some columns and option for others, as indicated:</p>
<ol>
<li><strong>Description (optional)</strong></li>
<li><strong>Enabled</strong> (required): Integer from the following list:
<ol start="0">
<li>Disabled</li>
<li>Enabled</li>
</ol>
</li>
<li><strong>Trigger form(s)</strong> (optional): A separated<sup>*</sup> list of form names.</li>
<li><strong>Trigger condition</strong> (optional): A REDCap logic expression.</li>
<li><strong>Destination URL</strong> (required): URL of endpoint where message will be sent.</li>
<li><strong>HTTP method</strong> (required): The desired HTTP verb: <code>POST</code> <code>GET</code> <code>PUT</code> <code>PATCH</code> <code>DELETE</code>.</li>
<li><strong>Payload</strong> (optional): Payload form e.g. as JSON (piping supported).</li>
<li><strong>Content type</strong> (optional): Content type for request, e.g. application/json (default), application/x-www-form-urlencoded.</li>
<li><strong>Additional headers</strong> (optional): Additional HTTP header content for request. One per line.</li>
<li><strong>cURL options</strong> (optional): cURL options for request. One per line.</li>
<li><strong>OAuth2 option</strong> (optional): Select "Client Credentials" to utilise OAuth2 for the API connection.</li>
<li><strong>OAuth2 configuration</strong> (required when OAuth2 option selected): JSON-format string of configuration information for the OAuth2 connection (piping supported).</li>
<li><strong>Response save field</strong> (optional): field in which to save the full request response.</li>
<li><strong>Response code save field</strong> (optional): field in which to save the HTTP code for the response.</li>
<li><strong>Field mapping - response property</strong> (repeating<sup>+</sup> - required): A separated<sup>*</sup> list of property names within the response.</li>
<li><strong>Field mapping - save to field</strong> (repeating<sup>+</sup> - required): A separated<sup>*</sup> list of fields response values will be saved to.</li>
</ol>
<p><sup>*</sup> "Separated list": repeating values can separated with any non-word character e.g. space, pipe, line-break. The import process will attempt to detect the separator character automatically.</p>
<p class="mb-0"><sup>+</sup> "Repeating": each of these mapping columns must have the same number of list entries.</p>
</div>
<style type="text/css">
h6 {
font-weight: bold;
}
#module-summary-table-container { max-width: 800px; }
#module-intro-info { margin: 1rem 0 1rem 0; }
#module-import-export {
max-width: 500px;
margin: 1rem 0 1rem 0;
}
#module-version { font-size: 85%; }
.module-hidden { display: none; }
.module-field-map-dialog-content { max-height: 500px; overflow-y: scroll; }
.module-field-map-index { display: inline-block; width: 35px; }
#module-summary-table .badge { font-weight: normal; padding: 3px 5px; }
.module-two-line-text {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
line-clamp: 2;
box-orient: vertical;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
height: 2.1em;
line-height: 1em;
}
</style>
<script type="text/javascript">
let module = <?=$this->getJavascriptModuleObjectName()?>;
module.exportUrl = '<?=$url?>&action=export';
module.importUrl = '<?=$url?>&action=import';
module.show = function() {
let title = $(this).attr('title');
let content = '<div style="overflow-wrap: break-word;">'+$(this).siblings('span:first').html()+'</div>';
simpleDialog(content, title, null, 810);
};
module.exportOk = function() {
window.location.href = module.exportUrl+'&sep='+module.export_sep+'&ver='+module.export_ver;
};
module.export = function() {
module.export_sep='line';
switch ($(this).attr('name')) {
case 'module-btn-export-space': module.export_sep='space'; break;
case 'module-btn-export-pipe': module.export_sep='pipe'; break;
default: break;
}
$('button.module-btn-export').removeClass('active');
$('button.module-btn-export').removeAttr('aria-current');
$(this).addClass('active');
$(this).attr('aria-current', 'true');
module.export_ver = $('select[name=module-version]').val();
let verLbl = $('select[name=module-version] option:selected').text();
let confText = 'Export copy instructions:<ul><li>Version: '+verLbl+'</li><li>Repeat value separator: '+module.export_sep+'</li></ul>';
simpleDialog(confText,'REDCap REST: Export CSV','module-export-dialog',350,null,'<?= \RCView::tt('global_53', false) ?>',
module.exportOk,
'<?= \RCView::tt('design_401', false) ?>'
);
};
// region import code inspired by method in FormDisplayLogicSetup.js
module.import_elements = {};
module.import_container = document.querySelector('#module-import-file-container');
module.import = function() {
simpleDialog($('#module-import-dialog-text').html(),'REDCap REST: Import CSV','module-import-dialog',650,null,'<?= \RCView::tt('global_53', false) ?>',
module.importFile,'<?= \RCView::tt('asi_006', false) ?>');
fitDialog($('#module-import-dialog'));
$('#module-import-dialog').dialog().next().find('button:last').addClass('ui-priority-primary').prepend('<img src="'+app_path_images+'xls.gif"> ');
};
module.submitImportFile = function() {
var data = new FormData(module.import_elements.uploadForm);
// can't use module.ajax() with file upload because payload gets put through JSON.stringify()
// module.ajax('< ?=static::IMPORT_ACTION?>', [data]).then(function(response) {
module.sendAjaxRequest('POST', data, { processData: false, contentType: false, })
.done(function(response){
if (response && response.result==1) {
simpleDialog('The CSV file was successfully imported. This page will now reload to reflect the changes.', 'REDCap REST: Instruction Import Successful', null, 700, 'window.location.reload();');
} else if (response && response.result==0) {
const errorList = '<ul>' + response.errors.map(item => `<li>${item}</li>`).join('') + '</ul>'
simpleDialog('<p class="my-0">The CSV file could not be imported. The following errors were encountered:</p>'+errorList, 'REDCap REST: Instruction Import Failed', null, 700);
} else {
console.log('response:');
console.log(response);
simpleDialog(woops);
}
}).fail(function(response){
console.log(response);
if (response.hasOwnProperty('message') && response.message == 'JSON.parse: unexpected character at line 1 column 1 of the JSON data') {
simpleDialog('File upload could not be processed. Refresh this page and retry.'); // usually csrf expired
} else {
simpleDialog(woops);
}
});
};
module.sendAjaxRequest = function(method, data, options = {}) {
data.redcap_csrf_token = get_csrf_token(); // add the csrf token
var dfd = $.Deferred();
var base_params = {
url: module.importUrl,
type: method,
data: data,
dataType: 'json',
};
var params = $.extend(base_params, options);
$.ajax(params)
.done( function( response, textStatus, jqXHR ) {
dfd.resolve(response);
}).fail( function( jqXHR, textStatus, errorThrown ) {
var response = {
status: "error",
//message: jqXHR.message
message: errorThrown
};
dfd.reject(response);
});
return dfd;
}
module.handleFileSelected = function(element) {
// upload the selected file
if(!!!element) return;
var self = this; // to maintain the scope inside the event listeners
// submit the upload form as a file is selected
element.addEventListener('change', function(e) {
e.preventDefault();
self.submitImportFile();
});
};
module.getFileInput = function() {
// create a file input element and register it's event handler
var fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
fileInput.setAttribute('name', 'files');
module.handleFileSelected(fileInput);
return fileInput;
};
module.createUploadForm = function() {
// create the upload form and add it to the container
var uploadForm = createUploadForm('module-import-form', module.getFileInput()); // function createUploadForm() in base.js
var action_input = document.createElement('input');
action_input.setAttribute('type', 'hidden');
action_input.setAttribute('name', 'ajax-action');
action_input.setAttribute('value', '<?= static::IMPORT_ACTION ?>');
uploadForm.appendChild(action_input);
module.import_elements.uploadForm = uploadForm;
module.import_container.appendChild(uploadForm);
};
module.importFile = function() {
// open the "select file" dialog box
if(!module.import_elements.uploadForm) module.createUploadForm();
var fileInput = module.import_elements.uploadForm.querySelector('input[type="file"]');
fileInput.click();
};
// end region import code
module.importInfo = function() {
simpleDialog(
$('#module-import-info').html(),
'REDCap REST: Instruction Import Information',
'module-import-info-dialog',850
);
};
module.editModuleSettings = function() {
window.location.href = app_path_webroot + 'ExternalModules/manager/project.php?pid='+pid+'&redcap_rest_config=1';
};
module.init = function() {
$('span.module-seq').each(function(i,e){ $(e).html(i+1) });
$('button.module-btn-show').on('click', module.show);
$('#module-summary-table').DataTable({ paging: false });