-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSharedPlacesModule.php
2030 lines (1630 loc) · 75.6 KB
/
SharedPlacesModule.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Cissee\Webtrees\Module\SharedPlaces;
use Cissee\Webtrees\Module\SharedPlaces\HelpTexts;
use Cissee\WebtreesExt\AbstractModule;
use Cissee\WebtreesExt\Elements\CustomLocationEvent;
use Cissee\WebtreesExt\Elements\LanguageIdReplacement;
use Cissee\WebtreesExt\Elements\XrefSharedPlace;
use Cissee\WebtreesExt\Factories\SharedPlaceFactory;
use Cissee\WebtreesExt\Functions\FunctionsPrintExt;
use Cissee\WebtreesExt\Http\Controllers\ModulePlaceHierarchyInterface;
use Cissee\WebtreesExt\Http\Controllers\PlaceHierarchyLink;
use Cissee\WebtreesExt\Http\Controllers\PlaceHierarchyParticipant;
use Cissee\WebtreesExt\Http\Controllers\PlaceUrls;
use Cissee\WebtreesExt\Http\Controllers\PlaceWithinHierarchy;
use Cissee\WebtreesExt\Http\RequestHandlers\AutoCompletePlaceExt;
use Cissee\WebtreesExt\Http\RequestHandlers\CreateSharedPlaceAction;
use Cissee\WebtreesExt\Http\RequestHandlers\CreateSharedPlaceModal;
use Cissee\WebtreesExt\Http\RequestHandlers\FunctionsPlaceProvidersAction;
use Cissee\WebtreesExt\Http\RequestHandlers\IndividualFactsTabExtenderProvidersAction;
use Cissee\WebtreesExt\Http\RequestHandlers\SharedPlacePage;
use Cissee\WebtreesExt\Http\RequestHandlers\TomSelectSharedPlace;
use Cissee\WebtreesExt\Module\ModuleMetaInterface;
use Cissee\WebtreesExt\Module\ModuleMetaTrait;
use Cissee\WebtreesExt\Module\ModuleVestalInterface;
use Cissee\WebtreesExt\Module\ModuleVestalTrait;
use Cissee\WebtreesExt\MoreI18N;
use Cissee\WebtreesExt\PlaceViaSharedPlace;
use Cissee\WebtreesExt\Requests;
use Cissee\WebtreesExt\Services\GedcomImportServiceExt;
use Cissee\WebtreesExt\Services\SearchServiceExt;
use Cissee\WebtreesExt\SharedPlace;
use Cissee\WebtreesExt\SharedPlacePreferences;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Elements\CustomElement;
use Fisharebest\Webtrees\Elements\GovIdType;
use Fisharebest\Webtrees\Elements\HierarchicalRelationship;
use Fisharebest\Webtrees\Elements\LocationRecord;
use Fisharebest\Webtrees\Elements\PlaceName;
use Fisharebest\Webtrees\Elements\UnknownElement;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Http\Middleware\AuthEditor;
use Fisharebest\Webtrees\Http\RequestHandlers\AutoCompletePlace;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateLocationAction;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateLocationModal;
use Fisharebest\Webtrees\Http\RequestHandlers\LocationPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Location;
use Fisharebest\Webtrees\Module\LocationListModule;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
use Fisharebest\Webtrees\Module\ModuleDataFixTrait;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
use Fisharebest\Webtrees\Module\ModuleInterface;
use Fisharebest\Webtrees\Module\ModuleListInterface;
use Fisharebest\Webtrees\Module\ModuleListTrait;
use Fisharebest\Webtrees\Place;
use Fisharebest\Webtrees\PlaceLocation;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\DataFixService;
use Fisharebest\Webtrees\Services\GedcomImportService;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Session;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\User;
use Fisharebest\Webtrees\Validator;
use Fisharebest\Webtrees\View;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Ramsey\Uuid\Uuid;
use Throwable;
use Vesta\CommonI18N;
use Vesta\Hook\HookInterfaces\ClippingsCartAddToCartInterface;
use Vesta\Hook\HookInterfaces\EmptyFunctionsPlace;
use Vesta\Hook\HookInterfaces\EmptyIndividualFactsTabExtender;
use Vesta\Hook\HookInterfaces\EmptyPrintFunctionsPlace;
use Vesta\Hook\HookInterfaces\FunctionsClippingsCartInterface;
use Vesta\Hook\HookInterfaces\FunctionsPlaceInterface;
use Vesta\Hook\HookInterfaces\FunctionsPlaceUtils;
use Vesta\Hook\HookInterfaces\GovIdEditControlsInterface;
use Vesta\Hook\HookInterfaces\GovIdEditControlsUtils;
use Vesta\Hook\HookInterfaces\IndividualFactsTabExtenderInterface;
use Vesta\Hook\HookInterfaces\IndividualFactsTabExtenderUtils;
use Vesta\Hook\HookInterfaces\PrintFunctionsPlaceInterface;
use Vesta\Model\GedcomDateInterval;
use Vesta\Model\GenericViewElement;
use Vesta\Model\GovReference;
use Vesta\Model\LocReference;
use Vesta\Model\MapCoordinates;
use Vesta\Model\PlaceStructure;
use Vesta\Model\Trace;
use Vesta\VestaAdminController;
use Vesta\VestaModuleTrait;
use function response;
use function route;
use function str_contains;
use function str_starts_with;
use function view;
//cannot use original AbstractModule because we override setName
class SharedPlacesModule extends AbstractModule implements
ModuleCustomInterface,
ModuleMetaInterface,
ModuleListInterface,
ModuleConfigInterface,
ModuleGlobalInterface,
ModuleDataFixInterface,
ModuleVestalInterface,
IndividualFactsTabExtenderInterface,
FunctionsPlaceInterface,
PrintFunctionsPlaceInterface,
FunctionsClippingsCartInterface,
PlaceHierarchyParticipant,
RequestHandlerInterface {
use ModuleCustomTrait, ModuleMetaTrait, ModuleListTrait, ModuleConfigTrait, ModuleGlobalTrait, VestaModuleTrait, ModuleDataFixTrait, ModuleVestalTrait {
VestaModuleTrait::customTranslations insteadof ModuleCustomTrait;
VestaModuleTrait::getAssetAction insteadof ModuleCustomTrait;
VestaModuleTrait::assetUrl insteadof ModuleCustomTrait;
VestaModuleTrait::getConfigLink insteadof ModuleConfigTrait;
ModuleMetaTrait::customModuleVersion insteadof ModuleCustomTrait;
ModuleMetaTrait::customModuleLatestVersion insteadof ModuleCustomTrait;
}
use SharedPlacesModuleTrait;
use EmptyIndividualFactsTabExtender;
use EmptyFunctionsPlace;
use EmptyPrintFunctionsPlace;
protected $module_service;
//list
protected const ROUTE_URL = '/tree/{tree}/shared-place-list';
public function __construct(
ModuleService $module_service) {
$this->module_service = $module_service;
}
public function customModuleAuthorName(): string {
return 'Richard Cissée';
}
public function customModuleSupportUrl(): string {
return 'https://cissee.de';
}
public function customModuleMetaDatasJson(): string {
return file_get_contents(__DIR__ . '/metadata.json');
}
public function customModuleLatestMetaDatasJsonUrl(): string {
return 'https://raw.githubusercontent.com/vesta-webtrees-2-custom-modules/vesta_shared_places/master/metadata.json';
}
public function resourcesFolder(): string {
return __DIR__ . '/resources/';
}
public function listTitle(): string {
return $this->getListTitle(I18N::translate("Shared places"));
}
public function listMenuClass(): string {
return 'menu-list-plac';
}
public function listUrl(Tree $tree, array $parameters = []): string {
$parameters['tree'] = $tree->name();
return route(SharedPlacesModule::class, $parameters);
}
public function handle(ServerRequestInterface $request): ResponseInterface {
$tree = Validator::attributes($request)->tree();
$user = Validator::attributes($request)->user();
Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
$link = null;
$useHierarchy = boolval($this->getPreference('USE_HIERARCHY', '1'));
$locationsToFix = $this->locationsToFixWrtHierarchy($tree);
$hasLocationsToFix = ($locationsToFix->count() > 0);
if ($useHierarchy && !$hasLocationsToFix) {
//link to place hierarchy
//Issue #28
//we're looking for the first ModuleListInterface that is also a ModulePlaceHierarchyInterface
//(and which the user is allowed to see)
//
//note: we cannot use
//->findByComponent(ModulePlaceHierarchyInterface::class, $tree, Auth::user())
//directly because the access level isn't set for this specific interface!
$module = \Vesta\VestaUtils::get(ModuleService::class)
->findByComponent(ModuleListInterface::class, $tree, Auth::user())
->first(static function (ModuleInterface $module): bool {
return $module instanceof ModulePlaceHierarchyInterface;
});
if ($module instanceof ModulePlaceHierarchyInterface) {
$url = $module->listUrl($tree, [
'place_id' => 0,
'tree' => $tree->name(),
'sharedPlaces' => 1,
]);
$link = new PlaceHierarchyLink(I18N::translate("View Shared places hierarchy"), null, $url);
} else {
//only show this to admins!
if (Auth::isAdmin()) {
//technically, the module may be enabled but its list component may be hidden from everybody,
//so this message is sometimes misleading
$link = new PlaceHierarchyLink(I18N::translate("Enable the Vesta Places and Pedigree map module to view the shared places hierarchy."), null, null);
}
}
}
$controller = new SharedPlacesListController($this, $hasLocationsToFix, $link);
$showLinkCounts = boolval($this->getPreference('LINK_COUNTS', '0'));
return $controller->sharedPlacesList($tree, $showLinkCounts);
}
/**
* Bootstrap the module
*/
public function onBoot(): void {
//extend
\Vesta\VestaUtils::set(GedcomImportService::class, new GedcomImportServiceExt());
//explicitly register in order to re-use in views where we cannot pass via variable
//(could also resolve via module service)
\Vesta\VestaUtils::set(SharedPlacesModule::class, $this);
$useHierarchy = boolval($this->getPreference('USE_HIERARCHY', '1'));
$useIndirectLinks = boolval($this->getPreference('INDIRECT_LINKS', '0'));
//TODO REVISIT ALL OF THIS
$addfactsStr = $this->getPreference('_LOC_FACTS_ADD', 'NAME,_LOC:TYPE,NOTE,SHARED_NOTE,SOUR,_LOC:_LOC');
$addfacts = FunctionsPrintExt::adjust(preg_split("/[, ]+/", $addfactsStr, -1, PREG_SPLIT_NO_EMPTY));
$uniquefactsStr = $this->getPreference('_LOC_FACTS_UNIQUE', 'MAP,_GOV');
$uniquefacts = FunctionsPrintExt::adjust(preg_split("/[, ]+/", $uniquefactsStr, -1, PREG_SPLIT_NO_EMPTY));
$requiredfactsStr = $this->getPreference('_LOC_FACTS_REQUIRED', '');
$requiredfacts = FunctionsPrintExt::adjust(preg_split("/[, ]+/", $requiredfactsStr, -1, PREG_SPLIT_NO_EMPTY));
$quickfactsStr = $this->getPreference('_LOC_FACTS_QUICK', 'NAME,_LOC:_LOC,MAP,NOTE,SHARED_NOTE,_GOV');
$quickfacts = FunctionsPrintExt::adjust(preg_split("/[, ]+/", $quickfactsStr, -1, PREG_SPLIT_NO_EMPTY));
$preferences = new SharedPlacePreferences(
$useHierarchy,
$useIndirectLinks,
$addfacts,
$uniquefacts,
$requiredfacts,
$quickfacts);
$sharedPlaceFactory = new SharedPlaceFactory($preferences);
Registry::locationFactory($sharedPlaceFactory);
//define our 'pretty' routes
//note: potentially problematic in case of name clashes;
//webtrees isn't interested in solving this properly, see
//https://www.webtrees.net/index.php/en/forum/2-open-discussion/33687-pretty-urls-in-2-x
/*
$router_container = \Vesta\VestaUtils::get(RouterContainer::class);
assert($router_container instanceof RouterContainer);
$router = $router_container->getMap();
*/
$router = Registry::routeFactory()->routeMap();
//(cf WebRoutes.php "Visitor routes with a tree")
//note: this format has the side effect of handling privacy properly (Issue #9)
//from 2.0.12, use standard name with specific handler!
//for this, we have to remove the original route, otherwise: RouteAlreadyExists (meh)
$existingRoutes = $router->getRoutes();
if (array_key_exists(LocationPage::class, $existingRoutes)) {
unset($existingRoutes[LocationPage::class]);
}
//while we're at it: webtrees 2.0.12 has CreateLocationModal and CreateLocationAction on the same urls
//as our CreateSharedPlaceModal and CreateSharedPlaceAction, we have to drop those!
if (array_key_exists(CreateLocationModal::class, $existingRoutes)) {
unset($existingRoutes[CreateLocationModal::class]);
}
if (array_key_exists(CreateLocationAction::class, $existingRoutes)) {
unset($existingRoutes[CreateLocationAction::class]);
}
if (array_key_exists(LocationListModule::class, $existingRoutes)) {
unset($existingRoutes[LocationListModule::class]);
}
//[2023/10] improve place autocomplete
if (array_key_exists(AutoCompletePlace::class, $existingRoutes)) {
unset($existingRoutes[AutoCompletePlace::class]);
}
$router->setRoutes($existingRoutes);
//[2023/10] improve place autocomplete
$router->get(AutoCompletePlace::class, '/tree/{tree}/autocomplete/place', \Vesta\VestaUtils::get(AutoCompletePlaceExt::class))
->extras(['middleware' => [AuthEditor::class]]);
$router->get(LocationPage::class, '/tree/{tree}/sharedPlace/{xref}{/slug}', SharedPlacePage::class);
$router->get(static::class, static::ROUTE_URL, $this);
//also redirect webtrees location-list, otherwise confusing to have to apparently similar lists
//(actual difference: create shared place button and other additions on top of actual list)
//(this makes our list menu entry somewhat redundant though)
//unset: see above
$router->get(LocationListModule::class, static::ROUTE_URL, $this);
// Replace an existing view with our own version.
// (referred to from media-page, from search results, etc)
View::registerCustomView('::lists/locations-table', $this->name() . '::lists/locations-table');
//plus adjustment for coordinates
View::registerCustomView('::edit/fact-location-edit', $this->name() . '::edit/fact-location-edit');
//adjustment for ajax-modal-vesta
View::registerCustomView('::edit/edit-fact', $this->name() . '::edit/edit-fact');
View::registerCustomView('::edit/link-spouse-to-individual', $this->name() . '::edit/link-spouse-to-individual');
View::registerCustomView('::edit/new-individual', $this->name() . '::edit/new-individual');
$createSharedPlaceModal = new CreateSharedPlaceModal($this);
$router->get(CreateSharedPlaceModal::class, '/tree/{tree}/create-location', $createSharedPlaceModal)
->extras(['middleware' => [AuthEditor::class]]);
$router->post(CreateSharedPlaceAction::class, '/tree/{tree}/create-location', CreateSharedPlaceAction::class)
->extras(['middleware' => [AuthEditor::class]]);
//TODO: cleanup - remove and/or integrate!
//(main difference is note in page.phtml wrt hasLocationsToFix, everything else was cosmetic and apparently obsolete!)
//(was maybe intended for separate shared places hierarchy list?)
//for GenericPlaceHierarchyController
//View::registerCustomView('::modules/generic-place-hierarchy-shared-places/place-hierarchy', $this->name() . '::modules/generic-place-hierarchy-shared-places/place-hierarchy');
//View::registerCustomView('::modules/generic-place-hierarchy-shared-places/list', $this->name() . '::modules/generic-place-hierarchy-shared-places/list');
//View::registerCustomView('::modules/generic-place-hierarchy-shared-places/page', $this->name() . '::modules/generic-place-hierarchy-shared-places/page');
//View::registerCustomView('::modules/generic-place-hierarchy-shared-places/sidebar', $this->name() . '::modules/generic-place-hierarchy-shared-places/sidebar');
////////////////////////////////////////////////////////////////////////////
// Location support, some of this overlaps with webtrees core now
//note that webtrees now (starting 2.0.16) also defines this view!
//still easier to use our view, but not everywhere (i.e. not when merging records)
//used via XrefSharedPlace
View::registerCustomView('::components/select-location-ext', $this->name() . '::components/select-location');
View::registerCustomView('::selects/location', $this->name() . '::selects/location');
$router->get(TomSelectSharedPlace::class, '/tree/{tree}/tom-select-shared-place', TomSelectSharedPlace::class);
////////////////////////////////////////////////////////////////////////////
/* I18N: translate just like 'Shared Place' for consistency */I18N::translate('Location');
/* I18N: translate just like 'Shared Places' for consistency */I18N::translate('Locations');
$ef = Registry::elementFactory();
/*
//make sure Gedcom-L tags are available even if the preference isn't checked
//
//this is no longer required - they are always available now!
//see Gedcom.php
//(but not always for editing, see customSubTags() in Gedcom.php)
*/
//but we need more (this partially overlaps with customSubTags() in Gedcom.php)
$ef->registerSubTags($this->customSubTags());
/*
foreach ($this->customSubTags() as $tag => $children) {
$element = $ef->make($tag);
foreach ($children as $child) {
$element->subtag(...$child);
}
}
*/
//for now, keep established terminology for specific tags
$ef->registerTags([
//redundant, we swap translation globally anyway!
'INDI:*:PLAC:_LOC' => new XrefSharedPlace(I18N::translate('Shared place')),
//redundant, we swap translation globally anyway!
'FAM:*:PLAC:_LOC' => new XrefSharedPlace(I18N::translate('Shared place')),
//redundant, we swap translation globally anyway!
'SOUR:DATA:EVEN:PLAC:_LOC' => new XrefSharedPlace(I18N::translate('Shared place')),
//redundant, we swap translation globally anyway!
'_LOC' => new LocationRecord(I18N::translate('Shared place')),
//fix #137
'_LOC:EVEN' => new CustomLocationEvent(MoreI18N::xlate('Event')),
//'Place' seems confusing here - if hierarchical shared places are used, this should be just one part of the place name
'_LOC:NAME' => new PlaceName(MoreI18N::xlate('Name'), ['ABBR' => '0:1', 'DATE' => '0:1', 'LANG' => '0:1', 'SOUR' => '0:M']),
'_LOC:_LOC' => new XrefSharedPlace(I18N::translate('Higher-level shared place'), ['DATE' => '0:1', 'SOUR' => '0:M', 'TYPE' => '0:1']),
//'_LOC:TYPE' => new CustomElement(I18N::translate('Type of location')), //anyway requires subtags!
'_LOC:_LOC:TYPE' => new HierarchicalRelationship(I18N::translate('Type of hierarchical relationship')),
'_LOC:NAME:LANG' => new LanguageIdReplacement(MoreI18N::xlate('Language')),
//these types of links are problematic because they mess up the shared place hierarchy (#140)
//but we have to support them at least this far, in oprder to avoid router errors
'_LOC:EVEN:PLAC:_LOC' => new XrefSharedPlace(I18N::translate('Location')),
]);
//register fallback in case Gov4Webtrees isn't active:
//(webtrees registers this as CustomElement via gedcomLTags(),
//even though there is something more specific in webtrees core code)
$element = $ef->make('_LOC:TYPE:_GOVTYPE');
if (($element instanceof CustomElement) || ($element instanceof UnknownElement)) {
$ef->registerTags([
'_LOC:TYPE:_GOVTYPE' => new GovIdType(I18N::translate('GOV id for type of location')),
]);
}
$this->flashWhatsNew('\Cissee\Webtrees\Module\SharedPlaces\WhatsNew', 4);
}
//this should probably be in Gedcom::gedcomLTags
/**
* @return array<string,array<int,array<int,string>>>
*/
protected function customSubTags(): array {
return [
'FAM:*:PLAC' => [['_LOC', '0:1']],
'INDI:*:PLAC' => [['_LOC', '0:1']],
'SOUR:DATA:EVEN:PLAC' => [['_LOC', '0:1']],
//the following are defined explicitly in ElementFactory, so we must address explicitly!
//(not enough to handle via wildcard)
'FAM:ENGA:PLAC' => [['_LOC', '0:1']],
'FAM:MARB:PLAC' => [['_LOC', '0:1']],
'FAM:MARR:PLAC' => [['_LOC', '0:1']],
'FAM:SLGS:PLAC' => [['_LOC', '0:1']],
'INDI:ADOP:PLAC' => [['_LOC', '0:1']],
'INDI:BAPL:PLAC' => [['_LOC', '0:1']],
'INDI:BAPM:PLAC' => [['_LOC', '0:1']],
'INDI:BARM:PLAC' => [['_LOC', '0:1']],
'INDI:BASM:PLAC' => [['_LOC', '0:1']],
'INDI:BIRT:PLAC' => [['_LOC', '0:1']],
'INDI:BLES:PLAC' => [['_LOC', '0:1']],
'INDI:BURI:PLAC' => [['_LOC', '0:1']],
'INDI:CENS:PLAC' => [['_LOC', '0:1']],
'INDI:CHR:PLAC' => [['_LOC', '0:1']],
'INDI:CONF:PLAC' => [['_LOC', '0:1']],
'INDI:CONL:PLAC' => [['_LOC', '0:1']],
'INDI:CREM:PLAC' => [['_LOC', '0:1']],
'INDI:DEAT:PLAC' => [['_LOC', '0:1']],
'INDI:EMIG:PLAC' => [['_LOC', '0:1']],
'INDI:ENDL:PLAC' => [['_LOC', '0:1']],
'INDI:EVEN:PLAC' => [['_LOC', '0:1']],
'INDI:FCOM:PLAC' => [['_LOC', '0:1']],
'INDI:IMMI:PLAC' => [['_LOC', '0:1']],
'INDI:NATU:PLAC' => [['_LOC', '0:1']],
'INDI:ORDN:PLAC' => [['_LOC', '0:1']],
'INDI:RESI:PLAC' => [['_LOC', '0:1']],
'INDI:SLGC:PLAC' => [['_LOC', '0:1']],
];
}
public function getHelpAction(ServerRequestInterface $request): ResponseInterface {
$topic = Requests::getString($request, 'topic');
return response(HelpTexts::helpText($topic));
}
//no longer required - css is static now
//public function assetsViaViews(): array {
// return [
// 'css/webtrees.css' => 'css/webtrees',
// 'css/minimal.css' => 'css/minimal'];
//}
//css for icons/shared-place
public function headContent(): string {
//easier to serve this globally, even if not strictly required on each page
//(we need the css in modified webtrees views, e.g. for media management)
//align with current theme (supporting the default webtrees themes, and specific custom themes)
$themeName = Session::get('theme');
if ('minimal' !== $themeName) {
if ('fab' === $themeName) {
//fab also uses font awesome icons
$themeName = 'minimal';
} else if ('_myartjaub_ruraltheme_' === $themeName) {
//and the custom 'rural' theme
$themeName = 'minimal';
} else if ('_jc-theme-justlight_' === $themeName) {
//and the custom 'JustLight' theme
$themeName = 'minimal';
} else {
//default
$themeName = 'webtrees';
}
}
$pre = '<link href="' . $this->assetUrl('css/'.$themeName.'.css') . '" type="text/css" rel="stylesheet" />';
return $pre;
}
public function bodyContent(): string {
return view($this->name() . '::js/webtreesExt');
}
public function hFactsTabGetStyleadds(
GedcomRecord $record,
Fact $fact): array {
//is it one of our facts?
foreach ($this->hFactsTabGetAdditionalFacts($record) as $additionalFact) {
if ($additionalFact->id() === $fact->id()) {
$styleadds = [];
$styleadds[] = 'wt-location-fact-pfh collapse'; //see hFactsTabGetOutputInDBox
return $styleadds;
}
}
return [];
}
public function hFactsTabGetAdditionalFacts(
GedcomRecord $record) {
if (!($record instanceof SharedPlace)) {
return [];
}
//use these regardless of any PLAC set there
//(which is anyway dubious and perhaps should be removed from the spec?)
return $record->facts(['FACT','EVEN']);
}
public function hFactsTabGetOutputInDBox(
GedcomRecord $record): GenericViewElement {
if (sizeof($this->hFactsTabGetAdditionalFacts($record)) === 0) {
return GenericViewElement::createEmpty();
}
$toggleable = true/*boolval($this->getPreference('TAB_TOGGLEABLE_LOC_FACTS', '1'))*/;
return $this->getOutputInDescriptionBox($toggleable, 'show-location-facts-factstab', 'wt-location-fact-pfh', I18N::translate('Shared place data'));
}
protected function getOutputInDescriptionBox(
bool $toggleable,
string $id,
string $targetClass,
string $label) {
ob_start();
if ($toggleable) {
?>
<label>
<input id="<?php echo $id; ?>" type="checkbox" data-bs-toggle="collapse" data-bs-target=".<?php echo $targetClass; ?>" data-wt-persist="<?php echo $targetClass; ?>" autocomplete="off">
<?php echo $label; ?>
</label>
<?php
}
return new GenericViewElement(ob_get_clean(), '');
}
public function hFactsTabGetOutputAfterTab(
GedcomRecord $record,
bool $ajax): GenericViewElement {
if (!$ajax) {
//nothing to do - in fact must not initialize twice!
return GenericViewElement::createEmpty();
}
if (sizeof($this->hFactsTabGetAdditionalFacts($record)) === 0) {
return GenericViewElement::createEmpty();
}
$toggleable = true/*boolval($this->getPreference('TAB_TOGGLEABLE_LOC_FACTS', '1'))*/;
return $this->getOutputAfterTab($toggleable, 'show-location-facts-factstab');
}
protected function getOutputAfterTab($toggleable, $toggle) {
$post = "";
if ($toggleable) {
$post = $this->getScript($toggle);
}
return new GenericViewElement('', $post);
}
protected function getScript($toggle) {
ob_start();
?>
<script>
webtrees.persistentToggle(document.querySelector('#<?php echo $toggle; ?>'));
</script>
<?php
return ob_get_clean();
}
public function hFactsTabRequiresModalVesta(Tree $tree): ?string {
//required via CreateSharedPlaceAction
$additionalControls = GovIdEditControlsUtils::accessibleModules($tree, Auth::user())
->map(function (GovIdEditControlsInterface $module) {
return $module->govIdEditControlSelectScriptSnippet();
})
->toArray();
return implode($additionalControls);
}
public function hFactsTabGetAdditionalEditControls(
Fact $fact): GenericViewElement {
if (!$fact->canEdit()) {
//not editable
return new GenericViewElement('', '');
}
if ($fact->attribute('PLAC') === '') {
//no PLAC, doesn't make sense to edit here
return new GenericViewElement('', '');
}
$useIndirectLinks = boolval($this->getPreference('INDIRECT_LINKS', '0'));
if (!$useIndirectLinks) {
//doesn't make sense to edit here
//(fact place must be linked explicitly to shared place anyway;
//we provide this functionality in fact place editor itself instead in this case)
return new GenericViewElement('', '');
}
//ok to edit - does a shared place with this name already exist? Or does the PLAC have an explicit _LOC link?
$ps = PlaceStructure::fromFact($fact);
if ($ps !== null) {
$sharedPlace = $this->plac2sharedPlace($ps);
} else {
//Issue #123
//unexpected, cf earlier check ($fact->attribute('PLAC') === '')
//anyway moving on
return new GenericViewElement('', '');
}
if ($sharedPlace !== null) {
//already exists
return new GenericViewElement('', '');
}
//we're using ajax-modal-vesta here
//because there may be modules with additional edit controls requiring this container
//
//this is somewhat hacky because we assume at the same time that these modules
//have initialized the container properly via hFactsTabGetOutputBeforeTab,
//which is strictly not enforced (in particular the modules aren't aware of the context of the edit control)
$html = view($this->name() . '::edit/icon-fact-create-shared-place', ['fact' => $fact, 'moduleName' => $this->name()]);
return new GenericViewElement($html, '');
}
protected static $seenSharedPlaces = [];
public function getLinkForSharedPlace(SharedPlace $sharedPlace): string {
return $this->linkIcon(
$this->name() . '::icons/shared-place',
I18N::translate('Shared place'),
$sharedPlace->url());
}
public function linkIcon($view, $title, $url) {
return '<a href="' . $url . '" rel="nofollow" title="' . $title . '">' .
view($view) .
'<span class="visually-hidden">' . $title . '</span>' .
'</a>';
}
////////////////////////////////////////////////////////////////////////////
public function placename2sharedPlaceImpl(
string $placeGedcomName,
Tree $tree): ?SharedPlace {
return $this->placename2sharedPlacesImpl($placeGedcomName, $tree)->first();
}
public function placename2sharedPlacesImpl(
string $placeGedcomName,
Tree $tree): Collection {
if ($placeGedcomName === '') {
return new Collection();
}
$searchService = \Vesta\VestaUtils::get(SearchServiceExt::class);
return $searchService->searchLocationsInPlace(new Place($placeGedcomName, $tree));
}
protected function placename2sharedPlace(
string $placeGedcomName,
Tree $tree): ?SharedPlace {
if ($placeGedcomName === '') {
return null;
}
$parentLevels = intval($this->getPreference('INDIRECT_LINKS_PARENT_LEVELS', 0));
return $this->placename2sharedPlacePL($placeGedcomName, $tree, $parentLevels);
}
protected function placename2sharedPlacePL(
string $placeGedcomName,
Tree $tree,
int $parentLevels): ?SharedPlace {
$match = $this->placename2sharedPlaceImpl($placeGedcomName, $tree);
if (($match === null) && ($parentLevels > 0)) {
$parts = SharedPlace::placeNameParts($placeGedcomName);
$tail = SharedPlace::placeNamePartsTail($parts);
return $this->placename2sharedPlacePL($tail, $tree, $parentLevels-1);
}
return $match;
}
protected function plac2sharedPlace(PlaceStructure $ps): ?SharedPlace {
$loc = $ps->getLoc();
if ($loc !== null) {
return Registry::gedcomRecordFactory()->make($loc, $ps->getTree());
}
$indirect = boolval($this->getPreference('INDIRECT_LINKS', '0'));
if ($indirect) {
return $this->placename2sharedPlace($ps->getGedcomName(), $ps->getTree());
}
return null;
}
public function plac2loc(PlaceStructure $ps): ?LocReference {
$loc = $ps->getLoc();
if ($loc !== null) {
$trace = new Trace('shared place via Shared Places module (gedcom _LOC tag)');
return new LocReference($loc, $ps->getTree(), $trace, $ps->getLevel());
}
$indirect = boolval($this->getPreference('INDIRECT_LINKS', '0'));
if ($indirect) {
$sharedPlace = $this->placename2sharedPlace($ps->getGedcomName(), $ps->getTree());
if ($sharedPlace !== null) {
$trace = new Trace('shared place via Shared Places module (mapping via place name)');
return new LocReference($sharedPlace->xref(), $sharedPlace->tree(), $trace, $ps->getLevel());
}
}
return null;
}
public function loc2gov(LocReference $loc): ?GovReference {
$sharedPlace = Registry::gedcomRecordFactory()->make($loc->getXref(), $loc->getTree());
if (($sharedPlace !== null) && ($sharedPlace instanceof SharedPlace)) {
$gov = $sharedPlace->getGov();
if ($gov !== null) {
$trace = $loc->getTrace();
$trace->add('GOV-Id via Shared Places module (gedcom _GOV tag)');
return new GovReference($gov, $trace, $loc->getLevel());
}
}
return null;
}
public function gov2loc(GovReference $gov, Tree $tree): ?LocReference {
$searchService = \Vesta\VestaUtils::get(SearchServiceExt::class);
$sharedPlaces = $searchService->searchLocationsEOL(array($tree), array("1 _GOV " . $gov->getId()));
foreach ($sharedPlaces as $sharedPlace) {
//first match wins
$trace = $gov->getTrace();
$trace->add('Location via Shared Places module');
return new LocReference($sharedPlace->xref(), $tree, $trace, $gov->getLevel());
}
return null;
}
public function loc2map(LocReference $loc): ?MapCoordinates {
$sharedPlace = Registry::gedcomRecordFactory()->make($loc->getXref(), $loc->getTree());
if ($sharedPlace !== null) {
$lati = $sharedPlace->getLati();
$long = $sharedPlace->getLong();
if (($lati !== null) && ($long !== null)) {
$trace = $loc->getTrace();
$trace->add('map coordinates via Shared Places module (gedcom MAP tag)');
return new MapCoordinates($lati, $long, $trace);
}
}
return null;
}
public function loc2plac(LocReference $loc): ?PlaceStructure {
$sharedPlace = Registry::gedcomRecordFactory()->make($loc->getXref(), $loc->getTree());
if ($sharedPlace !== null) {
if (!empty($sharedPlace->namesNN())) {
$ps = PlaceStructure::fromNameAndLoc($sharedPlace->primaryPlace()->gedcomName(), $sharedPlace->xref(), $sharedPlace->tree(), $loc->getLevel(), $sharedPlace);
if ($ps !== null) {
return $ps;
}
}
}
return null;
}
public function loc2placAt(LocReference $loc, GedcomDateInterval $date): ?PlaceStructure {
/** @var SharedPlace $sharedPlace */
$sharedPlace = Registry::gedcomRecordFactory()->make($loc->getXref(), $loc->getTree());
if ($sharedPlace !== null) {
return PlaceStructure::fromPlace($sharedPlace->primaryPlaceAt($date));
}
return null;
}
public function loc2linkIcon(LocReference $loc): ?string {
$sharedPlace = Registry::gedcomRecordFactory()->make($loc->getXref(), $loc->getTree());
if ($sharedPlace !== null) {
return $this->getLinkForSharedPlace($sharedPlace);
}
return null;
}
public function locPloc(
LocReference $locReference,
GedcomDateInterval $dateInterval,
Collection $typesOfLocation,
int $maxLevels = PHP_INT_MAX): Collection {
$sharedPlace = Registry::gedcomRecordFactory()->make($locReference->getXref(), $locReference->getTree());
$currentLevel = 0;
$trace = $locReference->getTrace();
$trace->add('Shared Place via Shared Places module (hierarchy)');
$ret = new Collection();
if ($sharedPlace !== null) {
foreach ($sharedPlace->getParents() as $parent) {
//TODO check type of location via _GOVTYPE
//TODO check dateInterval
//TODO use maxLevels for transitive parents
$ret->add(new LocReference($parent->xref(), $parent->tree(), $trace, $currentLevel));
}
}
return $ret;
}
public function factPlaceAdditionsBeforePlace(PlaceStructure $place): ?string {
$sharedPlace = $this->plac2sharedPlace($place);
if ($sharedPlace === null) {
return null;
}
return $this->getLinkForSharedPlace($sharedPlace);
}
public function factPlaceAdditionsAfterMap(PlaceStructure $place): ?string {
return null;
}
public function factPlaceAdditionsAfterNotes(PlaceStructure $place): ?string {
//would be cleaner to use plac2loc here - in practice same result
$sharedPlace = $this->plac2sharedPlace($place);
if ($sharedPlace === null) {
return null;
}
$html = '';
//restrict to specific events?
$restricted = boolval($this->getPreference('RESTRICTED', '0'));
if ($restricted) {
$restricted_indi = $this->getPreference('RESTRICTED_INDI', 'BIRT,MARR,OCCU,RESI,DEAT');
$restrictedTo = preg_split("/[, ;:]+/", $restricted_indi, -1, PREG_SPLIT_NO_EMPTY);
if (!in_array($place->getEventType(), $restrictedTo, true)) {
$restricted_fam = $this->getPreference('RESTRICTED_FAM', 'MARR');
$restrictedTo = preg_split("/[, ;:]+/", $restricted_fam, -1, PREG_SPLIT_NO_EMPTY);
if (!in_array($place->getEventType(), $restrictedTo, true)) {
return null;
}
}
}
//add name if different from PLAC name
$nameAt = $sharedPlace->primaryPlaceAt($place->getEventDateInterval())->gedcomName();
if ($nameAt !== $place->getGedcomName()) {
$html .= '<div class="ps-4 indent">';
$html .= $nameAt;
$html .= '</div>';
}
//add all (level 1) notes
if (preg_match('/1 NOTE (.*)/', $sharedPlace->gedcom(), $match)) {
//note may be restricted - in which case, do not add wrapper
//(and ultimately perhaps do not add entire 'shared place data', in case there is nothing else to display)
$note = view($this->name() . '::fact-notes-shared-place', ['fact' => $sharedPlace]);
if ($note !== '') {
$html .= '<div class="ps-4 indent">';
$html .= $note;
//$html .= '<br>';
$html .= '</div>';
}
}
//add all (level 1) media
if (preg_match_all("/1 OBJE @(.*)@/", $sharedPlace->gedcom(), $match)) {
$media = view($this->name() . '::fact-media-shared-place', ['fact' => $sharedPlace]);
if ($media !== '') {
$html .= '<div class="ps-4 indent">';
$html .= $media;
$html .= '<br class="media-separator" style="clear:both;">'; //otherwise layout issues wrt following elements, TODO handle differently!
$html .= '</div>';
}
}
//add all (level 1) sources
if (preg_match_all("/1 SOUR @(.*)@/", $sharedPlace->gedcom(), $match)) {
$sources = view($this->name() . '::fact-sources-shared-place', ['fact' => $sharedPlace]);
if ($sources !== '') {
$html .= '<div class="ps-4 indent">';
$html .= $sources;
$html .= '<br class="media-separator" style="clear:both;">'; //otherwise layout issues wrt following elements, TODO handle differently!
$html .= '</div>';
}
}
if ($html == '') {
return null;
}
//wrap in order to make expandable/collapsible
$expandSetting = $this->getPreference('EXPAND', '1');
if ($expandSetting == '0') {
$expanded = false;
} else if ($expandSetting == '1') {
if (in_array($sharedPlace->xref(), SharedPlacesModule::$seenSharedPlaces)) {
$expanded = false;
} else {
$expanded = true;
}
SharedPlacesModule::$seenSharedPlaces[] = $sharedPlace->xref();
} else {
$expanded = true;
}
$id = 'collapse-' . Uuid::uuid4()->toString();
$data = ''; //#170 remove br, use css for styling
$data .= '<div class="vesta_sp_data"><a href="#' . e($id) . '" role="button" data-bs-toggle="collapse" aria-controls="' . e($id) . '" aria-expanded="' . ($expanded ? 'true' : 'false') . '">' .
view('icons/expand') .
view('icons/collapse') .
'</a> ';
$data .= '<span class="label"><a href="' . $sharedPlace->url() . '">' . I18N::translate('Shared place data') . '</a></span>';