1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Registry; 24use Fisharebest\Webtrees\Http\ViewResponseTrait; 25use Fisharebest\Webtrees\Tree; 26use Fisharebest\Webtrees\Webtrees; 27use Illuminate\Database\Capsule\Manager as DB; 28use Illuminate\Support\Collection; 29use stdClass; 30 31/** 32 * Class AbstractModule - common functions for blocks 33 */ 34abstract class AbstractModule implements ModuleInterface 35{ 36 use ViewResponseTrait; 37 38 /** @var string A unique internal name for this module (based on the installation folder). */ 39 private $name = ''; 40 41 /** @var int The default access level for this module. It can be changed in the control panel. */ 42 protected $access_level = Auth::PRIV_PRIVATE; 43 44 /** @var bool The default status for this module. It can be changed in the control panel. */ 45 private $enabled = true; 46 47 /** @var string For custom modules - optional (recommended) version number */ 48 public const CUSTOM_VERSION = ''; 49 50 /** @var string For custom modules - link for support, upgrades, etc. */ 51 public const CUSTOM_WEBSITE = ''; 52 53 /** 54 * Called for all *enabled* modules. 55 */ 56 public function boot(): void 57 { 58 } 59 60 /** 61 * How should this module be identified in the control panel, etc.? 62 * 63 * @return string 64 */ 65 public function title(): string 66 { 67 return 'Module name goes here'; 68 } 69 70 /** 71 * A sentence describing what this module does. 72 * 73 * @return string 74 */ 75 public function description(): string 76 { 77 return $this->title(); 78 } 79 80 /** 81 * Get a block setting. 82 * 83 * Originally, this was just used for the home-page blocks. Now, it is used by any 84 * module that has repeated blocks of content on the same page. 85 * 86 * @param int $block_id 87 * @param string $setting_name 88 * @param string $default 89 * 90 * @return string 91 */ 92 final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 93 { 94 $settings = Registry::cache()->array()->remember('block-setting-' . $block_id, static function () use ($block_id): array { 95 return DB::table('block_setting') 96 ->where('block_id', '=', $block_id) 97 ->pluck('setting_value', 'setting_name') 98 ->all(); 99 }); 100 101 return $settings[$setting_name] ?? $default; 102 } 103 104 /** 105 * Set a block setting. 106 * 107 * @param int $block_id 108 * @param string $setting_name 109 * @param string $setting_value 110 * 111 * @return $this 112 */ 113 final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 114 { 115 DB::table('block_setting')->updateOrInsert([ 116 'block_id' => $block_id, 117 'setting_name' => $setting_name, 118 ], [ 119 'setting_value' => $setting_value, 120 ]); 121 122 return $this; 123 } 124 125 /** 126 * A unique internal name for this module (based on the installation folder). 127 * 128 * @param string $name 129 * 130 * @return void 131 */ 132 final public function setName(string $name): void 133 { 134 $this->name = $name; 135 } 136 137 /** 138 * A unique internal name for this module (based on the installation folder). 139 * 140 * @return string 141 */ 142 final public function name(): string 143 { 144 return $this->name; 145 } 146 147 /** 148 * Modules are either enabled or disabled. 149 * 150 * @param bool $enabled 151 * 152 * @return ModuleInterface 153 */ 154 final public function setEnabled(bool $enabled): ModuleInterface 155 { 156 $this->enabled = $enabled; 157 158 return $this; 159 } 160 161 /** 162 * Modules are either enabled or disabled. 163 * 164 * @return bool 165 */ 166 final public function isEnabled(): bool 167 { 168 return $this->enabled; 169 } 170 171 /** 172 * Should this module be enabled when it is first installed? 173 * 174 * @return bool 175 */ 176 public function isEnabledByDefault(): bool 177 { 178 return true; 179 } 180 181 /** 182 * Get a module setting. Return a default if the setting is not set. 183 * 184 * @param string $setting_name 185 * @param string $default 186 * 187 * @return string 188 */ 189 final public function getPreference(string $setting_name, string $default = ''): string 190 { 191 return DB::table('module_setting') 192 ->where('module_name', '=', $this->name()) 193 ->where('setting_name', '=', $setting_name) 194 ->value('setting_value') ?? $default; 195 } 196 197 /** 198 * Set a module setting. 199 * 200 * Since module settings are NOT NULL, setting a value to NULL will cause 201 * it to be deleted. 202 * 203 * @param string $setting_name 204 * @param string $setting_value 205 * 206 * @return void 207 */ 208 final public function setPreference(string $setting_name, string $setting_value): void 209 { 210 DB::table('module_setting')->updateOrInsert([ 211 'module_name' => $this->name(), 212 'setting_name' => $setting_name, 213 ], [ 214 'setting_value' => $setting_value, 215 ]); 216 } 217 218 /** 219 * Get a the current access level for a module 220 * 221 * @param Tree $tree 222 * @param string $interface 223 * 224 * @return int 225 */ 226 final public function accessLevel(Tree $tree, string $interface): int 227 { 228 $access_levels = Registry::cache()->array() 229 ->remember('module-privacy-' . $tree->id(), static function () use ($tree): Collection { 230 return DB::table('module_privacy') 231 ->where('gedcom_id', '=', $tree->id()) 232 ->get(); 233 }); 234 235 $row = $access_levels->first(function (stdClass $row) use ($interface): bool { 236 return $row->interface === $interface && $row->module_name === $this->name(); 237 }); 238 239 return $row ? (int) $row->access_level : $this->access_level; 240 } 241 242 /** 243 * Where does this module store its resources 244 * 245 * @return string 246 */ 247 public function resourcesFolder(): string 248 { 249 return Webtrees::ROOT_DIR . 'resources/'; 250 } 251} 252