1e2a378d3SGreg Roach<?php 23976b470SGreg 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 */ 17*fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 23606471d5SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 25f397d0fdSGreg Roachuse Fisharebest\Webtrees\Webtrees; 263f412448SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2749a243cbSGreg Roachuse Illuminate\Support\Collection; 2849a243cbSGreg Roachuse stdClass; 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 3749a243cbSGreg Roach /** @var string A unique internal name for this module (based on the installation folder). */ 3849a243cbSGreg Roach private $name = ''; 39e2a378d3SGreg Roach 4049a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 4149a243cbSGreg Roach protected $access_level = Auth::PRIV_PRIVATE; 4249a243cbSGreg Roach 4349a243cbSGreg Roach /** @var bool The default status for this module. It can be changed in the control panel. */ 4449a243cbSGreg Roach private $enabled = true; 45e2a378d3SGreg Roach 4687503df0SGreg Roach /** @var string For custom modules - optional (recommended) version number */ 4716d6367aSGreg Roach public const CUSTOM_VERSION = ''; 4887503df0SGreg Roach 4987503df0SGreg Roach /** @var string For custom modules - link for support, upgrades, etc. */ 5016d6367aSGreg Roach public const CUSTOM_WEBSITE = ''; 5187503df0SGreg Roach 52c1afbf58SGreg Roach /** 530cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 54c1afbf58SGreg Roach * 55c1afbf58SGreg Roach * @return string 56c1afbf58SGreg Roach */ 57c1afbf58SGreg Roach public function title(): string 58c1afbf58SGreg Roach { 59c1afbf58SGreg Roach return 'Module name goes here'; 60c1afbf58SGreg Roach } 61c1afbf58SGreg Roach 62c1afbf58SGreg Roach /** 63c1afbf58SGreg Roach * A sentence describing what this module does. 64c1afbf58SGreg Roach * 65c1afbf58SGreg Roach * @return string 66c1afbf58SGreg Roach */ 6749a243cbSGreg Roach public function description(): string 68c1010edaSGreg Roach { 69c1afbf58SGreg Roach return $this->title(); 70e2a378d3SGreg Roach } 71e2a378d3SGreg Roach 72e2a378d3SGreg Roach /** 7376692c8bSGreg Roach * Get a block setting. 7476692c8bSGreg Roach * 7549a243cbSGreg Roach * Originally, this was just used for the home-page blocks. Now, it is used by any 7649a243cbSGreg Roach * module that has repeated blocks of content on the same page. 7749a243cbSGreg Roach * 78cbc1590aSGreg Roach * @param int $block_id 79e2a378d3SGreg Roach * @param string $setting_name 8083c7613eSGreg Roach * @param string $default 81e2a378d3SGreg Roach * 8272ac996dSGreg Roach * @return string 83e2a378d3SGreg Roach */ 8452f398fdSGreg Roach final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 85c1010edaSGreg Roach { 860b5fd0a6SGreg Roach $settings = app('cache.array')->rememberForever('block_setting' . $block_id, static function () use ($block_id): array { 8783c7613eSGreg Roach return DB::table('block_setting') 8832a20a8cSGreg Roach ->where('block_id', '=', $block_id) 8983c7613eSGreg Roach ->pluck('setting_value', 'setting_name') 9083c7613eSGreg Roach ->all(); 9183c7613eSGreg Roach }); 92e2a378d3SGreg Roach 9383c7613eSGreg Roach return $settings[$setting_name] ?? $default; 94e2a378d3SGreg Roach } 95e2a378d3SGreg Roach 96e2a378d3SGreg Roach /** 9776692c8bSGreg Roach * Set a block setting. 9876692c8bSGreg Roach * 99cbc1590aSGreg Roach * @param int $block_id 100e2a378d3SGreg Roach * @param string $setting_name 10120ac4041SGreg Roach * @param string $setting_value 102e2a378d3SGreg Roach * 103e2a378d3SGreg Roach * @return $this 104e2a378d3SGreg Roach */ 10552f398fdSGreg Roach final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 106c1010edaSGreg Roach { 10732a20a8cSGreg Roach DB::table('block_setting')->updateOrInsert([ 108e2a378d3SGreg Roach 'block_id' => $block_id, 109e2a378d3SGreg Roach 'setting_name' => $setting_name, 11032a20a8cSGreg Roach ], [ 111e2a378d3SGreg Roach 'setting_value' => $setting_value, 11213abd6f3SGreg Roach ]); 113e2a378d3SGreg Roach 114e2a378d3SGreg Roach return $this; 115e2a378d3SGreg Roach } 116e2a378d3SGreg Roach 117e2a378d3SGreg Roach /** 11849a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 119e2a378d3SGreg Roach * 12049a243cbSGreg Roach * @param string $name 121e2a378d3SGreg Roach * 12237eb8894SGreg Roach * @return void 123e2a378d3SGreg Roach */ 12437eb8894SGreg Roach final public function setName(string $name): void 125c1010edaSGreg Roach { 12649a243cbSGreg Roach $this->name = $name; 127e2a378d3SGreg Roach } 128e2a378d3SGreg Roach 129e2a378d3SGreg Roach /** 13049a243cbSGreg Roach * A unique internal name for this module (based on the installation folder). 131e2a378d3SGreg Roach * 132e2a378d3SGreg Roach * @return string 133e2a378d3SGreg Roach */ 13426684e68SGreg Roach final public function name(): string 135c1010edaSGreg Roach { 13649a243cbSGreg Roach return $this->name; 137e2a378d3SGreg Roach } 138e2a378d3SGreg Roach 139e2a378d3SGreg Roach /** 14049a243cbSGreg Roach * Modules are either enabled or disabled. 141e2a378d3SGreg Roach * 14249a243cbSGreg Roach * @param bool $enabled 14318d7a90dSGreg Roach * 14449a243cbSGreg Roach * @return ModuleInterface 145e2a378d3SGreg Roach */ 14652f398fdSGreg Roach final public function setEnabled(bool $enabled): ModuleInterface 147c1010edaSGreg Roach { 14849a243cbSGreg Roach $this->enabled = $enabled; 14949a243cbSGreg Roach 15049a243cbSGreg Roach return $this; 151e2a378d3SGreg Roach } 15249a243cbSGreg Roach 15349a243cbSGreg Roach /** 15449a243cbSGreg Roach * Modules are either enabled or disabled. 15549a243cbSGreg Roach * 15649a243cbSGreg Roach * @return bool 15749a243cbSGreg Roach */ 15852f398fdSGreg Roach final public function isEnabled(): bool 15949a243cbSGreg Roach { 16049a243cbSGreg Roach return $this->enabled; 161e2a378d3SGreg Roach } 162e2a378d3SGreg Roach 163e2a378d3SGreg Roach /** 164888ddf4fSGreg Roach * Should this module be enabled when it is first installed? 165888ddf4fSGreg Roach * 166888ddf4fSGreg Roach * @return bool 167888ddf4fSGreg Roach */ 168888ddf4fSGreg Roach public function isEnabledByDefault(): bool 169888ddf4fSGreg Roach { 170888ddf4fSGreg Roach return true; 171888ddf4fSGreg Roach } 172888ddf4fSGreg Roach 173888ddf4fSGreg Roach /** 174e2a378d3SGreg Roach * Get a module setting. Return a default if the setting is not set. 175e2a378d3SGreg Roach * 176e2a378d3SGreg Roach * @param string $setting_name 177e2a378d3SGreg Roach * @param string $default 178e2a378d3SGreg Roach * 17915d603e7SGreg Roach * @return string 180e2a378d3SGreg Roach */ 18137eb8894SGreg Roach final public function getPreference(string $setting_name, string $default = ''): string 182c1010edaSGreg Roach { 18349a243cbSGreg Roach return DB::table('module_setting') 18426684e68SGreg Roach ->where('module_name', '=', $this->name()) 18549a243cbSGreg Roach ->where('setting_name', '=', $setting_name) 18649a243cbSGreg Roach ->value('setting_value') ?? $default; 187e2a378d3SGreg Roach } 188e2a378d3SGreg Roach 189e2a378d3SGreg Roach /** 190e2a378d3SGreg Roach * Set a module setting. 191e2a378d3SGreg Roach * 192e2a378d3SGreg Roach * Since module settings are NOT NULL, setting a value to NULL will cause 193e2a378d3SGreg Roach * it to be deleted. 194e2a378d3SGreg Roach * 195e2a378d3SGreg Roach * @param string $setting_name 196e2a378d3SGreg Roach * @param string $setting_value 19715d603e7SGreg Roach * 198ea02ddf4SGreg Roach * @return void 199e2a378d3SGreg Roach */ 20037eb8894SGreg Roach final public function setPreference(string $setting_name, string $setting_value): void 201c1010edaSGreg Roach { 20232a20a8cSGreg Roach DB::table('module_setting')->updateOrInsert([ 20326684e68SGreg Roach 'module_name' => $this->name(), 20432a20a8cSGreg Roach 'setting_name' => $setting_name, 20532a20a8cSGreg Roach ], [ 20632a20a8cSGreg Roach 'setting_value' => $setting_value, 207c1010edaSGreg Roach ]); 208e2a378d3SGreg Roach } 209e2a378d3SGreg Roach 210e2a378d3SGreg Roach /** 211e2a378d3SGreg Roach * Get a the current access level for a module 212e2a378d3SGreg Roach * 213e2a378d3SGreg Roach * @param Tree $tree 21487cca37cSGreg Roach * @param string $interface 215e2a378d3SGreg Roach * 216cbc1590aSGreg Roach * @return int 217e2a378d3SGreg Roach */ 21887cca37cSGreg Roach final public function accessLevel(Tree $tree, string $interface): int 219c1010edaSGreg Roach { 22049a243cbSGreg Roach $access_levels = app('cache.array') 2210b5fd0a6SGreg Roach ->rememberForever('module_privacy' . $tree->id(), static function () use ($tree): Collection { 22249a243cbSGreg Roach return DB::table('module_privacy') 22332a20a8cSGreg Roach ->where('gedcom_id', '=', $tree->id()) 22449a243cbSGreg Roach ->get(); 22549a243cbSGreg Roach }); 226e2a378d3SGreg Roach 2270797053bSGreg Roach $row = $access_levels->first(function (stdClass $row) use ($interface): bool { 22887cca37cSGreg Roach return $row->interface === $interface && $row->module_name === $this->name(); 2290797053bSGreg Roach }); 230b2ce94c6SRico Sonntag 23149a243cbSGreg Roach return $row ? (int) $row->access_level : $this->access_level; 232e2a378d3SGreg Roach } 233e2ae4578SGreg Roach 234e2ae4578SGreg Roach /** 23502086832SGreg Roach * Where does this module store its resources 23602086832SGreg Roach * 23702086832SGreg Roach * @return string 23802086832SGreg Roach */ 23902086832SGreg Roach public function resourcesFolder(): string 24002086832SGreg Roach { 241f397d0fdSGreg Roach return Webtrees::ROOT_DIR . 'resources/'; 24202086832SGreg Roach } 243e2a378d3SGreg Roach} 244