xref: /webtrees/app/Module/AbstractModule.php (revision dae8440ab1203b4eb5e333de33e187c1dfc5b759)
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 Symfony\Component\HttpFoundation\Response;
24
25/**
26 * Class AbstractModule - common functions for blocks
27 */
28abstract class AbstractModule
29{
30    /** @var string The directory where the module is installed */
31    private $directory;
32
33    /** @var string[] A cached copy of the module settings */
34    private $settings;
35
36    /** @var string For custom modules - optional (recommended) version number */
37    public const CUSTOM_VERSION = '';
38
39    /** @var string For custom modules - link for support, upgrades, etc. */
40    public const CUSTOM_WEBSITE = '';
41
42    protected $layout = 'layouts/default';
43
44    /**
45     * Create a new module.
46     *
47     * @param string $directory Where is this module installed
48     */
49    public function __construct(string $directory)
50    {
51        $this->directory = $directory;
52    }
53
54    /**
55     * Get a block setting.
56     *
57     * @param int    $block_id
58     * @param string $setting_name
59     * @param string $default_value
60     *
61     * @return string
62     */
63    public function getBlockSetting(int $block_id, string $setting_name, string $default_value = ''): string
64    {
65        $setting_value = DB::table('block_setting')
66            ->where('block_id', '=', $block_id)
67            ->where('setting_name', '=', $setting_name)
68            ->value('setting_value');
69
70        return $setting_value ?? $default_value;
71    }
72
73    /**
74     * Set a block setting.
75     *
76     * @param int    $block_id
77     * @param string $setting_name
78     * @param string $setting_value
79     *
80     * @return $this
81     */
82    public function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
83    {
84        DB::table('block_setting')->updateOrInsert([
85            'block_id'      => $block_id,
86            'setting_name'  => $setting_name,
87        ], [
88            'setting_value' => $setting_value,
89        ]);
90
91        return $this;
92    }
93
94    /**
95     * What is the default access level for this module?
96     *
97     * Some modules are aimed at admins or managers, and are not generally shown to users.
98     *
99     * @return int
100     */
101    public function defaultAccessLevel(): int
102    {
103        // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN
104        return Auth::PRIV_PRIVATE;
105    }
106
107    /**
108     * Provide a unique internal name for this module
109     *
110     * @return string
111     */
112    public function getName(): string
113    {
114        return basename($this->directory);
115    }
116
117    /**
118     * Load all the settings for the module into a cache.
119     *
120     * Since modules may have many settings, and will probably want to use
121     * lots of them, load them all at once and cache them.
122     *
123     * @return void
124     */
125    private function loadAllSettings()
126    {
127        if ($this->settings === null) {
128            $this->settings = DB::table('module_setting')
129                ->where('module_name', '=', $this->getName())
130                ->pluck('setting_value', 'setting_name')
131                ->all();
132        }
133    }
134
135    /**
136     * Get a module setting. Return a default if the setting is not set.
137     *
138     * @param string $setting_name
139     * @param string $default
140     *
141     * @return string
142     */
143    public function getPreference($setting_name, $default = '')
144    {
145        $this->loadAllSettings();
146
147        if (array_key_exists($setting_name, $this->settings)) {
148            return $this->settings[$setting_name];
149        }
150
151        return $default;
152    }
153
154    /**
155     * Set a module setting.
156     *
157     * Since module settings are NOT NULL, setting a value to NULL will cause
158     * it to be deleted.
159     *
160     * @param string $setting_name
161     * @param string $setting_value
162     *
163     * @return $this
164     */
165    public function setPreference($setting_name, $setting_value): self
166    {
167        $this->loadAllSettings();
168
169        DB::table('module_setting')->updateOrInsert([
170            'module_name'  => $this->getName(),
171            'setting_name' => $setting_name,
172        ], [
173            'setting_value' => $setting_value,
174        ]);
175
176        $this->settings[$setting_name] = $setting_value;
177
178        return $this;
179    }
180
181    /**
182     * Get a the current access level for a module
183     *
184     * @param Tree   $tree
185     * @param string $component tab, block, menu, etc
186     *
187     * @return int
188     */
189    public function getAccessLevel(Tree $tree, $component)
190    {
191        $access_level = DB::table('module_privacy')
192            ->where('gedcom_id', '=', $tree->id())
193            ->where('module_name', '=', $this->getName())
194            ->where('component', '=', $component)
195            ->value('access_level');
196
197        if ($access_level === null) {
198            return $this->defaultAccessLevel();
199        }
200
201        return (int) $access_level;
202    }
203
204    /**
205     * Create a response object from a view.
206     *
207     * @param string  $view_name
208     * @param mixed[] $view_data
209     * @param int     $status
210     *
211     * @return Response
212     */
213    protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
214    {
215        // Make the view's data available to the layout.
216        $layout_data = $view_data;
217
218        // Render the view
219        $layout_data['content'] = view($view_name, $view_data);
220
221        // Insert the view into the layout
222        $html = view($this->layout, $layout_data);
223
224        return new Response($html, $status);
225    }
226}
227