-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFunctions.php
375 lines (332 loc) · 11.5 KB
/
Functions.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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2022 webtrees development team
* <http://webtrees.net>
*
* RepositoryHierarchy (webtrees custom module):
* Copyright (C) 2022 Markus Hemprich
* <http://www.familienforschung-hemprich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Jefferson49\Webtrees\Module\RepositoryHierarchy;
use DOMDocument;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\Date\AbstractCalendarDate;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleListInterface;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Support\Collection;
use RuntimeException;
/**
* General functions and utils, which can be used by several classes
*/
class Functions extends \Jefferson49\Webtrees\Helpers\Functions
{
/**
* Write DOM document to a stream
*
* @param DOMDocument $dom
*
* @return resource
*/
public static function export(DOMDocument $dom)
{
$stream = fopen('php://memory', 'wb+');
if ($stream === false) {
throw new RuntimeException('Failed to create temporary stream');
}
//Write xml to stream
$bytes_written = fwrite($stream, $dom->saveXML());
if ($bytes_written !== strlen($dom->saveXML())) {
throw new RuntimeException('Unable to write to stream. Perhaps the disk is full?');
}
if (rewind($stream) === false) {
throw new RuntimeException('Cannot rewind temporary stream');
}
return $stream;
}
/**
* Get level 1 or 2 address tags for repositories in GEDCOM
*
* @param int $level
*
* @return array [string with address tags]
*/
public static function getGedcomAddressTags(int $level): array
{
switch($level) {
case 1:
return [
'REPO:ADDR',
'REPO:PHON',
'REPO:EMAIL',
'REPO:FAX',
'REPO:WWW',
];
case 2:
return [
'ADR1',
'ADR2',
'ADR3',
'CITY',
'STAE',
'POST',
'CTRY',
];
default:
return [];
}
}
/**
* Get address lines of a repository
*
* @param Repository $repository
*
* @return array [string with address line]
*/
public static function getRepositoryAddressLines(Repository $repository): array
{
$address_lines = [];
$level1_address_tags = self::getGedcomAddressTags(1);
$level2_address_tags = self::getGedcomAddressTags(2);
foreach ($repository->facts() as $fact) {
if ($fact->tag() === 'REPO:ADDR') {
if ($fact->value() !== '') {
$matches = preg_split('/\n/', $fact->value(), -1, PREG_SPLIT_NO_EMPTY);
$line = 1;
foreach ($matches as $match) {
$address_lines[$fact->tag() . ':LINE' . $line] = $match;
$line++;
}
}
foreach ($level2_address_tags as $tag) {
if ($fact->attribute($tag) !== '') {
$address_lines[$tag] = Registry::elementFactory()->make('REPO:ADDR:' . $tag)->label() . ': ' . $fact->attribute($tag);
}
}
} else {
if (in_array($fact->tag(), $level1_address_tags)) {
$address_lines[$fact->tag()] = $fact->label() . ': ' . $fact->value();
}
}
}
return $address_lines;
}
/**
* Get URL of a repository
*
* @param Repository $repository
*
* @return string
*/
public static function getRepositoryUrl(Repository $repository): string
{
foreach ($repository->facts() as $fact) {
if ($fact->tag() === 'REPO:WWW') {
if ($fact->value() !== '') {
$matches = preg_split('/\n/', $fact->value(), -1, PREG_SPLIT_NO_EMPTY);
return $matches[0];
}
}
}
return '';
}
/**
* Display a date range
*
* @param Date $date_range
* @param Tree $tree
* @param string $date format
*
* @return string
*/
public static function displayDateRange(Date $date_range = null, Tree $tree = null, string $date_format = null): string
{
if (($date_range !== null) && $date_range->isOK()) {
return $date_range->display($tree, $date_format);
} else {
return '';
}
}
/**
* Display a date range in ISO format
*
* @param Date $date_range
* @param string $delimiter [ISO 8601 allows: '/' or '--']
*
* @return string
*/
public static function displayISOformatForDateRange(Date $date_range = null, string $delimiter = '/'): string
{
if (($date_range !== null) && $date_range->isOK()) {
$min_date = $date_range->minimumDate();
$max_date = $date_range->maximumDate();
$date_range_text = $min_date->format('%Y-%m-%d') . $delimiter . $max_date->format('%Y-%m-%d');
$patterns = [
'/\A(\d+)\/\Z/', // 1659/
'/\A\/(\d+)\Z/', // /1659
'/\A(\d\d\d)\/(.*)/', // 873/*
'/\A(\d\d)\/(.*)/', // 87/*
'/\A(\d)\/(.*)/', // 7/*
'/\A(\d\d\d)-(.+?)\/(.*)/', // 873-*/*
'/\A(\d\d)-(.+?)\/(.*)/', // 87-*/*
'/\A(\d)-(.+?)\/(.*)/', // 7-*/*
'/(.*)\/(\d\d\d)\Z/', // */873
'/(.*)\/(\d\d)\Z/', // */87
'/(.*)\/(\d)\Z/', // */8
'/(.*)\/(\d\d\d)-(.+)/', // */873-
'/(.*)\/(\d\d)-(.+)/', // */87-
'/(.*)\/(\d)-(.+)/', // */8-
];
$replacements = [
'$1', // 1659/
'$1', // /1659
'0$1/$2', // 873/*
'00$1/$2', // 87/*
'000$1/$2', // 8/*
'0$1-$2/$3', // 873-*/*
'00$1-$2/$3', // 87-*/*
'000$1-$2/$3', // 8-*/*
'$1/0$2', // */873
'$1/00$2', // */87
'$1/000$2', // */8
'$1/0$2/$3', // */873-
'$1/00$2/$3', // */87-
'$1/000$2/$3', // */8-
];
return preg_replace($patterns, $replacements, $date_range_text);
}
return '';
}
/**
* Validate whether a string is an URL
*
* @param string $url
*
* @return bool
*/
public static function validateWhetherURL(string $url): bool
{
$path = parse_url($url, PHP_URL_PATH);
$encoded_path = array_map('urlencode', explode('/', $path));
$url = str_replace($path, implode('/', $encoded_path), $url);
return filter_var($url, FILTER_VALIDATE_URL) ? true : false;
}
/**
* Remove html tags
*
* @param string $text
*
* @return string
*/
public static function removeHtmlTags(string $text): string
{
return preg_replace('/<[a-z]+[^<>]+?>([^<>]+?)<\/[a-z]+?>/', '$1', $text);
}
/**
* Get overall date range for a set of date ranges,
* i.e. minimum and maximum dates of all the date ranges
*
* @param array $dates [Date]
*
* @return Date
*/
public static function getOverallDateRange(array $dates): ?Date
{
$dates_found = 0;
foreach ($dates as $date) {
$dates_found++;
//Calclulate new max/min values for date range if more than one date is found
if ($dates_found > 1) {
if (AbstractCalendarDate::compare($date->minimumDate(), $date_range->minimumDate()) < 1) {
$min_date = $date->minimumDate();
} else {
$min_date = $date_range->minimumDate();
}
if (AbstractCalendarDate::compare($date->maximumDate(), $date_range->maximumDate()) > 0) {
$max_date = $date->maximumDate();
} else {
$max_date = $date_range->maximumDate();
}
$date_range = new Date('FROM ' . $min_date->format('%A %O %E') . ' TO ' . $max_date->format('%A %O %E'));
} else {
$date_range = $date;
}
}
return ($dates_found > 0) ? $date_range : null;
}
/**
* Get collection for an array
*
* @param array $items
*
* @return Collection
*/
public static function getCollectionForArray(array $items): Collection
{
$collection = new Collection();
foreach ($items as $item) {
$collection->push($item);
}
return $collection;
}
/**
* Get xref of default repository
*
* @param AbstractModule $module
* @param Tree $tree
* @param UserInterface $user
*
* @return string
*/
public static function getDefaultRepositoryXref(AbstractModule $module, Tree $tree, UserInterface $user): string
{
Auth::checkComponentAccess($module, ModuleListInterface::class, $tree, $user);
$repositories = DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Repository::RECORD_TYPE)
->get()
->map(Registry::repositoryFactory()->mapper($tree))
->filter(GedcomRecord::accessFilter());
foreach ($repositories as $repository) {
return $repository->xref();
}
return '';
}
/**
* Get meta repositories for a repository
*
* @param Repository $repository
*
* @return string xref
*/
public static function getMetaRepository(Repository $repository): string
{
$meta_repository = '';
foreach ($repository->facts() as $fact) {
if (($fact->tag() === 'REPO:REFN') && ($fact->attribute('TYPE') === RepositoryHierarchy::SOUR_REFN_TYPE_META_REPO)) {
$meta_repository = $fact->value();
break;
}
}
return $meta_repository;
}
}