forked from craigk5n/webcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.php
396 lines (362 loc) · 13.9 KB
/
layers.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
389
390
391
392
393
394
395
396
<?php
/**
* This page handles managing a user's layers
* and works with layer_ajax.php to make changes.
*/
require_once 'includes/init.php';
send_no_cache_header();
$layer_user = $login;
$public = getValue('public');
$u_url = '';
$updating_public = false;
if ($is_admin && !empty($public) && $PUBLIC_ACCESS == 'Y') {
$layer_user = '__public__';
$u_url = '&public=1';
$updating_public = true;
}
load_user_layers($layer_user, 1);
$layers_enabled = 0;
$res = dbi_execute('SELECT cal_value FROM webcal_user_pref
WHERE cal_setting = "LAYERS_STATUS"
AND cal_login = ?', [$layer_user]);
if ($res) {
$row = dbi_fetch_row($res);
$layers_enabled = ($row && !empty($row) && $row[0] == 'Y' ? 1 : 0);
dbi_free_result($res);
}
$areYouSureStr = translate('Are you sure you want to delete this layer?');
$colorStr = translate('Color');
$deleteLayerStr = translate('Delete layer');
$deleteStr = translate('Delete');
$disabledStr = translate('Disabled');
$duplicatesStr = translate('Duplicates');
$editLayerStr = translate('Edit layer');
$editStr = translate('Edit');
$layerStr = translate('Layer');
$LAYERS_DISABLED = translate('Layers are currently disabled.');
$LAYERS_ENABLED = translate('Layers are currently enabled.');
$noStr = translate('No');
$sourceStr = translate('Source');
$yesStr = translate('Yes');
$LOADING = '<center><div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div></center>';
$public_link = str_replace(
'XXX',
$PUBLIC_ACCESS_FULLNAME,
translate('Click to modify layers settings for XXX')
);
$headExtras = '';
print_header ( ['js/translate.js.php'], $headExtras, 'onload="load_layers();"');
if ($ALLOW_VIEW_OTHER != 'Y')
echo print_not_auth();
else {
if (empty($PUBLIC_ACCESS))
$PUBLIC_ACCESS = 'N';
if ($is_admin && empty($public) && $PUBLIC_ACCESS == 'Y') {
?>
<div class="rightsidetip">
<a href="layers.php?public=1"><?php echo $public_link; ?></a>
</div>
<?php
}
echo '
<h2>' . ($updating_public
? translate($PUBLIC_ACCESS_FULLNAME) . ' ' : '')
. translate('Layers') . ' <img src="images/bootstrap-icons/question-circle-fill.svg" alt="'
. translate('Help') . '" class="help" onclick="window.open( '
. '\'help_layers.php\', \'cal_help\', \'dependent,menubar,scrollbars,'
. 'height=400,width=400,innerHeight=420,outerWidth=420\' );"></h2>
' . display_admin_link();
?>
<form>
<div class="note">
<span id="layerstatus">
<?php echo $layers_enabled ? $LAYERS_ENABLED : $LAYERS_DISABLED; ?>
</span>
<button class="btn btn-primary" id="enablebutton" type="button"<?php
echo $layers_enabled ? ' disabled' : ''; ?>
onclick="return set_layer_status(true);"><?php
etranslate ( 'Enable layers' ) ?></button>
<button class="btn btn-secondary" id="disablebutton" type="button" <?php
echo $layers_enabled ? '' : ' disabled'; ?>
onclick="return set_layer_status(false);"><?php
etranslate ( 'Disable Layers' ); ?></button>
</div>
<br><br>
<div id="layerlist" style="margin-left: 25px;"> <?php echo $LOADING; ?> </div>
<br>
<div class="layerButtons" style="margin-left: 25px;">
<button class="btn btn-primary" type="button"
onclick="return edit_layer(-1)"><?php etranslate ( 'Add layer' ) ?>...</button>
</div>
<br>
<?php
}
// Create list of users for edit layer dialog.
$userlist = [];
if ($single_user == 'N') {
$otherlist = $userlist = get_my_users('', 'view');
if ($NONUSER_ENABLED == 'Y') {
// Restrict NUC list if groups are enabled.
$nonusers = get_my_nonusers($login, true, 'view');
$userlist = ($NONUSER_AT_TOP == 'Y'
? array_merge($nonusers, $userlist)
: array_merge($userlist, $nonusers));
}
if ($REMOTES_ENABLED == 'Y') {
$remotes = get_nonuser_cals($login, true);
$userlist = ($NONUSER_AT_TOP == 'Y'
? array_merge($remotes, $userlist)
: array_merge($userlist, $remotes));
}
$num_users = $osize = $size = 0;
$others = $users = '';
for ($i = 0, $cnt = count($userlist); $i < $cnt; $i++) {
if ($userlist[$i]['cal_login'] != $layer_user) {
$users .= '<option value="' . $userlist[$i]['cal_login'] . '"' .
'>' . $userlist[$i]['cal_fullname'] . '</option>';
}
}
for ($i = 0, $cnt = count($otherlist); $i < $cnt; $i++) {
if ($otherlist[$i]['cal_login'] != $layer_user) {
$osize++;
$others .= '<option value="' . $otherlist[$i]['cal_login'] . '">'
. $otherlist[$i]['cal_fullname'] . '</option>';
}
}
// TODO: handle $otherlist like 1.2 code
}
?>
</form>
<div id="edit-layer-dialog" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 id="edit-layer-title" class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="$('#edit-layer-dialog').hide();">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form name="editLayerForm" id="editLayerForm">
<input type="hidden" name="editLayerId" id="editLayerId" value="">
<input type="hidden" name="editLayerDelete" id="editLayerDelete" value="0">
<table class="table table-borderless table-responsive">
<tr>
<td data-toggle="tooltip" data-placement="top" title="<?php etranslate('Specifies the user that you would like to see displayed in your calendar.'); ?>"><label><?php echo $sourceStr; ?>:</label></td>
<td><select class="form-control" id="editLayerSource" name="editLayerSource">
<?php echo $users; ?>
</td>
</tr>
<tr>
<td data-toggle="tooltip" data-placement="top" title="<?php etranslate('The text color of the new layer that will be displayed in your calendar.'); ?>"><label><?php echo $colorStr; ?>:</label></td>
<td><?php echo print_color_input_html('editLayerColor', '', '#000000'); ?>
</td>
</tr>
<tr>
<td data-toggle="tooltip" data-placement="top" title="<?php etranslate('If checked, events that are duplicates of your events will be shown.'); ?>"><label><?php echo $duplicatesStr; ?>:</label></td>
<td><input class="form-control" type="checkbox" name="editLayerDups" id="editLayerDups">
</td>
</tr>
</table>
<div class="modal-footer">
<button class="btn btn-secondary" data-dismiss="modal"
type="button" onclick="$('#edit-layer-dialog').hide();"><?php
etranslate ( 'Cancel' ); ?></button>
<button class="btn btn-danger" id="editLayerDeleteButton"
type="button" onclick="if ( confirm ( '<?php echo $areYouSureStr; ?>' ) ) {
$('#editLayerDelete').prop ('value', '1'); edit_window_closed();
$('#edit-layer-dialog').hide(); }"><?php etranslate ( 'Delete' ); ?></button>
<button class="btn btn-primary" data-dismiss="modal" type="button"
onclick="edit_window_closed(); $('#edit-layer-dialog').hide();"><?php
etranslate ( 'Save' ) ?></button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('[data-toggle="tooltip"]').tooltip();
});
var layers = [];
// Set the LAYER_STATUS value in webcal_user_pref for either the current
// user or the public user ('__public__') with an AJAX call to
// layers_ajax.php.
function set_layer_status(enable) {
var layerstatus = (enable ? 'enable' : 'disable');
$.post('layers_ajax.php', {
action: layerstatus,
csrf_form_key: '<?php echo getFormKey(); ?>'
<?php
if ($updating_public) {
echo ', public: "1"';
}
?>
},
function(data, status) {
var stringified = JSON.stringify(data);
console.log("set_layer_status Data: " + stringified + "\nStatus: " + status);
try {
var response = jQuery.parseJSON(stringified);
console.log('set_layer_status response=' + response);
} catch (err) {
alert('<?php etranslate('Error'); ?>: <?php etranslate('JSON error'); ?> - ' + err);
return;
}
if (response.error) {
console.log('Ajax error: ' + response);
alert('<?php etranslate("Error"); ?>:\n\n' + response.message);
} else {
//alert("Success! \n\n" + response);
if (enable) {
$('#layerstatus').html('<?php echo $LAYERS_ENABLED; ?>');
$('#enablebutton').prop('disabled', true);
$('#disablebutton').prop('disabled', false);
alert('<?php echo strip_tags($LAYERS_ENABLED); ?>');
} else {
$('#layerstatus').html('<?php echo $LAYERS_DISABLED; ?>');
$('#disablebutton').prop('disabled', true);
$('#enablebutton').prop('disabled', false);
alert('<?php echo strip_tags($LAYERS_DISABLED); ?>');
}
}
});
}
function load_layers() {
layers = [];
$('#layerlist').html('<?php echo $LOADING; ?>');
$.post('layers_ajax.php', {
action: 'list',
csrf_form_key: '<?php echo getFormKey(); ?>'
<?php
if ($updating_public) {
echo ', public: "1"';
}
?>
},
function(data, status) {
console.log("Data: " + data + "\nStatus: " + status);
try {
var response = jQuery.parseJSON(data);
} catch (err) {
alert('<?php etranslate('Error'); ?>: <?php etranslate('JSON error'); ?> - ' + err);
return;
}
if (response.error) {
alert('<?php etranslate('Error'); ?>: ' + response.message);
return;
}
console.log('response.layers.length=' + response.layers.length);
var x = '<table class="table table-striped" id="layertable" border="1"><tr><th><?php echo $sourceStr; ?></th><th><?php echo $colorStr; ?></th><th><?php echo $duplicatesStr; ?></th></tr>\n';
for (var i = 0; i < response.layers.length; i++) {
var l = response.layers[i];
layers[l.id] = {
id: l.id,
source: l.source,
color: l.color,
dups: l.dups,
fullname: l.fullname
};
x += '<tr onclick="return edit_layer(' + l.id + ')">' +
'<td>' + l.fullname + '</td><td>' + l.color +
'<span class="colorsample" style="background-color: ' + l.color +
'"> </span></td><td>' +
(l.dups == 'Y' ? '<?php echo $yesStr; ?>' : '<?php echo $noStr; ?>') +
'</td></tr>\n';
}
x += '</table>\n';
$('#layerlist').html(x);
//console.log('x=' + x);
});
}
function edit_window_closed() {
var layeruser = '<?php echo $layer_user; ?>';
var source = $('#editLayerSource').val();
var color = $('#editLayerColor').val();
var dups = $('#editLayerDups').is(':checked') ? 'Y' : 'N';
var del = $('#editLayerDelete').val();
var id = $('#editLayerId').val();
var action = (del > 0) ? 'delete' : 'save';
console.log("Sending save...\nid: " + id + "\nlayeruser: " + layeruser +
"\nsource: " + source + "\ncolor: " + color + "\ndups: " + dups +
"\ndelete: " + del);
$.post('layers_ajax.php', {
action: action,
id: id,
layeruser: layeruser,
source: source,
color: color,
dups: dups,
csrf_form_key: '<?php echo getFormKey(); ?>'
},
function(data, status) {
var stringified = JSON.stringify(data);
console.log("set_layer_status Data: " + stringified + "\nStatus: " + status);
try {
var response = jQuery.parseJSON(stringified);
console.log('set_layer_status response=' + response);
} catch (err) {
alert('<?php etranslate('Error'); ?>: <?php etranslate('JSON error'); ?> - ' + err);
return;
}
if (response.error) {
alert('<?php etranslate('Error'); ?>: ' + response.message);
return;
}
// Reload layers
load_layers();
});
}
function edit_layer(id) {
console.log('edit_layer(' + id + ')');
var titleStr = '';
if (id < 0)
titleStr = '<?php etranslate('Add Layer'); ?>';
else
titleStr = '<?php etranslate('Edit Layer'); ?>';
$('#edit-layer-title').html(titleStr);
if (id < 0) {
$('#editLayerDeleteButton').prop('disabled', true);
} else {
$('#editLayerDeleteButton').prop('disabled', false);
}
$('#editLayerDelete').prop("value", 0);
// Find correct user in select list
var o = $('#editLayerSource');
var optionValues = $("#editLayerSource>option").map(function() { return $(this).val(); });;
var found = false;
if (id > 0) {
console.log('id=' + id + "\noptionValues.length=" + optionValues.length);
var n = layers[id]['source'];
for (var i = 0; i < optionValues.length; i++) {
console.log("Compare " + optionValues[i] + " vs " + n);
if (optionValues[i] == n) {
o.val(n);
console.log('Selecting index ' + i + ", value=" + n);
found = true;
}
}
}
if(!found) {
console.log("Layer ID not found");
// alert?
}
$('#editLayerId').prop("value", id);
$('#editLayerColor').prop("value", id < 0 ? '#000000' : layers[id]['color']);
// Also change the background color of the sample.
//$('#editLayerColor_sample').style.background =
// ( id < 0 ? '#000000' : layers[id]['color'] );
if (id < 0)
$('#editLayerDups').prop("checked", false);
else if (layers[id]['dups'] == 'Y')
$('#editLayerDups').prop("checked", true);
else
$('#editLayerDups').prop("checked", false);
$('#edit-layer-dialog').show();
}
</script>
<?php
echo print_trailer();
?>