xref: /webtrees/app/Module/AbstractModule.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\DB;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\Webtrees;
28use Illuminate\Support\Collection;
29
30/**
31 * Class AbstractModule - common functions for blocks
32 */
33abstract class AbstractModule implements ModuleInterface
34{
35    use ViewResponseTrait;
36
37    // A unique internal name for this module (based on the installation folder).
38    private string $name = '';
39
40    // The default access level for this module.  It can be changed in the control panel.
41    protected int $access_level = Auth::PRIV_PRIVATE;
42
43    // The default status for this module.  It can be changed in the control panel.
44    private bool $enabled = true;
45
46    /**
47     * Called for all *enabled* modules.
48     */
49    public function boot(): void
50    {
51    }
52
53    /**
54     * How should this module be identified in the control panel, etc.?
55     *
56     * @return string
57     */
58    public function title(): string
59    {
60        return 'Module name goes here';
61    }
62
63    /**
64     * A sentence describing what this module does.
65     *
66     * @return string
67     */
68    public function description(): string
69    {
70        return $this->title();
71    }
72
73    /**
74     * Get a block setting.
75     *
76     * Originally, this was just used for the home-page blocks.  Now, it is used by any
77     * module that has repeated blocks of content on the same page.
78     *
79     * @param int    $block_id
80     * @param string $setting_name
81     * @param string $default
82     *
83     * @return string
84     */
85    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
86    {
87        $settings = Registry::cache()->array()
88            ->remember('block-setting-' . $block_id, static fn(): array => DB::table('block_setting')
89                ->where('block_id', '=', $block_id)
90                ->pluck('setting_value', 'setting_name')
91                ->all());
92
93        return $settings[$setting_name] ?? $default;
94    }
95
96    /**
97     * Set a block setting.
98     *
99     * @param int    $block_id
100     * @param string $setting_name
101     * @param string $setting_value
102     *
103     * @return self
104     */
105    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
106    {
107        DB::table('block_setting')->updateOrInsert([
108            'block_id'      => $block_id,
109            'setting_name'  => $setting_name,
110        ], [
111            'setting_value' => $setting_value,
112        ]);
113
114        return $this;
115    }
116
117    /**
118     * A unique internal name for this module (based on the installation folder).
119     *
120     * @param string $name
121     *
122     * @return void
123     */
124    final public function setName(string $name): void
125    {
126        $this->name = $name;
127    }
128
129    /**
130     * A unique internal name for this module (based on the installation folder).
131     *
132     * @return string
133     */
134    final public function name(): string
135    {
136        return $this->name;
137    }
138
139    /**
140     * Modules are either enabled or disabled.
141     *
142     * @param bool $enabled
143     *
144     * @return ModuleInterface
145     */
146    final public function setEnabled(bool $enabled): ModuleInterface
147    {
148        $this->enabled = $enabled;
149
150        return $this;
151    }
152
153    /**
154     * Modules are either enabled or disabled.
155     *
156     * @return bool
157     */
158    final public function isEnabled(): bool
159    {
160        return $this->enabled;
161    }
162
163    /**
164     * Should this module be enabled when it is first installed?
165     *
166     * @return bool
167     */
168    public function isEnabledByDefault(): bool
169    {
170        return true;
171    }
172
173    /**
174     * Get a module setting. Return a default if the setting is not set.
175     *
176     * @param string $setting_name
177     * @param string $default
178     *
179     * @return string
180     */
181    final public function getPreference(string $setting_name, string $default = ''): string
182    {
183        return DB::table('module_setting')
184            ->where('module_name', '=', $this->name())
185            ->where('setting_name', '=', $setting_name)
186            ->value('setting_value') ?? $default;
187    }
188
189    /**
190     * Set a module setting.
191     *
192     * Since module settings are NOT NULL, setting a value to NULL will cause
193     * it to be deleted.
194     *
195     * @param string $setting_name
196     * @param string $setting_value
197     *
198     * @return void
199     */
200    final public function setPreference(string $setting_name, string $setting_value): void
201    {
202        DB::table('module_setting')->updateOrInsert([
203            'module_name'  => $this->name(),
204            'setting_name' => $setting_name,
205        ], [
206            'setting_value' => $setting_value,
207        ]);
208    }
209
210    /**
211     * Get the current access level for a module
212     *
213     * @template T
214     *
215     * @param Tree            $tree
216     * @param class-string<T> $interface
217     *
218     * @return int
219     */
220    final public function accessLevel(Tree $tree, string $interface): int
221    {
222        $access_levels = Registry::cache()->array()
223            ->remember('module-privacy-' . $tree->id(), static fn(): Collection => DB::table('module_privacy')
224                ->where('gedcom_id', '=', $tree->id())
225                ->get());
226
227        $row = $access_levels->first(fn(object $row): bool => $row->interface === $interface && $row->module_name === $this->name());
228
229        return $row ? (int) $row->access_level : $this->access_level;
230    }
231
232    /**
233     * Where does this module store its resources
234     *
235     * @return string
236     */
237    public function resourcesFolder(): string
238    {
239        return Webtrees::ROOT_DIR . '/resources/';
240    }
241}
242