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