1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2016 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; 21 22/** 23 * Class AbstractModule - common functions for blocks 24 */ 25abstract class AbstractModule { 26 /** @var string The directory where the module is installed */ 27 private $directory; 28 29 /** @var string[] A cached copy of the module settings */ 30 private $settings; 31 32 /** 33 * Create a new module. 34 * 35 * @param string $directory Where is this module installed 36 */ 37 public function __construct($directory) { 38 $this->directory = $directory; 39 } 40 41 /** 42 * Get a block setting. 43 * 44 * @param int $block_id 45 * @param string $setting_name 46 * @param string|null $default_value 47 * 48 * @return null|string 49 */ 50 public function getBlockSetting($block_id, $setting_name, $default_value = null) { 51 $setting_value = Database::prepare( 52 "SELECT SQL_CACHE setting_value FROM `##block_setting` WHERE block_id = :block_id AND setting_name = :setting_name" 53 )->execute(array( 54 'block_id' => $block_id, 55 'setting_name' => $setting_name, 56 ))->fetchOne(); 57 58 return $setting_value === null ? $default_value : $setting_value; 59 } 60 61 /** 62 * Set a block setting. 63 * 64 * @param int $block_id 65 * @param string $setting_name 66 * @param string|null $setting_value 67 * 68 * @return $this 69 */ 70 public function setBlockSetting($block_id, $setting_name, $setting_value) { 71 if ($setting_value === null) { 72 Database::prepare( 73 "DELETE FROM `##block_setting` WHERE block_id = :block_id AND setting_name = :setting_name" 74 )->execute(array( 75 'block_id' => $block_id, 76 'setting_name' => $setting_name, 77 )); 78 } else { 79 Database::prepare( 80 "REPLACE INTO `##block_setting` (block_id, setting_name, setting_value) VALUES (:block_id, :setting_name, :setting_value)" 81 )->execute(array( 82 'block_id' => $block_id, 83 'setting_name' => $setting_name, 84 'setting_value' => $setting_value, 85 )); 86 } 87 88 return $this; 89 } 90 91 /** 92 * How should this module be labelled on tabs, menus, etc.? 93 * 94 * @return string 95 */ 96 abstract public function getTitle(); 97 98 /** 99 * A sentence describing what this module does. 100 * 101 * @return string 102 */ 103 abstract public function getDescription(); 104 105 /** 106 * What is the default access level for this module? 107 * 108 * Some modules are aimed at admins or managers, and are not generally shown to users. 109 * 110 * @return int 111 */ 112 public function defaultAccessLevel() { 113 // Returns one of: Auth::PRIV_HIDE, Auth::PRIV_PRIVATE, Auth::PRIV_USER, WT_PRIV_ADMIN 114 return Auth::PRIV_PRIVATE; 115 } 116 117 /** 118 * Provide a unique internal name for this module 119 * 120 * @return string 121 */ 122 public function getName() { 123 return basename($this->directory); 124 } 125 126 /** 127 * Load all the settings for the module into a cache. 128 * 129 * Since modules may have many settings, and will probably want to use 130 * lots of them, load them all at once and cache them. 131 */ 132 private function loadAllSettings() { 133 if ($this->settings === null) { 134 $this->settings = Database::prepare( 135 "SELECT SQL_CACHE setting_name, setting_value FROM `##module_setting` WHERE module_name = ?" 136 )->execute(array($this->getName()))->fetchAssoc(); 137 } 138 } 139 140 /** 141 * Get a module setting. Return a default if the setting is not set. 142 * 143 * @param string $setting_name 144 * @param string $default 145 * 146 * @return string|null 147 */ 148 public function getSetting($setting_name, $default = null) { 149 $this->loadAllSettings(); 150 151 if (array_key_exists($setting_name, $this->settings)) { 152 return $this->settings[$setting_name]; 153 } else { 154 return $default; 155 } 156 } 157 158 /** 159 * Set a module setting. 160 * 161 * Since module settings are NOT NULL, setting a value to NULL will cause 162 * it to be deleted. 163 * 164 * @param string $setting_name 165 * @param string $setting_value 166 */ 167 public function setSetting($setting_name, $setting_value) { 168 $this->loadAllSettings(); 169 170 if ($setting_value === null) { 171 Database::prepare( 172 "DELETE FROM `##module_setting` WHERE module_name = ? AND setting_name = ?" 173 )->execute(array($this->getName(), $setting_name)); 174 unset($this->settings[$setting_name]); 175 } elseif (!array_key_exists($setting_name, $this->settings)) { 176 Database::prepare( 177 "INSERT INTO `##module_setting` (module_name, setting_name, setting_value) VALUES (?, ?, ?)" 178 )->execute(array($this->getName(), $setting_name, $setting_value)); 179 $this->settings[$setting_name] = $setting_value; 180 } elseif ($setting_value != $this->settings[$setting_name]) { 181 Database::prepare( 182 "UPDATE `##module_setting` SET setting_value = ? WHERE module_name = ? AND setting_name = ?" 183 )->execute(array($setting_value, $this->getName(), $setting_name)); 184 $this->settings[$setting_name] = $setting_value; 185 } else { 186 // Setting already exists, but with the same value - do nothing. 187 } 188 } 189 190 /** 191 * This is a general purpose hook, allowing modules to respond to routes 192 * of the form module.php?mod=FOO&mod_action=BAR 193 * 194 * @param string $mod_action 195 */ 196 public function modAction($mod_action) { 197 } 198 199 /** 200 * Get a the current access level for a module 201 * 202 * @param Tree $tree 203 * @param string $component tab, block, menu, etc 204 * 205 * @return int 206 */ 207 public function getAccessLevel(Tree $tree, $component) { 208 $access_level = Database::prepare( 209 "SELECT access_level FROM `##module_privacy` WHERE gedcom_id = :gedcom_id AND module_name = :module_name AND component = :component" 210 )->execute(array( 211 'gedcom_id' => $tree->getTreeId(), 212 'module_name' => $this->getName(), 213 'component' => $component, 214 ))->fetchOne(); 215 216 if ($access_level === null) { 217 return $this->defaultAccessLevel(); 218 } else { 219 return (int) $access_level; 220 } 221 } 222} 223