xref: /webtrees/app/Module/AbstractModule.php (revision e24444eeefe1caed93aa11866313e0407b4ce028)
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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Database;
22use Fisharebest\Webtrees\Tree;
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    const CUSTOM_VERSION = '';
38
39    /** @var string For custom modules - link for support, upgrades, etc. */
40    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 = Database::prepare(
66            "SELECT setting_value FROM `##block_setting` WHERE block_id = :block_id AND setting_name = :setting_name"
67        )->execute([
68            'block_id'     => $block_id,
69            'setting_name' => $setting_name,
70        ])->fetchOne();
71
72        return $setting_value ?? $default_value;
73    }
74
75    /**
76     * Set a block setting.
77     *
78     * @param int    $block_id
79     * @param string $setting_name
80     * @param string $setting_value
81     *
82     * @return $this
83     */
84    public function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
85    {
86        Database::prepare(
87            "REPLACE INTO `##block_setting` (block_id, setting_name, setting_value) VALUES (:block_id, :setting_name, :setting_value)"
88        )->execute([
89            'block_id'      => $block_id,
90            'setting_name'  => $setting_name,
91            'setting_value' => $setting_value,
92        ]);
93
94        return $this;
95    }
96
97    /**
98     * What is the default access level for this module?
99     *
100     * Some modules are aimed at admins or managers, and are not generally shown to users.
101     *
102     * @return int
103     */
104    public function defaultAccessLevel(): int
105    {
106        // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN
107        return Auth::PRIV_PRIVATE;
108    }
109
110    /**
111     * Provide a unique internal name for this module
112     *
113     * @return string
114     */
115    public function getName(): string
116    {
117        return basename($this->directory);
118    }
119
120    /**
121     * Load all the settings for the module into a cache.
122     *
123     * Since modules may have many settings, and will probably want to use
124     * lots of them, load them all at once and cache them.
125     *
126     * @return void
127     */
128    private function loadAllSettings()
129    {
130        if ($this->settings === null) {
131            $this->settings = Database::prepare(
132                "SELECT setting_name, setting_value FROM `##module_setting` WHERE module_name = ?"
133            )->execute([$this->getName()])->fetchAssoc();
134        }
135    }
136
137    /**
138     * Get a module setting. Return a default if the setting is not set.
139     *
140     * @param string $setting_name
141     * @param string $default
142     *
143     * @return string
144     */
145    public function getPreference($setting_name, $default = '')
146    {
147        $this->loadAllSettings();
148
149        if (array_key_exists($setting_name, $this->settings)) {
150            return $this->settings[$setting_name];
151        }
152
153        return $default;
154    }
155
156    /**
157     * Set a module setting.
158     *
159     * Since module settings are NOT NULL, setting a value to NULL will cause
160     * it to be deleted.
161     *
162     * @param string $setting_name
163     * @param string $setting_value
164     *
165     * @return $this
166     */
167    public function setPreference($setting_name, $setting_value): self
168    {
169        $this->loadAllSettings();
170
171        if (!array_key_exists($setting_name, $this->settings)) {
172            Database::prepare(
173                "INSERT INTO `##module_setting` (module_name, setting_name, setting_value) VALUES (?, ?, ?)"
174            )->execute([
175                $this->getName(),
176                $setting_name,
177                $setting_value,
178            ]);
179
180            $this->settings[$setting_name] = $setting_value;
181        } elseif ($setting_value !== $this->getPreference($setting_name)) {
182            Database::prepare(
183                "UPDATE `##module_setting` SET setting_value = ? WHERE module_name = ? AND setting_name = ?"
184            )->execute([
185                $setting_value,
186                $this->getName(),
187                $setting_name,
188            ]);
189
190            $this->settings[$setting_name] = $setting_value;
191        }
192
193        return $this;
194    }
195
196    /**
197     * Get a the current access level for a module
198     *
199     * @param Tree   $tree
200     * @param string $component tab, block, menu, etc
201     *
202     * @return int
203     */
204    public function getAccessLevel(Tree $tree, $component)
205    {
206        $access_level = Database::prepare(
207            "SELECT access_level FROM `##module_privacy` WHERE gedcom_id = :gedcom_id AND module_name = :module_name AND component = :component"
208        )->execute([
209            'gedcom_id'   => $tree->getTreeId(),
210            'module_name' => $this->getName(),
211            'component'   => $component,
212        ])->fetchOne();
213
214        if ($access_level === null) {
215            return $this->defaultAccessLevel();
216        }
217
218        return (int) $access_level;
219    }
220
221    /**
222     * Create a response object from a view.
223     *
224     * @param string  $view_name
225     * @param mixed[] $view_data
226     * @param int     $status
227     *
228     * @return Response
229     */
230    protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
231    {
232        // Make the view's data available to the layout.
233        $layout_data = $view_data;
234
235        // Render the view
236        $layout_data['content'] = view($view_name, $view_data);
237
238        // Insert the view into the layout
239        $html = view($this->layout, $layout_data);
240
241        return new Response($html, $status);
242    }
243}
244