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