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