1e2a378d3SGreg Roach<?php 2*3976b470SGreg 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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2076692c8bSGreg Roach 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 22606471d5SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 24f397d0fdSGreg Roachuse Fisharebest\Webtrees\Webtrees; 253f412448SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2649a243cbSGreg Roachuse Illuminate\Support\Collection; 2749a243cbSGreg Roachuse stdClass; 28e2a378d3SGreg Roach 29e2a378d3SGreg Roach/** 30e2a378d3SGreg Roach * Class AbstractModule - common functions for blocks 31e2a378d3SGreg Roach */ 3249a243cbSGreg Roachabstract class AbstractModule implements ModuleInterface 33c1010edaSGreg Roach{ 34606471d5SGreg Roach use ViewResponseTrait; 35606471d5SGreg Roach 3649a243cbSGreg Roach /** @var string A unique internal name for this module (based on the installation folder). */ 3749a243cbSGreg Roach private $name = ''; 38e2a378d3SGreg Roach 3949a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 4049a243cbSGreg Roach protected $access_level = Auth::PRIV_PRIVATE; 4149a243cbSGreg Roach 4249a243cbSGreg Roach /** @var bool The default status for this module. It can be changed in the control panel. */ 4349a243cbSGreg Roach private $enabled = true; 44e2a378d3SGreg Roach 4587503df0SGreg Roach /** @var string For custom modules - optional (recommended) version number */ 4616d6367aSGreg Roach public const CUSTOM_VERSION = ''; 4787503df0SGreg Roach 4887503df0SGreg Roach /** @var string For custom modules - link for support, upgrades, etc. */ 4916d6367aSGreg Roach public const CUSTOM_WEBSITE = ''; 5087503df0SGreg Roach 51c1afbf58SGreg Roach /** 520cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 53c1afbf58SGreg Roach * 54c1afbf58SGreg Roach * @return string 55c1afbf58SGreg Roach */ 56c1afbf58SGreg Roach public function title(): string 57c1afbf58SGreg Roach { 58c1afbf58SGreg Roach return 'Module name goes here'; 59c1afbf58SGreg Roach } 60c1afbf58SGreg Roach 61c1afbf58SGreg Roach /** 62c1afbf58SGreg Roach * A sentence describing what this module does. 63c1afbf58SGreg Roach * 64c1afbf58SGreg Roach * @return string 65c1afbf58SGreg Roach */ 6649a243cbSGreg Roach public function description(): string 67c1010edaSGreg Roach { 68c1afbf58SGreg Roach return $this->title(); 69e2a378d3SGreg Roach } 70e2a378d3SGreg Roach 71e2a378d3SGreg Roach /** 7276692c8bSGreg Roach * Get a block setting. 7376692c8bSGreg Roach * 7449a243cbSGreg Roach * Originally, this was just used for the home-page blocks. Now, it is used by any 7549a243cbSGreg Roach * module that has repeated blocks of content on the same page. 7649a243cbSGreg Roach * 77cbc1590aSGreg Roach * @param int $block_id 78e2a378d3SGreg Roach * @param string $setting_name 7983c7613eSGreg Roach * @param string $default 80e2a378d3SGreg Roach * 8172ac996dSGreg Roach * @return string 82e2a378d3SGreg Roach */ 8352f398fdSGreg Roach final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 84c1010edaSGreg Roach { 850b5fd0a6SGreg Roach $settings = app('cache.array')->rememberForever('block_setting' . $block_id, static function () use ($block_id): array { 8683c7613eSGreg Roach return DB::table('block_setting') 8732a20a8cSGreg Roach ->where('block_id', '=', $block_id) 8883c7613eSGreg Roach ->pluck('setting_value', 'setting_name') 8983c7613eSGreg Roach ->all(); 9083c7613eSGreg Roach }); 91e2a378d3SGreg Roach 9283c7613eSGreg Roach return $settings[$setting_name] ?? $default; 93e2a378d3SGreg Roach } 94e2a378d3SGreg Roach 95e2a378d3SGreg Roach /** 9676692c8bSGreg Roach * Set a block setting. 9776692c8bSGreg Roach * 98cbc1590aSGreg Roach * @param int $block_id 99e2a378d3SGreg Roach * @param string $setting_name 10020ac4041SGreg Roach * @param string $setting_value 101e2a378d3SGreg Roach * 102e2a378d3SGreg Roach * @return $this 103e2a378d3SGreg Roach */ 10452f398fdSGreg Roach final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 105c1010edaSGreg Roach { 10632a20a8cSGreg Roach DB::table('block_setting')->updateOrInsert([ 107e2a378d3SGreg Roach 'block_id' => $block_id, 108e2a378d3SGreg Roach 'setting_name' => $setting_name, 10932a20a8cSGreg Roach ], [ 110e2a378d3SGreg Roach 'setting_value' => $setting_value, 11113abd6f3SGreg Roach ]); 112e2a378d3SGreg Roach 113e2a378d3SGreg Roach return $this; 114e2a378d3SGreg Roach } 115e2a378d3SGreg Roach 116e2a378d3SGreg Roach /** 11749a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 118e2a378d3SGreg Roach * 11949a243cbSGreg Roach * @param string $name 120e2a378d3SGreg Roach * 12137eb8894SGreg Roach * @return void 122e2a378d3SGreg Roach */ 12337eb8894SGreg Roach final public function setName(string $name): void 124c1010edaSGreg Roach { 12549a243cbSGreg Roach $this->name = $name; 126e2a378d3SGreg Roach } 127e2a378d3SGreg Roach 128e2a378d3SGreg Roach /** 12949a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 130e2a378d3SGreg Roach * 131e2a378d3SGreg Roach * @return string 132e2a378d3SGreg Roach */ 13326684e68SGreg Roach final public function name(): string 134c1010edaSGreg Roach { 13549a243cbSGreg Roach return $this->name; 136e2a378d3SGreg Roach } 137e2a378d3SGreg Roach 138e2a378d3SGreg Roach /** 13949a243cbSGreg Roach * Modules are either enabled or disabled. 140e2a378d3SGreg Roach * 14149a243cbSGreg Roach * @param bool $enabled 14218d7a90dSGreg Roach * 14349a243cbSGreg Roach * @return ModuleInterface 144e2a378d3SGreg Roach */ 14552f398fdSGreg Roach final public function setEnabled(bool $enabled): ModuleInterface 146c1010edaSGreg Roach { 14749a243cbSGreg Roach $this->enabled = $enabled; 14849a243cbSGreg Roach 14949a243cbSGreg Roach return $this; 150e2a378d3SGreg Roach } 15149a243cbSGreg Roach 15249a243cbSGreg Roach /** 15349a243cbSGreg Roach * Modules are either enabled or disabled. 15449a243cbSGreg Roach * 15549a243cbSGreg Roach * @return bool 15649a243cbSGreg Roach */ 15752f398fdSGreg Roach final public function isEnabled(): bool 15849a243cbSGreg Roach { 15949a243cbSGreg Roach return $this->enabled; 160e2a378d3SGreg Roach } 161e2a378d3SGreg Roach 162e2a378d3SGreg Roach /** 163888ddf4fSGreg Roach * Should this module be enabled when it is first installed? 164888ddf4fSGreg Roach * 165888ddf4fSGreg Roach * @return bool 166888ddf4fSGreg Roach */ 167888ddf4fSGreg Roach public function isEnabledByDefault(): bool 168888ddf4fSGreg Roach { 169888ddf4fSGreg Roach return true; 170888ddf4fSGreg Roach } 171888ddf4fSGreg Roach 172888ddf4fSGreg Roach /** 173e2a378d3SGreg Roach * Get a module setting. Return a default if the setting is not set. 174e2a378d3SGreg Roach * 175e2a378d3SGreg Roach * @param string $setting_name 176e2a378d3SGreg Roach * @param string $default 177e2a378d3SGreg Roach * 17815d603e7SGreg Roach * @return string 179e2a378d3SGreg Roach */ 18037eb8894SGreg Roach final public function getPreference(string $setting_name, string $default = ''): string 181c1010edaSGreg Roach { 18249a243cbSGreg Roach return DB::table('module_setting') 18326684e68SGreg Roach ->where('module_name', '=', $this->name()) 18449a243cbSGreg Roach ->where('setting_name', '=', $setting_name) 18549a243cbSGreg Roach ->value('setting_value') ?? $default; 186e2a378d3SGreg Roach } 187e2a378d3SGreg Roach 188e2a378d3SGreg Roach /** 189e2a378d3SGreg Roach * Set a module setting. 190e2a378d3SGreg Roach * 191e2a378d3SGreg Roach * Since module settings are NOT NULL, setting a value to NULL will cause 192e2a378d3SGreg Roach * it to be deleted. 193e2a378d3SGreg Roach * 194e2a378d3SGreg Roach * @param string $setting_name 195e2a378d3SGreg Roach * @param string $setting_value 19615d603e7SGreg Roach * 197ea02ddf4SGreg Roach * @return void 198e2a378d3SGreg Roach */ 19937eb8894SGreg Roach final public function setPreference(string $setting_name, string $setting_value): void 200c1010edaSGreg Roach { 20132a20a8cSGreg Roach DB::table('module_setting')->updateOrInsert([ 20226684e68SGreg Roach 'module_name' => $this->name(), 20332a20a8cSGreg Roach 'setting_name' => $setting_name, 20432a20a8cSGreg Roach ], [ 20532a20a8cSGreg Roach 'setting_value' => $setting_value, 206c1010edaSGreg Roach ]); 207e2a378d3SGreg Roach } 208e2a378d3SGreg Roach 209e2a378d3SGreg Roach /** 210e2a378d3SGreg Roach * Get a the current access level for a module 211e2a378d3SGreg Roach * 212e2a378d3SGreg Roach * @param Tree $tree 21387cca37cSGreg Roach * @param string $interface 214e2a378d3SGreg Roach * 215cbc1590aSGreg Roach * @return int 216e2a378d3SGreg Roach */ 21787cca37cSGreg Roach final public function accessLevel(Tree $tree, string $interface): int 218c1010edaSGreg Roach { 21949a243cbSGreg Roach $access_levels = app('cache.array') 2200b5fd0a6SGreg Roach ->rememberForever('module_privacy' . $tree->id(), static function () use ($tree): Collection { 22149a243cbSGreg Roach return DB::table('module_privacy') 22232a20a8cSGreg Roach ->where('gedcom_id', '=', $tree->id()) 22349a243cbSGreg Roach ->get(); 22449a243cbSGreg Roach }); 225e2a378d3SGreg Roach 2260797053bSGreg Roach $row = $access_levels->first(function (stdClass $row) use ($interface): bool { 22787cca37cSGreg Roach return $row->interface === $interface && $row->module_name === $this->name(); 2280797053bSGreg Roach }); 229b2ce94c6SRico Sonntag 23049a243cbSGreg Roach return $row ? (int) $row->access_level : $this->access_level; 231e2a378d3SGreg Roach } 232e2ae4578SGreg Roach 233e2ae4578SGreg Roach /** 23402086832SGreg Roach * Where does this module store its resources 23502086832SGreg Roach * 23602086832SGreg Roach * @return string 23702086832SGreg Roach */ 23802086832SGreg Roach public function resourcesFolder(): string 23902086832SGreg Roach { 240f397d0fdSGreg Roach return Webtrees::ROOT_DIR . 'resources/'; 24102086832SGreg Roach } 242e2a378d3SGreg Roach} 243