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