-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.php
280 lines (243 loc) · 8.32 KB
/
module.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
<?php
declare(strict_types=1);
namespace JustCarmen\Webtrees\Module\SimpleMenu;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\FlashMessages;
use Psr\Http\Message\ResponseInterface;
use Fisharebest\Localization\Translation;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleMenuTrait;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
use Fisharebest\Webtrees\Module\ModuleMenuInterface;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesMenusAction;
/**
* Anonymous class - provide a custom menu option and page
*/
return new class extends AbstractModule implements ModuleCustomInterface, ModuleMenuInterface, ModuleConfigInterface, ModuleGlobalInterface, RequestHandlerInterface
{
use ModuleCustomTrait;
use ModuleMenuTrait;
use ModuleConfigTrait;
use ModuleGlobalTrait;
protected const ROUTE_URL = '/tree/{tree}/jc-simple-menu-1/{menu}';
// Module constants
public const CUSTOM_AUTHOR = 'JustCarmen';
public const CUSTOM_VERSION = '1.4';
public const GITHUB_REPO = 'webtrees-simple-menu';
public const AUTHOR_WEBSITE = 'https://justcarmen.nl';
public const CUSTOM_SUPPORT_URL = self::AUTHOR_WEBSITE . '/modules-webtrees-2/simple-menu/';
/**
* How should this module be identified in the control panel, etc.?
*
* @return string
*/
public function title(): string
{
/* I18N: Name of a module */
if ($this->getPreference('menu-title')){
return I18N::translate('Simple menu module') . ' - ' . $this->getPreference('menu-title');
} else {
return I18N::translate('Simple menu module');
}
}
/**
* A sentence describing what this module does.
*
* @return string
*/
public function description(): string
{
/* I18N: Description of the “Simple Menu” module */
return I18N::translate('Add an extra mainmenu item and page.');
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleAuthorName()
*/
public function customModuleAuthorName(): string
{
return self::CUSTOM_AUTHOR;
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion()
*/
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';
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleSupportUrl()
*/
public function customModuleSupportUrl(): string
{
return self::CUSTOM_SUPPORT_URL;
}
/**
* Bootstrap the module
*/
public function boot(): void
{
Registry::routeFactory()->routeMap()
->get(static::class, static::ROUTE_URL, $this);
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
/**
* Where does this module store its resources
*
* @return string
*/
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
/**
* Raw content, to be added at the end of the <head> element.
* Typically, this will be <link> and <meta> elements.
*
* @return string
*/
public function headContent(): string
{
return
'<style>
.wt-theme-clouds .' . $this->name() . ' .nav-link:before {
content: url(' . $this->assetUrl('icons/menu-icon-clouds.png') . ');
}
.wt-theme-colors .' . $this->name() . ' .nav-link:before {
content: url(' . $this->assetUrl('icons/menu-icon-colors.png') . ');
}
.wt-theme-webtrees .' . $this->name() . ' .nav-link:before {
content: url(' . $this->assetUrl('icons/menu-icon-webtrees.png') . ');
}
.wt-theme-xenea .' . $this->name() . ' .nav-link:before {
content: url(' . $this->assetUrl('icons/menu-icon-xenea.png') . ');
}
.wt-theme-_myartjaub_ruraltheme_ .' . $this->name() . ' .nav-link:before {
content: url(' . $this->assetUrl('icons/menu-icon-rural.png') . ');
}
</style>';
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function getAdminAction(ServerRequestInterface $request): ResponseInterface
{
$this->layout = 'layouts/administration';
return $this->viewResponse($this->name() . '::edit', [
'title' => $this->title(),
'menu_title' => $this->getPreference('menu-title'),
'page_title' => $this->getPreference('page-title'),
'page_body' => $this->getPreference('page-body'),
]);
}
/**
* Save the user preference.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$params = (array) $request->getParsedBody();
$this->setPreference('menu-title', $params['menu-title']);
$this->setPreference('page-title', $params['page-title']);
$this->setPreference('page-body', $params['page-body']);
$message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
FlashMessages::addMessage($message, 'success');
return redirect(route(ModulesMenusAction::class));
}
/**
* The default position for this menu. It can be changed in the control panel.
*
* @return int
*/
public function defaultMenuOrder(): int
{
return 99;
}
/**
* A menu, to be added to the main application menu.
*
* @param Tree $tree
*
* @return Menu|null
*/
public function getMenu(Tree $tree): ?Menu
{
if ($tree === null) {
return '';
}
$menu_title = $this->getPreference('menu-title');
$url = route(static::class, [
'tree' => $tree->name(),
'menu' => $this->getSlug($menu_title)
]);
return new Menu($menu_title, e($url), $this->name());
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
$page_title = $this->getPreference('page-title');
$page_body = $this->getPreference('page-body');
return $this->viewResponse($this->name() . '::page', [
'tree' => $tree,
'title' => $this->title(),
'module' => $this->name(),
'is_admin' => Auth::isAdmin(),
'page_title' => $page_title,
'page_body' => $page_body
]);
}
/**
* Get the url slug for this page
*/
public function getSlug($string): String
{
return preg_replace('/\s+/', '-', strtolower(preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($string))));
}
/**
* Additional/updated translations.
*
* @param string $language
*
* @return array<string>
*/
public function customTranslations(string $language): array
{
$file = $this->resourcesFolder() . 'lang/' . $language . '.php';
return file_exists($file) ? (new Translation($file))->asArray() : [];
}
};