xref: /webtrees/app/Module/AbstractModule.php (revision 25c4634f8837160c7da388f44cc23f1b2de1459b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Tree;
21use Symfony\Component\HttpFoundation\Response;
22
23/**
24 * Class AbstractModule - common functions for blocks
25 */
26abstract class AbstractModule
27{
28    /** @var string The directory where the module is installed */
29    private $directory;
30
31    /** @var string[] A cached copy of the module settings */
32    private $settings;
33
34    /** @var string For custom modules - optional (recommended) version number */
35    const CUSTOM_VERSION = '';
36
37    /** @var string For custom modules - link for support, upgrades, etc. */
38    const CUSTOM_WEBSITE = '';
39
40    protected $layout = 'layouts/default';
41
42    /**
43     * Create a new module.
44     *
45     * @param string $directory Where is this module installed
46     */
47    public function __construct($directory)
48    {
49        $this->directory = $directory;
50    }
51
52    /**
53     * Get a block setting.
54     *
55     * @param int    $block_id
56     * @param string $setting_name
57     * @param string $default_value
58     *
59     * @return string
60     */
61    public function getBlockSetting($block_id, $setting_name, $default_value = ''): string
62    {
63        $setting_value = Database::prepare(
64            "SELECT setting_value FROM `##block_setting` WHERE block_id = :block_id AND setting_name = :setting_name"
65        )->execute([
66            'block_id'     => $block_id,
67            'setting_name' => $setting_name,
68        ])->fetchOne();
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        Database::prepare(
85            "REPLACE INTO `##block_setting` (block_id, setting_name, setting_value) VALUES (:block_id, :setting_name, :setting_value)"
86        )->execute([
87            'block_id'      => $block_id,
88            'setting_name'  => $setting_name,
89            'setting_value' => $setting_value,
90        ]);
91
92        return $this;
93    }
94
95    /**
96     * How should this module be labelled on tabs, menus, etc.?
97     *
98     * @return string
99     */
100    abstract public function getTitle(): string;
101
102    /**
103     * A sentence describing what this module does.
104     *
105     * @return string
106     */
107    abstract public function getDescription(): string;
108
109    /**
110     * What is the default access level for this module?
111     *
112     * Some modules are aimed at admins or managers, and are not generally shown to users.
113     *
114     * @return int
115     */
116    public function defaultAccessLevel(): int
117    {
118        // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN
119        return Auth::PRIV_PRIVATE;
120    }
121
122    /**
123     * Provide a unique internal name for this module
124     *
125     * @return string
126     */
127    public function getName(): string
128    {
129        return basename($this->directory);
130    }
131
132    /**
133     * Load all the settings for the module into a cache.
134     *
135     * Since modules may have many settings, and will probably want to use
136     * lots of them, load them all at once and cache them.
137     */
138    private function loadAllSettings()
139    {
140        if ($this->settings === null) {
141            $this->settings = Database::prepare(
142                "SELECT setting_name, setting_value FROM `##module_setting` WHERE module_name = ?"
143            )->execute([$this->getName()])->fetchAssoc();
144        }
145    }
146
147    /**
148     * Get a module setting. Return a default if the setting is not set.
149     *
150     * @param string $setting_name
151     * @param string $default
152     *
153     * @return string
154     */
155    public function getPreference($setting_name, $default = '')
156    {
157        $this->loadAllSettings();
158
159        if (array_key_exists($setting_name, $this->settings)) {
160            return $this->settings[$setting_name];
161        } else {
162            return $default;
163        }
164    }
165
166    /**
167     * Set a module setting.
168     *
169     * Since module settings are NOT NULL, setting a value to NULL will cause
170     * it to be deleted.
171     *
172     * @param string $setting_name
173     * @param string $setting_value
174     *
175     * @return $this
176     */
177    public function setPreference($setting_name, $setting_value): self
178    {
179        $this->loadAllSettings();
180
181        if (!array_key_exists($setting_name, $this->settings)) {
182            Database::prepare(
183                "INSERT INTO `##module_setting` (module_name, setting_name, setting_value) VALUES (?, ?, ?)"
184            )->execute([
185                $this->getName(),
186                $setting_name,
187                $setting_value,
188            ]);
189
190            $this->settings[$setting_name] = $setting_value;
191        } elseif ($setting_value !== $this->getPreference($setting_name)) {
192            Database::prepare(
193                "UPDATE `##module_setting` SET setting_value = ? WHERE module_name = ? AND setting_name = ?"
194            )->execute([
195                $setting_value,
196                $this->getName(),
197                $setting_name,
198            ]);
199
200            $this->settings[$setting_name] = $setting_value;
201        } else {
202            // Setting already exists, but with the same value - do nothing.
203        }
204
205        return $this;
206    }
207
208    /**
209     * Get a the current access level for a module
210     *
211     * @param Tree   $tree
212     * @param string $component tab, block, menu, etc
213     *
214     * @return int
215     */
216    public function getAccessLevel(Tree $tree, $component)
217    {
218        $access_level = Database::prepare(
219            "SELECT access_level FROM `##module_privacy` WHERE gedcom_id = :gedcom_id AND module_name = :module_name AND component = :component"
220        )->execute([
221            'gedcom_id'   => $tree->getTreeId(),
222            'module_name' => $this->getName(),
223            'component'   => $component,
224        ])->fetchOne();
225
226        if ($access_level === null) {
227            return $this->defaultAccessLevel();
228        } else {
229            return (int)$access_level;
230        }
231    }
232
233    /**
234     * Create a response object from a view.
235     *
236     * @param string  $view_name
237     * @param mixed[] $view_data
238     * @param int     $status
239     *
240     * @return Response
241     */
242    protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
243    {
244        // Make the view's data available to the layout.
245        $layout_data = $view_data;
246
247        // Render the view
248        $layout_data['content'] = view($view_name, $view_data);
249
250        // Insert the view into the layout
251        $html = view($this->layout, $layout_data);
252
253        return new Response($html, $status);
254    }
255}
256