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