1e2a378d3SGreg Roach<?php 23976b470SGreg Roach 3e2a378d3SGreg Roach/** 4e2a378d3SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 1589f7189bSGreg 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; 236f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 24606471d5SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 25f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 276c84de1dSGreg Roachuse Fisharebest\Webtrees\Webtrees; 2849a243cbSGreg Roachuse Illuminate\Support\Collection; 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 3733c746f1SGreg Roach // A unique internal name for this module (based on the installation folder). 3833c746f1SGreg Roach private string $name = ''; 39e2a378d3SGreg Roach 4033c746f1SGreg Roach // The default access level for this module. It can be changed in the control panel. 4133c746f1SGreg Roach protected int $access_level = Auth::PRIV_PRIVATE; 4249a243cbSGreg Roach 4333c746f1SGreg Roach // The default status for this module. It can be changed in the control panel. 4433c746f1SGreg Roach private bool $enabled = true; 4587503df0SGreg Roach 46c1afbf58SGreg Roach /** 479e18e23bSGreg Roach * Called for all *enabled* modules. 489e18e23bSGreg Roach */ 499e18e23bSGreg Roach public function boot(): void 509e18e23bSGreg Roach { 519e18e23bSGreg Roach } 529e18e23bSGreg Roach 539e18e23bSGreg Roach /** 540cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 55c1afbf58SGreg Roach * 56c1afbf58SGreg Roach * @return string 57c1afbf58SGreg Roach */ 58c1afbf58SGreg Roach public function title(): string 59c1afbf58SGreg Roach { 60c1afbf58SGreg Roach return 'Module name goes here'; 61c1afbf58SGreg Roach } 62c1afbf58SGreg Roach 6349a243cbSGreg Roach public function description(): string 64c1010edaSGreg Roach { 65c1afbf58SGreg Roach return $this->title(); 66e2a378d3SGreg Roach } 67e2a378d3SGreg Roach 68e2a378d3SGreg Roach /** 6976692c8bSGreg Roach * Get a block setting. 7076692c8bSGreg Roach * 7149a243cbSGreg Roach * Originally, this was just used for the home-page blocks. Now, it is used by any 7249a243cbSGreg Roach * module that has repeated blocks of content on the same page. 7349a243cbSGreg Roach * 74cbc1590aSGreg Roach * @param int $block_id 75e2a378d3SGreg Roach * @param string $setting_name 7683c7613eSGreg Roach * @param string $default 77e2a378d3SGreg Roach * 7872ac996dSGreg Roach * @return string 79e2a378d3SGreg Roach */ 8052f398fdSGreg Roach final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 81c1010edaSGreg Roach { 82*f25fc0f9SGreg Roach $settings = Registry::cache()->array() 83*f25fc0f9SGreg Roach ->remember('block-setting-' . $block_id, static fn (): array => DB::table('block_setting') 8432a20a8cSGreg Roach ->where('block_id', '=', $block_id) 8583c7613eSGreg Roach ->pluck('setting_value', 'setting_name') 86*f25fc0f9SGreg Roach ->all()); 87e2a378d3SGreg Roach 8883c7613eSGreg Roach return $settings[$setting_name] ?? $default; 89e2a378d3SGreg Roach } 90e2a378d3SGreg Roach 91e2a378d3SGreg Roach /** 9276692c8bSGreg Roach * Set a block setting. 9376692c8bSGreg Roach * 94cbc1590aSGreg Roach * @param int $block_id 95e2a378d3SGreg Roach * @param string $setting_name 9620ac4041SGreg Roach * @param string $setting_value 97e2a378d3SGreg Roach * 986612c384SGreg Roach * @return self 99e2a378d3SGreg Roach */ 10052f398fdSGreg Roach final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 101c1010edaSGreg Roach { 10232a20a8cSGreg Roach DB::table('block_setting')->updateOrInsert([ 103e2a378d3SGreg Roach 'block_id' => $block_id, 104e2a378d3SGreg Roach 'setting_name' => $setting_name, 10532a20a8cSGreg Roach ], [ 106e2a378d3SGreg Roach 'setting_value' => $setting_value, 10713abd6f3SGreg Roach ]); 108e2a378d3SGreg Roach 109e2a378d3SGreg Roach return $this; 110e2a378d3SGreg Roach } 111e2a378d3SGreg Roach 112e2a378d3SGreg Roach /** 11349a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 114e2a378d3SGreg Roach * 11549a243cbSGreg Roach * @param string $name 116e2a378d3SGreg Roach * 11737eb8894SGreg Roach * @return void 118e2a378d3SGreg Roach */ 11937eb8894SGreg Roach final public function setName(string $name): void 120c1010edaSGreg Roach { 12149a243cbSGreg Roach $this->name = $name; 122e2a378d3SGreg Roach } 123e2a378d3SGreg Roach 124e2a378d3SGreg Roach /** 12549a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 126e2a378d3SGreg Roach * 127e2a378d3SGreg Roach * @return string 128e2a378d3SGreg Roach */ 12926684e68SGreg Roach final public function name(): string 130c1010edaSGreg Roach { 13149a243cbSGreg Roach return $this->name; 132e2a378d3SGreg Roach } 133e2a378d3SGreg Roach 134e2a378d3SGreg Roach /** 13549a243cbSGreg Roach * Modules are either enabled or disabled. 136e2a378d3SGreg Roach * 13749a243cbSGreg Roach * @param bool $enabled 13818d7a90dSGreg Roach * 13949a243cbSGreg Roach * @return ModuleInterface 140e2a378d3SGreg Roach */ 14152f398fdSGreg Roach final public function setEnabled(bool $enabled): ModuleInterface 142c1010edaSGreg Roach { 14349a243cbSGreg Roach $this->enabled = $enabled; 14449a243cbSGreg Roach 14549a243cbSGreg Roach return $this; 146e2a378d3SGreg Roach } 14749a243cbSGreg Roach 14849a243cbSGreg Roach /** 14949a243cbSGreg Roach * Modules are either enabled or disabled. 15049a243cbSGreg Roach * 15149a243cbSGreg Roach * @return bool 15249a243cbSGreg Roach */ 15352f398fdSGreg Roach final public function isEnabled(): bool 15449a243cbSGreg Roach { 15549a243cbSGreg Roach return $this->enabled; 156e2a378d3SGreg Roach } 157e2a378d3SGreg Roach 158e2a378d3SGreg Roach /** 159888ddf4fSGreg Roach * Should this module be enabled when it is first installed? 160888ddf4fSGreg Roach * 161888ddf4fSGreg Roach * @return bool 162888ddf4fSGreg Roach */ 163888ddf4fSGreg Roach public function isEnabledByDefault(): bool 164888ddf4fSGreg Roach { 165888ddf4fSGreg Roach return true; 166888ddf4fSGreg Roach } 167888ddf4fSGreg Roach 168888ddf4fSGreg Roach /** 169e2a378d3SGreg Roach * Get a module setting. Return a default if the setting is not set. 170e2a378d3SGreg Roach * 171e2a378d3SGreg Roach * @param string $setting_name 172e2a378d3SGreg Roach * @param string $default 173e2a378d3SGreg Roach * 17415d603e7SGreg Roach * @return string 175e2a378d3SGreg Roach */ 17637eb8894SGreg Roach final public function getPreference(string $setting_name, string $default = ''): string 177c1010edaSGreg Roach { 17849a243cbSGreg Roach return DB::table('module_setting') 17926684e68SGreg Roach ->where('module_name', '=', $this->name()) 18049a243cbSGreg Roach ->where('setting_name', '=', $setting_name) 18149a243cbSGreg Roach ->value('setting_value') ?? $default; 182e2a378d3SGreg Roach } 183e2a378d3SGreg Roach 184e2a378d3SGreg Roach /** 185e2a378d3SGreg Roach * Set a module setting. 186e2a378d3SGreg Roach * 187e2a378d3SGreg Roach * Since module settings are NOT NULL, setting a value to NULL will cause 188e2a378d3SGreg Roach * it to be deleted. 189e2a378d3SGreg Roach * 190e2a378d3SGreg Roach * @param string $setting_name 191e2a378d3SGreg Roach * @param string $setting_value 19215d603e7SGreg Roach * 193ea02ddf4SGreg Roach * @return void 194e2a378d3SGreg Roach */ 19537eb8894SGreg Roach final public function setPreference(string $setting_name, string $setting_value): void 196c1010edaSGreg Roach { 19732a20a8cSGreg Roach DB::table('module_setting')->updateOrInsert([ 19826684e68SGreg Roach 'module_name' => $this->name(), 19932a20a8cSGreg Roach 'setting_name' => $setting_name, 20032a20a8cSGreg Roach ], [ 20132a20a8cSGreg Roach 'setting_value' => $setting_value, 202c1010edaSGreg Roach ]); 203e2a378d3SGreg Roach } 204e2a378d3SGreg Roach 205e2a378d3SGreg Roach /** 206ac19d600SGreg Roach * Get the current access level for a module 207ac19d600SGreg Roach * 208ac19d600SGreg Roach * @template T 209e2a378d3SGreg Roach * 210e2a378d3SGreg Roach * @param Tree $tree 211ac19d600SGreg Roach * @param class-string<T> $interface 212e2a378d3SGreg Roach * 213cbc1590aSGreg Roach * @return int 214e2a378d3SGreg Roach */ 21587cca37cSGreg Roach final public function accessLevel(Tree $tree, string $interface): int 216c1010edaSGreg Roach { 2176b9cb339SGreg Roach $access_levels = Registry::cache()->array() 218*f25fc0f9SGreg Roach ->remember('module-privacy-' . $tree->id(), static fn (): Collection => DB::table('module_privacy') 21932a20a8cSGreg Roach ->where('gedcom_id', '=', $tree->id()) 220*f25fc0f9SGreg Roach ->get()); 221e2a378d3SGreg Roach 222*f25fc0f9SGreg Roach $row = $access_levels->first(fn (object $row): bool => $row->interface === $interface && $row->module_name === $this->name()); 223b2ce94c6SRico Sonntag 22449a243cbSGreg Roach return $row ? (int) $row->access_level : $this->access_level; 225e2a378d3SGreg Roach } 226e2ae4578SGreg Roach 227e2ae4578SGreg Roach /** 22802086832SGreg Roach * Where does this module store its resources 22902086832SGreg Roach * 23002086832SGreg Roach * @return string 23102086832SGreg Roach */ 23202086832SGreg Roach public function resourcesFolder(): string 23302086832SGreg Roach { 2346c84de1dSGreg Roach return Webtrees::ROOT_DIR . '/resources/'; 23502086832SGreg Roach } 236e2a378d3SGreg Roach} 237