-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFancyResearchLinksModule.php
418 lines (364 loc) · 12.9 KB
/
FancyResearchLinksModule.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
declare(strict_types=1);
namespace JustCarmen\Webtrees\Module\FancyResearchLinks;
use Throwable;
use function str_replace;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\Registry;
use Illuminate\Support\Collection;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\FlashMessages;
use Psr\Http\Message\ResponseInterface;
use Fisharebest\Localization\Translation;
use Psr\Http\Message\ServerRequestInterface;
use Illuminate\Database\Capsule\Manager as DB;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleSidebarTrait;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
use Fisharebest\Webtrees\Statistics\Service\CountryService;
class FancyResearchLinksModule extends AbstractModule implements ModuleCustomInterface, ModuleConfigInterface, ModuleSidebarInterface
{
use ModuleCustomTrait;
use ModuleConfigTrait;
use ModuleSidebarTrait;
public const CUSTOM_AUTHOR = 'JustCarmen';
public const CUSTOM_VERSION = '2.5.0';
public const GITHUB_REPO = 'webtrees-fancy-research-links';
public const AUTHOR_WEBSITE = 'https://justcarmen.nl';
public const CUSTOM_SUPPORT_URL = self::AUTHOR_WEBSITE . '/modules-webtrees-2/fancy-research-links/';
private $country_service;
/**
* FancyResearchLinks constructor.
*
* @param CountryService $country_service
*/
public function __construct(CountryService $country_service)
{
$this->country_service = $country_service;
}
/**
* How should this module be identified in the control panel, etc.?
*
* @return string
*/
public function title(): string
{
return I18N::translate('Fancy Research Links');
}
/**
* A sentence describing what this module does.
*
* @return string
*/
public function description(): string
{
return I18N::translate('A sidebar tool to provide quick links to popular research web sites.');
}
/**
* The person or organisation who created this module.
*
* @return string
*/
public function customModuleAuthorName(): string
{
return self::CUSTOM_AUTHOR;
}
/**
* The version of this module.
*
* @return string
*/
public function customModuleVersion(): string
{
return self::CUSTOM_VERSION;
}
/**
* A URL that will provide the latest stable version of this module.
*
* @return string
*/
public function customModuleLatestVersionUrl(): string
{
return 'https://raw.githubusercontent.com/' . self::CUSTOM_AUTHOR . '/' . self::GITHUB_REPO . '/main/latest-version.txt';
}
/**
* Where to get support for this module. Perhaps a github repository?
*
* @return string
*/
public function customModuleSupportUrl(): string
{
return self::CUSTOM_SUPPORT_URL;
}
/**
* Bootstrap. This function is called on *enabled* modules.
* It is a good place to register routes and views.
*
* @return void
*/
public function boot(): void
{
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
// Changes in version 2.3.1 require to reset the expanded_area setting if neccessary
if (!array_key_exists($this->getPreference('expanded-area'), $this->getCountryList())) {
DB::table('module_setting')
->where('module_name', '=', $this->name())
->where('setting_name', '=', 'expanded-area')
->delete();
}
}
/**
* Where does this module store its resources
*
* @return string
*/
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function getAdminAction(): ResponseInterface
{
$this->layout = 'layouts/administration';
return $this->viewResponse($this->name() . '::settings', [
'enabled_plugins' => collect(explode(', ', $this->getPreference('enabled-plugins', $this->getPluginsByName()->join(', ')))),
'expanded_area' => $this->getCountryList()[$this->getPreference('expanded-area', 'INT')],
'expand_sidebar' => $this->getPreference('expand-sidebar'),
'plugins' => $this->getPluginsByArea()->sortkeys(),
'target_blank' => $this->getPreference('target-blank'),
'title' => $this->title()
]);
}
/**
* Save the user preference.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$params = (array) $request->getParsedBody();
if (isset($params['enabled-plugins'])) {
$enabled_plugins = (string) implode(', ', array_keys($params['enabled-plugins']));
} else {
$enabled_plugins = (string) $this->getPluginsByName()->join(', ');
}
if ($params['save'] === '1') {
$this->setPreference('enabled-plugins', $enabled_plugins);
$this->setPreference('expanded-area', array_search($params['expanded-area'], $this->getCountryList()) ?? 'INT');
$this->setPreference('expand-sidebar', $params['expand-sidebar'] ?? '0');
$this->setPreference('target-blank', $params['target-blank'] ?? '0');
$message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
FlashMessages::addMessage($message, 'success');
}
return redirect($this->getConfigLink());
}
/**
* The text that appears on the sidebar's title.
*
* @return string
*/
public function sidebarTitle(Individual $individual): string
{
return view($this->name() . '::sidebar-header', [
'module' => $this,
'is_admin' => Auth::isAdmin(),
]);
}
/**
* The default position for this sidebar. It can be changed in the control panel.
*
* @return int
*/
public function defaultSidebarOrder(): int
{
return 1;
}
/**
* Does this sidebar have anything to display for this individual?
*
* @param Individual $individual
*
* @return bool
*/
public function hasSidebarContent(Individual $individual): bool
{
return true;
}
/**
* Load this sidebar synchronously.
*
* @param Individual $individual
*
* @return string
*/
public function getSidebarContent(Individual $individual): string
{
$expand_sidebar = (bool) $this->getPreference('expand-sidebar') && Auth::isEditor($individual->tree());
$enabled_plugins = collect(explode(', ', $this->getPreference('enabled-plugins', $this->getPluginsByName()->join(', '))));
$expanded_area = $this->getCountryList()[$this->getPreference('expanded-area', 'INT')];
return view($this->name() . '::sidebar', [
'attributes' => $this->getAttributes($individual),
'enabled_plugins' => $enabled_plugins,
'expanded_area' => $expanded_area,
'expand_sidebar' => $expand_sidebar,
'plugins' => $this->getPluginsByArea(),
'target_blank' => $this->getPreference('target-blank'),
'tree' => $individual->tree(),
]);
}
/**
* Additional/updated translations.
*
* @param string $language
*
* @return array
*/
public function customTranslations(string $language): array
{
$lang_dir = $this->resourcesFolder() . 'lang/';
$file = $lang_dir . $language . '.mo';
if (file_exists($file)) {
return (new Translation($file))->asArray();
} else {
return [];
}
}
/**
* Collect all plugins from the plugins folder
*
* @return Collection
*/
private function getPlugins(): Collection
{
$pattern = __DIR__ . '/plugins/*Plugin.php';
$filenames = glob($pattern);
$collection = Collection::make($filenames)
->map(static function (string $filename) {
try {
$path_parts = pathinfo($filename);
$plugin = Registry::container()->get(__NAMESPACE__ . '\Plugin\\' . $path_parts['filename']);
return $plugin;
} catch (Throwable $ex) {
FlashMessages::addMessage(I18N::translate('There was an error loading the plugin ' . $path_parts['filename'] . '.') . '<br>' . e($ex->getMessage()), 'danger');
throw $ex;
}
});
// We need to sort the collection by plugin label
$plugins = $collection->mapToGroups(function ($plugin) {
return [$plugin->pluginLabel() => $plugin];
});
// We only need the values of the sorted collection in a flattened way
return $plugins->sortkeys()->values()->flatten();
}
/**
* Group plugins by Area
*
* @return Collection
*/
private function getPluginsByArea(): Collection
{
$plugins = $this->getPlugins();
$collection = $plugins->mapToGroups(function ($plugin) {
$area = $plugin->researchArea();
$area_fullname = $this->getCountryList()[$area];
return [$area_fullname => $plugin];
});
// The first group should be the expanded area. Remove the group from the collection
$expanded_area = $this->getCountryList()[$this->getPreference('expanded-area', 'INT')];
$pluginlist = $collection->filter()->except($expanded_area);
// return the localized sorted list with the expanded area on top
return $pluginlist->sortkeys()->prepend($collection->get($expanded_area), $expanded_area);
}
/**
* Get a list of plugins by their plugin name
*
* @return Collection
*/
private function getPluginsByName(): Collection
{
$plugins = $this->getPlugins();
$pluginlist = $plugins->map(function ($plugin) {
return $plugin->pluginName();
});
return $pluginlist;
}
/**
* Get a list of all countries
*
* @return array
*/
private function getCountryList(): array
{
// Add our 'International' area to the list of countries
$countries = $this->country_service->getAllCountries();
$countries['INT'] = I18N::translate('International');
return $countries;
}
/**
* Make the attributes array for use in the plugins.
*
* @param Individual $individual
*
* @return array
*/
public function getAttributes(Individual $individual): array
{
$all_names = $individual->getAllNames();
$name = $all_names[0];
// add some custom name attributes
$name['first'] = explode(" ", $name['givn'])[0];
$name['prefix'] = trim(str_replace($name['surn'], '', $name['surname']));
$name['fullNN'] = trim(strip_tags(str_replace('@N.N.', '', $name['fullNN'])));
// extract the Marriage name
$name['msurname'] = '';
if (count($all_names) > 1) {
foreach ($all_names as $names) {
if ($names['type'] === '_MARNM') {
$name['msurname'] = $names['surname'];
break;
}
}
}
// support all birth (birt, chr, bapm) and death events (deat, buri, crem)
$gedcom_events = array_merge(Gedcom::BIRTH_EVENTS, Gedcom::DEATH_EVENTS);
foreach ($gedcom_events as $event) {
$year[$event] = '';
$place[$event] = '';
$country[$event] = '';
$edates = $individual->getAllEventDates([$event]);
if ($edates !== []) {
foreach ($edates as $edate) {
$year[$event] = $edate->minimumDate()->format('%Y');
}
}
$eplaces = $individual->getAllEventPlaces([$event]);
if ($eplaces !== []) {
foreach ($eplaces as $eplace) {
$place[$event] = strip_tags($eplace->placeName());
$country[$event] = strip_tags($eplace->lastParts(1)->first());
}
}
}
$attributes = array(
'NAME' => $name,
'YEAR' => $year,
'PLACE' => $place,
'COUNTRY' => $country
);
return $attributes;
}
};