forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_edit.php
388 lines (362 loc) · 14.1 KB
/
index_edit.php
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2017 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 3 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, see <http://www.gnu.org/licenses/>.
*/
namespace Fisharebest\Webtrees;
use Fisharebest\Webtrees\Controller\PageController;
use Fisharebest\Webtrees\Functions\FunctionsDb;
/** @global Tree $WT_TREE */
global $WT_TREE;
require 'includes/session.php';
$controller = new PageController;
// Only one of $user_id and $gedcom_id should be set
$user_id = Filter::get('user_id', WT_REGEX_INTEGER, Filter::post('user_id', WT_REGEX_INTEGER));
$gedcom_id = Filter::get('gedcom_id', WT_REGEX_INTEGER, Filter::post('gedcom_id', WT_REGEX_INTEGER));
if ($user_id) {
$gedcom_id = null;
if ($user_id < 0) {
$controller->setPageTitle(I18N::translate('Set the default blocks for new users'));
$can_reset = false;
} else {
$controller->setPageTitle(I18N::translate('Change the “My page” blocks'));
$can_reset = true;
}
} else {
if ($gedcom_id < 0) {
$controller->setPageTitle(I18N::translate('Set the default blocks for new family trees'));
$can_reset = false;
} else {
$controller->setPageTitle(I18N::translate('Change the “Home page” blocks'));
$can_reset = true;
}
}
// Only an admin can edit the "default" page
// Only managers can edit the "home page"
// Only a user or an admin can edit a user’s "my page"
if (
$gedcom_id < 0 && !Auth::isAdmin() ||
$gedcom_id > 0 && !Auth::isManager(Tree::findById($gedcom_id)) ||
$user_id && Auth::id() != $user_id && !Auth::isAdmin()
) {
header('Location: index.php');
return;
}
$action = Filter::get('action');
if ($can_reset && Filter::post('default') === '1') {
if ($user_id) {
$defaults = FunctionsDb::getUserBlocks(-1);
} else {
$defaults = FunctionsDb::getTreeBlocks(-1);
}
$main = $defaults['main'];
$right = $defaults['side'];
} else {
if (isset($_REQUEST['main'])) {
$main = $_REQUEST['main'];
} else {
$main = [];
}
if (isset($_REQUEST['side'])) {
$side = $_REQUEST['side'];
} else {
$side = [];
}
}
$all_blocks = [];
foreach (Module::getActiveBlocks($WT_TREE) as $name => $block) {
if ($user_id && $block->isUserBlock() || $gedcom_id && $block->isGedcomBlock()) {
$all_blocks[$name] = $block;
}
}
if ($user_id) {
$blocks = FunctionsDb::getUserBlocks($user_id);
} else {
$blocks = FunctionsDb::getTreeBlocks($gedcom_id);
}
if ($action === 'update') {
foreach (['main', 'side'] as $location) {
if ($location === 'main') {
$new_blocks = $main;
} else {
$new_blocks = $side;
}
foreach ($new_blocks as $order => $block_name) {
if (is_numeric($block_name)) {
// existing block
Database::prepare("UPDATE `##block` SET block_order=? WHERE block_id=?")->execute([$order, $block_name]);
// existing block moved location
Database::prepare("UPDATE `##block` SET location=? WHERE block_id=?")->execute([$location, $block_name]);
} else {
// new block
if ($user_id) {
Database::prepare("INSERT INTO `##block` (user_id, location, block_order, module_name) VALUES (?, ?, ?, ?)")->execute([$user_id, $location, $order, $block_name]);
} else {
Database::prepare("INSERT INTO `##block` (gedcom_id, location, block_order, module_name) VALUES (?, ?, ?, ?)")->execute([$gedcom_id, $location, $order, $block_name]);
}
}
}
// deleted blocks
foreach ($blocks[$location] as $block_id => $block_name) {
if (!in_array($block_id, $main) && !in_array($block_id, $side)) {
Database::prepare("DELETE FROM `##block_setting` WHERE block_id=?")->execute([$block_id]);
Database::prepare("DELETE FROM `##block` WHERE block_id=?")->execute([$block_id]);
}
}
}
if ($user_id < 0 || $gedcom_id < 0 ) {
header('Location: admin.php');
} elseif ($user_id > 0) {
header('Location: index.php?ctype=user&ged=' . $WT_TREE->getNameUrl());
} else {
header('Location: index.php?ctype=gedcom&ged=' . $WT_TREE->getNameUrl());
}
return;
}
$controller->pageHeader();
?>
<script>
/**
* This function moves the selected option up in the given select list
*
* @param section_name the name of the select to move the options
*/
function move_up_block(section_name) {
var section_select = document.getElementById(section_name);
if (section_select) {
if (section_select.selectedIndex <= 0) {
return false;
}
var index = section_select.selectedIndex;
var temp = new Option(section_select.options[index-1].text, section_select.options[index-1].value);
section_select.options[index-1] = new Option(section_select.options[index].text, section_select.options[index].value);
section_select.options[index] = temp;
section_select.selectedIndex = index-1;
}
return false;
}
/**
* This function moves the selected option down in the given select list
*
* @param section_name the name of the select to move the options
*/
function move_down_block(section_name) {
var section_select = document.getElementById(section_name);
if (section_select) {
if (section_select.selectedIndex < 0) {
return false;
}
if (section_select.selectedIndex >= section_select.length - 1) {
return false;
}
var index = section_select.selectedIndex;
var temp = new Option(section_select.options[index + 1].text, section_select.options[index + 1].value);
section_select.options[index + 1] = new Option(section_select.options[index].text, section_select.options[index].value);
section_select.options[index] = temp;
section_select.selectedIndex = index + 1;
}
return false;
}
/**
* This function moves the selected option down in the given select list
*
* @param from_column the name of the select to move the option from
* @param to_column the name of the select to remove the option to
*/
function move_left_right_block(from_column, to_column) {
var to_select = document.getElementById(to_column);
var from_select = document.getElementById(from_column);
if ((to_select) && (from_select)) {
var add_option = from_select.options[from_select.selectedIndex];
if (to_column !== "available_select") {
to_select.options[to_select.length] = new Option(add_option.text, add_option.value);
}
if (from_column !== "available_select") {
from_select.options[from_select.selectedIndex] = null; //remove from list
}
}
return false;
}
/**
* Select Options Javascript function
*
* This function selects all the options in the multiple select lists
*/
function select_options() {
var section_select = document.getElementById("main_select");
var i;
if (section_select) {
for (i = 0; i < section_select.length; i++) {
section_select.options[i].selected=true;
}
}
section_select = document.getElementById("side_select");
if (section_select) {
for (i = 0; i < section_select.length; i++) {
section_select.options[i].selected=true;
}
}
return true;
}
/**
* Show Block Description Javascript function
*
* This function shows a description for the selected option
*
* @param list_name the name of the select to get the option from
*/
function show_description(list_name) {
var list_select = document.getElementById(list_name);
var instruct = document.getElementById("instructions");
if (block_descr[list_select.options[list_select.selectedIndex].value] && instruct) {
instruct.innerHTML = block_descr[list_select.options[list_select.selectedIndex].value];
} else {
instruct.innerHTML = block_descr["advice1"];
}
if (list_name === "main_select") {
document.getElementById("available_select").selectedIndex = -1;
document.getElementById("side_select").selectedIndex = -1;
}
if (list_name === "available_select") {
document.getElementById("main_select").selectedIndex = -1;
document.getElementById("side_select").selectedIndex = -1;
}
if (list_name === "side_select") {
document.getElementById("main_select").selectedIndex = -1;
document.getElementById("available_select").selectedIndex = -1;
}
}
var block_descr = { advice1: " "};
<?php foreach ($all_blocks as $block_name => $block): ?>
block_descr[<?= json_encode($block_name) ?>] = <?= json_encode($block->getDescription()) ?>;
<?php endforeach ?>
</script>
<h2><?= $controller->getPageTitle() ?></h2>
<p>
<?= I18N::translate('Select a block and use the arrows to move it.') ?>
</p>
<form name="config_setup" method="post" action="index_edit.php?action=update" onsubmit="select_options();" >
<input type="hidden" name="user_id" value="<?= $user_id ?>">
<input type="hidden" name="gedcom_id" value="<?= $gedcom_id ?>">
<table border="1" id="change_blocks">
<thead>
<tr>
<th class="descriptionbox center vmiddle" colspan="2">
<label for="main_select">
<?= I18N::translate('Main section blocks') ?>
</label>
</th>
<th class="descriptionbox center vmiddle" colspan="3">
<label for="available_select">
<?= I18N::translate('Available blocks') ?>
</label>
</th>
<th class="descriptionbox center vmiddle" colspan="2">
<label for="side_select">
<?= I18N::translate('Right section blocks') ?>
</label>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="optionbox center vmiddle">
<?= FontAwesome::linkIcon('arrow-up', I18N::translate('Move up'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_up_block("main_select");']) ?>
<br>
<?= FontAwesome::linkIcon('arrow-end', I18N::translate('Move right'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_left_right_block("main_select", "side_select");']) ?>
<br>
<?= FontAwesome::linkIcon('delete', I18N::translate('Remove'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_left_right_block("main_select", "available_select");']) ?>
<br>
<?= FontAwesome::linkIcon('arrow-down', I18N::translate('Move down'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_down_block("main_select");']) ?>
</td>
<td class="optionbox center">
<select multiple="multiple" id="main_select" name="main[]" size="10" onchange="show_description('main_select');">
<?php foreach ($blocks['main'] as $block_id => $block_name): ?>
<option value="<?= $block_id ?>">
<?= $all_blocks[$block_name]->getTitle() ?>
</option>
<?php endforeach ?>
</select>
</td>
<td class="optionbox center vmiddle">
<?= FontAwesome::linkIcon('arrow-start', I18N::translate('Add'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_left_right_block("available_select", "main_select");']) ?>
</td>
<td class="optionbox center">
<select id="available_select" name="available[]" size="10" onchange="show_description('available_select');">
<?php foreach ($all_blocks as $block_name => $block): ?>
<option value="<?= $block_name ?>">
<?= $block->getTitle() ?>
</option>
<?php endforeach ?>
</select>
</td>
<td class="optionbox center vmiddle">
<?= FontAwesome::linkIcon('arrow-end', I18N::translate('Add'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_left_right_block("available_select", "side_select");']) ?>
</td>
<td class="optionbox center">
<select multiple="multiple" id="side_select" name="side[]" size="10" onchange="show_description('side_select');">
<?php foreach ($blocks['side'] as $block_id => $block_name): ?>
<option value="<?= $block_id ?>">
<?= $all_blocks[$block_name]->getTitle() ?>
</option>
<?php endforeach ?>
</select>
</td>
<td class="optionbox center vmiddle">
<?= FontAwesome::linkIcon('arrow-up', I18N::translate('Move up'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_up_block("side_select");']) ?>
<br>
<?= FontAwesome::linkIcon('arrow-start', I18N::translate('Move left'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_left_right_block("side_select", "main_select");']) ?>
<br>
<?= FontAwesome::linkIcon('delete', I18N::translate('Remove'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_left_right_block("side_select", "available_select");']) ?>
<br>
<?= FontAwesome::linkIcon('arrow-down', I18N::translate('Move down'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return move_down_block("side_select");']) ?>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="descriptionbox wrap" colspan="7">
<div id="instructions">
</div>
</td>
</tr>
<tr>
<td class="topbottombar" colspan="4">
<?php if ($can_reset): ?>
<label>
<input type="checkbox" name="default" value="1">
<?= I18N::translate('Restore the default block layout') ?>
</label>
<?php endif ?>
</td>
<td class="topbottombar" colspan="3">
<button type="submit" class="btn btn-primary">
<?= FontAwesome::decorativeIcon('save') ?>
<?= I18N::translate('save') ?>
</button>
<?php if ($user_id < 0 || $gedcom_id < 0 ): ?>
<a class="btn btn-secondary" href="admin.php">
<?php elseif ($user_id > 0): ?>
<a class="btn btn-secondary" href="index.php?ctype=user&ged=<?= $WT_TREE->getNameHtml() ?>">
<?php else: ?>
<a class="btn btn-secondary" href="index.php?ctype=gedcom&ged=<?= $WT_TREE->getNameHtml() ?>">
<?php endif ?>
<?= FontAwesome::decorativeIcon('cancel') ?>
<?= I18N::translate('cancel') ?>
</a>
</td>
</tr>
</tfoot>
</table>
</form>