xref: /webtrees/app/Module/AbstractModule.php (revision cab242e7d7a773b0a6dab130048696e26fd6612c)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Tree;
22use Illuminate\Database\Capsule\Manager as DB;
23use Illuminate\Support\Collection;
24use stdClass;
25use Symfony\Component\HttpFoundation\Response;
26
27/**
28 * Class AbstractModule - common functions for blocks
29 */
30abstract class AbstractModule implements ModuleInterface
31{
32    /** @var string A unique internal name for this module (based on the installation folder). */
33    private $name = '';
34
35    /** @var int The default access level for this module.  It can be changed in the control panel. */
36    protected $access_level = Auth::PRIV_PRIVATE;
37
38    /** @var bool The default status for this module.  It can be changed in the control panel. */
39    private $enabled = true;
40
41    /** @var string For custom modules - optional (recommended) version number */
42    public const CUSTOM_VERSION = '';
43
44    /** @var string For custom modules - link for support, upgrades, etc. */
45    public const CUSTOM_WEBSITE = '';
46
47    /** @var string How to render view responses */
48    protected $layout = 'layouts/default';
49
50    /**
51     * How should this module be labelled on tabs, menus, etc.?
52     *
53     * @return string
54     */
55    public function title(): string
56    {
57        return 'Module name goes here';
58    }
59
60    /**
61     * A sentence describing what this module does.
62     *
63     * @return string
64     */
65    public function description(): string
66    {
67        return $this->title();
68    }
69
70    /**
71     * Get a block setting.
72     *
73     * Originally, this was just used for the home-page blocks.  Now, it is used by any
74     * module that has repeated blocks of content on the same page.
75     *
76     * @param int    $block_id
77     * @param string $setting_name
78     * @param string $default
79     *
80     * @return string
81     */
82    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
83    {
84        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, function () use ($block_id): array {
85            return DB::table('block_setting')
86                ->where('block_id', '=', $block_id)
87                ->pluck('setting_value', 'setting_name')
88                ->all();
89        });
90
91        return $settings[$setting_name] ?? $default;
92    }
93
94    /**
95     * Set a block setting.
96     *
97     * @param int    $block_id
98     * @param string $setting_name
99     * @param string $setting_value
100     *
101     * @return $this
102     */
103    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
104    {
105        DB::table('block_setting')->updateOrInsert([
106            'block_id'      => $block_id,
107            'setting_name'  => $setting_name,
108        ], [
109            'setting_value' => $setting_value,
110        ]);
111
112        return $this;
113    }
114
115    /**
116     * A unique internal name for this module (based on the installation folder).
117     *
118     * @param string $name
119     *
120     * @return void
121     */
122    final public function setName(string $name): void
123    {
124        $this->name = $name;
125    }
126
127    /**
128     * A unique internal name for this module (based on the installation folder).
129     *
130     * @return string
131     */
132    final public function name(): string
133    {
134        return $this->name;
135    }
136
137    /**
138     * Modules are either enabled or disabled.
139     *
140     * @param bool $enabled
141     *
142     * @return ModuleInterface
143     */
144    final public function setEnabled(bool $enabled): ModuleInterface
145    {
146        $this->enabled = $enabled;
147
148        return $this;
149    }
150
151    /**
152     * Modules are either enabled or disabled.
153     *
154     * @return bool
155     */
156    final public function isEnabled(): bool
157    {
158        return $this->enabled;
159    }
160
161    /**
162     * Should this module be enabled when it is first installed?
163     *
164     * @return bool
165     */
166    public function isEnabledByDefault(): bool
167    {
168        return true;
169    }
170
171    /**
172     * Get a module setting. Return a default if the setting is not set.
173     *
174     * @param string $setting_name
175     * @param string $default
176     *
177     * @return string
178     */
179    final public function getPreference(string $setting_name, string $default = ''): string
180    {
181        return DB::table('module_setting')
182            ->where('module_name', '=', $this->name())
183            ->where('setting_name', '=', $setting_name)
184            ->value('setting_value') ?? $default;
185    }
186
187    /**
188     * Set a module setting.
189     *
190     * Since module settings are NOT NULL, setting a value to NULL will cause
191     * it to be deleted.
192     *
193     * @param string $setting_name
194     * @param string $setting_value
195     *
196     * @return void
197     */
198    final public function setPreference(string $setting_name, string $setting_value): void
199    {
200        DB::table('module_setting')->updateOrInsert([
201            'module_name'  => $this->name(),
202            'setting_name' => $setting_name,
203        ], [
204            'setting_value' => $setting_value,
205        ]);
206    }
207
208    /**
209     * Get a the current access level for a module
210     *
211     * @param Tree   $tree
212     * @param string $interface
213     *
214     * @return int
215     */
216    final public function accessLevel(Tree $tree, string $interface): int
217    {
218        $access_levels = app('cache.array')
219            ->rememberForever('module_privacy' . $tree->id(), function () use ($tree): Collection {
220                return DB::table('module_privacy')
221                    ->where('gedcom_id', '=', $tree->id())
222                    ->get();
223            });
224
225        $row = $access_levels->filter(function (stdClass $row) use ($interface): bool {
226            return $row->interface === $interface && $row->module_name === $this->name();
227        })->first();
228
229        return $row ? (int) $row->access_level : $this->access_level;
230    }
231
232    /**
233     * Create a response object from a view.
234     *
235     * @param string  $view_name
236     * @param mixed[] $view_data
237     * @param int     $status
238     *
239     * @return Response
240     */
241    final protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
242    {
243        // Make the view's data available to the layout.
244        $layout_data = $view_data;
245
246        // Render the view
247        $layout_data['content'] = view($view_name, $view_data);
248
249        // Insert the view into the layout
250        $html = view($this->layout, $layout_data);
251
252        return new Response($html, $status);
253    }
254}
255