-
Notifications
You must be signed in to change notification settings - Fork 3
/
DatabaseSearch.php
376 lines (328 loc) · 14 KB
/
DatabaseSearch.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
<?php
require_once('vendor/autoload.php');
use airmoi\FileMaker\FileMaker;
use airmoi\FileMaker\FileMakerException;
use airmoi\FileMaker\Object\Layout;
use airmoi\FileMaker\Object\Result;
/**
* Class DatabaseSearch
* Represents a database the client is currently searching in.
*/
class DatabaseSearch {
/**
* The FileMaker instance connected to this database
* @var FileMaker
*/
private FileMaker $fileMaker;
/**
* The name of this database
* @var string
*/
private string $name;
private ?Layout $search_layout;
private ?Layout $result_layout;
private ?Layout $detail_layout;
/**
* @throws FileMakerException
*/
function __construct($fileMaker, $database) {
$this->fileMaker = $fileMaker;
$this->name = $database;
$this->setLayouts();
}
/**
* Creates a object from the database name
* @param string $databaseName
* @return DatabaseSearch|false
* @throws FileMakerException
*/
public static function fromDatabaseName(string $databaseName): bool|DatabaseSearch
{
list($FM_FILE, $FM_HOST, $FM_USER, $FM_PASS) = getDBCredentials($databaseName);
if (!$FM_PASS or !$FM_FILE or !$FM_HOST or !$FM_USER) {
return false;
}
$fileMaker = new FileMaker($FM_FILE, $FM_HOST, $FM_USER, $FM_PASS);
return new self($fileMaker, $databaseName);
}
function getFileMaker(): FileMaker
{
return $this->fileMaker;
}
function getName(): string
{
return $this->name;
}
function getSearchLayout(): Layout
{
return $this->search_layout;
}
function getResultLayout(): Layout
{
return $this->result_layout;
}
public function getDetailLayout(): Layout
{
return $this->detail_layout;
}
/**
* Searches all available layouts from FMP and sets the correct Search and
* Result layout for this DatabaseSearch.
*
* All databases have a search, results and details layout. However, the MIW and MI databases have two of each
* one for MI and one for MIW on the same database. Therefore the extra if statement.
* @throws FileMakerException
*/
private function setLayouts() {
# list of layout names!
$availableLayouts = $this->fileMaker->listLayouts();
foreach ($availableLayouts as $layoutName){
if ($this->name === 'mi' or $this->name === 'miw') {
if ($layoutName == 'search-'.strtoupper($this->name)) {
$this->search_layout = $this->fileMaker->getLayout($layoutName);
} else if ($layoutName == 'results-'.strtoupper($this->name)) {
$this->result_layout = $this->fileMaker->getLayout($layoutName);
} else if ($layoutName == 'details-'.strtoupper($this->name)) {
$this->detail_layout = $this->fileMaker->getLayout($layoutName);
}
} else {
if (str_contains($layoutName, 'search')) {
$this->search_layout = $this->fileMaker->getLayout($layoutName);
} else if (str_contains($layoutName, 'results')) {
$this->result_layout = $this->fileMaker->getLayout($layoutName);
} else if (str_contains($layoutName, 'details')) {
$this->detail_layout = $this->fileMaker->getLayout($layoutName);
}
}
}
}
/**
* Will query the FileMakerPro API for a result item. The query takes query fields,
* and also deals with:
* - data pagination with the pageNumber field
* - sorting the data with the sortType and sortQuery
*
* @param int $maxResponseAmount amount of responses to query for
* @param string[] $getFields query fields to use, must be not empty
* @param string $logicalOperator on of 'or' or 'and'
* @param string|null $sortQuery
* @param int $pageNumber used to calculate a multiplier of maxResponseAmount for pagination
* @param string|null $sortType one of ascend or descend
* @return Result Result or Error (Error if no entries for query too)
* @throws FileMakerException
*/
function queryForResults(int $maxResponseAmount, array $getFields, string $logicalOperator, ?string $sortQuery,
int $pageNumber, ?string $sortType): Result
{
// Find on all inputs with values
$findCommand = $this->fileMaker->newFindCommand($this->search_layout->getName());
$findCommand->setLogicalOperator(operator: strtolower($logicalOperator) == 'or' ? FileMaker::FIND_OR : FileMaker::FIND_AND);
/**
* TODO Fix the Fossils collection, searching is not working! Not even in deployed app.
*/
# The different strings used in FMP to mean the access number or ID
$accessionNumberOptions = ['Accession_Number', 'catalogNumber', 'Accession_No'];
# handle all regular search fields
foreach ($getFields as $fieldName => $fieldValue) {
$layoutField = str_replace("_", " ", $fieldName);
# handle image field
if ($fieldName == 'hasImage') {
$layoutField = 'Photographs::photoFileName' or 'Imaged'; # TODO fix this!
$findCommand->addFindCriterion(
fieldName: $layoutField,
value: $this->name == 'entomology' ? 'Photographed' : '*'
);
}
# handle accession number 'ID' field
else if (in_array($fieldName, $accessionNumberOptions)) {
switch ($this->name) {
case 'vwsp'; case 'bryophytes';
case 'fungi'; case 'lichen'; case 'algae':
$findCommand->addFindCriterion(
fieldName: is_numeric($fieldValue) ? "Accession Numerical" : "Accession Number",
value: $fieldValue
);
break;
case 'fossil'; case 'avian';
case 'herpetology'; case 'mammal':
$findCommand->addFindCriterion(
fieldName: is_numeric($fieldValue) ? "SortNum" : "catalogNumber",
value: $fieldValue
);
break;
case 'mi'; case 'miw':
$findCommand->addFindCriterion(
fieldName: is_numeric($fieldValue) ? "SortNum" : 'Accession No',
value: $fieldValue,
);
break;
case 'fish':
$findCommand->addFindCriterion(
fieldName: 'accessionNo',
value: $fieldValue,
);
break;
case 'entomology':
$findCommand->addFindCriterion(
fieldName: 'SEM #',
value: $fieldValue,
);
break;
}
}
# all other fields just go in as they come
else {
$findCommand->addFindCriterion($layoutField, $fieldValue);
}
}
# handle the sort property
if ($sortQuery and $sortQuery != '') {
# preliminary sort query, will only change in certain situations
$sortBy = $sortQuery;
# accession number sort is different for databases, handle it here
if (mapField($sortQuery) === 'Accession Number') {
if ($this->name == 'vwsp' or $this->name == 'bryophytes' or
$this->name == 'fungi' or $this->name == 'lichen' or $this->name == 'algae') {
$sortBy = 'Accession Numerical';
}
else {
$sortBy = 'sortNum';
}
}
# for entomology and fish we can only sort by accession number? TODO check this!
if($this->name == 'entomology') {
$sortBy = 'SEM #';
}
if($this->name == 'fish') {
$sortBy = 'accessionNo';
}
# handles the order of the sort
$findCommand->addSortRule(fieldName: str_replace('+', ' ', $sortBy), precedence: 1,
order: $sortType === 'Descend' ? FileMaker::SORT_DESCEND : FileMaker::SORT_ASCEND);
}
# handle different table pages
if ($pageNumber) {
$findCommand->setRange(skip: ($pageNumber - 1) * $maxResponseAmount, max: $maxResponseAmount);
}
return $findCommand->execute();
}
/**
* Echos a data table to show the results for this database search.
* @param Result $result
*/
function echoDataTable(Result $result) {
# filter out unnecessary fields
$ignoredFields = ['SortNum', 'Accession Numerical', 'Imaged', 'IIFRNo', 'Photographs::photoFileName',
'Event::eventDate', 'card01', 'Has Image', 'imaged'];
$usefulFields = array_diff($this->result_layout->listFields(), $ignoredFields);
# basic table setup with helper functions
echo '
<!-- construct table for given layout and fields -->
<div class="container-fluid no-padding">
<!-- id table for special color -->
<table class="table table-hover table-striped table-responsive" id="table">
<thead>
<tr>
';
$this->echoTableHeads($usefulFields);
echo '
</tr>
</thead>
<tbody>
';
$this->echoTableRows($result->getRecords(), $usefulFields);
echo '
</tbody>
</table>
</div>
';
}
/**
* Echos all rows for a table given the records and fields in use.
* @param $records
* @param $fields
*/
private function echoTableRows($records, $fields) {
foreach ($records as $record) {
# each record has its table row
echo '<tr>';
foreach ($fields as $field) {
# ID field logic
if (formatField($field) === 'Accession Number' or $field === 'SEM #') {
$url = htmlspecialchars($this->name). '&AccessionNo='.htmlspecialchars($record->getField($field));
$id = htmlspecialchars(trim($record->getField($field)));
$hasImage = false;
if($this->name === 'entomology' and $record->getField("Imaged") === "Photographed") $hasImage = true;
else if ($this->name === 'fish' and $record->getField("imaged") === "Yes") $hasImage = true;
else if ($this->name === 'mammal' or $this->name === 'avian' or $this->name === 'herpetology') {
if ($record->getField("Photographs::photoFileName") !== "") $hasImage = true;
}
# for vwsp lichen bryophytes fungi algae
else if ($record->getField("Imaged") === "Yes") $hasImage = true;
echo "
<th scope='row' id='data'>
<a href='details.php?Database=$url'>
" . ($hasImage ? '<span style="display:inline" id = "icon" class="oi oi-image"></span>' : '') . "
<b>$id</b>
</th>
";
}
# genus or species field for special style
else if (formatField($field) == 'Genus' || formatField($field) == 'Species') {
echo '<td id="data" style="font-style:italic;">' . htmlspecialchars($record->getField($field)) . '</td>';
}
else {
echo '<td id="data">'. $record->getField($field) . '</td>';
}
}
echo '</tr>';
}
}
/**
* Echos the table heads given the fields in use.
* Will also link the heads with sort urls.
* TODO remove all $_GET and $_SERVER calls
* @param $fields
*/
private function echoTableHeads($fields) {
$page = $_GET['Page'] ?? 1;
foreach($fields as $field) {
$id = htmlspecialchars(formatField($field));
$payloadList = [
'Database' => $this->name,
'Sort' => $field,
'SortOrder' => $_GET['SortOrder'] ?? 'Descend' == 'Descend' ? 'Ascend' : 'Descend',
'Page' => $page,
];
$href = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')) . '?' . http_build_query($payloadList);
$href = str_replace('%3A', ':', $href);
$icon_class = $_GET['SortOrder'] ?? '' === 'Descend' ? 'oi-sort-descending' : 'oi-sort-ascending';
echo "
<th scope='col' id=$id>
<a style='padding: 0; white-space:nowrap;' href=$href>
<!-- order icon -->
<span id='icon' class='oi $icon_class'></span>
<!-- field name -->
<b>$id</b>
</a>
</th>
";
}
}
/**
* Returns the ID or accession number field name, it is different for different databases.
* Optional $isNumeric field will change the name used for some databases.
* @param bool $isNumeric
* @return string
*/
function getIDFieldName(bool $isNumeric = false): string
{
return match ($this->name) {
'vwsp', 'bryophytes', 'fungi', 'lichen', 'algae' => $isNumeric ? 'Accession Numerical' : 'Accession Number',
'fossil', 'avian', 'herpetology', 'mammal' => $isNumeric ? 'SortNum' : 'catalogNumber',
'mi', 'miw' => $isNumeric ? 'SortNum' : 'Accession No',
'fish' => 'accessionNo',
'entomology' => 'SEM #',
};
}
}