forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDADocumentService.php
236 lines (212 loc) · 7.49 KB
/
CDADocumentService.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
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
<?php
/**
* Document Service for CCDA
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2021 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use CouchDB;
use OpenEMR\Common\Crypto\CryptoGen;
use OpenEMR\Common\Uuid\UuidRegistry;
use Symfony\Component\HttpClient\HttpClient;
/**
* Class CDADocumentService
*
* @package OpenEMR\Services
*
* See interface/modules/zend_modules/module/Carecoordination/src/Carecoordination/Controller/EncounterccdadispatchController.php
* indexAction() and interface/modules/zend_modules/public/index.php
*/
class CDADocumentService extends BaseService
{
const TABLE_NAME = "ccda";
protected $serverUrl;
public function __construct()
{
parent::__construct(self::TABLE_NAME);
UuidRegistry::createMissingUuidsForTables([self::TABLE_NAME]);
$this->serverUrl = $GLOBALS['qualified_site_addr'];
}
/**
* @param $pid
* @return array|false|null
*/
public function getLastCdaMeta($pid)
{
$query = "SELECT cc.uuid, cc.date, pd.fname, pd.lname, pd.pid FROM ccda AS cc
LEFT JOIN patient_data AS pd ON pd.pid=cc.pid
WHERE cc.pid = ?
ORDER BY cc.id DESC LIMIT 1";
return sqlQuery($query, array($pid));
}
/**
* @param $id
* @return false|string
*/
public function getFile($id)
{
$query = "select couch_docid, couch_revid, ccda_data, encrypted from ccda where uuid=?";
$row = sqlQuery($query, array($id));
$content = '';
if (!empty($row)) {
if (!empty($row['couch_docid'])) {
$couch = new CouchDB();
$resp = $couch->retrieve_doc($row['couch_docid']);
if ($row['encrypted']) {
$cryptoGen = new CryptoGen();
$content = $cryptoGen->decryptStandard($resp->data, null, 'database');
} else {
$content = base64_decode($resp->data);
}
} elseif (!empty($row['ccda_data'])) {
$fccda = fopen($row['ccda_data'], "r");
if ($row['encrypted']) {
$cryptoGen = new CryptoGen();
$content = $cryptoGen->decryptStandard(fread($fccda, filesize($row['ccda_data'])), null, 'database');
} else {
$content = fread($fccda, filesize($row['ccda_data']));
}
fclose($fccda);
}
}
return $content;
}
/**
* @param $pid
* @return string
*/
public function generateCCDHtml($pid): string
{
$url = $this->serverUrl . "/interface/modules/zend_modules/public/encounterccdadispatch";
$httpClient = HttpClient::create([
"verify_peer" => false,
"verify_host" => false
]);
$response = $httpClient->request('GET', $url, [
'query' => [
'combination' => $pid,
'recipient' => 'self',
'view' => '1',
'site' => $_SESSION ['site_id'],
'sent_by_app' => 'core_api',
'me' => session_id()
]
]);
$status = $response->getStatusCode(); // @todo validate
return $response->getContent();
}
/**
* @param $pid
* @return string
*/
public function generateCCDXml($pid): string
{
$url = $this->serverUrl . "/interface/modules/zend_modules/public/encounterccdadispatch";
$httpClient = HttpClient::create([
"verify_peer" => false,
"verify_host" => false
]);
$response = $httpClient->request('GET', $url, [
'query' => [
'combination' => $pid,
'recipient' => 'patient',
'view' => '0',
'hiehook' => '1',
'sent_by_app' => 'core_api',
'me' => session_id()
]
]);
$status = $response->getStatusCode(); // @todo validate
return $response->getContent();
}
/**
* @param $pid
* @return string
*/
public function portalGenerateCCD($pid): string
{
$url = $this->serverUrl . "/interface/modules/zend_modules/public/encounterccdadispatch";
$httpClient = HttpClient::create([
"verify_peer" => false,
"verify_host" => false
]);
$response = $httpClient->request('GET', $url, [
'query' => [
'combination' => $pid,
'recipient' => 'patient',
'view' => '1',
'me' => session_id(),// to authenticate in CCM. Portal only.
'site' => $_SESSION ['site_id']
]
]);
$status = $response->getStatusCode(); // @todo validate
return $response->getContent();
}
/**
* @param $pid
* @return string
*/
public function portalGenerateCCDZip($pid): string
{
$parameterArray = array(
'combination' => $pid,
'components' => 'allergies|medications|problems|immunizations|procedures|results|plan_of_care|vitals|social_history|encounters|functional_status|referral|instructions|medical_devices|goals',
'downloadccda' => 'download_ccda',
'latestccda' => '0',
'send_to' => 'download_all',
'sent_by_app' => 'portal',
'ccda_pid' => [0 => $pid],
'view' => 0,
'recipient' => 'patient',
'site' => $_SESSION ['site_id'],
);
$url = $this->serverUrl . "/interface/modules/zend_modules/public/encounterccdadispatch";
$httpClient = HttpClient::create([
"verify_peer" => false,
"verify_host" => false
]);
$response = $httpClient->request('POST', $url, [
'query' => ['me' => session_id()], // to authenticate in CCM. Portal only.
'body' => $parameterArray
]);
$status = $response->getStatusCode(); // @todo validate
return $response->getContent();
}
/**
* Complete zip of xml, html version
* when called within an openemr authorized session.
*
* @param $pid
* @return string
*/
public function generateCCDZip($pid): string
{
$parameterArray = array(
'combination' => $pid,
'components' => 'allergies|medications|problems|immunizations|procedures|results|plan_of_care|vitals|social_history|encounters|functional_status|referral|instructions|medical_devices|goals',
'downloadccda' => 'download_ccda',
'latestccda' => '0',
'send_to' => 'download_all',
'sent_by_app' => 'core_api',
'ccda_pid' => [0 => $pid],
'view' => 0,
'recipient' => 'self',
'site' => $_SESSION['site_id'],
);
$url = $this->serverUrl . "/interface/modules/zend_modules/public/encounterccdadispatch"; // add for debug ?XDEBUG_SESSION=PHPSTORM
$httpClient = HttpClient::create([
"verify_peer" => false,
"verify_host" => false
]);
$response = $httpClient->request('POST', $url, [
'query' => ['me' => session_id()],
'body' => $parameterArray
]);
$status = $response->getStatusCode(); // @todo validate
return $response->getContent();
}
}