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