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