forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationService.php
198 lines (179 loc) · 6.66 KB
/
LocationService.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
<?php
/**
* LocationService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Yash Bothra <yashrajbothra786gmail.com>
* @copyright Copyright (c) 2020 Yash Bothra <yashrajbothra786gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Common\Uuid\UuidMapping;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Validators\BaseValidator;
use OpenEMR\Validators\ProcessingResult;
class LocationService extends BaseService
{
private const PATIENT_TABLE = "patient_data";
private const PRACTITIONER_TABLE = "users";
private const FACILITY_TABLE = "facility";
const TYPE_FACILITY = "facility";
const TYPE_USER = "user";
const TYPE_PATIENT = "patient";
/**
* Default constructor.
*/
public function __construct()
{
UuidRegistry::createMissingUuidsForTables([self::PATIENT_TABLE, self::PRACTITIONER_TABLE, self::FACILITY_TABLE]);
UuidMapping::createMissingResourceUuids("Location", self::PATIENT_TABLE);
UuidMapping::createMissingResourceUuids("Location", self::PRACTITIONER_TABLE);
UuidMapping::createMissingResourceUuids("Location", self::FACILITY_TABLE);
}
public function getUuidFields(): array
{
return ['uuid'];
}
/**
* Returns a list of locations matching optional search criteria.
* Search criteria is conveyed by array where key = field/column name, value = field value.
* If no search criteria is provided, all records are returned.
*
* @param $search search array parameters
* @param $isAndCondition specifies if AND condition is used for multiple criteria. Defaults to true.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getAll($search = array(), $isAndCondition = true)
{
$sqlBindArray = array();
$sql = 'SELECT location.*, uuid_mapping.uuid FROM
(SELECT
uuid as table_uuid,
"Home Address" as name,
street,
city,
postal_code,
state,
country_code,
phone_cell as phone,
null as fax,
null as website,
email,
"' . self::TYPE_PATIENT . '" AS `type`
from
patient_data
UNION SELECT
uuid as table_uuid,
name,
street,
city,
postal_code,
state,
country_code,
phone,
fax,
website,
email,
"' . self::TYPE_FACILITY . '" AS `type`
from
facility
UNION SELECT
uuid as table_uuid,
"Home Address" as name,
street,
city,
zip as postal_code,
state,
null as country_code,
phone,
fax,
url as website,
email,
"' . self::TYPE_USER . '" AS `type`
from
users
) as location
LEFT JOIN uuid_mapping ON uuid_mapping.target_uuid=location.table_uuid AND uuid_mapping.resource="Location"';
$whereClause = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sql .= $whereClause->getFragment();
$sqlBindArray = $whereClause->getBoundValues();
$statementResults = QueryUtils::sqlStatementThrowException($sql, $sqlBindArray);
$processingResult = new ProcessingResult();
foreach ($statementResults as $result) {
$record = $this->createResultRecordFromDatabaseResult($result);
$processingResult->addData($record);
}
return $processingResult;
}
/**
* Returns a single location record by id.
* @param $uuid - The location uuid identifier in string format.
* @return ProcessingResult which contains validation messages, internal error messages, and the data
* payload.
*/
public function getOne($uuid)
{
$processingResult = new ProcessingResult();
$isValid = BaseValidator::validateId(
"uuid",
"uuid_mapping",
$uuid,
true
);
if ($isValid !== true) {
$validationMessages = [
'uuid' => ["invalid or nonexisting value" => " value " . $uuid]
];
$processingResult->setValidationMessages($validationMessages);
return $processingResult;
}
$sql = 'SELECT location.*, uuid_mapping.uuid FROM
(SELECT
uuid as target_uuid,
CONCAT(fname,"\'s Home") as name,
street,
city,
postal_code,
state,
country_code,
phone_cell as phone,
null as fax,
null as website,
email from patient_data
UNION SELECT
uuid as target_uuid,
name,
street,
city,
postal_code,
state,
country_code,
phone,
fax,
website,
email from facility
UNION SELECT
uuid as target_uuid,
CONCAT(fname,"\'s Home") as name,
street,
city,
zip as postal_code,
state,
null as country_code,
phone,
fax,
url as website,
email from users) as location
LEFT JOIN uuid_mapping ON uuid_mapping.target_uuid=location.target_uuid AND uuid_mapping.resource="Location"
WHERE uuid_mapping.uuid=?';
$uuidBinary = UuidRegistry::uuidToBytes($uuid);
$sqlResult = sqlQuery($sql, [$uuidBinary]);
$sqlResult['uuid'] = UuidRegistry::uuidToString($sqlResult['uuid']);
$processingResult->addData($sqlResult);
return $processingResult;
}
}