xref: /webtrees/app/Module/AbstractModule.php (revision 87cca37c8b5ee0f07397179f377cdfde768951bb)
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;
2349a243cbSGreg Roachuse Illuminate\Support\Collection;
2449a243cbSGreg Roachuse stdClass;
25e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
26e2a378d3SGreg Roach
27e2a378d3SGreg Roach/**
28e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks
29e2a378d3SGreg Roach */
3049a243cbSGreg Roachabstract class AbstractModule implements ModuleInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    /** @var string A unique internal name for this module (based on the installation folder). */
3349a243cbSGreg Roach    private $name = '';
34e2a378d3SGreg Roach
3549a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
3649a243cbSGreg Roach    protected $access_level = Auth::PRIV_PRIVATE;
3749a243cbSGreg Roach
3849a243cbSGreg Roach    /** @var bool The default status for this module.  It can be changed in the control panel. */
3949a243cbSGreg Roach    private $enabled = true;
40e2a378d3SGreg Roach
4187503df0SGreg Roach    /** @var string For custom modules - optional (recommended) version number */
4216d6367aSGreg Roach    public const CUSTOM_VERSION = '';
4387503df0SGreg Roach
4487503df0SGreg Roach    /** @var string For custom modules - link for support, upgrades, etc. */
4516d6367aSGreg Roach    public const CUSTOM_WEBSITE = '';
4687503df0SGreg Roach
4749a243cbSGreg Roach    /** @var string How to render view responses */
48e2ae4578SGreg Roach    protected $layout = 'layouts/default';
49e2ae4578SGreg Roach
50c1afbf58SGreg Roach    /**
51c1afbf58SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
52c1afbf58SGreg Roach     *
53c1afbf58SGreg Roach     * @return string
54c1afbf58SGreg Roach     */
55c1afbf58SGreg Roach    public function title(): string
56c1afbf58SGreg Roach    {
57c1afbf58SGreg Roach        return 'Module name goes here';
58c1afbf58SGreg Roach    }
59c1afbf58SGreg Roach
60c1afbf58SGreg Roach    /**
61c1afbf58SGreg Roach     * A sentence describing what this module does.
62c1afbf58SGreg Roach     *
63c1afbf58SGreg Roach     * @return string
64c1afbf58SGreg Roach     */
6549a243cbSGreg Roach    public function description(): string
66c1010edaSGreg Roach    {
67c1afbf58SGreg Roach        return $this->title();
68e2a378d3SGreg Roach    }
69e2a378d3SGreg Roach
70e2a378d3SGreg Roach    /**
7176692c8bSGreg Roach     * Get a block setting.
7276692c8bSGreg Roach     *
7349a243cbSGreg Roach     * Originally, this was just used for the home-page blocks.  Now, it is used by any
7449a243cbSGreg Roach     * module that has repeated blocks of content on the same page.
7549a243cbSGreg Roach     *
76cbc1590aSGreg Roach     * @param int    $block_id
77e2a378d3SGreg Roach     * @param string $setting_name
7883c7613eSGreg Roach     * @param string $default
79e2a378d3SGreg Roach     *
8072ac996dSGreg Roach     * @return string
81e2a378d3SGreg Roach     */
8252f398fdSGreg Roach    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
83c1010edaSGreg Roach    {
8449a243cbSGreg Roach        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, function () use ($block_id): array {
8583c7613eSGreg Roach            return DB::table('block_setting')
8632a20a8cSGreg Roach                ->where('block_id', '=', $block_id)
8783c7613eSGreg Roach                ->pluck('setting_value', 'setting_name')
8883c7613eSGreg Roach                ->all();
8983c7613eSGreg Roach        });
90e2a378d3SGreg Roach
9183c7613eSGreg Roach        return $settings[$setting_name] ?? $default;
92e2a378d3SGreg Roach    }
93e2a378d3SGreg Roach
94e2a378d3SGreg Roach    /**
9576692c8bSGreg Roach     * Set a block setting.
9676692c8bSGreg Roach     *
97cbc1590aSGreg Roach     * @param int    $block_id
98e2a378d3SGreg Roach     * @param string $setting_name
9920ac4041SGreg Roach     * @param string $setting_value
100e2a378d3SGreg Roach     *
101e2a378d3SGreg Roach     * @return $this
102e2a378d3SGreg Roach     */
10352f398fdSGreg Roach    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
104c1010edaSGreg Roach    {
10532a20a8cSGreg Roach        DB::table('block_setting')->updateOrInsert([
106e2a378d3SGreg Roach            'block_id'      => $block_id,
107e2a378d3SGreg Roach            'setting_name'  => $setting_name,
10832a20a8cSGreg Roach        ], [
109e2a378d3SGreg Roach            'setting_value' => $setting_value,
11013abd6f3SGreg Roach        ]);
111e2a378d3SGreg Roach
112e2a378d3SGreg Roach        return $this;
113e2a378d3SGreg Roach    }
114e2a378d3SGreg Roach
115e2a378d3SGreg Roach    /**
11649a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
117e2a378d3SGreg Roach     *
11849a243cbSGreg Roach     * @param string $name
119e2a378d3SGreg Roach     *
12037eb8894SGreg Roach     * @return void
121e2a378d3SGreg Roach     */
12237eb8894SGreg Roach    final public function setName(string $name): void
123c1010edaSGreg Roach    {
12449a243cbSGreg Roach        $this->name = $name;
125e2a378d3SGreg Roach    }
126e2a378d3SGreg Roach
127e2a378d3SGreg Roach    /**
12849a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
129e2a378d3SGreg Roach     *
130e2a378d3SGreg Roach     * @return string
131e2a378d3SGreg Roach     */
13226684e68SGreg Roach    final public function name(): string
133c1010edaSGreg Roach    {
13449a243cbSGreg Roach        return $this->name;
135e2a378d3SGreg Roach    }
136e2a378d3SGreg Roach
137e2a378d3SGreg Roach    /**
13849a243cbSGreg Roach     * Modules are either enabled or disabled.
139e2a378d3SGreg Roach     *
14049a243cbSGreg Roach     * @param bool $enabled
14118d7a90dSGreg Roach     *
14249a243cbSGreg Roach     * @return ModuleInterface
143e2a378d3SGreg Roach     */
14452f398fdSGreg Roach    final public function setEnabled(bool $enabled): ModuleInterface
145c1010edaSGreg Roach    {
14649a243cbSGreg Roach        $this->enabled = $enabled;
14749a243cbSGreg Roach
14849a243cbSGreg Roach        return $this;
149e2a378d3SGreg Roach    }
15049a243cbSGreg Roach
15149a243cbSGreg Roach    /**
15249a243cbSGreg Roach     * Modules are either enabled or disabled.
15349a243cbSGreg Roach     *
15449a243cbSGreg Roach     * @return bool
15549a243cbSGreg Roach     */
15652f398fdSGreg Roach    final public function isEnabled(): bool
15749a243cbSGreg Roach    {
15849a243cbSGreg Roach        return $this->enabled;
159e2a378d3SGreg Roach    }
160e2a378d3SGreg Roach
161e2a378d3SGreg Roach    /**
162888ddf4fSGreg Roach     * Should this module be enabled when it is first installed?
163888ddf4fSGreg Roach     *
164888ddf4fSGreg Roach     * @return bool
165888ddf4fSGreg Roach     */
166888ddf4fSGreg Roach    public function isEnabledByDefault(): bool
167888ddf4fSGreg Roach    {
168888ddf4fSGreg Roach        return true;
169888ddf4fSGreg Roach    }
170888ddf4fSGreg Roach
171888ddf4fSGreg Roach    /**
172e2a378d3SGreg Roach     * Get a module setting. Return a default if the setting is not set.
173e2a378d3SGreg Roach     *
174e2a378d3SGreg Roach     * @param string $setting_name
175e2a378d3SGreg Roach     * @param string $default
176e2a378d3SGreg Roach     *
17715d603e7SGreg Roach     * @return string
178e2a378d3SGreg Roach     */
17937eb8894SGreg Roach    final public function getPreference(string $setting_name, string $default = ''): string
180c1010edaSGreg Roach    {
18149a243cbSGreg Roach        return DB::table('module_setting')
18226684e68SGreg Roach            ->where('module_name', '=', $this->name())
18349a243cbSGreg Roach            ->where('setting_name', '=', $setting_name)
18449a243cbSGreg Roach            ->value('setting_value') ?? $default;
185e2a378d3SGreg Roach    }
186e2a378d3SGreg Roach
187e2a378d3SGreg Roach    /**
188e2a378d3SGreg Roach     * Set a module setting.
189e2a378d3SGreg Roach     *
190e2a378d3SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
191e2a378d3SGreg Roach     * it to be deleted.
192e2a378d3SGreg Roach     *
193e2a378d3SGreg Roach     * @param string $setting_name
194e2a378d3SGreg Roach     * @param string $setting_value
19515d603e7SGreg Roach     *
196ea02ddf4SGreg Roach     * @return void
197e2a378d3SGreg Roach     */
19837eb8894SGreg Roach    final public function setPreference(string $setting_name, string $setting_value): void
199c1010edaSGreg Roach    {
20032a20a8cSGreg Roach        DB::table('module_setting')->updateOrInsert([
20126684e68SGreg Roach            'module_name'  => $this->name(),
20232a20a8cSGreg Roach            'setting_name' => $setting_name,
20332a20a8cSGreg Roach        ], [
20432a20a8cSGreg Roach            'setting_value' => $setting_value,
205c1010edaSGreg Roach        ]);
206e2a378d3SGreg Roach    }
207e2a378d3SGreg Roach
208e2a378d3SGreg Roach    /**
209e2a378d3SGreg Roach     * Get a the current access level for a module
210e2a378d3SGreg Roach     *
211e2a378d3SGreg Roach     * @param Tree   $tree
212*87cca37cSGreg Roach     * @param string $interface
213e2a378d3SGreg Roach     *
214cbc1590aSGreg Roach     * @return int
215e2a378d3SGreg Roach     */
216*87cca37cSGreg Roach    final public function accessLevel(Tree $tree, string $interface): int
217c1010edaSGreg Roach    {
21849a243cbSGreg Roach        $access_levels = app('cache.array')
21949a243cbSGreg Roach            ->rememberForever('module_privacy' . $tree->id(), function () use ($tree): Collection {
22049a243cbSGreg Roach                return DB::table('module_privacy')
22132a20a8cSGreg Roach                    ->where('gedcom_id', '=', $tree->id())
22249a243cbSGreg Roach                    ->get();
22349a243cbSGreg Roach            });
224e2a378d3SGreg Roach
225*87cca37cSGreg Roach        $row = $access_levels->filter(function (stdClass $row) use ($interface): bool {
226*87cca37cSGreg Roach            return $row->interface === $interface && $row->module_name === $this->name();
22749a243cbSGreg Roach        })->first();
228b2ce94c6SRico Sonntag
22949a243cbSGreg Roach        return $row ? (int) $row->access_level : $this->access_level;
230e2a378d3SGreg Roach    }
231e2ae4578SGreg Roach
232e2ae4578SGreg Roach    /**
233e2ae4578SGreg Roach     * Create a response object from a view.
234e2ae4578SGreg Roach     *
235e2ae4578SGreg Roach     * @param string  $view_name
236e2ae4578SGreg Roach     * @param mixed[] $view_data
237e2ae4578SGreg Roach     * @param int     $status
238e2ae4578SGreg Roach     *
239e2ae4578SGreg Roach     * @return Response
240e2ae4578SGreg Roach     */
24152f398fdSGreg Roach    final protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
242c1010edaSGreg Roach    {
243e2ae4578SGreg Roach        // Make the view's data available to the layout.
244e2ae4578SGreg Roach        $layout_data = $view_data;
245e2ae4578SGreg Roach
246e2ae4578SGreg Roach        // Render the view
247e2ae4578SGreg Roach        $layout_data['content'] = view($view_name, $view_data);
248e2ae4578SGreg Roach
249e2ae4578SGreg Roach        // Insert the view into the layout
250e2ae4578SGreg Roach        $html = view($this->layout, $layout_data);
251e2ae4578SGreg Roach
252e2ae4578SGreg Roach        return new Response($html, $status);
253e2ae4578SGreg Roach    }
254e2a378d3SGreg Roach}
255