1e2a378d3SGreg Roach<?php 23976b470SGreg Roach 3e2a378d3SGreg Roach/** 4e2a378d3SGreg Roach * webtrees: online genealogy 5*89f7189bSGreg Roach * Copyright (C) 2021 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 15*89f7189bSGreg Roach * along with this program. If not, see <https://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; 236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 24606471d5SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 26f397d0fdSGreg Roachuse Fisharebest\Webtrees\Webtrees; 273f412448SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2849a243cbSGreg Roachuse Illuminate\Support\Collection; 2949a243cbSGreg Roachuse stdClass; 30e2a378d3SGreg Roach 31e2a378d3SGreg Roach/** 32e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks 33e2a378d3SGreg Roach */ 3449a243cbSGreg Roachabstract class AbstractModule implements ModuleInterface 35c1010edaSGreg Roach{ 36606471d5SGreg Roach use ViewResponseTrait; 37606471d5SGreg Roach 3849a243cbSGreg Roach /** @var string A unique internal name for this module (based on the installation folder). */ 3949a243cbSGreg Roach private $name = ''; 40e2a378d3SGreg Roach 4149a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 4249a243cbSGreg Roach protected $access_level = Auth::PRIV_PRIVATE; 4349a243cbSGreg Roach 4449a243cbSGreg Roach /** @var bool The default status for this module. It can be changed in the control panel. */ 4549a243cbSGreg Roach private $enabled = true; 46e2a378d3SGreg Roach 4787503df0SGreg Roach /** @var string For custom modules - optional (recommended) version number */ 4816d6367aSGreg Roach public const CUSTOM_VERSION = ''; 4987503df0SGreg Roach 5087503df0SGreg Roach /** @var string For custom modules - link for support, upgrades, etc. */ 5116d6367aSGreg Roach public const CUSTOM_WEBSITE = ''; 5287503df0SGreg Roach 53c1afbf58SGreg Roach /** 549e18e23bSGreg Roach * Called for all *enabled* modules. 559e18e23bSGreg Roach */ 569e18e23bSGreg Roach public function boot(): void 579e18e23bSGreg Roach { 589e18e23bSGreg Roach } 599e18e23bSGreg Roach 609e18e23bSGreg Roach /** 610cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 62c1afbf58SGreg Roach * 63c1afbf58SGreg Roach * @return string 64c1afbf58SGreg Roach */ 65c1afbf58SGreg Roach public function title(): string 66c1afbf58SGreg Roach { 67c1afbf58SGreg Roach return 'Module name goes here'; 68c1afbf58SGreg Roach } 69c1afbf58SGreg Roach 70c1afbf58SGreg Roach /** 71c1afbf58SGreg Roach * A sentence describing what this module does. 72c1afbf58SGreg Roach * 73c1afbf58SGreg Roach * @return string 74c1afbf58SGreg Roach */ 7549a243cbSGreg Roach public function description(): string 76c1010edaSGreg Roach { 77c1afbf58SGreg Roach return $this->title(); 78e2a378d3SGreg Roach } 79e2a378d3SGreg Roach 80e2a378d3SGreg Roach /** 8176692c8bSGreg Roach * Get a block setting. 8276692c8bSGreg Roach * 8349a243cbSGreg Roach * Originally, this was just used for the home-page blocks. Now, it is used by any 8449a243cbSGreg Roach * module that has repeated blocks of content on the same page. 8549a243cbSGreg Roach * 86cbc1590aSGreg Roach * @param int $block_id 87e2a378d3SGreg Roach * @param string $setting_name 8883c7613eSGreg Roach * @param string $default 89e2a378d3SGreg Roach * 9072ac996dSGreg Roach * @return string 91e2a378d3SGreg Roach */ 9252f398fdSGreg Roach final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 93c1010edaSGreg Roach { 946b9cb339SGreg Roach $settings = Registry::cache()->array()->remember('block-setting-' . $block_id, static function () use ($block_id): array { 9583c7613eSGreg Roach return DB::table('block_setting') 9632a20a8cSGreg Roach ->where('block_id', '=', $block_id) 9783c7613eSGreg Roach ->pluck('setting_value', 'setting_name') 9883c7613eSGreg Roach ->all(); 9983c7613eSGreg Roach }); 100e2a378d3SGreg Roach 10183c7613eSGreg Roach return $settings[$setting_name] ?? $default; 102e2a378d3SGreg Roach } 103e2a378d3SGreg Roach 104e2a378d3SGreg Roach /** 10576692c8bSGreg Roach * Set a block setting. 10676692c8bSGreg Roach * 107cbc1590aSGreg Roach * @param int $block_id 108e2a378d3SGreg Roach * @param string $setting_name 10920ac4041SGreg Roach * @param string $setting_value 110e2a378d3SGreg Roach * 111e2a378d3SGreg Roach * @return $this 112e2a378d3SGreg Roach */ 11352f398fdSGreg Roach final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 114c1010edaSGreg Roach { 11532a20a8cSGreg Roach DB::table('block_setting')->updateOrInsert([ 116e2a378d3SGreg Roach 'block_id' => $block_id, 117e2a378d3SGreg Roach 'setting_name' => $setting_name, 11832a20a8cSGreg Roach ], [ 119e2a378d3SGreg Roach 'setting_value' => $setting_value, 12013abd6f3SGreg Roach ]); 121e2a378d3SGreg Roach 122e2a378d3SGreg Roach return $this; 123e2a378d3SGreg Roach } 124e2a378d3SGreg Roach 125e2a378d3SGreg Roach /** 12649a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 127e2a378d3SGreg Roach * 12849a243cbSGreg Roach * @param string $name 129e2a378d3SGreg Roach * 13037eb8894SGreg Roach * @return void 131e2a378d3SGreg Roach */ 13237eb8894SGreg Roach final public function setName(string $name): void 133c1010edaSGreg Roach { 13449a243cbSGreg Roach $this->name = $name; 135e2a378d3SGreg Roach } 136e2a378d3SGreg Roach 137e2a378d3SGreg Roach /** 13849a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 139e2a378d3SGreg Roach * 140e2a378d3SGreg Roach * @return string 141e2a378d3SGreg Roach */ 14226684e68SGreg Roach final public function name(): string 143c1010edaSGreg Roach { 14449a243cbSGreg Roach return $this->name; 145e2a378d3SGreg Roach } 146e2a378d3SGreg Roach 147e2a378d3SGreg Roach /** 14849a243cbSGreg Roach * Modules are either enabled or disabled. 149e2a378d3SGreg Roach * 15049a243cbSGreg Roach * @param bool $enabled 15118d7a90dSGreg Roach * 15249a243cbSGreg Roach * @return ModuleInterface 153e2a378d3SGreg Roach */ 15452f398fdSGreg Roach final public function setEnabled(bool $enabled): ModuleInterface 155c1010edaSGreg Roach { 15649a243cbSGreg Roach $this->enabled = $enabled; 15749a243cbSGreg Roach 15849a243cbSGreg Roach return $this; 159e2a378d3SGreg Roach } 16049a243cbSGreg Roach 16149a243cbSGreg Roach /** 16249a243cbSGreg Roach * Modules are either enabled or disabled. 16349a243cbSGreg Roach * 16449a243cbSGreg Roach * @return bool 16549a243cbSGreg Roach */ 16652f398fdSGreg Roach final public function isEnabled(): bool 16749a243cbSGreg Roach { 16849a243cbSGreg Roach return $this->enabled; 169e2a378d3SGreg Roach } 170e2a378d3SGreg Roach 171e2a378d3SGreg Roach /** 172888ddf4fSGreg Roach * Should this module be enabled when it is first installed? 173888ddf4fSGreg Roach * 174888ddf4fSGreg Roach * @return bool 175888ddf4fSGreg Roach */ 176888ddf4fSGreg Roach public function isEnabledByDefault(): bool 177888ddf4fSGreg Roach { 178888ddf4fSGreg Roach return true; 179888ddf4fSGreg Roach } 180888ddf4fSGreg Roach 181888ddf4fSGreg Roach /** 182e2a378d3SGreg Roach * Get a module setting. Return a default if the setting is not set. 183e2a378d3SGreg Roach * 184e2a378d3SGreg Roach * @param string $setting_name 185e2a378d3SGreg Roach * @param string $default 186e2a378d3SGreg Roach * 18715d603e7SGreg Roach * @return string 188e2a378d3SGreg Roach */ 18937eb8894SGreg Roach final public function getPreference(string $setting_name, string $default = ''): string 190c1010edaSGreg Roach { 19149a243cbSGreg Roach return DB::table('module_setting') 19226684e68SGreg Roach ->where('module_name', '=', $this->name()) 19349a243cbSGreg Roach ->where('setting_name', '=', $setting_name) 19449a243cbSGreg Roach ->value('setting_value') ?? $default; 195e2a378d3SGreg Roach } 196e2a378d3SGreg Roach 197e2a378d3SGreg Roach /** 198e2a378d3SGreg Roach * Set a module setting. 199e2a378d3SGreg Roach * 200e2a378d3SGreg Roach * Since module settings are NOT NULL, setting a value to NULL will cause 201e2a378d3SGreg Roach * it to be deleted. 202e2a378d3SGreg Roach * 203e2a378d3SGreg Roach * @param string $setting_name 204e2a378d3SGreg Roach * @param string $setting_value 20515d603e7SGreg Roach * 206ea02ddf4SGreg Roach * @return void 207e2a378d3SGreg Roach */ 20837eb8894SGreg Roach final public function setPreference(string $setting_name, string $setting_value): void 209c1010edaSGreg Roach { 21032a20a8cSGreg Roach DB::table('module_setting')->updateOrInsert([ 21126684e68SGreg Roach 'module_name' => $this->name(), 21232a20a8cSGreg Roach 'setting_name' => $setting_name, 21332a20a8cSGreg Roach ], [ 21432a20a8cSGreg Roach 'setting_value' => $setting_value, 215c1010edaSGreg Roach ]); 216e2a378d3SGreg Roach } 217e2a378d3SGreg Roach 218e2a378d3SGreg Roach /** 219e2a378d3SGreg Roach * Get a the current access level for a module 220e2a378d3SGreg Roach * 221e2a378d3SGreg Roach * @param Tree $tree 22287cca37cSGreg Roach * @param string $interface 223e2a378d3SGreg Roach * 224cbc1590aSGreg Roach * @return int 225e2a378d3SGreg Roach */ 22687cca37cSGreg Roach final public function accessLevel(Tree $tree, string $interface): int 227c1010edaSGreg Roach { 2286b9cb339SGreg Roach $access_levels = Registry::cache()->array() 229c692965aSGreg Roach ->remember('module-privacy-' . $tree->id(), static function () use ($tree): Collection { 23049a243cbSGreg Roach return DB::table('module_privacy') 23132a20a8cSGreg Roach ->where('gedcom_id', '=', $tree->id()) 23249a243cbSGreg Roach ->get(); 23349a243cbSGreg Roach }); 234e2a378d3SGreg Roach 2350797053bSGreg Roach $row = $access_levels->first(function (stdClass $row) use ($interface): bool { 23687cca37cSGreg Roach return $row->interface === $interface && $row->module_name === $this->name(); 2370797053bSGreg Roach }); 238b2ce94c6SRico Sonntag 23949a243cbSGreg Roach return $row ? (int) $row->access_level : $this->access_level; 240e2a378d3SGreg Roach } 241e2ae4578SGreg Roach 242e2ae4578SGreg Roach /** 24302086832SGreg Roach * Where does this module store its resources 24402086832SGreg Roach * 24502086832SGreg Roach * @return string 24602086832SGreg Roach */ 24702086832SGreg Roach public function resourcesFolder(): string 24802086832SGreg Roach { 249f397d0fdSGreg Roach return Webtrees::ROOT_DIR . 'resources/'; 25002086832SGreg Roach } 251e2a378d3SGreg Roach} 252