xref: /webtrees/app/Module/AbstractModule.php (revision 73df4c545c87012519818766e1a8eb02272a59b5)
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	/** @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(array(
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(array(
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(array(
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(array($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|null
153	 */
154	public function getSetting($setting_name, $default = null) {
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	public function setSetting($setting_name, $setting_value) {
174		$this->loadAllSettings();
175
176		if ($setting_value === null) {
177			Database::prepare(
178				"DELETE FROM `##module_setting` WHERE module_name = ? AND setting_name = ?"
179			)->execute(array($this->getName(), $setting_name));
180			unset($this->settings[$setting_name]);
181		} elseif (!array_key_exists($setting_name, $this->settings)) {
182			Database::prepare(
183				"INSERT INTO `##module_setting` (module_name, setting_name, setting_value) VALUES (?, ?, ?)"
184			)->execute(array($this->getName(), $setting_name, $setting_value));
185			$this->settings[$setting_name] = $setting_value;
186		} elseif ($setting_value != $this->settings[$setting_name]) {
187			Database::prepare(
188				"UPDATE `##module_setting` SET setting_value = ? WHERE module_name = ? AND setting_name = ?"
189			)->execute(array($setting_value, $this->getName(), $setting_name));
190			$this->settings[$setting_name] = $setting_value;
191		} else {
192			// Setting already exists, but with the same value - do nothing.
193		}
194	}
195
196	/**
197	 * This is a general purpose hook, allowing modules to respond to routes
198	 * of the form module.php?mod=FOO&mod_action=BAR
199	 *
200	 * @param string $mod_action
201	 */
202	public function modAction($mod_action) {
203	}
204
205	/**
206	 * Get a the current access level for a module
207	 *
208	 * @param Tree   $tree
209	 * @param string $component tab, block, menu, etc
210	 *
211	 * @return int
212	 */
213	public function getAccessLevel(Tree $tree, $component) {
214		$access_level = Database::prepare(
215			"SELECT access_level FROM `##module_privacy` WHERE gedcom_id = :gedcom_id AND module_name = :module_name AND component = :component"
216		)->execute(array(
217			'gedcom_id'   => $tree->getTreeId(),
218			'module_name' => $this->getName(),
219			'component'   => $component,
220		))->fetchOne();
221
222		if ($access_level === null) {
223			return $this->defaultAccessLevel();
224		} else {
225			return (int) $access_level;
226		}
227	}
228}
229