xref: /webtrees/app/Module/AbstractModule.php (revision d3d68c00895c5db24007b987c2ee5f9f44403bef)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Registry;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Support\Collection;
28
29/**
30 * Class AbstractModule - common functions for blocks
31 */
32abstract class AbstractModule implements ModuleInterface
33{
34    use ViewResponseTrait;
35
36    /** @var string A unique internal name for this module (based on the installation folder). */
37    private $name = '';
38
39    /** @var int The default access level for this module.  It can be changed in the control panel. */
40    protected $access_level = Auth::PRIV_PRIVATE;
41
42    /** @var bool The default status for this module.  It can be changed in the control panel. */
43    private $enabled = true;
44
45    /** @var string For custom modules - optional (recommended) version number */
46    public const CUSTOM_VERSION = '';
47
48    /** @var string For custom modules - link for support, upgrades, etc. */
49    public const CUSTOM_WEBSITE = '';
50
51    /**
52     * Called for all *enabled* modules.
53     */
54    public function boot(): void
55    {
56    }
57
58    /**
59     * How should this module be identified in the control panel, etc.?
60     *
61     * @return string
62     */
63    public function title(): string
64    {
65        return 'Module name goes here';
66    }
67
68    /**
69     * A sentence describing what this module does.
70     *
71     * @return string
72     */
73    public function description(): string
74    {
75        return $this->title();
76    }
77
78    /**
79     * Get a block setting.
80     *
81     * Originally, this was just used for the home-page blocks.  Now, it is used by any
82     * module that has repeated blocks of content on the same page.
83     *
84     * @param int    $block_id
85     * @param string $setting_name
86     * @param string $default
87     *
88     * @return string
89     */
90    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
91    {
92        $settings = Registry::cache()->array()->remember('block-setting-' . $block_id, static function () use ($block_id): array {
93            return DB::table('block_setting')
94                ->where('block_id', '=', $block_id)
95                ->pluck('setting_value', 'setting_name')
96                ->all();
97        });
98
99        return $settings[$setting_name] ?? $default;
100    }
101
102    /**
103     * Set a block setting.
104     *
105     * @param int    $block_id
106     * @param string $setting_name
107     * @param string $setting_value
108     *
109     * @return $this
110     */
111    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
112    {
113        DB::table('block_setting')->updateOrInsert([
114            'block_id'      => $block_id,
115            'setting_name'  => $setting_name,
116        ], [
117            'setting_value' => $setting_value,
118        ]);
119
120        return $this;
121    }
122
123    /**
124     * A unique internal name for this module (based on the installation folder).
125     *
126     * @param string $name
127     *
128     * @return void
129     */
130    final public function setName(string $name): void
131    {
132        $this->name = $name;
133    }
134
135    /**
136     * A unique internal name for this module (based on the installation folder).
137     *
138     * @return string
139     */
140    final public function name(): string
141    {
142        return $this->name;
143    }
144
145    /**
146     * Modules are either enabled or disabled.
147     *
148     * @param bool $enabled
149     *
150     * @return ModuleInterface
151     */
152    final public function setEnabled(bool $enabled): ModuleInterface
153    {
154        $this->enabled = $enabled;
155
156        return $this;
157    }
158
159    /**
160     * Modules are either enabled or disabled.
161     *
162     * @return bool
163     */
164    final public function isEnabled(): bool
165    {
166        return $this->enabled;
167    }
168
169    /**
170     * Should this module be enabled when it is first installed?
171     *
172     * @return bool
173     */
174    public function isEnabledByDefault(): bool
175    {
176        return true;
177    }
178
179    /**
180     * Get a module setting. Return a default if the setting is not set.
181     *
182     * @param string $setting_name
183     * @param string $default
184     *
185     * @return string
186     */
187    final public function getPreference(string $setting_name, string $default = ''): string
188    {
189        return DB::table('module_setting')
190            ->where('module_name', '=', $this->name())
191            ->where('setting_name', '=', $setting_name)
192            ->value('setting_value') ?? $default;
193    }
194
195    /**
196     * Set a module setting.
197     *
198     * Since module settings are NOT NULL, setting a value to NULL will cause
199     * it to be deleted.
200     *
201     * @param string $setting_name
202     * @param string $setting_value
203     *
204     * @return void
205     */
206    final public function setPreference(string $setting_name, string $setting_value): void
207    {
208        DB::table('module_setting')->updateOrInsert([
209            'module_name'  => $this->name(),
210            'setting_name' => $setting_name,
211        ], [
212            'setting_value' => $setting_value,
213        ]);
214    }
215
216    /**
217     * Get a the current access level for a module
218     *
219     * @param Tree   $tree
220     * @param string $interface
221     *
222     * @return int
223     */
224    final public function accessLevel(Tree $tree, string $interface): int
225    {
226        $access_levels = Registry::cache()->array()
227            ->remember('module-privacy-' . $tree->id(), static function () use ($tree): Collection {
228                return DB::table('module_privacy')
229                    ->where('gedcom_id', '=', $tree->id())
230                    ->get();
231            });
232
233        $row = $access_levels->first(function (object $row) use ($interface): bool {
234            return $row->interface === $interface && $row->module_name === $this->name();
235        });
236
237        return $row ? (int) $row->access_level : $this->access_level;
238    }
239
240    /**
241     * Where does this module store its resources
242     *
243     * @return string
244     */
245    public function resourcesFolder(): string
246    {
247        return 'resources/';
248    }
249}
250