-
Notifications
You must be signed in to change notification settings - Fork 32
/
ExternalModule.php
144 lines (113 loc) · 4.39 KB
/
ExternalModule.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
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
<?php
/**
* @file
* Provides ExternalModule class for Image Map.
*/
namespace ImageMap\ExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use Form;
Use Stanford\Utility\ActionTagHelper;
/**
* ExternalModule class for Image Map.
*/
class ExternalModule extends AbstractExternalModule {
public $tag = "@IMAGEMAP";
/**
* @inheritdoc
*/
function redcap_every_page_top($project_id) {
if (PAGE == 'Design/online_designer.php' && $project_id) {
echo "<script>var imageMapEM = imageMapEM || {};</script>";
echo "<script>imageMapEM.helpUrl = " . json_encode($this->getUrl('documentation.php')) . ";</script>";
echo "<script>imageMapEM.maps = " . json_encode($this->getImageMapParams()) . ";</script>";
echo "
<style>
span.imagemap {
margin-right: 5px;
overflow-wrap: normal;
word-wrap: normal;
}
div.imagemap-container {
overflow-wrap: normal;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
</style>
";
$this->includeJs('js/helper.js');
}
if (!in_array(PAGE, array('DataEntry/index.php', 'surveys/index.php', 'Surveys/theme_view.php'))) {
return;
}
if (empty($_GET['id'])) {
return;
}
// Checking additional conditions for survey pages.
if (PAGE == 'surveys/index.php' && !(isset($_GET['s']) && defined('NOAUTH'))) {
return;
}
global $Proj;
$settings = array();
// Loop through action tags
$instrument = $_GET['page']; // This is a bit of a hack, but in surveys this is set before the every_page_top hook is called
//TODO: Consider switching over to ActionTagHelper to support single-param overrides in imagemap (such as hiding or showing the radio/checkboxes)
// Check action-tags for this page
foreach (array_keys($Proj->forms[$instrument]['fields']) as $field_name) {
$field_info = $Proj->metadata[$field_name];
if (!$display_mode = Form::getValueInActionTag($field_info['misc'], $this->tag)) {
continue;
}
// $row = $this->getDefaultConfig($display_mode);
$row = $this->getImageMapParams($display_mode);
if (empty($row)) {
// The specified imagemap is not defined
} else {
// Add the imagemap to the settings
$row['field'] = $field_name;
$dir = $this->getModulePath();
$b64 = base64_encode(file_get_contents($dir . $row['image']));
$src = "data:image/png;base64,$b64";
$row['src'] = $src;
$row['areas'] = file_get_contents($dir . $row['map']);
$row['type'] = $field_info['element_type'];
$settings[] = $row;
}
}
if (empty($settings)) {
return;
}
echo '<script>var imageMapEM = imageMapEM || {};</script>';
echo '<script>imageMapEM.settings = ' . json_encode($settings) . ';</script>';
$this->includeJs('js/imageMapster.js');
$this->includeJs('js/imagemap.js');
}
/**
* Includes a local JS file - uses the API endpoint if auth type is shib
*
* @param string $path
* The relative path to the js file.
*/
protected function includeJs($path) {
// Use noauth method, but not the API endpoint, to load resources while not in network
$ext_path = $this->getUrl($path, true, false);
echo '<script src="' . $ext_path . '"></script>';
}
/**
* Return the array of params for the specified imagemap (or all maps)
*
* @param $image_map
* @return mixed
*/
public function getImageMapParams($image_map = null) {
//TODO: Support having custom-maps defined via the EM config
$image_maps = $this->getConfig()['default-image-maps'];
if ($image_map !== null) {
return $image_maps[$image_map];
} else {
return $image_maps;
}
}
}