summaryrefslogtreecommitdiff
path: root/admin_modules.php
blob: 9b3f765c474784de00261611b1f2e5fe606c6eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
// Module Administration User Interface.
//
// webtrees: Web based Family History software
// Copyright (C) 2015 webtrees development team.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

use WT\Auth;

define('WT_SCRIPT_NAME', 'admin_modules.php');
require 'includes/session.php';
require WT_ROOT . 'includes/functions/functions_edit.php';

$controller = new WT_Controller_Page;
$controller
	->restrictAccess(Auth::isAdmin())
	->setPageTitle(WT_I18N::translate('Module administration'));

$modules       = WT_Module::getInstalledModules('disabled');
$module_status = WT_DB::prepare("SELECT module_name, status FROM `##module`")->fetchAssoc();

if (WT_Filter::post('action') === 'update_mods' && WT_Filter::checkCsrf()) {
	foreach ($modules as $module) {
		$new_status = WT_Filter::post('status-' . $module->getName(), '[01]');
		if ($new_status !== null) {
			$new_status = $new_status ? 'enabled' : 'disabled';
			$old_status = $module_status[$module->getName()];
			if ($new_status !== $old_status) {
				WT_DB::prepare("UPDATE `##module` SET status=? WHERE module_name=?")->execute(array($new_status, $module->getName()));
				if ($new_status === 'disabled') {
					WT_FlashMessages::addMessage(WT_I18N::translate('The module “%s” has been disabled.', $module->getTitle()), 'success');
				} else {
					WT_FlashMessages::addMessage(WT_I18N::translate('The module “%s” has been enabled.', $module->getTitle()), 'success');
				}
			}
		}
	}

	header('Location: ' . WT_SERVER_NAME . WT_SCRIPT_PATH . 'admin_modules.php');

	return;
}

if (WT_Filter::post('action') === 'delete' && WT_Filter::checkCsrf()) {
	$module_name = WT_Filter::post('module_name');
	WT_DB::prepare(
		"DELETE `##block_setting`" .
		" FROM `##block_setting`" .
		" JOIN `##block` USING (block_id)" .
		" JOIN `##module` USING (module_name)" .
		" WHERE module_name=?"
	)->execute(array($module_name));
	WT_DB::prepare(
		"DELETE `##block`" .
		" FROM `##block`" .
		" JOIN `##module` USING (module_name)" .
		" WHERE module_name=?"
	)->execute(array($module_name));
	WT_DB::prepare("DELETE FROM `##module_setting` WHERE module_name=?")->execute(array($module_name));
	WT_DB::prepare("DELETE FROM `##module_privacy` WHERE module_name=?")->execute(array($module_name));
	WT_DB::prepare("DELETE FROM `##module`         WHERE module_name=?")->execute(array($module_name));

	WT_FlashMessages::addMessage(WT_I18N::translate('The preferences for the module “%s” have been deleted.', $module_name), 'success');

	header('Location: ' . WT_SERVER_NAME . WT_SCRIPT_PATH . 'admin_modules.php');

	return;
}

// Module can’t be found on disk?
// Don't delete it automatically.  It may be temporarily missing, after a re-installation, etc.
foreach ($module_status as $module_name => $status) {
	if (!array_key_exists($module_name, $modules)) {
		$html =
			WT_I18N::translate('Preferences exist for the module “%s”, but this module no longer exists.', '<span dir="ltr">' . $module_name . '</span>') .
			'<form method="post" class="form-inline">' .
			WT_Filter::getCsrf() .
			'<input type="hidden" name="action" value="delete">' .
			'<input type="hidden" name="module_name" value="' . $module_name . '">' .
			'<button type="submit" class="btn btn-link">' . WT_I18N::translate('Delete the preferences for this module.') . '</button>' .
			'</form>';
		WT_FlashMessages::addMessage($html, 'warning');
	}
}

$controller
	->pageHeader()
	->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
	->addExternalJavascript(WT_DATATABLES_BOOTSTRAP_JS_URL)
	->addInlineJavascript('
	  function reindexMods(id) {
			jQuery("#" + id + " input").each(
				function (index, value) {
					value.value = index+1;
				});
	  }
		jQuery("#installed_table").dataTable( {
			paging: false,
			' . WT_I18N::datatablesI18N() . ',
			sorting: [[ 1, "asc" ]],
			columns : [
				{ sortable: false, class: "center" },
				null,
				null,
				{ class: "center" },
				{ class: "center" },
				{ class: "center" },
				{ class: "center" },
				{ class: "center", visible: false }, // The WT_Module system does not yet include charts
				{ class: "center" },
				{ class: "center", visible: false } // The WT_Module system does not yet include themes
			]
		});
	');

?>
<ol class="breadcrumb small">
	<li><a href="admin.php"><?php echo WT_I18N::translate('Control panel'); ?></a></li>
	<li class="active"><?php echo $controller->getPageTitle(); ?></li>
</ol>

<h1><?php echo $controller->getPageTitle(); ?></h1>

<form method="post" action="<?php echo WT_SCRIPT_NAME; ?>">
	<input type="hidden" name="action" value="update_mods">
	<?php echo WT_Filter::getCsrf(); ?>
	<table class="table table-bordered table-hover table-condensed table-module-administration">
		<caption class="sr-only">
			<?php echo WT_I18N::translate('Module administration'); ?>
		</caption>
		<thead>
		<tr>
			<th><?php echo WT_I18N::translate('Enabled'); ?></th>
			<th><?php echo WT_I18N::translate('Module'); ?></th>
			<th><?php echo WT_I18N::translate('Description'); ?></th>
			<th class="hidden-xs"><a href="admin_module_menus.php"><?php echo WT_I18N::translate('Menus'); ?></a></th>
			<th class="hidden-xs"><a href="admin_module_tabs.php"><?php echo WT_I18N::translate('Tabs'); ?></a></th>
			<th class="hidden-xs"><a href="admin_module_sidebar.php"><?php echo WT_I18N::translate('Sidebars'); ?></a></th>
			<th class="hidden-xs"><a href="admin_module_blocks.php"><?php echo WT_I18N::translate('Blocks'); ?></a></th>
			<th class="hidden"><?php echo WT_I18N::translate('Charts'); ?></th>
			<th class="hidden-xs"><a href="admin_module_reports.php"><?php echo WT_I18N::translate('Reports'); ?></a></th>
			<th class="hidden"><?php echo WT_I18N::translate('Themes'); ?></th>
		</tr>
		</thead>
		<tbody>
		<?php
		foreach ($modules as $module) {
			$status = $module_status[$module->getName()];
			echo
			'<tr><td class="text-center">', two_state_checkbox('status-' . $module->getName(), $status === 'enabled'), '</td>',
			'<td>';
			if ($module instanceof WT_Module_Config) {
				echo '<a href="', $module->getConfigLink(), '">';
			}
			echo $module->getTitle();
			if ($module instanceof WT_Module_Config) {
				echo ' <i class="fa fa-cogs"></i></a>';
			}
			echo
			'</td>',
			'<td>', $module->getDescription(), '</td>',
			'<td class="text-center text-muted hidden-xs">', $module instanceof WT_Module_Menu ? '<i class="fa fa-list-ul" title="' . WT_I18N::translate('Menu') . '"></i>' : '-', '</td>',
			'<td class="text-center text-muted hidden-xs">', $module instanceof WT_Module_Tab ? '<i class="fa fa-folder" title="' . WT_I18N::translate('Tab') . '"></i>' : '-', '</td>',
			'<td class="text-center text-muted hidden-xs">', $module instanceof WT_Module_Sidebar ? '<i class="fa fa-th-large" title="' . WT_I18N::translate('Sidebar') . '"></i>' : '-', '</td>',
			'<td class="text-center text-muted hidden-xs">', $module instanceof WT_Module_Block ? (($module->isUserBlock() ? '<i class="fa fa-user" title="' . WT_I18N::translate('My page') . '"></i>' : '') . ($module->isGedcomBlock() ? '<i class="fa fa-tree" title="' . WT_I18N::translate('Home page') . '"></i>' : '')) : '-', '</td>',
			'<td class="text-center text-muted hidden">', $module instanceof WT_Module_Chart ? '<i class="fa fa-check" title="' . WT_I18N::translate('Chart') . '"></i>' : '-', '</td>',
			'<td class="text-center text-muted hidden-xs">', $module instanceof WT_Module_Report ? '<i class="fa fa-file" title="' . WT_I18N::translate('Report') . '"></i>' : '-', '</td>',
			'<td class="text-center text-muted hidden">', $module instanceof WT_Module_Theme ? '<i class="fa fa-check" title="' . WT_I18N::translate('Theme') . '"></i>' : '-', '</td>',
			'</tr>';
		}
		?>
		</tbody>
	</table>
	<button class="btn btn-primary" type="submit">
		<i class="fa fa-check"></i>
		<?php echo WT_I18N::translate('save'); ?></button>
</form>