xref: /webtrees/app/Module/AbstractModule.php (revision 32a20a8c86e55eaf348a020a167450a0215d52d2)
1e2a378d3SGreg Roach<?php
2e2a378d3SGreg Roach/**
3e2a378d3SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5e2a378d3SGreg Roach * This program is free software: you can redistribute it and/or modify
6e2a378d3SGreg Roach * it under the terms of the GNU General Public License as published by
7e2a378d3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8e2a378d3SGreg Roach * (at your option) any later version.
9e2a378d3SGreg Roach * This program is distributed in the hope that it will be useful,
10e2a378d3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11e2a378d3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12e2a378d3SGreg Roach * GNU General Public License for more details.
13e2a378d3SGreg Roach * You should have received a copy of the GNU General Public License
14e2a378d3SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15e2a378d3SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
223f412448SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
23e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
24e2a378d3SGreg Roach
25e2a378d3SGreg Roach/**
26e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks
27e2a378d3SGreg Roach */
28c1010edaSGreg Roachabstract class AbstractModule
29c1010edaSGreg Roach{
30e2a378d3SGreg Roach    /** @var string The directory where the module is installed */
31e2a378d3SGreg Roach    private $directory;
32e2a378d3SGreg Roach
33e2a378d3SGreg Roach    /** @var string[] A cached copy of the module settings */
34e2a378d3SGreg Roach    private $settings;
35e2a378d3SGreg Roach
3687503df0SGreg Roach    /** @var string For custom modules - optional (recommended) version number */
3716d6367aSGreg Roach    public const CUSTOM_VERSION = '';
3887503df0SGreg Roach
3987503df0SGreg Roach    /** @var string For custom modules - link for support, upgrades, etc. */
4016d6367aSGreg Roach    public const CUSTOM_WEBSITE = '';
4187503df0SGreg Roach
42e2ae4578SGreg Roach    protected $layout = 'layouts/default';
43e2ae4578SGreg Roach
44e2a378d3SGreg Roach    /**
45e2a378d3SGreg Roach     * Create a new module.
46e2a378d3SGreg Roach     *
47e2a378d3SGreg Roach     * @param string $directory Where is this module installed
48e2a378d3SGreg Roach     */
49027c6af4SGreg Roach    public function __construct(string $directory)
50c1010edaSGreg Roach    {
51e2a378d3SGreg Roach        $this->directory = $directory;
52e2a378d3SGreg Roach    }
53e2a378d3SGreg Roach
54e2a378d3SGreg Roach    /**
5576692c8bSGreg Roach     * Get a block setting.
5676692c8bSGreg Roach     *
57cbc1590aSGreg Roach     * @param int    $block_id
58e2a378d3SGreg Roach     * @param string $setting_name
5972ac996dSGreg Roach     * @param string $default_value
60e2a378d3SGreg Roach     *
6172ac996dSGreg Roach     * @return string
62e2a378d3SGreg Roach     */
63acf76a54SGreg Roach    public function getBlockSetting(int $block_id, string $setting_name, string $default_value = ''): string
64c1010edaSGreg Roach    {
65*32a20a8cSGreg Roach        $setting_value = DB::table('block_setting')
66*32a20a8cSGreg Roach            ->where('block_id', '=', $block_id)
67*32a20a8cSGreg Roach            ->where('setting_name', '=', $setting_name)
68*32a20a8cSGreg Roach            ->value('setting_value');
69e2a378d3SGreg Roach
7025c4634fSRico Sonntag        return $setting_value ?? $default_value;
71e2a378d3SGreg Roach    }
72e2a378d3SGreg Roach
73e2a378d3SGreg Roach    /**
7476692c8bSGreg Roach     * Set a block setting.
7576692c8bSGreg Roach     *
76cbc1590aSGreg Roach     * @param int    $block_id
77e2a378d3SGreg Roach     * @param string $setting_name
7820ac4041SGreg Roach     * @param string $setting_value
79e2a378d3SGreg Roach     *
80e2a378d3SGreg Roach     * @return $this
81e2a378d3SGreg Roach     */
828f53f488SRico Sonntag    public function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
83c1010edaSGreg Roach    {
84*32a20a8cSGreg Roach        DB::table('block_setting')->updateOrInsert([
85e2a378d3SGreg Roach            'block_id'      => $block_id,
86e2a378d3SGreg Roach            'setting_name'  => $setting_name,
87*32a20a8cSGreg Roach        ], [
88e2a378d3SGreg Roach            'setting_value' => $setting_value,
8913abd6f3SGreg Roach        ]);
90e2a378d3SGreg Roach
91e2a378d3SGreg Roach        return $this;
92e2a378d3SGreg Roach    }
93e2a378d3SGreg Roach
94e2a378d3SGreg Roach    /**
95e2a378d3SGreg Roach     * What is the default access level for this module?
96e2a378d3SGreg Roach     *
97e2a378d3SGreg Roach     * Some modules are aimed at admins or managers, and are not generally shown to users.
98e2a378d3SGreg Roach     *
99cbc1590aSGreg Roach     * @return int
100e2a378d3SGreg Roach     */
1018f53f488SRico Sonntag    public function defaultAccessLevel(): int
102c1010edaSGreg Roach    {
103e2a378d3SGreg Roach        // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN
104e2a378d3SGreg Roach        return Auth::PRIV_PRIVATE;
105e2a378d3SGreg Roach    }
106e2a378d3SGreg Roach
107e2a378d3SGreg Roach    /**
108e2a378d3SGreg Roach     * Provide a unique internal name for this module
109e2a378d3SGreg Roach     *
110e2a378d3SGreg Roach     * @return string
111e2a378d3SGreg Roach     */
1128f53f488SRico Sonntag    public function getName(): string
113c1010edaSGreg Roach    {
114e2a378d3SGreg Roach        return basename($this->directory);
115e2a378d3SGreg Roach    }
116e2a378d3SGreg Roach
117e2a378d3SGreg Roach    /**
118e2a378d3SGreg Roach     * Load all the settings for the module into a cache.
119e2a378d3SGreg Roach     *
120e2a378d3SGreg Roach     * Since modules may have many settings, and will probably want to use
121e2a378d3SGreg Roach     * lots of them, load them all at once and cache them.
12218d7a90dSGreg Roach     *
12318d7a90dSGreg Roach     * @return void
124e2a378d3SGreg Roach     */
125c1010edaSGreg Roach    private function loadAllSettings()
126c1010edaSGreg Roach    {
127e2a378d3SGreg Roach        if ($this->settings === null) {
1283f412448SGreg Roach            $this->settings = DB::table('module_setting')
1293f412448SGreg Roach                ->where('module_name', '=', $this->getName())
1303f412448SGreg Roach                ->pluck('setting_value', 'setting_name')
1313f412448SGreg Roach                ->all();
132e2a378d3SGreg Roach        }
133e2a378d3SGreg Roach    }
134e2a378d3SGreg Roach
135e2a378d3SGreg Roach    /**
136e2a378d3SGreg Roach     * Get a module setting. Return a default if the setting is not set.
137e2a378d3SGreg Roach     *
138e2a378d3SGreg Roach     * @param string $setting_name
139e2a378d3SGreg Roach     * @param string $default
140e2a378d3SGreg Roach     *
14115d603e7SGreg Roach     * @return string
142e2a378d3SGreg Roach     */
143c1010edaSGreg Roach    public function getPreference($setting_name, $default = '')
144c1010edaSGreg Roach    {
145e2a378d3SGreg Roach        $this->loadAllSettings();
146e2a378d3SGreg Roach
147e2a378d3SGreg Roach        if (array_key_exists($setting_name, $this->settings)) {
148e2a378d3SGreg Roach            return $this->settings[$setting_name];
149e2a378d3SGreg Roach        }
150b2ce94c6SRico Sonntag
151b2ce94c6SRico Sonntag        return $default;
152e2a378d3SGreg Roach    }
153e2a378d3SGreg Roach
154e2a378d3SGreg Roach    /**
155e2a378d3SGreg Roach     * Set a module setting.
156e2a378d3SGreg Roach     *
157e2a378d3SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
158e2a378d3SGreg Roach     * it to be deleted.
159e2a378d3SGreg Roach     *
160e2a378d3SGreg Roach     * @param string $setting_name
161e2a378d3SGreg Roach     * @param string $setting_value
16215d603e7SGreg Roach     *
16315d603e7SGreg Roach     * @return $this
164e2a378d3SGreg Roach     */
1658f53f488SRico Sonntag    public function setPreference($setting_name, $setting_value): self
166c1010edaSGreg Roach    {
167e2a378d3SGreg Roach        $this->loadAllSettings();
168e2a378d3SGreg Roach
169*32a20a8cSGreg Roach        DB::table('module_setting')->updateOrInsert([
170*32a20a8cSGreg Roach            'module_name'  => $this->getName(),
171*32a20a8cSGreg Roach            'setting_name' => $setting_name,
172*32a20a8cSGreg Roach        ], [
173*32a20a8cSGreg Roach            'setting_value' => $setting_value,
174c1010edaSGreg Roach        ]);
175b7ee5e66SCarmen Pijpers
176b7ee5e66SCarmen Pijpers        $this->settings[$setting_name] = $setting_value;
17715d603e7SGreg Roach
17815d603e7SGreg Roach        return $this;
179e2a378d3SGreg Roach    }
180e2a378d3SGreg Roach
181e2a378d3SGreg Roach    /**
182e2a378d3SGreg Roach     * Get a the current access level for a module
183e2a378d3SGreg Roach     *
184e2a378d3SGreg Roach     * @param Tree   $tree
185cbc1590aSGreg Roach     * @param string $component tab, block, menu, etc
186e2a378d3SGreg Roach     *
187cbc1590aSGreg Roach     * @return int
188e2a378d3SGreg Roach     */
189c1010edaSGreg Roach    public function getAccessLevel(Tree $tree, $component)
190c1010edaSGreg Roach    {
191*32a20a8cSGreg Roach        $access_level = DB::table('module_privacy')
192*32a20a8cSGreg Roach            ->where('gedcom_id', '=', $tree->id())
193*32a20a8cSGreg Roach            ->where('module_name', '=', $this->getName())
194*32a20a8cSGreg Roach            ->where('component', '=', $component)
195*32a20a8cSGreg Roach            ->value('access_level');
196e2a378d3SGreg Roach
197e2a378d3SGreg Roach        if ($access_level === null) {
198e2a378d3SGreg Roach            return $this->defaultAccessLevel();
199e2a378d3SGreg Roach        }
200b2ce94c6SRico Sonntag
201b2ce94c6SRico Sonntag        return (int) $access_level;
202e2a378d3SGreg Roach    }
203e2ae4578SGreg Roach
204e2ae4578SGreg Roach    /**
205e2ae4578SGreg Roach     * Create a response object from a view.
206e2ae4578SGreg Roach     *
207e2ae4578SGreg Roach     * @param string  $view_name
208e2ae4578SGreg Roach     * @param mixed[] $view_data
209e2ae4578SGreg Roach     * @param int     $status
210e2ae4578SGreg Roach     *
211e2ae4578SGreg Roach     * @return Response
212e2ae4578SGreg Roach     */
213c1010edaSGreg Roach    protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
214c1010edaSGreg Roach    {
215e2ae4578SGreg Roach        // Make the view's data available to the layout.
216e2ae4578SGreg Roach        $layout_data = $view_data;
217e2ae4578SGreg Roach
218e2ae4578SGreg Roach        // Render the view
219e2ae4578SGreg Roach        $layout_data['content'] = view($view_name, $view_data);
220e2ae4578SGreg Roach
221e2ae4578SGreg Roach        // Insert the view into the layout
222e2ae4578SGreg Roach        $html = view($this->layout, $layout_data);
223e2ae4578SGreg Roach
224e2ae4578SGreg Roach        return new Response($html, $status);
225e2ae4578SGreg Roach    }
226e2a378d3SGreg Roach}
227