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\Tree; 22use Illuminate\Database\Capsule\Manager as DB; 23use Symfony\Component\HttpFoundation\Response; 24 25/** 26 * Class AbstractModule - common functions for blocks 27 */ 28abstract class AbstractModule 29{ 30 /** @var string The directory where the module is installed */ 31 private $directory; 32 33 /** @var string[] A cached copy of the module settings */ 34 private $settings; 35 36 /** @var string For custom modules - optional (recommended) version number */ 37 public const CUSTOM_VERSION = ''; 38 39 /** @var string For custom modules - link for support, upgrades, etc. */ 40 public const CUSTOM_WEBSITE = ''; 41 42 protected $layout = 'layouts/default'; 43 44 /** 45 * Create a new module. 46 * 47 * @param string $directory Where is this module installed 48 */ 49 public function __construct(string $directory) 50 { 51 $this->directory = $directory; 52 } 53 54 /** 55 * Get a block setting. 56 * 57 * @param int $block_id 58 * @param string $setting_name 59 * @param string $default 60 * 61 * @return string 62 */ 63 public function getBlockSetting(int $block_id, string $setting_name, string $default = ''): string 64 { 65 $settings = app('cache.array')->rememberForever('block_setting' . $block_id, function () use ($block_id) { 66 return DB::table('block_setting') 67 ->where('block_id', '=', $block_id) 68 ->pluck('setting_value', 'setting_name') 69 ->all(); 70 }); 71 72 return $settings[$setting_name] ?? $default; 73 } 74 75 /** 76 * Set a block setting. 77 * 78 * @param int $block_id 79 * @param string $setting_name 80 * @param string $setting_value 81 * 82 * @return $this 83 */ 84 public function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 85 { 86 DB::table('block_setting')->updateOrInsert([ 87 'block_id' => $block_id, 88 'setting_name' => $setting_name, 89 ], [ 90 'setting_value' => $setting_value, 91 ]); 92 93 return $this; 94 } 95 96 /** 97 * What is the default access level for this module? 98 * 99 * Some modules are aimed at admins or managers, and are not generally shown to users. 100 * 101 * @return int 102 */ 103 public function defaultAccessLevel(): int 104 { 105 // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN 106 return Auth::PRIV_PRIVATE; 107 } 108 109 /** 110 * Provide a unique internal name for this module 111 * 112 * @return string 113 */ 114 public function getName(): string 115 { 116 return basename($this->directory); 117 } 118 119 /** 120 * Load all the settings for the module into a cache. 121 * 122 * Since modules may have many settings, and will probably want to use 123 * lots of them, load them all at once and cache them. 124 * 125 * @return void 126 */ 127 private function loadAllSettings() 128 { 129 if ($this->settings === null) { 130 $this->settings = DB::table('module_setting') 131 ->where('module_name', '=', $this->getName()) 132 ->pluck('setting_value', 'setting_name') 133 ->all(); 134 } 135 } 136 137 /** 138 * Get a module setting. Return a default if the setting is not set. 139 * 140 * @param string $setting_name 141 * @param string $default 142 * 143 * @return string 144 */ 145 public function getPreference($setting_name, $default = '') 146 { 147 $this->loadAllSettings(); 148 149 if (array_key_exists($setting_name, $this->settings)) { 150 return $this->settings[$setting_name]; 151 } 152 153 return $default; 154 } 155 156 /** 157 * Set a module setting. 158 * 159 * Since module settings are NOT NULL, setting a value to NULL will cause 160 * it to be deleted. 161 * 162 * @param string $setting_name 163 * @param string $setting_value 164 * 165 * @return $this 166 */ 167 public function setPreference($setting_name, $setting_value): self 168 { 169 $this->loadAllSettings(); 170 171 DB::table('module_setting')->updateOrInsert([ 172 'module_name' => $this->getName(), 173 'setting_name' => $setting_name, 174 ], [ 175 'setting_value' => $setting_value, 176 ]); 177 178 $this->settings[$setting_name] = $setting_value; 179 180 return $this; 181 } 182 183 /** 184 * Get a the current access level for a module 185 * 186 * @param Tree $tree 187 * @param string $component tab, block, menu, etc 188 * 189 * @return int 190 */ 191 public function getAccessLevel(Tree $tree, $component) 192 { 193 $access_level = DB::table('module_privacy') 194 ->where('gedcom_id', '=', $tree->id()) 195 ->where('module_name', '=', $this->getName()) 196 ->where('component', '=', $component) 197 ->value('access_level'); 198 199 if ($access_level === null) { 200 return $this->defaultAccessLevel(); 201 } 202 203 return (int) $access_level; 204 } 205 206 /** 207 * Create a response object from a view. 208 * 209 * @param string $view_name 210 * @param mixed[] $view_data 211 * @param int $status 212 * 213 * @return Response 214 */ 215 protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response 216 { 217 // Make the view's data available to the layout. 218 $layout_data = $view_data; 219 220 // Render the view 221 $layout_data['content'] = view($view_name, $view_data); 222 223 // Insert the view into the layout 224 $html = view($this->layout, $layout_data); 225 226 return new Response($html, $status); 227 } 228} 229