xref: /webtrees/app/Module/AbstractModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
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    public function description(): string
64    {
65        return $this->title();
66    }
67
68    /**
69     * Get a block setting.
70     *
71     * Originally, this was just used for the home-page blocks.  Now, it is used by any
72     * module that has repeated blocks of content on the same page.
73     *
74     * @param int    $block_id
75     * @param string $setting_name
76     * @param string $default
77     *
78     * @return string
79     */
80    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
81    {
82        $settings = Registry::cache()->array()
83            ->remember('block-setting-' . $block_id, static fn (): array => DB::table('block_setting')
84                ->where('block_id', '=', $block_id)
85                ->pluck('setting_value', 'setting_name')
86                ->all());
87
88        return $settings[$setting_name] ?? $default;
89    }
90
91    /**
92     * Set a block setting.
93     *
94     * @param int    $block_id
95     * @param string $setting_name
96     * @param string $setting_value
97     *
98     * @return self
99     */
100    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
101    {
102        DB::table('block_setting')->updateOrInsert([
103            'block_id'      => $block_id,
104            'setting_name'  => $setting_name,
105        ], [
106            'setting_value' => $setting_value,
107        ]);
108
109        return $this;
110    }
111
112    /**
113     * A unique internal name for this module (based on the installation folder).
114     *
115     * @param string $name
116     *
117     * @return void
118     */
119    final public function setName(string $name): void
120    {
121        $this->name = $name;
122    }
123
124    /**
125     * A unique internal name for this module (based on the installation folder).
126     *
127     * @return string
128     */
129    final public function name(): string
130    {
131        return $this->name;
132    }
133
134    /**
135     * Modules are either enabled or disabled.
136     *
137     * @param bool $enabled
138     *
139     * @return ModuleInterface
140     */
141    final public function setEnabled(bool $enabled): ModuleInterface
142    {
143        $this->enabled = $enabled;
144
145        return $this;
146    }
147
148    /**
149     * Modules are either enabled or disabled.
150     *
151     * @return bool
152     */
153    final public function isEnabled(): bool
154    {
155        return $this->enabled;
156    }
157
158    /**
159     * Should this module be enabled when it is first installed?
160     *
161     * @return bool
162     */
163    public function isEnabledByDefault(): bool
164    {
165        return true;
166    }
167
168    /**
169     * Get a module setting. Return a default if the setting is not set.
170     *
171     * @param string $setting_name
172     * @param string $default
173     *
174     * @return string
175     */
176    final public function getPreference(string $setting_name, string $default = ''): string
177    {
178        return DB::table('module_setting')
179            ->where('module_name', '=', $this->name())
180            ->where('setting_name', '=', $setting_name)
181            ->value('setting_value') ?? $default;
182    }
183
184    /**
185     * Set a module setting.
186     *
187     * Since module settings are NOT NULL, setting a value to NULL will cause
188     * it to be deleted.
189     *
190     * @param string $setting_name
191     * @param string $setting_value
192     *
193     * @return void
194     */
195    final public function setPreference(string $setting_name, string $setting_value): void
196    {
197        DB::table('module_setting')->updateOrInsert([
198            'module_name'  => $this->name(),
199            'setting_name' => $setting_name,
200        ], [
201            'setting_value' => $setting_value,
202        ]);
203    }
204
205    /**
206     * Get the current access level for a module
207     *
208     * @template T
209     *
210     * @param Tree            $tree
211     * @param class-string<T> $interface
212     *
213     * @return int
214     */
215    final public function accessLevel(Tree $tree, string $interface): int
216    {
217        $access_levels = Registry::cache()->array()
218            ->remember('module-privacy-' . $tree->id(), static fn (): Collection => DB::table('module_privacy')
219                ->where('gedcom_id', '=', $tree->id())
220                ->get());
221
222        $row = $access_levels->first(fn (object $row): bool => $row->interface === $interface && $row->module_name === $this->name());
223
224        return $row ? (int) $row->access_level : $this->access_level;
225    }
226
227    /**
228     * Where does this module store its resources
229     *
230     * @return string
231     */
232    public function resourcesFolder(): string
233    {
234        return Webtrees::ROOT_DIR . '/resources/';
235    }
236}
237