xref: /webtrees/app/Module/AbstractModule.php (revision 9e18e23b968678b192e5541acd3252e4978d69c3)
1e2a378d3SGreg Roach<?php
23976b470SGreg Roach
3e2a378d3SGreg Roach/**
4e2a378d3SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6e2a378d3SGreg Roach * This program is free software: you can redistribute it and/or modify
7e2a378d3SGreg Roach * it under the terms of the GNU General Public License as published by
8e2a378d3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e2a378d3SGreg Roach * (at your option) any later version.
10e2a378d3SGreg Roach * This program is distributed in the hope that it will be useful,
11e2a378d3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e2a378d3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e2a378d3SGreg Roach * GNU General Public License for more details.
14e2a378d3SGreg Roach * You should have received a copy of the GNU General Public License
15e2a378d3SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16e2a378d3SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23606471d5SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
25f397d0fdSGreg Roachuse Fisharebest\Webtrees\Webtrees;
263f412448SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2749a243cbSGreg Roachuse Illuminate\Support\Collection;
2849a243cbSGreg Roachuse stdClass;
29e2a378d3SGreg Roach
30e2a378d3SGreg Roach/**
31e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks
32e2a378d3SGreg Roach */
3349a243cbSGreg Roachabstract class AbstractModule implements ModuleInterface
34c1010edaSGreg Roach{
35606471d5SGreg Roach    use ViewResponseTrait;
36606471d5SGreg Roach
3749a243cbSGreg Roach    /** @var string A unique internal name for this module (based on the installation folder). */
3849a243cbSGreg Roach    private $name = '';
39e2a378d3SGreg Roach
4049a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
4149a243cbSGreg Roach    protected $access_level = Auth::PRIV_PRIVATE;
4249a243cbSGreg Roach
4349a243cbSGreg Roach    /** @var bool The default status for this module.  It can be changed in the control panel. */
4449a243cbSGreg Roach    private $enabled = true;
45e2a378d3SGreg Roach
4687503df0SGreg Roach    /** @var string For custom modules - optional (recommended) version number */
4716d6367aSGreg Roach    public const CUSTOM_VERSION = '';
4887503df0SGreg Roach
4987503df0SGreg Roach    /** @var string For custom modules - link for support, upgrades, etc. */
5016d6367aSGreg Roach    public const CUSTOM_WEBSITE = '';
5187503df0SGreg Roach
52c1afbf58SGreg Roach    /**
53*9e18e23bSGreg Roach     * Called for all *enabled* modules.
54*9e18e23bSGreg Roach     */
55*9e18e23bSGreg Roach    public function boot(): void
56*9e18e23bSGreg Roach    {
57*9e18e23bSGreg Roach    }
58*9e18e23bSGreg Roach
59*9e18e23bSGreg Roach    /**
600cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
61c1afbf58SGreg Roach     *
62c1afbf58SGreg Roach     * @return string
63c1afbf58SGreg Roach     */
64c1afbf58SGreg Roach    public function title(): string
65c1afbf58SGreg Roach    {
66c1afbf58SGreg Roach        return 'Module name goes here';
67c1afbf58SGreg Roach    }
68c1afbf58SGreg Roach
69c1afbf58SGreg Roach    /**
70c1afbf58SGreg Roach     * A sentence describing what this module does.
71c1afbf58SGreg Roach     *
72c1afbf58SGreg Roach     * @return string
73c1afbf58SGreg Roach     */
7449a243cbSGreg Roach    public function description(): string
75c1010edaSGreg Roach    {
76c1afbf58SGreg Roach        return $this->title();
77e2a378d3SGreg Roach    }
78e2a378d3SGreg Roach
79e2a378d3SGreg Roach    /**
8076692c8bSGreg Roach     * Get a block setting.
8176692c8bSGreg Roach     *
8249a243cbSGreg Roach     * Originally, this was just used for the home-page blocks.  Now, it is used by any
8349a243cbSGreg Roach     * module that has repeated blocks of content on the same page.
8449a243cbSGreg Roach     *
85cbc1590aSGreg Roach     * @param int    $block_id
86e2a378d3SGreg Roach     * @param string $setting_name
8783c7613eSGreg Roach     * @param string $default
88e2a378d3SGreg Roach     *
8972ac996dSGreg Roach     * @return string
90e2a378d3SGreg Roach     */
9152f398fdSGreg Roach    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
92c1010edaSGreg Roach    {
930b5fd0a6SGreg Roach        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, static function () use ($block_id): array {
9483c7613eSGreg Roach            return DB::table('block_setting')
9532a20a8cSGreg Roach                ->where('block_id', '=', $block_id)
9683c7613eSGreg Roach                ->pluck('setting_value', 'setting_name')
9783c7613eSGreg Roach                ->all();
9883c7613eSGreg Roach        });
99e2a378d3SGreg Roach
10083c7613eSGreg Roach        return $settings[$setting_name] ?? $default;
101e2a378d3SGreg Roach    }
102e2a378d3SGreg Roach
103e2a378d3SGreg Roach    /**
10476692c8bSGreg Roach     * Set a block setting.
10576692c8bSGreg Roach     *
106cbc1590aSGreg Roach     * @param int    $block_id
107e2a378d3SGreg Roach     * @param string $setting_name
10820ac4041SGreg Roach     * @param string $setting_value
109e2a378d3SGreg Roach     *
110e2a378d3SGreg Roach     * @return $this
111e2a378d3SGreg Roach     */
11252f398fdSGreg Roach    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
113c1010edaSGreg Roach    {
11432a20a8cSGreg Roach        DB::table('block_setting')->updateOrInsert([
115e2a378d3SGreg Roach            'block_id'      => $block_id,
116e2a378d3SGreg Roach            'setting_name'  => $setting_name,
11732a20a8cSGreg Roach        ], [
118e2a378d3SGreg Roach            'setting_value' => $setting_value,
11913abd6f3SGreg Roach        ]);
120e2a378d3SGreg Roach
121e2a378d3SGreg Roach        return $this;
122e2a378d3SGreg Roach    }
123e2a378d3SGreg Roach
124e2a378d3SGreg Roach    /**
12549a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
126e2a378d3SGreg Roach     *
12749a243cbSGreg Roach     * @param string $name
128e2a378d3SGreg Roach     *
12937eb8894SGreg Roach     * @return void
130e2a378d3SGreg Roach     */
13137eb8894SGreg Roach    final public function setName(string $name): void
132c1010edaSGreg Roach    {
13349a243cbSGreg Roach        $this->name = $name;
134e2a378d3SGreg Roach    }
135e2a378d3SGreg Roach
136e2a378d3SGreg Roach    /**
13749a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
138e2a378d3SGreg Roach     *
139e2a378d3SGreg Roach     * @return string
140e2a378d3SGreg Roach     */
14126684e68SGreg Roach    final public function name(): string
142c1010edaSGreg Roach    {
14349a243cbSGreg Roach        return $this->name;
144e2a378d3SGreg Roach    }
145e2a378d3SGreg Roach
146e2a378d3SGreg Roach    /**
14749a243cbSGreg Roach     * Modules are either enabled or disabled.
148e2a378d3SGreg Roach     *
14949a243cbSGreg Roach     * @param bool $enabled
15018d7a90dSGreg Roach     *
15149a243cbSGreg Roach     * @return ModuleInterface
152e2a378d3SGreg Roach     */
15352f398fdSGreg Roach    final public function setEnabled(bool $enabled): ModuleInterface
154c1010edaSGreg Roach    {
15549a243cbSGreg Roach        $this->enabled = $enabled;
15649a243cbSGreg Roach
15749a243cbSGreg Roach        return $this;
158e2a378d3SGreg Roach    }
15949a243cbSGreg Roach
16049a243cbSGreg Roach    /**
16149a243cbSGreg Roach     * Modules are either enabled or disabled.
16249a243cbSGreg Roach     *
16349a243cbSGreg Roach     * @return bool
16449a243cbSGreg Roach     */
16552f398fdSGreg Roach    final public function isEnabled(): bool
16649a243cbSGreg Roach    {
16749a243cbSGreg Roach        return $this->enabled;
168e2a378d3SGreg Roach    }
169e2a378d3SGreg Roach
170e2a378d3SGreg Roach    /**
171888ddf4fSGreg Roach     * Should this module be enabled when it is first installed?
172888ddf4fSGreg Roach     *
173888ddf4fSGreg Roach     * @return bool
174888ddf4fSGreg Roach     */
175888ddf4fSGreg Roach    public function isEnabledByDefault(): bool
176888ddf4fSGreg Roach    {
177888ddf4fSGreg Roach        return true;
178888ddf4fSGreg Roach    }
179888ddf4fSGreg Roach
180888ddf4fSGreg Roach    /**
181e2a378d3SGreg Roach     * Get a module setting. Return a default if the setting is not set.
182e2a378d3SGreg Roach     *
183e2a378d3SGreg Roach     * @param string $setting_name
184e2a378d3SGreg Roach     * @param string $default
185e2a378d3SGreg Roach     *
18615d603e7SGreg Roach     * @return string
187e2a378d3SGreg Roach     */
18837eb8894SGreg Roach    final public function getPreference(string $setting_name, string $default = ''): string
189c1010edaSGreg Roach    {
19049a243cbSGreg Roach        return DB::table('module_setting')
19126684e68SGreg Roach            ->where('module_name', '=', $this->name())
19249a243cbSGreg Roach            ->where('setting_name', '=', $setting_name)
19349a243cbSGreg Roach            ->value('setting_value') ?? $default;
194e2a378d3SGreg Roach    }
195e2a378d3SGreg Roach
196e2a378d3SGreg Roach    /**
197e2a378d3SGreg Roach     * Set a module setting.
198e2a378d3SGreg Roach     *
199e2a378d3SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
200e2a378d3SGreg Roach     * it to be deleted.
201e2a378d3SGreg Roach     *
202e2a378d3SGreg Roach     * @param string $setting_name
203e2a378d3SGreg Roach     * @param string $setting_value
20415d603e7SGreg Roach     *
205ea02ddf4SGreg Roach     * @return void
206e2a378d3SGreg Roach     */
20737eb8894SGreg Roach    final public function setPreference(string $setting_name, string $setting_value): void
208c1010edaSGreg Roach    {
20932a20a8cSGreg Roach        DB::table('module_setting')->updateOrInsert([
21026684e68SGreg Roach            'module_name'  => $this->name(),
21132a20a8cSGreg Roach            'setting_name' => $setting_name,
21232a20a8cSGreg Roach        ], [
21332a20a8cSGreg Roach            'setting_value' => $setting_value,
214c1010edaSGreg Roach        ]);
215e2a378d3SGreg Roach    }
216e2a378d3SGreg Roach
217e2a378d3SGreg Roach    /**
218e2a378d3SGreg Roach     * Get a the current access level for a module
219e2a378d3SGreg Roach     *
220e2a378d3SGreg Roach     * @param Tree   $tree
22187cca37cSGreg Roach     * @param string $interface
222e2a378d3SGreg Roach     *
223cbc1590aSGreg Roach     * @return int
224e2a378d3SGreg Roach     */
22587cca37cSGreg Roach    final public function accessLevel(Tree $tree, string $interface): int
226c1010edaSGreg Roach    {
22749a243cbSGreg Roach        $access_levels = app('cache.array')
2280b5fd0a6SGreg Roach            ->rememberForever('module_privacy' . $tree->id(), static function () use ($tree): Collection {
22949a243cbSGreg Roach                return DB::table('module_privacy')
23032a20a8cSGreg Roach                    ->where('gedcom_id', '=', $tree->id())
23149a243cbSGreg Roach                    ->get();
23249a243cbSGreg Roach            });
233e2a378d3SGreg Roach
2340797053bSGreg Roach        $row = $access_levels->first(function (stdClass $row) use ($interface): bool {
23587cca37cSGreg Roach            return $row->interface === $interface && $row->module_name === $this->name();
2360797053bSGreg Roach        });
237b2ce94c6SRico Sonntag
23849a243cbSGreg Roach        return $row ? (int) $row->access_level : $this->access_level;
239e2a378d3SGreg Roach    }
240e2ae4578SGreg Roach
241e2ae4578SGreg Roach    /**
24202086832SGreg Roach     * Where does this module store its resources
24302086832SGreg Roach     *
24402086832SGreg Roach     * @return string
24502086832SGreg Roach     */
24602086832SGreg Roach    public function resourcesFolder(): string
24702086832SGreg Roach    {
248f397d0fdSGreg Roach        return Webtrees::ROOT_DIR . 'resources/';
24902086832SGreg Roach    }
250e2a378d3SGreg Roach}
251