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 Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Support\Collection; 28 29/** 30 * Class AbstractModule - common functions for blocks 31 */ 32abstract class AbstractModule implements ModuleInterface 33{ 34 use ViewResponseTrait; 35 36 // A unique internal name for this module (based on the installation folder). 37 private string $name = ''; 38 39 // The default access level for this module. It can be changed in the control panel. 40 protected int $access_level = Auth::PRIV_PRIVATE; 41 42 // The default status for this module. It can be changed in the control panel. 43 private bool $enabled = true; 44 45 /** 46 * Called for all *enabled* modules. 47 */ 48 public function boot(): void 49 { 50 } 51 52 /** 53 * How should this module be identified in the control panel, etc.? 54 * 55 * @return string 56 */ 57 public function title(): string 58 { 59 return 'Module name goes here'; 60 } 61 62 /** 63 * A sentence describing what this module does. 64 * 65 * @return string 66 */ 67 public function description(): string 68 { 69 return $this->title(); 70 } 71 72 /** 73 * Get a block setting. 74 * 75 * Originally, this was just used for the home-page blocks. Now, it is used by any 76 * module that has repeated blocks of content on the same page. 77 * 78 * @param int $block_id 79 * @param string $setting_name 80 * @param string $default 81 * 82 * @return string 83 */ 84 final protected function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 85 { 86 $settings = Registry::cache()->array()->remember('block-setting-' . $block_id, static function () use ($block_id): array { 87 return DB::table('block_setting') 88 ->where('block_id', '=', $block_id) 89 ->pluck('setting_value', 'setting_name') 90 ->all(); 91 }); 92 93 return $settings[$setting_name] ?? $default; 94 } 95 96 /** 97 * Set a block setting. 98 * 99 * @param int $block_id 100 * @param string $setting_name 101 * @param string $setting_value 102 * 103 * @return $this 104 */ 105 final protected function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 106 { 107 DB::table('block_setting')->updateOrInsert([ 108 'block_id' => $block_id, 109 'setting_name' => $setting_name, 110 ], [ 111 'setting_value' => $setting_value, 112 ]); 113 114 return $this; 115 } 116 117 /** 118 * A unique internal name for this module (based on the installation folder). 119 * 120 * @param string $name 121 * 122 * @return void 123 */ 124 final public function setName(string $name): void 125 { 126 $this->name = $name; 127 } 128 129 /** 130 * A unique internal name for this module (based on the installation folder). 131 * 132 * @return string 133 */ 134 final public function name(): string 135 { 136 return $this->name; 137 } 138 139 /** 140 * Modules are either enabled or disabled. 141 * 142 * @param bool $enabled 143 * 144 * @return ModuleInterface 145 */ 146 final public function setEnabled(bool $enabled): ModuleInterface 147 { 148 $this->enabled = $enabled; 149 150 return $this; 151 } 152 153 /** 154 * Modules are either enabled or disabled. 155 * 156 * @return bool 157 */ 158 final public function isEnabled(): bool 159 { 160 return $this->enabled; 161 } 162 163 /** 164 * Should this module be enabled when it is first installed? 165 * 166 * @return bool 167 */ 168 public function isEnabledByDefault(): bool 169 { 170 return true; 171 } 172 173 /** 174 * Get a module setting. Return a default if the setting is not set. 175 * 176 * @param string $setting_name 177 * @param string $default 178 * 179 * @return string 180 */ 181 final public function getPreference(string $setting_name, string $default = ''): string 182 { 183 return DB::table('module_setting') 184 ->where('module_name', '=', $this->name()) 185 ->where('setting_name', '=', $setting_name) 186 ->value('setting_value') ?? $default; 187 } 188 189 /** 190 * Set a module setting. 191 * 192 * Since module settings are NOT NULL, setting a value to NULL will cause 193 * it to be deleted. 194 * 195 * @param string $setting_name 196 * @param string $setting_value 197 * 198 * @return void 199 */ 200 final public function setPreference(string $setting_name, string $setting_value): void 201 { 202 DB::table('module_setting')->updateOrInsert([ 203 'module_name' => $this->name(), 204 'setting_name' => $setting_name, 205 ], [ 206 'setting_value' => $setting_value, 207 ]); 208 } 209 210 /** 211 * Get a the current access level for a module 212 * 213 * @param Tree $tree 214 * @param string $interface 215 * 216 * @return int 217 */ 218 final public function accessLevel(Tree $tree, string $interface): int 219 { 220 $access_levels = Registry::cache()->array() 221 ->remember('module-privacy-' . $tree->id(), static function () use ($tree): Collection { 222 return DB::table('module_privacy') 223 ->where('gedcom_id', '=', $tree->id()) 224 ->get(); 225 }); 226 227 $row = $access_levels->first(function (object $row) use ($interface): bool { 228 return $row->interface === $interface && $row->module_name === $this->name(); 229 }); 230 231 return $row ? (int) $row->access_level : $this->access_level; 232 } 233 234 /** 235 * Where does this module store its resources 236 * 237 * @return string 238 */ 239 public function resourcesFolder(): string 240 { 241 return 'resources/'; 242 } 243} 244