-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResearchSuggestionsModule.php
287 lines (231 loc) · 11.1 KB
/
ResearchSuggestionsModule.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
<?php
namespace Cissee\Webtrees\Module\ResearchSuggestions;
use Aura\Router\Route;
use Cissee\WebtreesExt\AbstractModule;
use Cissee\WebtreesExt\Elements\EventsRecordedExt;
use Cissee\WebtreesExt\Elements\XrefSourceExt;
use Cissee\WebtreesExt\Module\ModuleMetaInterface;
use Cissee\WebtreesExt\Module\ModuleMetaTrait;
use Cissee\WebtreesExt\MoreI18N;
use Fisharebest\Webtrees\Elements\Marriage;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Http\RequestHandlers\TreePreferencesAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\SearchService;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Vesta\Hook\HookInterfaces\EmptyIndividualFactsTabExtender;
use Vesta\Hook\HookInterfaces\IndividualFactsTabExtenderInterface;
use Vesta\Model\GenericViewElement;
use Vesta\VestaModuleTrait;
use function app;
use function view;
class ResearchSuggestionsModule extends AbstractModule implements
ModuleCustomInterface,
ModuleMetaInterface,
ModuleConfigInterface,
ModuleGlobalInterface,
MiddlewareInterface,
IndividualFactsTabExtenderInterface {
use ModuleCustomTrait, ModuleMetaTrait, ModuleConfigTrait, ModuleGlobalTrait, VestaModuleTrait {
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 EmptyIndividualFactsTabExtender;
use ResearchSuggestionsModuleTrait;
public function customModuleAuthorName(): string {
return 'Richard Cissée';
}
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_research_suggestions/master/metadata.json';
}
public function customModuleSupportUrl(): string {
return 'https://cissee.de';
}
public function resourcesFolder(): string {
return __DIR__ . '/resources/';
}
public function onBoot(): void {
app()->instance(ResearchSuggestionsService::class, new ResearchSuggestionsService(
$this,
app(SearchService::class)));
//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 = app(RouterContainer::class);
assert($router_container instanceof RouterContainer);
$router = $router_container->getMap();
*/
$router = Registry::routeFactory()->routeMap();
$router->get(TomSelectSourceWithSuggestions::class, '/tree/{tree}/tom-select-source-with-suggestions', TomSelectSourceWithSuggestions::class);
// Replace existing views with our own versions.
//but not everywhere (i.e. not when merging records)
//used via XrefSourceExt
View::registerCustomView('::components/select-source-ext', $this->name() . '::components/select-source');
// Replace existing views with our own versions.
View::registerCustomView('::lists/sources-table', $this->name() . '::lists/sources-table');
//TODO Issue #2
// Replace existing views with our own versions.
View::registerCustomView('::admin/trees-preferences', $this->name() . '::admin/trees-preferences');
View::registerCustomView('::admin/trees-preferences-ext', $this->name() . '::admin/trees-preferences-ext');
//TODO: handle this better!
$ef = Registry::elementFactory();
$ef->registerTags(['INDI:MARR' => new Marriage(MoreI18N::xlate('Marriage'))]);
$ef->registerTags(['SOUR:DATA:EVEN' => new EventsRecordedExt(MoreI18N::xlate('Events'))]);
//replace XrefSource everywhere
$ef->registerTags([
'FAM:*:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'FAM:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'INDI:*:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'INDI:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'NOTE:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'OBJE:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
]);
//Gedcom-L
$ef->registerTags([
'FAM:*:_ASSO:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'INDI:*:_ASSO:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'_LOC:EVEN:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'_LOC:NAME:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
'_LOC:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
'_LOC:TYPE:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
'_LOC:_AIDN:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
'_LOC:_DMGD:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
'_LOC:_LOC:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
'_LOC:_POST:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source')),
]);
//webtrees
$ef->registerTags([
'FAM:*:_ASSO:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
'INDI:*:_ASSO:SOUR' => new XrefSourceExt(MoreI18N::xlate('Source citation')),
]);
$this->flashWhatsNew('\Cissee\Webtrees\Module\ResearchSuggestions\WhatsNew', 1);
}
//IndividualFactsTabExtenderInterface
public function hFactsTabGetOutputBeforeTab(
GedcomRecord $record): GenericViewElement {
if (sizeof($this->hFactsTabGetAdditionalFacts($record)) === 0) {
return GenericViewElement::createEmpty();
}
$pre = '<link href="' . $this->assetUrl('css/style.css') . '" type="text/css" rel="stylesheet" />';
return new GenericViewElement($pre, '');
}
public function hFactsTabGetStyleadds(
GedcomRecord $record,
Fact $fact): array {
$styleadds = [];
if ($fact->id() === 'research') {
$styleadds['research'] = 'wt-research-fact-pfh collapse'; //see style.css, and hFactsTabGetOutputInDBox
}
return $styleadds;
}
public function hFactsTabGetOutputInDBox(
GedcomRecord $record): GenericViewElement {
if (sizeof($this->hFactsTabGetAdditionalFacts($record)) === 0) {
return GenericViewElement::createEmpty();
}
$toggleableResearch = boolval($this->getPreference('TAB_TOGGLEABLE_RESEARCH', '1'));
return $this->getOutputInDescriptionBox($toggleableResearch, 'show-research-suggestions-factstab', 'wt-research-fact-pfh', I18N::translate('Research Suggestions'));
}
protected function getOutputInDescriptionBox(
bool $toggleableResearch,
string $id,
string $targetClass,
string $label) {
ob_start();
if ($toggleableResearch) {
?>
<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();
}
$toggleableResearch = boolval($this->getPreference('TAB_TOGGLEABLE_RESEARCH', '1'));
return $this->getOutputAfterTab($toggleableResearch, 'show-research-suggestions-factstab');
}
protected function getOutputAfterTab($toggleableRels, $toggle) {
$post = "";
if ($toggleableRels) {
$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 hFactsTabGetAdditionalFacts(
GedcomRecord $record) {
$cacheKey = ResearchSuggestionsModule::class . '_hFactsTabGetAdditionalFacts_' . $record->tree()->id() . $record->xref();
$ret = Registry::cache()->array()->remember($cacheKey, static function () use ($record): array {
//TODO make this configurable! here and elsewhere!
$ignorePartialRanges = true;
return app(ResearchSuggestionsService::class)->getAdditionalFacts(
$record,
$ignorePartialRanges);
});
return $ret;
}
//TODO Issue #2
public function bodyContent(): string {
//we need additional javascript for tom select sources
return view($this->name() . '::js/webtreesExt');
}
//TODO Issue #2
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler): ResponseInterface {
$route = $request->getAttribute('route');
assert($route instanceof Route);
if ($route->handler === TreePreferencesAction::class) {
$this->preferencesUpdateExt($request);
}
// Generate the response.
return $handler->handle($request);
}
public function preferencesUpdateExt(ServerRequestInterface $request) {
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
$params = (array) $request->getParsedBody();
$tree->setPreference('SOUR_DATA_EVEN_FACTS', implode(',', $params['SOUR_DATA_EVEN_FACTS'] ?? []));
}
}