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