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