xref: /webtrees/app/Module/AbstractModule.php (revision 606471d5101bcf787c03697c5d514ea070b86571)
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;
21*606471d5SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
233f412448SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2449a243cbSGreg Roachuse Illuminate\Support\Collection;
2549a243cbSGreg Roachuse stdClass;
26e2a378d3SGreg Roach
27e2a378d3SGreg Roach/**
28e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks
29e2a378d3SGreg Roach */
3049a243cbSGreg Roachabstract class AbstractModule implements ModuleInterface
31c1010edaSGreg Roach{
32*606471d5SGreg Roach    use ViewResponseTrait;
33*606471d5SGreg Roach
3449a243cbSGreg Roach    /** @var string A unique internal name for this module (based on the installation folder). */
3549a243cbSGreg Roach    private $name = '';
36e2a378d3SGreg Roach
3749a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
3849a243cbSGreg Roach    protected $access_level = Auth::PRIV_PRIVATE;
3949a243cbSGreg Roach
4049a243cbSGreg Roach    /** @var bool The default status for this module.  It can be changed in the control panel. */
4149a243cbSGreg Roach    private $enabled = true;
42e2a378d3SGreg Roach
4387503df0SGreg Roach    /** @var string For custom modules - optional (recommended) version number */
4416d6367aSGreg Roach    public const CUSTOM_VERSION = '';
4587503df0SGreg Roach
4687503df0SGreg Roach    /** @var string For custom modules - link for support, upgrades, etc. */
4716d6367aSGreg Roach    public const CUSTOM_WEBSITE = '';
4887503df0SGreg Roach
49c1afbf58SGreg Roach    /**
500cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
51c1afbf58SGreg Roach     *
52c1afbf58SGreg Roach     * @return string
53c1afbf58SGreg Roach     */
54c1afbf58SGreg Roach    public function title(): string
55c1afbf58SGreg Roach    {
56c1afbf58SGreg Roach        return 'Module name goes here';
57c1afbf58SGreg Roach    }
58c1afbf58SGreg Roach
59c1afbf58SGreg Roach    /**
60c1afbf58SGreg Roach     * A sentence describing what this module does.
61c1afbf58SGreg Roach     *
62c1afbf58SGreg Roach     * @return string
63c1afbf58SGreg Roach     */
6449a243cbSGreg Roach    public function description(): string
65c1010edaSGreg Roach    {
66c1afbf58SGreg Roach        return $this->title();
67e2a378d3SGreg Roach    }
68e2a378d3SGreg Roach
69e2a378d3SGreg Roach    /**
7076692c8bSGreg Roach     * Get a block setting.
7176692c8bSGreg Roach     *
7249a243cbSGreg Roach     * Originally, this was just used for the home-page blocks.  Now, it is used by any
7349a243cbSGreg Roach     * module that has repeated blocks of content on the same page.
7449a243cbSGreg Roach     *
75cbc1590aSGreg Roach     * @param int    $block_id
76e2a378d3SGreg Roach     * @param string $setting_name
7783c7613eSGreg Roach     * @param string $default
78e2a378d3SGreg Roach     *
7972ac996dSGreg Roach     * @return string
80e2a378d3SGreg Roach     */
8152f398fdSGreg Roach    final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
82c1010edaSGreg Roach    {
830b5fd0a6SGreg Roach        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, static function () use ($block_id): array {
8483c7613eSGreg Roach            return DB::table('block_setting')
8532a20a8cSGreg Roach                ->where('block_id', '=', $block_id)
8683c7613eSGreg Roach                ->pluck('setting_value', 'setting_name')
8783c7613eSGreg Roach                ->all();
8883c7613eSGreg Roach        });
89e2a378d3SGreg Roach
9083c7613eSGreg Roach        return $settings[$setting_name] ?? $default;
91e2a378d3SGreg Roach    }
92e2a378d3SGreg Roach
93e2a378d3SGreg Roach    /**
9476692c8bSGreg Roach     * Set a block setting.
9576692c8bSGreg Roach     *
96cbc1590aSGreg Roach     * @param int    $block_id
97e2a378d3SGreg Roach     * @param string $setting_name
9820ac4041SGreg Roach     * @param string $setting_value
99e2a378d3SGreg Roach     *
100e2a378d3SGreg Roach     * @return $this
101e2a378d3SGreg Roach     */
10252f398fdSGreg Roach    final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
103c1010edaSGreg Roach    {
10432a20a8cSGreg Roach        DB::table('block_setting')->updateOrInsert([
105e2a378d3SGreg Roach            'block_id'      => $block_id,
106e2a378d3SGreg Roach            'setting_name'  => $setting_name,
10732a20a8cSGreg Roach        ], [
108e2a378d3SGreg Roach            'setting_value' => $setting_value,
10913abd6f3SGreg Roach        ]);
110e2a378d3SGreg Roach
111e2a378d3SGreg Roach        return $this;
112e2a378d3SGreg Roach    }
113e2a378d3SGreg Roach
114e2a378d3SGreg Roach    /**
11549a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
116e2a378d3SGreg Roach     *
11749a243cbSGreg Roach     * @param string $name
118e2a378d3SGreg Roach     *
11937eb8894SGreg Roach     * @return void
120e2a378d3SGreg Roach     */
12137eb8894SGreg Roach    final public function setName(string $name): void
122c1010edaSGreg Roach    {
12349a243cbSGreg Roach        $this->name = $name;
124e2a378d3SGreg Roach    }
125e2a378d3SGreg Roach
126e2a378d3SGreg Roach    /**
12749a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
128e2a378d3SGreg Roach     *
129e2a378d3SGreg Roach     * @return string
130e2a378d3SGreg Roach     */
13126684e68SGreg Roach    final public function name(): string
132c1010edaSGreg Roach    {
13349a243cbSGreg Roach        return $this->name;
134e2a378d3SGreg Roach    }
135e2a378d3SGreg Roach
136e2a378d3SGreg Roach    /**
13749a243cbSGreg Roach     * Modules are either enabled or disabled.
138e2a378d3SGreg Roach     *
13949a243cbSGreg Roach     * @param bool $enabled
14018d7a90dSGreg Roach     *
14149a243cbSGreg Roach     * @return ModuleInterface
142e2a378d3SGreg Roach     */
14352f398fdSGreg Roach    final public function setEnabled(bool $enabled): ModuleInterface
144c1010edaSGreg Roach    {
14549a243cbSGreg Roach        $this->enabled = $enabled;
14649a243cbSGreg Roach
14749a243cbSGreg Roach        return $this;
148e2a378d3SGreg Roach    }
14949a243cbSGreg Roach
15049a243cbSGreg Roach    /**
15149a243cbSGreg Roach     * Modules are either enabled or disabled.
15249a243cbSGreg Roach     *
15349a243cbSGreg Roach     * @return bool
15449a243cbSGreg Roach     */
15552f398fdSGreg Roach    final public function isEnabled(): bool
15649a243cbSGreg Roach    {
15749a243cbSGreg Roach        return $this->enabled;
158e2a378d3SGreg Roach    }
159e2a378d3SGreg Roach
160e2a378d3SGreg Roach    /**
161888ddf4fSGreg Roach     * Should this module be enabled when it is first installed?
162888ddf4fSGreg Roach     *
163888ddf4fSGreg Roach     * @return bool
164888ddf4fSGreg Roach     */
165888ddf4fSGreg Roach    public function isEnabledByDefault(): bool
166888ddf4fSGreg Roach    {
167888ddf4fSGreg Roach        return true;
168888ddf4fSGreg Roach    }
169888ddf4fSGreg Roach
170888ddf4fSGreg Roach    /**
171e2a378d3SGreg Roach     * Get a module setting. Return a default if the setting is not set.
172e2a378d3SGreg Roach     *
173e2a378d3SGreg Roach     * @param string $setting_name
174e2a378d3SGreg Roach     * @param string $default
175e2a378d3SGreg Roach     *
17615d603e7SGreg Roach     * @return string
177e2a378d3SGreg Roach     */
17837eb8894SGreg Roach    final public function getPreference(string $setting_name, string $default = ''): string
179c1010edaSGreg Roach    {
18049a243cbSGreg Roach        return DB::table('module_setting')
18126684e68SGreg Roach            ->where('module_name', '=', $this->name())
18249a243cbSGreg Roach            ->where('setting_name', '=', $setting_name)
18349a243cbSGreg Roach            ->value('setting_value') ?? $default;
184e2a378d3SGreg Roach    }
185e2a378d3SGreg Roach
186e2a378d3SGreg Roach    /**
187e2a378d3SGreg Roach     * Set a module setting.
188e2a378d3SGreg Roach     *
189e2a378d3SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
190e2a378d3SGreg Roach     * it to be deleted.
191e2a378d3SGreg Roach     *
192e2a378d3SGreg Roach     * @param string $setting_name
193e2a378d3SGreg Roach     * @param string $setting_value
19415d603e7SGreg Roach     *
195ea02ddf4SGreg Roach     * @return void
196e2a378d3SGreg Roach     */
19737eb8894SGreg Roach    final public function setPreference(string $setting_name, string $setting_value): void
198c1010edaSGreg Roach    {
19932a20a8cSGreg Roach        DB::table('module_setting')->updateOrInsert([
20026684e68SGreg Roach            'module_name'  => $this->name(),
20132a20a8cSGreg Roach            'setting_name' => $setting_name,
20232a20a8cSGreg Roach        ], [
20332a20a8cSGreg Roach            'setting_value' => $setting_value,
204c1010edaSGreg Roach        ]);
205e2a378d3SGreg Roach    }
206e2a378d3SGreg Roach
207e2a378d3SGreg Roach    /**
208e2a378d3SGreg Roach     * Get a the current access level for a module
209e2a378d3SGreg Roach     *
210e2a378d3SGreg Roach     * @param Tree   $tree
21187cca37cSGreg Roach     * @param string $interface
212e2a378d3SGreg Roach     *
213cbc1590aSGreg Roach     * @return int
214e2a378d3SGreg Roach     */
21587cca37cSGreg Roach    final public function accessLevel(Tree $tree, string $interface): int
216c1010edaSGreg Roach    {
21749a243cbSGreg Roach        $access_levels = app('cache.array')
2180b5fd0a6SGreg Roach            ->rememberForever('module_privacy' . $tree->id(), static function () use ($tree): Collection {
21949a243cbSGreg Roach                return DB::table('module_privacy')
22032a20a8cSGreg Roach                    ->where('gedcom_id', '=', $tree->id())
22149a243cbSGreg Roach                    ->get();
22249a243cbSGreg Roach            });
223e2a378d3SGreg Roach
22487cca37cSGreg Roach        $row = $access_levels->filter(function (stdClass $row) use ($interface): bool {
22587cca37cSGreg Roach            return $row->interface === $interface && $row->module_name === $this->name();
22649a243cbSGreg Roach        })->first();
227b2ce94c6SRico Sonntag
22849a243cbSGreg Roach        return $row ? (int) $row->access_level : $this->access_level;
229e2a378d3SGreg Roach    }
230e2ae4578SGreg Roach
231e2ae4578SGreg Roach    /**
23202086832SGreg Roach     * Where does this module store its resources
23302086832SGreg Roach     *
23402086832SGreg Roach     * @return string
23502086832SGreg Roach     */
23602086832SGreg Roach    public function resourcesFolder(): string
23702086832SGreg Roach    {
23802086832SGreg Roach        return WT_ROOT . 'resources/';
23902086832SGreg Roach    }
240e2a378d3SGreg Roach}
241