xref: /webtrees/app/Module/AbstractModule.php (revision 8121b9bec19818120092699199161a1357bb8f3f)
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     * Get a module setting. Return a default if the setting is not set.
163     *
164     * @param string $setting_name
165     * @param string $default
166     *
167     * @return string
168     */
169    final public function getPreference(string $setting_name, string $default = ''): string
170    {
171        return DB::table('module_setting')
172            ->where('module_name', '=', $this->name())
173            ->where('setting_name', '=', $setting_name)
174            ->value('setting_value') ?? $default;
175    }
176
177    /**
178     * Set a module setting.
179     *
180     * Since module settings are NOT NULL, setting a value to NULL will cause
181     * it to be deleted.
182     *
183     * @param string $setting_name
184     * @param string $setting_value
185     *
186     * @return $this
187     */
188    final public function setPreference(string $setting_name, string $setting_value): void
189    {
190        DB::table('module_setting')->updateOrInsert([
191            'module_name'  => $this->name(),
192            'setting_name' => $setting_name,
193        ], [
194            'setting_value' => $setting_value,
195        ]);
196    }
197
198    /**
199     * Get a the current access level for a module
200     *
201     * @param Tree   $tree
202     * @param string $component tab, block, menu, etc
203     *
204     * @return int
205     */
206    final public function accessLevel(Tree $tree, string $component): int
207    {
208        $access_levels = app('cache.array')
209            ->rememberForever('module_privacy' . $tree->id(), function () use ($tree): Collection {
210                return DB::table('module_privacy')
211                    ->where('gedcom_id', '=', $tree->id())
212                    ->get();
213            });
214
215        $row = $access_levels->filter(function (stdClass $row) use ($component): bool {
216            return $row->component === $component && $row->module_name === $this->name();
217        })->first();
218
219        return $row ? (int) $row->access_level : $this->access_level;
220    }
221
222    /**
223     * Create a response object from a view.
224     *
225     * @param string  $view_name
226     * @param mixed[] $view_data
227     * @param int     $status
228     *
229     * @return Response
230     */
231    final protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
232    {
233        // Make the view's data available to the layout.
234        $layout_data = $view_data;
235
236        // Render the view
237        $layout_data['content'] = view($view_name, $view_data);
238
239        // Insert the view into the layout
240        $html = view($this->layout, $layout_data);
241
242        return new Response($html, $status);
243    }
244}
245