-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConceptSearchSourceCollection.inc.php
418 lines (394 loc) · 12 KB
/
ConceptSearchSourceCollection.inc.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
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
<?php
require_once(MCL_ROOT . 'ConceptSearchSource.inc.php');
class ConceptSearchSourceCollection
{
private $arr_source = array();
public function __construct() {
//do nothing
}
/**
* Add a ConceptSearchSource object or an array of ConceptSearchSource objects to the collection.
* @param mixed $s ConceptSearchSource object or array of ConceptSearchSource objects to add to the collection
*/
public function add($s)
{
if (is_array($s)) {
foreach ($s as $css) {
if ($css instanceof ConceptSearchSource) {
$this->arr_source[] = $css;
} else {
trigger_error('All elements of array parameter must be of type ConceptSearchSource in ConceptSearchSourceCollection::add($s)', E_USER_ERROR);
}
}
} else if ($s instanceof ConceptSearchSource) {
$this->arr_source[] = $s;
} else {
var_dump($s);
trigger_error('ConceptSearchSourceCollection::add($s) parameter must be of type ConceptSearchSource or array', E_USER_ERROR);
}
}
/**
* Tries to resolve an integer or string to a source identifier, which is either a source
* numeric ID or name. Matches first concept lists, then map sources, then dictionaries.
* Use $flags to prevent matching of a source type. Limit the map source search to specific
* dictionaries using the $arr_dict_filter. For example, a source identifier of
* 'HL-7 CVX' will be compared against all concept list names, then all map source names for
* all dictionaries (unless limited by $arr_dict_filter). Since it matches a map source, it
* will not search dictionary names. In a second example, a source identifier of the integer
* 1 matches a list, multiple map sources, and a dictionary, but will match the concept list
* because it is always matched first.
* @param mixed $source_identifier Integer or string identifier for a source
* @param array $arr_dict_filter Optional array of ConceptSearchSource objects to filter the map source and ditionary search
* @param int $flags Optional flags to control which source types are searched
* @return ConceptSearchSource
*/
function resolveSourceIdentifier($source_identifier, array $arr_dict_filter = null,
$flags = MCL_SOURCE_TYPE_ALL)
{
if ( ( $flags & MCL_SOURCE_TYPE_LIST ) &&
$css = $this->getConceptList($source_identifier) )
{
return $css;
}
if ( $flags & MCL_SOURCE_TYPE_MAP )
{
if ($arr_dict_filter) {
foreach ($arr_dict_filter as $css_dict) {
if ( $css = $this->getMapSource($source_identifier, $css_dict) ) {
return $css;
}
}
} else if ( $css = $this->getMapSource($source_identifier, null) ) {
return $css;
}
}
if ( ( $flags & MCL_SOURCE_TYPE_DICTIONARY ) &&
$css = $this->getDictionary($source_identifier) )
{
return $css;
}
return null;
}
/**
* Get an array of all ConceptSearchSource objects.
* @return Array of ConceptSearchSource objects
*/
public function getAllSources()
{
return $this->arr_source;
}
/**
* Get an array of ConceptSearchSource objects of type MCL_SOURCE_TYPE_DICTIONARY
* @return Array of ConceptSearchSource objects
*/
public function getDictionaries()
{
$arr_source = array();
foreach ($this->arr_source as $s)
{
if ($s->type == MCL_SOURCE_TYPE_DICTIONARY) $arr_source[] = $s;
}
return $arr_source;
}
/**
* Get an array of ConceptSearchSource objects of type MCL_SOURCE_TYPE_MAP that
* are members of the passed dictionary source.
* @param ConceptSearchSource $css_dict ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY
* @return Array of ConceptSearchSource objects
*/
public function getMapSources(ConceptSearchSource $css_dict = null)
{
$arr_source = array();
foreach ($this->arr_source as $s)
{
if ($s->type == MCL_SOURCE_TYPE_MAP &&
( !$css_dict || ($s->dict_id == $css_dict->dict_id) ) )
{
$arr_source[] = $s;
}
}
return $arr_source;
}
/**
* Get an array of ConceptSearchSource objects of type MCL_SOURCE_TYPE_LIST
* @return Array of ConceptSearchSource objects
*/
public function getConceptLists()
{
$arr_source = array();
foreach ($this->arr_source as $s)
{
if ($s->type == MCL_SOURCE_TYPE_LIST) $arr_source[] = $s;
}
return $arr_source;
}
/**
* Number of items in the collection
* @return int Number of items in the collection
*/
public function Count()
{
return count($this->arr_source);
}
/**
* Parse a source text and return a ConceptSearchSource object if exists.
* @param string $source_text
*/
public function parse($source_text)
{
$matches = array();
if ($source_text == '*')
{
$css = new ConceptSearchSource();
$css->setSourceAllDictionaries();
return $css;
}
elseif ( preg_match('/^(.+):map\((\d+)\)/', $source_text, $matches) )
{
$css = $this->getMapSource($matches[2], $matches[1]);
return $css;
}
elseif ( preg_match('/^list\((\d+)\)/', $source_text, $matches) )
{
$css = $this->getConceptList($matches[1]);
return $css;
}
else
{
$css = $this->getDictionary($source_text);
return $css;
}
return null;
}
/**
* Get an array of all sources that is formatted for an HTML select dropdown.
* @return array
*/
public function getHtmlSelectArray()
{
$arr_source_display = array();
$arr_source_display[] = array( 'value'=>'-' , 'display'=>'--- CONCEPT DICTIONARIES ---' );
$arr_source_display[] = array( 'value'=>'*' , 'display'=>'All Dictionaries' );
foreach ($this->getDictionaries() as $css_dict)
{
$arr_source_display[] = array('value'=>'-', 'display'=>'');
$_value = $css_dict->dict_db;
$_display = $css_dict->dict_name . ' (' . $css_dict->dict_db . ')';
$arr_source_display[] = array('value'=>$_value, 'display'=>$_display);
foreach ($this->getMapSources($css_dict) as $css_map) {
$_value = $css_map->dict_db . ':map(' . $css_map->map_source_id . ')';
$_display = ' - ' . $css_map->map_source_name;
$arr_source_display[] = array('value'=>$_value, 'display'=>$_display);
}
}
$arr_source_display[] = array('value'=>'-', 'display'=>'');
$arr_source_display[] = array('value'=>'-', 'display'=>'--- CONCEPT LISTS ---');
foreach ($this->getConceptLists() as $css_list) {
$_value = 'list(' . $css_list->list_id . ')';
$_display = $css_list->list_name;
$arr_source_display[] = array('value'=>$_value, 'display'=>$_display);
}
return $arr_source_display;
}
/**
* Returns a Html version of the collection for display (not coded/structured)
* @return string String representation of the collection object
*/
public function toHtml()
{
$s = '<br>Dictionaries and Map Sources<ul>';
foreach ($this->getDictionaries() as $css_dict) {
$s .= '<li>' . $css_dict->toString() . '<ul>';
foreach ($this->getMapSources($css_dict) as $css_map) {
$s .= '<li>' . $css_map->toString() . '</li>';
}
$s .= '</ul></li>';
}
$s .= '</ul><br>Concept Lists<ul>';
foreach ($this->getConceptLists() as $css_list) {
$s .= '<li>' . $css_list->toString() . '</li>';
}
$s .= '</ul>';
return $s;
}
/**
* Returns an array of all ConceptSearchSource objects of type MCL_SOURCE_TYPE_DICTIONARY
* referenced in this collection. This includes any dictionary sources and dictionary
* sources referenced by map sources and concept lists.
* @return array
*/
public function getUniqueDictionarySources()
{
$arr_css_dict = array();
foreach ($this->getDictionaries() as $css_dict) {
if (!isset($arr_css_dict[$css_dict->dict_db])) {
$arr_css_dict[$css_dict->dict_db] = $css_dict;
}
}
foreach ($this->getMapSources() as $css_map) {
$arr_map_dict = $css_map->getDictionarySources()->getAllSources();
foreach ($arr_map_dict as $css_dict) {
if (!isset($arr_css_dict[$css_dict->dict_db])) {
$arr_css_dict[$css_dict->dict_db] = $css_dict;
}
}
}
foreach ($this->getConceptLists() as $css_list) {
$arr_map_dict = $css_list->getDictionarySources()->getAllSources();
foreach ($arr_map_dict as $css_dict) {
if (!isset($arr_css_dict[$css_dict->dict_db])) {
$arr_css_dict[$css_dict->dict_db] = $css_dict;
}
}
}
return $arr_css_dict;
}
/**
* Whether or not this collections contains a source of type MCL_SOURCE_SEARCH_ALL
* @return bool
*/
public function containsAllSearch()
{
foreach ($this->arr_source as $css_dict) {
if ($css_dict->type == MCL_SOURCE_SEARCH_ALL) return true;
}
return false;
}
/**
* Get string describing the dictionaries referenced in this object.
*/
public function toString()
{
if ($this->containsAllSearch()) {
return '<b>All Dictionaries</b>';
}
$s = '';
$arr_css_dict = $this->getUniqueDictionarySources();
foreach ($arr_css_dict as $css_dict) {
if ($s) $s .= ', ';
$s .= '<b>' . $css_dict->dict_name . '</b>';
if ($css_dict->dict_last_updated) $s .= ' (last updated ' . $css_dict->dict_last_updated . ')';
}
return $s;
}
/**
* Get the ConceptSearchSource object of type MCL_SOURCE_TYPE_DICTIONARY that matches the passed source identifier.
* Identifier can be a map_source_id or map_source_name.
* @param string $source_identifier
* @return ConceptSearchSource
*/
public function getDictionary($source_identifier)
{
if ( $this->isInteger($source_identifier) )
{
$source_identifier = (int)$source_identifier;
foreach (array_keys($this->arr_source) as $key)
{
$css = $this->arr_source[$key];
if ( ( $css->type == MCL_SOURCE_TYPE_DICTIONARY ) &&
( $css->dict_id == $source_identifier ) )
{
return $css;
}
}
}
else
{
$source_identifier = strtolower($source_identifier);
foreach ($this->arr_source as $css)
{
if ($css->type == MCL_SOURCE_TYPE_DICTIONARY &&
strtolower($css->dict_db) == $source_identifier)
{
return $css;
}
}
}
return null;
}
/**
* Get the ConceptSearchSource object of type MCL_SOURCE_TYPE_MAP that matches the passed source identifier.
* Identifier can be a map_source_id or map_source_name. Optionally filter map sources by dictionary.
* @param string $source_identifier
* @param mixed $dict_db ConceptSearchSource of type MCL_SOURCE_TYPE_DICTIONARY or string of the database name
* @return ConceptSearchSource
*/
public function getMapSource($source_identifier, $css_dict = null)
{
// Set the dictionary name
if ($css_dict instanceof ConceptSearchSource) {
$dict_db = $css_dict->dict_db;
} else {
$dict_db = $css_dict;
}
// If source identifier is integer
if ( $this->isInteger($source_identifier) )
{
$source_identifier = (int)$source_identifier;
foreach ($this->arr_source as $css)
{
if ( $css->type == MCL_SOURCE_TYPE_MAP &&
( is_null($dict_db) || (!is_null($dict_db) && ($css->dict_db == $dict_db)) ) &&
$css->map_source_id == $source_identifier )
{
return $css;
}
}
}
else
{
$source_identifier = strtolower($source_identifier);
foreach ($this->arr_source as $css)
{
if ( $css->type == MCL_SOURCE_TYPE_MAP &&
( is_null($dict_db) || (!is_null($dict_db) && ($css->dict_db == $dict_db)) ) &&
strtolower($css->map_source_name) == $source_identifier )
{
return $css;
}
}
}
return null;
}
/**
* Get the ConceptSearchSource object of type MCL_SOURCE_TYPE_LIST that matches the passed source identifier.
* Identifier can be a list_id or list_name.
* @param string $source_identifier
* @return ConceptSearchSource
*/
public function getConceptList($source_identifier)
{
if ($this->isInteger($source_identifier)) {
$source_identifier = (int)$source_identifier;
foreach ($this->arr_source as $css)
{
if ($css->type == MCL_SOURCE_TYPE_LIST &&
$css->list_id == $source_identifier)
{
return $css;
}
}
}
else
{
$source_identifier = strtolower($source_identifier);
foreach ($this->arr_source as $css)
{
if ($css->type == MCL_SOURCE_TYPE_LIST &&
strtolower($css->list_name) == $source_identifier)
{
return $css;
}
}
}
return null;
}
private function isInteger($s)
{
if ( is_numeric($s) && ((int)$s) == $s ) {
return true;
}
return false;
}
}
?>