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