xref: /webtrees/app/Module/AbstractModule.php (revision 49a243cb5fe7c21b24e262552d556b018bfe3f41)
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;
23*49a243cbSGreg Roachuse Illuminate\Support\Collection;
24*49a243cbSGreg Roachuse stdClass;
25e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
26e2a378d3SGreg Roach
27e2a378d3SGreg Roach/**
28e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks
29e2a378d3SGreg Roach */
30*49a243cbSGreg Roachabstract class AbstractModule implements ModuleInterface
31c1010edaSGreg Roach{
32*49a243cbSGreg Roach    /** @var string A unique internal name for this module (based on the installation folder). */
33*49a243cbSGreg Roach    private $name = '';
34e2a378d3SGreg Roach
35*49a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
36*49a243cbSGreg Roach    protected $access_level = Auth::PRIV_PRIVATE;
37*49a243cbSGreg Roach
38*49a243cbSGreg Roach    /** @var bool The default status for this module.  It can be changed in the control panel. */
39*49a243cbSGreg 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
47*49a243cbSGreg Roach    /** @var string How to render view responses */
48e2ae4578SGreg Roach    protected $layout = 'layouts/default';
49e2ae4578SGreg Roach
50*49a243cbSGreg Roach    public function description(): string
51c1010edaSGreg Roach    {
52*49a243cbSGreg Roach        // TODO: Implement description() method.
53e2a378d3SGreg Roach    }
54e2a378d3SGreg Roach
55e2a378d3SGreg Roach    /**
5676692c8bSGreg Roach     * Get a block setting.
5776692c8bSGreg Roach     *
58*49a243cbSGreg Roach     * Originally, this was just used for the home-page blocks.  Now, it is used by any
59*49a243cbSGreg Roach     * module that has repeated blocks of content on the same page.
60*49a243cbSGreg Roach     *
61cbc1590aSGreg Roach     * @param int    $block_id
62e2a378d3SGreg Roach     * @param string $setting_name
6383c7613eSGreg Roach     * @param string $default
64e2a378d3SGreg Roach     *
6572ac996dSGreg Roach     * @return string
66e2a378d3SGreg Roach     */
67*49a243cbSGreg Roach    protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string
68c1010edaSGreg Roach    {
69*49a243cbSGreg Roach        $settings = app('cache.array')->rememberForever('block_setting' . $block_id, function () use ($block_id): array {
7083c7613eSGreg Roach            return DB::table('block_setting')
7132a20a8cSGreg Roach                ->where('block_id', '=', $block_id)
7283c7613eSGreg Roach                ->pluck('setting_value', 'setting_name')
7383c7613eSGreg Roach                ->all();
7483c7613eSGreg Roach        });
75e2a378d3SGreg Roach
7683c7613eSGreg Roach        return $settings[$setting_name] ?? $default;
77e2a378d3SGreg Roach    }
78e2a378d3SGreg Roach
79e2a378d3SGreg Roach    /**
8076692c8bSGreg Roach     * Set a block setting.
8176692c8bSGreg Roach     *
82cbc1590aSGreg Roach     * @param int    $block_id
83e2a378d3SGreg Roach     * @param string $setting_name
8420ac4041SGreg Roach     * @param string $setting_value
85e2a378d3SGreg Roach     *
86e2a378d3SGreg Roach     * @return $this
87e2a378d3SGreg Roach     */
88*49a243cbSGreg Roach    protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self
89c1010edaSGreg Roach    {
9032a20a8cSGreg Roach        DB::table('block_setting')->updateOrInsert([
91e2a378d3SGreg Roach            'block_id'      => $block_id,
92e2a378d3SGreg Roach            'setting_name'  => $setting_name,
9332a20a8cSGreg Roach        ], [
94e2a378d3SGreg Roach            'setting_value' => $setting_value,
9513abd6f3SGreg Roach        ]);
96e2a378d3SGreg Roach
97e2a378d3SGreg Roach        return $this;
98e2a378d3SGreg Roach    }
99e2a378d3SGreg Roach
100e2a378d3SGreg Roach    /**
101*49a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
102e2a378d3SGreg Roach     *
103*49a243cbSGreg Roach     * @param string $name
104e2a378d3SGreg Roach     *
105*49a243cbSGreg Roach     * @return ModuleInterface
106e2a378d3SGreg Roach     */
107*49a243cbSGreg Roach    public function setName(string $name): ModuleInterface
108c1010edaSGreg Roach    {
109*49a243cbSGreg Roach        $this->name = $name;
110*49a243cbSGreg Roach
111*49a243cbSGreg Roach        return $this;
112e2a378d3SGreg Roach    }
113e2a378d3SGreg Roach
114e2a378d3SGreg Roach    /**
115*49a243cbSGreg Roach     * A unique internal name for this module (based on the installation folder).
116e2a378d3SGreg Roach     *
117e2a378d3SGreg Roach     * @return string
118e2a378d3SGreg Roach     */
1198f53f488SRico Sonntag    public function getName(): string
120c1010edaSGreg Roach    {
121*49a243cbSGreg Roach        return $this->name;
122e2a378d3SGreg Roach    }
123e2a378d3SGreg Roach
124e2a378d3SGreg Roach    /**
125*49a243cbSGreg Roach     * Modules are either enabled or disabled.
126e2a378d3SGreg Roach     *
127*49a243cbSGreg Roach     * @param bool $enabled
12818d7a90dSGreg Roach     *
129*49a243cbSGreg Roach     * @return ModuleInterface
130e2a378d3SGreg Roach     */
131*49a243cbSGreg Roach    public function setEnabled(bool $enabled): ModuleInterface
132c1010edaSGreg Roach    {
133*49a243cbSGreg Roach        $this->enabled = $enabled;
134*49a243cbSGreg Roach
135*49a243cbSGreg Roach        return $this;
136e2a378d3SGreg Roach    }
137*49a243cbSGreg Roach
138*49a243cbSGreg Roach    /**
139*49a243cbSGreg Roach     * Modules are either enabled or disabled.
140*49a243cbSGreg Roach     *
141*49a243cbSGreg Roach     * @return bool
142*49a243cbSGreg Roach     */
143*49a243cbSGreg Roach    public function isEnabled(): bool
144*49a243cbSGreg Roach    {
145*49a243cbSGreg Roach        return $this->enabled;
146e2a378d3SGreg Roach    }
147e2a378d3SGreg Roach
148e2a378d3SGreg Roach    /**
149e2a378d3SGreg Roach     * Get a module setting. Return a default if the setting is not set.
150e2a378d3SGreg Roach     *
151e2a378d3SGreg Roach     * @param string $setting_name
152e2a378d3SGreg Roach     * @param string $default
153e2a378d3SGreg Roach     *
15415d603e7SGreg Roach     * @return string
155e2a378d3SGreg Roach     */
156*49a243cbSGreg Roach    public function getPreference($setting_name, $default = ''): string
157c1010edaSGreg Roach    {
158*49a243cbSGreg Roach        return DB::table('module_setting')
159*49a243cbSGreg Roach            ->where('module_name', '=', $this->getName())
160*49a243cbSGreg Roach            ->where('setting_name', '=', $setting_name)
161*49a243cbSGreg Roach            ->value('setting_value') ?? $default;
162e2a378d3SGreg Roach    }
163e2a378d3SGreg Roach
164e2a378d3SGreg Roach    /**
165e2a378d3SGreg Roach     * Set a module setting.
166e2a378d3SGreg Roach     *
167e2a378d3SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
168e2a378d3SGreg Roach     * it to be deleted.
169e2a378d3SGreg Roach     *
170e2a378d3SGreg Roach     * @param string $setting_name
171e2a378d3SGreg Roach     * @param string $setting_value
17215d603e7SGreg Roach     *
17315d603e7SGreg Roach     * @return $this
174e2a378d3SGreg Roach     */
1758f53f488SRico Sonntag    public function setPreference($setting_name, $setting_value): self
176c1010edaSGreg Roach    {
17732a20a8cSGreg Roach        DB::table('module_setting')->updateOrInsert([
17832a20a8cSGreg Roach            'module_name'  => $this->getName(),
17932a20a8cSGreg Roach            'setting_name' => $setting_name,
18032a20a8cSGreg Roach        ], [
18132a20a8cSGreg Roach            'setting_value' => $setting_value,
182c1010edaSGreg Roach        ]);
183b7ee5e66SCarmen Pijpers
18415d603e7SGreg Roach        return $this;
185e2a378d3SGreg Roach    }
186e2a378d3SGreg Roach
187e2a378d3SGreg Roach    /**
188e2a378d3SGreg Roach     * Get a the current access level for a module
189e2a378d3SGreg Roach     *
190e2a378d3SGreg Roach     * @param Tree   $tree
191cbc1590aSGreg Roach     * @param string $component tab, block, menu, etc
192e2a378d3SGreg Roach     *
193cbc1590aSGreg Roach     * @return int
194e2a378d3SGreg Roach     */
195*49a243cbSGreg Roach    public function accessLevel(Tree $tree, string $component): int
196c1010edaSGreg Roach    {
197*49a243cbSGreg Roach        $access_levels = app('cache.array')
198*49a243cbSGreg Roach            ->rememberForever('module_privacy' . $tree->id(), function () use ($tree): Collection {
199*49a243cbSGreg Roach                return DB::table('module_privacy')
20032a20a8cSGreg Roach                    ->where('gedcom_id', '=', $tree->id())
201*49a243cbSGreg Roach                    ->get();
202*49a243cbSGreg Roach            });
203e2a378d3SGreg Roach
204*49a243cbSGreg Roach        $row = $access_levels->filter(function (stdClass $row) use ($component): bool {
205*49a243cbSGreg Roach            return $row->component === $component && $row->module_name === $this->getName();
206*49a243cbSGreg Roach        })->first();
207b2ce94c6SRico Sonntag
208*49a243cbSGreg Roach        return $row ? (int) $row->access_level : $this->access_level;
209e2a378d3SGreg Roach    }
210e2ae4578SGreg Roach
211e2ae4578SGreg Roach    /**
212e2ae4578SGreg Roach     * Create a response object from a view.
213e2ae4578SGreg Roach     *
214e2ae4578SGreg Roach     * @param string  $view_name
215e2ae4578SGreg Roach     * @param mixed[] $view_data
216e2ae4578SGreg Roach     * @param int     $status
217e2ae4578SGreg Roach     *
218e2ae4578SGreg Roach     * @return Response
219e2ae4578SGreg Roach     */
220c1010edaSGreg Roach    protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response
221c1010edaSGreg Roach    {
222e2ae4578SGreg Roach        // Make the view's data available to the layout.
223e2ae4578SGreg Roach        $layout_data = $view_data;
224e2ae4578SGreg Roach
225e2ae4578SGreg Roach        // Render the view
226e2ae4578SGreg Roach        $layout_data['content'] = view($view_name, $view_data);
227e2ae4578SGreg Roach
228e2ae4578SGreg Roach        // Insert the view into the layout
229e2ae4578SGreg Roach        $html = view($this->layout, $layout_data);
230e2ae4578SGreg Roach
231e2ae4578SGreg Roach        return new Response($html, $status);
232e2ae4578SGreg Roach    }
233e2a378d3SGreg Roach}
234