1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Tree; 21use Symfony\Component\HttpFoundation\Response; 22 23/** 24 * Class AbstractModule - common functions for blocks 25 */ 26abstract class AbstractModule 27{ 28 /** @var string The directory where the module is installed */ 29 private $directory; 30 31 /** @var string[] A cached copy of the module settings */ 32 private $settings; 33 34 /** @var string For custom modules - optional (recommended) version number */ 35 const CUSTOM_VERSION = ''; 36 37 /** @var string For custom modules - link for support, upgrades, etc. */ 38 const CUSTOM_WEBSITE = ''; 39 40 protected $layout = 'layouts/default'; 41 42 /** 43 * Create a new module. 44 * 45 * @param string $directory Where is this module installed 46 */ 47 public function __construct($directory) 48 { 49 $this->directory = $directory; 50 } 51 52 /** 53 * Get a block setting. 54 * 55 * @param int $block_id 56 * @param string $setting_name 57 * @param string $default_value 58 * 59 * @return string 60 */ 61 public function getBlockSetting(int $block_id, string $setting_name, string $default_value = ''): string 62 { 63 $setting_value = Database::prepare( 64 "SELECT setting_value FROM `##block_setting` WHERE block_id = :block_id AND setting_name = :setting_name" 65 )->execute([ 66 'block_id' => $block_id, 67 'setting_name' => $setting_name, 68 ])->fetchOne(); 69 70 return $setting_value ?? $default_value; 71 } 72 73 /** 74 * Set a block setting. 75 * 76 * @param int $block_id 77 * @param string $setting_name 78 * @param string $setting_value 79 * 80 * @return $this 81 */ 82 public function setBlockSetting(int $block_id, string $setting_name, string $setting_value): self 83 { 84 Database::prepare( 85 "REPLACE INTO `##block_setting` (block_id, setting_name, setting_value) VALUES (:block_id, :setting_name, :setting_value)" 86 )->execute([ 87 'block_id' => $block_id, 88 'setting_name' => $setting_name, 89 'setting_value' => $setting_value, 90 ]); 91 92 return $this; 93 } 94 95 /** 96 * How should this module be labelled on tabs, menus, etc.? 97 * 98 * @return string 99 */ 100 abstract public function getTitle(): string; 101 102 /** 103 * A sentence describing what this module does. 104 * 105 * @return string 106 */ 107 abstract public function getDescription(): string; 108 109 /** 110 * What is the default access level for this module? 111 * 112 * Some modules are aimed at admins or managers, and are not generally shown to users. 113 * 114 * @return int 115 */ 116 public function defaultAccessLevel(): int 117 { 118 // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN 119 return Auth::PRIV_PRIVATE; 120 } 121 122 /** 123 * Provide a unique internal name for this module 124 * 125 * @return string 126 */ 127 public function getName(): string 128 { 129 return basename($this->directory); 130 } 131 132 /** 133 * Load all the settings for the module into a cache. 134 * 135 * Since modules may have many settings, and will probably want to use 136 * lots of them, load them all at once and cache them. 137 * 138 * @return void 139 */ 140 private function loadAllSettings() 141 { 142 if ($this->settings === null) { 143 $this->settings = Database::prepare( 144 "SELECT setting_name, setting_value FROM `##module_setting` WHERE module_name = ?" 145 )->execute([$this->getName()])->fetchAssoc(); 146 } 147 } 148 149 /** 150 * Get a module setting. Return a default if the setting is not set. 151 * 152 * @param string $setting_name 153 * @param string $default 154 * 155 * @return string 156 */ 157 public function getPreference($setting_name, $default = '') 158 { 159 $this->loadAllSettings(); 160 161 if (array_key_exists($setting_name, $this->settings)) { 162 return $this->settings[$setting_name]; 163 } 164 165 return $default; 166 } 167 168 /** 169 * Set a module setting. 170 * 171 * Since module settings are NOT NULL, setting a value to NULL will cause 172 * it to be deleted. 173 * 174 * @param string $setting_name 175 * @param string $setting_value 176 * 177 * @return $this 178 */ 179 public function setPreference($setting_name, $setting_value): self 180 { 181 $this->loadAllSettings(); 182 183 if (!array_key_exists($setting_name, $this->settings)) { 184 Database::prepare( 185 "INSERT INTO `##module_setting` (module_name, setting_name, setting_value) VALUES (?, ?, ?)" 186 )->execute([ 187 $this->getName(), 188 $setting_name, 189 $setting_value, 190 ]); 191 192 $this->settings[$setting_name] = $setting_value; 193 } elseif ($setting_value !== $this->getPreference($setting_name)) { 194 Database::prepare( 195 "UPDATE `##module_setting` SET setting_value = ? WHERE module_name = ? AND setting_name = ?" 196 )->execute([ 197 $setting_value, 198 $this->getName(), 199 $setting_name, 200 ]); 201 202 $this->settings[$setting_name] = $setting_value; 203 } else { 204 // Setting already exists, but with the same value - do nothing. 205 } 206 207 return $this; 208 } 209 210 /** 211 * Get a the current access level for a module 212 * 213 * @param Tree $tree 214 * @param string $component tab, block, menu, etc 215 * 216 * @return int 217 */ 218 public function getAccessLevel(Tree $tree, $component) 219 { 220 $access_level = Database::prepare( 221 "SELECT access_level FROM `##module_privacy` WHERE gedcom_id = :gedcom_id AND module_name = :module_name AND component = :component" 222 )->execute([ 223 'gedcom_id' => $tree->getTreeId(), 224 'module_name' => $this->getName(), 225 'component' => $component, 226 ])->fetchOne(); 227 228 if ($access_level === null) { 229 return $this->defaultAccessLevel(); 230 } 231 232 return (int) $access_level; 233 } 234 235 /** 236 * Create a response object from a view. 237 * 238 * @param string $view_name 239 * @param mixed[] $view_data 240 * @param int $status 241 * 242 * @return Response 243 */ 244 protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response 245 { 246 // Make the view's data available to the layout. 247 $layout_data = $view_data; 248 249 // Render the view 250 $layout_data['content'] = view($view_name, $view_data); 251 252 // Insert the view into the layout 253 $html = view($this->layout, $layout_data); 254 255 return new Response($html, $status); 256 } 257} 258