-
Notifications
You must be signed in to change notification settings - Fork 5
/
Settings.php
279 lines (216 loc) · 8.93 KB
/
Settings.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
<?php
namespace EvanG\Modules\MailSystem;
use DateInterval;
use DateTimeImmutable;
use Exception;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Location;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Services\UserService;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Submitter;
class Settings
{
private UserService $userService;
private TreeService $treeService;
private array $allTrees;
private array $allUsers;
private array $allImageFormat;
public function __construct()
{
$this->userService = app(UserService::class);
$this->treeService = app(TreeService::class);
$users = [];
foreach ($this->userService->all() as $u) $users[$u->username()] = $u->username();
$this->allUsers = $users;
$trees = [];
foreach ($this->treeService->all() as $t) $trees[$t->name()] = $t->name();
$this->allTrees = $trees;
$formats = [];
foreach (["png", "svg"] as $f) $formats[$f] = $f;
$this->allImageFormat = $formats;
}
#region General
public function getUsers(): array
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_USERS');
if (empty($pref)) return $this->getAllUsers();
return explode(",", $pref);
}
public function getAllUsers(): array { return $this->allUsers; }
public function setUsers($value) { Site::setPreference('EVANG_MAILSYSTEM_USERS', implode(',', $value)); }
public function getTrees(): array
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_TREES');
if (empty($pref)) return $this->getAllTrees();
return explode(",", $pref);
}
public function getAllTrees(): array { return $this->allTrees; }
public function setTrees($value) { Site::setPreference('EVANG_MAILSYSTEM_TREES', implode(',', $value)); }
public function getEmpty(): bool
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_EMPTY');
if (empty($pref)) return false;
return (bool)$pref;
}
public function setEmpty($value) { Site::setPreference('EVANG_MAILSYSTEM_EMPTY', $value); }
public function setDays($value) { Site::setPreference('EVANG_MAILSYSTEM_DAYS', $value); }
public function getAllImageDataType(): array
{
return [
"link" => I18N::translate("Direct URLs"),
"data" => I18N::translate("Data URLs"),
"none" => I18N::translate("No images")
];
}
public function getImageDataType(): string
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_IMAGEDATA');
if (empty($pref)) return "data";
return $pref;
}
public function setImageDataType($value) { Site::setPreference('EVANG_MAILSYSTEM_IMAGEDATA', $value); }
public function getAllImageFormat(): array { return $this->allImageFormat; }
public function getImageFormat(): string
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_IMAGEFORMAT');
if (empty($pref)) return "png";
return $pref;
}
public function setImageFormat($value) { Site::setPreference('EVANG_MAILSYSTEM_IMAGEFORMAT', $value); }
#endregion
#region News
public function getNewsEnabled(): bool
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_NEWS_ENABLED');
if (!isset($pref)) return true;
return (bool)$pref;
}
public function setNewsEnabled($value) { Site::setPreference('EVANG_MAILSYSTEM_NEWS_ENABLED', $value); }
#endregion
#region Change-list
public function getChangelistEnabled(): bool
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_CHANGE_ENABLED');
if (!isset($pref)) return true;
return (bool)$pref;
}
public function setChangelistEnabled($value) { Site::setPreference('EVANG_MAILSYSTEM_CHANGE_ENABLED', $value); }
public function getAllChangelistTags(): array
{
return [
Individual::RECORD_TYPE => I18N::translate("Individual"),
Family::RECORD_TYPE => I18N::translate("Family"),
Media::RECORD_TYPE => I18N::translate("Media"),
Note::RECORD_TYPE => I18N::translate("Note"),
Source::RECORD_TYPE => I18N::translate("Source"),
Submitter::RECORD_TYPE => I18N::translate("Submitter"),
Repository::RECORD_TYPE => I18N::translate("Repository"),
Location::RECORD_TYPE => I18N::translate('Location')];
}
public function getChangelistTags(): array
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_CHANGE_TAGS');
if (empty($pref)) return [Individual::RECORD_TYPE, Family::RECORD_TYPE];
return explode(",", $pref);
}
public function setChangelistTags($value) { Site::setPreference('EVANG_MAILSYSTEM_CHANGE_TAGS', implode(',', $value)); }
#endregion
#region Anniversaries
public function getAnniversariesEnabled(): bool
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_ANNIV_ENABLED');
if (!isset($pref)) return true;
return (bool)$pref;
}
public function setAnniversariesEnabled($value) { Site::setPreference('EVANG_MAILSYSTEM_ANNIV_ENABLED', $value); }
public function getAnniversariesDeceased(): bool
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_ANNIV_DECEASED');
if (!isset($pref)) return false;
return (bool)$pref;
}
public function setAnniversariesDeceased($value) { Site::setPreference('EVANG_MAILSYSTEM_ANNIV_DECEASED', $value); }
public function getAllAnniversariesTags(): array
{
$data = [];
foreach (array_merge(Gedcom::BIRTH_EVENTS, Gedcom::DEATH_EVENTS) as $tag) $data[$tag] = Registry::elementFactory()->make("INDI:" . $tag)->label();
foreach (array_merge(Gedcom::MARRIAGE_EVENTS) as $tag) $data[$tag] = Registry::elementFactory()->make("FAM:" . $tag)->label();
return $data;
}
public function getAnniversariesTags(): array
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_ANNIV_TAGS');
if (empty($pref)) return ["BIRT"];
return explode(",", $pref);
}
public function setAnniversariesTags($value) { Site::setPreference('EVANG_MAILSYSTEM_ANNIV_TAGS', implode(',', $value)); }
#endregion
#region Infos
public function setLastSend(?DateTimeImmutable $date)
{
Site::setPreference('EVANG_MAILSYSTEM_LASTCRONDATE', $date == null ? "" : $date->format("Y-m-d"));
}
public function getNextSend(): DateTimeImmutable
{
try {
return $this->getThisSend()->add(new DateInterval("P" . $this->getDays() . "D"));
} catch (Exception $e) {
return $this->getThisSend();
}
}
public function getThisSend(): DateTimeImmutable
{
$lastCronDate = $this->getLastSend();
$today = new DateTimeImmutable("midnight");
if ($lastCronDate == null) return $today;
try {
return $lastCronDate->add(new DateInterval('P' . $this->getDays() . 'D'));
} catch (Exception $e) {
return $today;
}
}
public function getLastSend(): ?DateTimeImmutable
{
$lastCronTxt = Site::getPreference('EVANG_MAILSYSTEM_LASTCRONDATE');
if (empty($lastCronTxt)) return null;
try {
return new DateTimeImmutable($lastCronTxt);
} catch (Exception $e) {
/* The date of the last send couldn't be understood, so we send the mails to give it a known value */
return null;
}
}
public function getDays(): int
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_DAYS');
if (empty($pref)) return 7;
return (int)$pref;
}
#endregion
#region Footer
public function getFooterEnabled(): string
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_FOOTER_ENABLED');
if (!isset($pref)) return true;
return $pref;
}
public function setFooterEnabled($value) { Site::setPreference('EVANG_MAILSYSTEM_FOOTER_ENABLED', $value); }
public function getFooterMessage(): string
{
$pref = Site::getPreference('EVANG_MAILSYSTEM_FOOTER_MESSAGE');
if (empty($pref)) return I18N::translate("This email was autogenerated by Webtrees and programmed with ❤ by %s",
"<a href=\"https://github.com/06Games/Webtrees-MailSystem\">Evan Galli</a>");
return $pref;
}
public function setFooterMessage($value) { Site::setPreference('EVANG_MAILSYSTEM_FOOTER_MESSAGE', $value); }
#endregion
}