xref: /webtrees/app/Module/AbstractModule.php (revision 49a243cb5fe7c21b24e262552d556b018bfe3f41)
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    public function description(): string
51    {
52        // TODO: Implement description() method.
53    }
54
55    /**
56     * Get a block setting.
57     *
58     * Originally, this was just used for the home-page blocks.  Now, it is used by any
59     * module that has repeated blocks of content on the same page.
60     *
61     * @param int    $block_id
62     * @param string $setting_name
63     * @param string $default
64     *
65     * @return string
66     */
67    protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
68    {
69        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, function () use ($block_id): array {
70            return DB::table('block_setting')
71                ->where('block_id', '=', $block_id)
72                ->pluck('setting_value', 'setting_name')
73                ->all();
74        });
75
76        return $settings[$setting_name] ?? $default;
77    }
78
79    /**
80     * Set a block setting.
81     *
82     * @param int    $block_id
83     * @param string $setting_name
84     * @param string $setting_value
85     *
86     * @return $this
87     */
88    protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
89    {
90        DB::table('block_setting')->updateOrInsert([
91            'block_id'      => $block_id,
92            'setting_name'  => $setting_name,
93        ], [
94            'setting_value' => $setting_value,
95        ]);
96
97        return $this;
98    }
99
100    /**
101     * A unique internal name for this module (based on the installation folder).
102     *
103     * @param string $name
104     *
105     * @return ModuleInterface
106     */
107    public function setName(string $name): ModuleInterface
108    {
109        $this->name = $name;
110
111        return $this;
112    }
113
114    /**
115     * A unique internal name for this module (based on the installation folder).
116     *
117     * @return string
118     */
119    public function getName(): string
120    {
121        return $this->name;
122    }
123
124    /**
125     * Modules are either enabled or disabled.
126     *
127     * @param bool $enabled
128     *
129     * @return ModuleInterface
130     */
131    public function setEnabled(bool $enabled): ModuleInterface
132    {
133        $this->enabled = $enabled;
134
135        return $this;
136    }
137
138    /**
139     * Modules are either enabled or disabled.
140     *
141     * @return bool
142     */
143    public function isEnabled(): bool
144    {
145        return $this->enabled;
146    }
147
148    /**
149     * Get a module setting. Return a default if the setting is not set.
150     *
151     * @param string $setting_name
152     * @param string $default
153     *
154     * @return string
155     */
156    public function getPreference($setting_name, $default = ''): string
157    {
158        return DB::table('module_setting')
159            ->where('module_name', '=', $this->getName())
160            ->where('setting_name', '=', $setting_name)
161            ->value('setting_value') ?? $default;
162    }
163
164    /**
165     * Set a module setting.
166     *
167     * Since module settings are NOT NULL, setting a value to NULL will cause
168     * it to be deleted.
169     *
170     * @param string $setting_name
171     * @param string $setting_value
172     *
173     * @return $this
174     */
175    public function setPreference($setting_name, $setting_value): self
176    {
177        DB::table('module_setting')->updateOrInsert([
178            'module_name'  => $this->getName(),
179            'setting_name' => $setting_name,
180        ], [
181            'setting_value' => $setting_value,
182        ]);
183
184        return $this;
185    }
186
187    /**
188     * Get a the current access level for a module
189     *
190     * @param Tree   $tree
191     * @param string $component tab, block, menu, etc
192     *
193     * @return int
194     */
195    public function accessLevel(Tree $tree, string $component): int
196    {
197        $access_levels = app('cache.array')
198            ->rememberForever('module_privacy' . $tree->id(), function () use ($tree): Collection {
199                return DB::table('module_privacy')
200                    ->where('gedcom_id', '=', $tree->id())
201                    ->get();
202            });
203
204        $row = $access_levels->filter(function (stdClass $row) use ($component): bool {
205            return $row->component === $component && $row->module_name === $this->getName();
206        })->first();
207
208        return $row ? (int) $row->access_level : $this->access_level;
209    }
210
211    /**
212     * Create a response object from a view.
213     *
214     * @param string  $view_name
215     * @param mixed[] $view_data
216     * @param int     $status
217     *
218     * @return Response
219     */
220    protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
221    {
222        // Make the view's data available to the layout.
223        $layout_data = $view_data;
224
225        // Render the view
226        $layout_data['content'] = view($view_name, $view_data);
227
228        // Insert the view into the layout
229        $html = view($this->layout, $layout_data);
230
231        return new Response($html, $status);
232    }
233}
234