xref: /webtrees/app/Module/AbstractModule.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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\Tree;
25use Fisharebest\Webtrees\Webtrees;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Support\Collection;
28use stdClass;
29
30/**
31 * Class AbstractModule - common functions for blocks
32 */
33abstract class AbstractModule implements ModuleInterface
34{
35    use ViewResponseTrait;
36
37    /** @var string A unique internal name for this module (based on the installation folder). */
38    private $name = '';
39
40    /** @var int The default access level for this module.  It can be changed in the control panel. */
41    protected $access_level = Auth::PRIV_PRIVATE;
42
43    /** @var bool The default status for this module.  It can be changed in the control panel. */
44    private $enabled = true;
45
46    /** @var string For custom modules - optional (recommended) version number */
47    public const CUSTOM_VERSION = '';
48
49    /** @var string For custom modules - link for support, upgrades, etc. */
50    public const CUSTOM_WEBSITE = '';
51
52    /**
53     * How should this module be identified in the control panel, etc.?
54     *
55     * @return string
56     */
57    public function title(): string
58    {
59        return 'Module name goes here';
60    }
61
62    /**
63     * A sentence describing what this module does.
64     *
65     * @return string
66     */
67    public function description(): string
68    {
69        return $this->title();
70    }
71
72    /**
73     * Get a block setting.
74     *
75     * Originally, this was just used for the home-page blocks.  Now, it is used by any
76     * module that has repeated blocks of content on the same page.
77     *
78     * @param int    $block_id
79     * @param string $setting_name
80     * @param string $default
81     *
82     * @return string
83     */
84    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
85    {
86        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, static function () use ($block_id): array {
87            return DB::table('block_setting')
88                ->where('block_id', '=', $block_id)
89                ->pluck('setting_value', 'setting_name')
90                ->all();
91        });
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 $this
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 a the current access level for a module
212     *
213     * @param Tree   $tree
214     * @param string $interface
215     *
216     * @return int
217     */
218    final public function accessLevel(Tree $tree, string $interface): int
219    {
220        $access_levels = app('cache.array')
221            ->rememberForever('module_privacy' . $tree->id(), static function () use ($tree): Collection {
222                return DB::table('module_privacy')
223                    ->where('gedcom_id', '=', $tree->id())
224                    ->get();
225            });
226
227        $row = $access_levels->first(function (stdClass $row) use ($interface): bool {
228            return $row->interface === $interface && $row->module_name === $this->name();
229        });
230
231        return $row ? (int) $row->access_level : $this->access_level;
232    }
233
234    /**
235     * Where does this module store its resources
236     *
237     * @return string
238     */
239    public function resourcesFolder(): string
240    {
241        return Webtrees::ROOT_DIR . 'resources/';
242    }
243}
244