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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
<?php
/*
* 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.
*/
?>
<?
/* vim: set ts=4 sw=4 et : */
$_title = "PDF-Dokumente generieren";
require_once __DIR__ . "/check_auth.php";
require_once __DIR__ . "/../includes/common.php";
require_once __DIR__ . "/lookup.php";
require_once __DIR__ . "/header.php";
if (isset($_GET['demo'])) $_SESSION['demo'] = true;
session_write_close();
$num = 0;
if ($_POST['ids'] != '')
$num = count(explode(',', $_POST['ids']));
?>
<div id="pdf_modal" style="display: none;" class="transparent_modal">
<button onclick="abortAll()" class="emergency_off">NOT-AUS</button>
<div class="sk-folding-cube hvcenter">
<div class="sk-cube1 sk-cube"></div>
<div class="sk-cube2 sk-cube"></div>
<div class="sk-cube4 sk-cube"></div>
<div class="sk-cube3 sk-cube"></div>
</div>
</div>
<h1>PDF-Dokumente generieren
<? if ($num > 1) { ?>
<span style="font-size: .5em;">für <?=$num?> Datensätze</span>
<? } else { ?>
<span style="font-size: .5em;">für <?=$num?> Datensatz</span>
<? } ?>
</h1>
<? if ($_SESSION['demo']) { ?>
<p>Demo-Flag ist an</p>
<? } ?>
<form id="getpdf" action="/db/main/dopdf.php?test" target="_blank" method="post" style="display: inline-block;">
<table border="0" style="display: inline-block;" class="consignee">
<tr>
<td>Organisation<sup><small>Vä</small></sup></td>
<td colspan="3"><input type="text" style="width: 530px;" class="changeable" name="organisation" value=""></td>
</tr>
<tr>
<td>Anrede|Briefkopf<sup><small>V</small></sup></td>
<td><input type="text" readonly name="anrede" style="width: 12ch;" value=""> | <input type="text" readonly name="anrede_briefkopf" style="width: 12ch;" value=""></td>
<td>Titel<sup><small>V</small></sup></td>
<td><input type="text" readonly name="titel" value=""></td>
</tr>
<tr>
<td>Vorname<sup><small>V</small></sup></td>
<td><input type="text" readonly name="vorname" value=""></td>
<td>Nachname<sup><small>V</small></sup></td>
<td><input type="text" readonly name="nachname" value=""></td>
</tr>
<tr>
<td>Straße<sup><small>Vä</small></sup></td>
<td><input type="text" class="changeable" name="strasse" value=""></td>
<td>Adresszusatz<sup><small>Vä</small></sup></td>
<td><input type="text" class="changeable" name="adresszusatz" value=""></td>
</tr>
<tr>
<td>PLZ<sup><small>Vä</small></sup></td>
<td><input type="text" class="changeable" name="plz" value=""></td>
<td>Ort<sup><small>Vä</small></sup></td>
<td><input type="text" class="changeable" name="ort" value=""></td>
</tr>
<tr>
<td colspan="4" style="padding-top: 0;"><small>V: Vorschau, ä: änderbar</small></td>
</tr>
<tr>
<td>Dokument</td>
<td colspan="3">
<select class="mand tchange" name="fn" style="width: 530px;">
<? _lookup_pdf_documents(); foreach ($pdf_documents as $d) { ?>
<option value="<?=$d['name']?>"<? if ($d['name'] == $_POST['file']) { ?> selected<? }?>><?=$d['name']?> - <?=$d['description']?></option>
<? } ?>
<option value="" style="color: red !important;"><span style="color: red !important;">Bitte Auswahl treffen!</span></option>
<? if (false) { ?><input type="text" readonly name="fn" value="<?=$_POST['file']?>"><? } ?>
</select>
</td>
</tr>
<tr>
<td>Betreff</td>
<td colspan="3"><input style="width: 530px;" class="mand tchange" type="text" name="subject" placeholder="Bescheid fuer..."></td>
</tr>
<tr>
<td>Datum <button style="padding: 0; margin: 0; font-size: .8em;" onclick="$('#templdate').val($.datepicker.formatDate('dd.mm.yy', new Date())); return false;">heute</button></td>
<td><input class="mand tchange" type="text" id="templdate" name="date" placeholder="01.10.2019"></td>
</tr>
</table>
<input type="hidden" name="template" type="text" value="">
<input type="hidden" name="html" type="text" value="">
<input type="hidden" name="data" type="text" value="">
<input type="hidden" name="uid" type="text" value="">
</form>
<div style="display: inline-block; margin-left: 1em; vertical-align: top;">
<button onclick="return sendAll();" class="medium" style="width: 12em;"><i class="fas fa-file-pdf"></i><i class "fas fa-folder-open"></i> alle generieren</button><br />
<button onclick="return sendOne();" class="medium" style="width: 12em;"><i class="fas fa-file-pdf"></i><i class="fas fa-save"></i> dieses generieren</button><br />
<button onclick="return getOne();" class="medium" style="width: 12em;"><i class="fas fa-file-pdf"></i><i class="fas fa-question"></i> dieses testen</button>
<div style="color: darkorange; margin-top: .5em;">
Letzter Status:
<p id="pdf_status"></p>
</div>
</div>
<div style="display: inline-block; margin-left: 1em; vertical-align: top;">
<b>Templates</b>
<div style="color: darkorange; margin-top: .5em;">
<?
$sql = "SELECT id, name, text, subject, date, fn, ts FROM pdf_templates ORDER BY name";
$stmt = $mysqli->prepare($sql);
$stmt->bind_result($id, $name, $text, $subject, $date, $fn, $ts);
$stmt->execute();
?>
<select id="pdf_template" style="max-width: 500px;">
<option data-text="" data-subject="" data-date="" value="" selected></option>
<?
$templates = [];
while ($stmt->fetch()) {
$templates[$id] = ['name' => $name, 'text' => $text, 'ts' => $ts];
?>
<option data-id="<?=$id?>"
data-name="<?=str_replace("\"", """, $name)?>"
data-subject="<?=str_replace("\"", """, $subject)?>"
data-date="<?=str_replace("\"", """, $date)?>"
data-text="<?=str_replace("\"", """, $text)?>"
data-fn="<?=str_replace("\"", """, $fn)?>"
value="<?=$id?>"><?=$name?> (ID <?=$id?>; <?=$ts?>)</option>
<?
}
?>
</select>
<br />
<br />
<button class="small" onclick="return loadPDFTemplate();"><i class="fas fa-upload"></i> Laden</button>
<button class="small" onclick="return savePDFTemplate();" style="background: orange"><i class="fas fa-download"></i> Überschreiben</button>
<button class="small" onclick="return newPDFTemplate();"><i class="far fa-file"></i> Neu</button>
<button class="small" onclick="return renamePDFTemplate();"><i class="fas fa-pencil-alt"></i> Umbenennen</button>
<button class="small" onclick="return delPDFTemplate();" style="background: red;"><i class="fas fa-trash"></i> Löschen</button>
<?
$stmt->reset();
?>
</div>
</div>
<script>
<? minStart(); ?>
$('#pdf_template').on('change', function () {
$('#pdf_template').css('background', 'white');
});
$('.tchange').on('input change cut copy paste', function () {
$('#pdf_template').css('background', 'lightgrey');
});
$(document).ready(function () {
quill.on('text-change', function () {
$('#pdf_template').css('background', 'lightgrey');
});
setTimeout(function () {
var tid = localStorage.getItem('pdf_template');
if (tid !== '' && tid > 0) {
$('select#pdf_template').val(tid);
localStorage.removeItem('pdf_template');
setTimeout(function () {
loadPDFTemplate();
}, 100);
}
}, 200);
$('select[name=fn]').on('select change', function () {
if ($(this).val() == '') {
$(this).parent().parent().css('background', 'red');
} else {
$(this).parent().parent().css('background', 'unset');
}
});
});
function newPDFTemplate() {
var n = prompt('Name des neuen Templates:', '');
if (!n) return;
var text = $("#editor .ql-editor").html();
var subject = $('input[name=subject]').val();
var date = $('input[name=date]').val();
$.ajaxSetup({async:false});
var rv = 0;
$.post('/db/main/addtemplate.php', {
'name': n,
'text': text,
'subject': subject,
'date': date,
'pdf': true
}, function (rv) { tid = rv; });
localStorage.setItem('pdf_template', tid);
alert(unescape('Neues Template ID ' + tid + ' mit den aktuellen Werten wurde erzeugt.\r\n\r\nDie Seite lädt nun neu.'));
location.reload();
}
function renamePDFTemplate() {
var id = $('#pdf_template :selected').attr('data-id');
var on = $('#pdf_template :selected').attr('data-name');
var n = prompt('Name des Templates:', on);
if (!n) return;
$.ajaxSetup({async:false});
$.post('/db/main/rentemplate.php', {
'id': id,
'name': n,
'pdf': true
});
$('#pdf_template :selected').attr('data-name', n);
$('#pdf_template :selected').text(n + ' (ID ' + id + ')');
}
function delPDFTemplate() {
var id = $('#pdf_template').val();
var v = $('#pdf_template :selected').text();
if (!v) return;
if (confirm(unescape('Wirklich Template\n "' + v + '"\nunwiderruflich verwerfen?'))) {
$.ajaxSetup({async:false});
$.post('/db/main/deltemplate.php', {
'id': id,
'pdf': true
});
$('#pdf_template :selected').remove();
$('#pdf_template').css('background', 'red');
}
}
function loadPDFTemplate() {
console.log($('#pdf_template :selected').attr('data-text').replace(/"/g, '"'));
$("#editor .ql-editor").html($('#pdf_template :selected').attr('data-text'));
$('input[name=subject]').val($('#pdf_template :selected').attr('data-subject'));
$('input[name=date]').val($('#pdf_template :selected').attr('data-date'));
$('select[name=fn]').val($('#pdf_template :selected').attr('data-fn'));
setTimeout(function () {
$('#pdf_template').css('background', '#cdf5cd');
}, 200);
}
function savePDFTemplate() {
var text = $("#editor .ql-editor").html();
var subject = $('input[name=subject').val();
var date = $('input[name=date]').val();
var fn = $('select[name=fn]').val();
var id = $('#pdf_template').val();
$('#pdf_template :selected').attr('data-text', text);
$('#pdf_template :selected').attr('data-subject', subject);
$('#pdf_template :selected').attr('data-date', date);
$('#pdf_template :selected').attr('data-fn', fn);
$('#pdf_template :selected').text($('#pdf_template :selected').attr('data-name') + ' (ID ' + $('#pdf_template :selected').attr('data-id') + ')');
$.ajaxSetup({async:false});
$.post('/db/main/updtemplate.php', {
'id': id,
'text': text,
'subject': subject,
'date': date,
'fn': fn,
'pdf': true
});
setTimeout(function () {
$('#pdf_template').css('background', '#cdf5cd');
}, 200);
}
<? minEnd(); ?>
</script>
<?
$sql = $_POST['sql'];
$post_sql = $_POST['postsql'];
$payload_sql = $_POST['payload_sql'];
$payload_sql_order = $_POST['payload_sql_order'];
$independent_payload_sql = $_POST['independent_payload_sql'];
$independent_payload_sql_order = $_POST['independent_payload_sql_order'];
require_once __DIR__ . "/templater_common.php";
?>
<script>
<? minStart(); ?>
function getOne() {
if ($('select[name=fn]').val() == "") {
alert(unescape('Bitte Auswahl bei Dokument (Dateiname) treffen!'));
return;
}
var html = $('#preview').html();
$('input[name=html]').val(html);
$('#getpdf').submit();
}
function sendOne(skip) {
if ($('select[name=fn]').val() == "") {
alert(unescape('Bitte Auswahl bei Dokument (Dateiname) treffen!'));
return;
}
if (typeof skip !== 'boolean')
skip = false;
if (typeof pivot.__done !== 'undefined') {
if (!confirm(unescape('Diese Adressatenkombination wurde schon generiert. Wiederholen und Überschreiben?')))
return;
}
var subject = $('input[name=subject]').val();
var fn = $('select[name=fn]').val();
var date = $('input[name=date]').val();
var organisation = $('input[name=organisation]').val();
var anrede = $('input[name=anrede]').val();
var anrede_briefkopf = $('input[name=anrede_briefkopf]').val();
var titel = $('input[name=titel]').val();
var vorname = $('input[name=vorname]').val();
var nachname = $('input[name=nachname]').val();
var strasse = $('input[name=strasse]').val();
var adresszusatz = $('input[name=adresszusatz]').val();
var plz = $('input[name=plz]').val();
var ort = $('input[name=ort]').val();
var template = $('input[name=template]').val();
var uid = $('input[name=uid]').val();
var ddata = $('input[name=data]').val();
var html = $('#preview').html();
var noerror = false;
var response = "";
$.ajaxSetup({async:false});
$.ajax({
type: 'POST',
url: '/db/main/dopdf.php?save',
data: {
'subject': subject,
'fn': fn,
'date': date,
'organisation': organisation,
'anrede': anrede,
'anrede_briefkopf': anrede_briefkopf,
'titel': titel,
'vorname': vorname,
'nachname': nachname,
'strasse': strasse,
'adresszusatz': adresszusatz,
'plz': plz,
'ort': ort,
'template': template,
'uid': uid,
'data': ddata,
'html': html
},
success: function (d, s, x) {
if (d == 1) {
noerror = true;
response = x.responseText;
}
}
});
if (noerror && response == '1') {
pivot.__done = true;
delete pivot.__failed;
redoRows($("tr[data-id='" + pivot.uid + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.UID + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.ruid + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.RUID + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.id + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.ID + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.genuid + "']")[0], 0, 0, 0, pivot.index);
var stl = "";
if (organisation) stl += organisation + ' / ';
stl += vorname + ' ' + nachname;
if (!skip) alert('PDF erfolgreich fuer ' + stl + ' erstellt.');
$('#pdf_status').html('PDF erfolgreich: ' + stl);
return true;
}
pivot.__failed = true;
delete pivot.__done;
redoRows($("tr[data-id='" + pivot.uid + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.UID + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.id + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.ID + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.ruid + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.RUID + "']")[0], 0, 0, 0, pivot.index);
redoRows($("tr[data-id='" + pivot.genuid + "']")[0], 0, 0, 0, pivot.index);
var stl = "";
if (organisation) stl += organisation + ' / ';
stl += vorname + ' ' + nachname;
alert('Fehler beim Generieren fuer ' + stl + ': ' + response);
$('#pdf_status').html('Fehler: ' + stl);
return false;
}
function waitASec(time) {
$.ajaxSetup({async:false});
$.post('/db/main/delay.php', {
time: time
});
}
function sendAllDone() {
$('#pdf_modal').hide();
window.scrollTo(0, 0);
alert(unescape('Fertig. Erfolg für ' + count_s + ' Adressaten.\nFehlgeschlagen bei ' + count_f + '.'));
$('#pdf_status').html('Erfolg: ' + count_s + ', Fehler: ' + count_f);
}
function sendAllLoop() {
if (typeof window.emergencyOff !== 'undefined') {
sendAllDone();
delete window.emergencyOff;
return;
}
if (typeof pivot.__done === 'undefined') {
$('#pdf_status').html('Generiere fuer: ' + pivot['Vorname'] + ' ' + pivot['Nachname']);
if (sendOne(true)) count_s++;
else count_f++;
pivot_iter++;
if (pivot_iter < data.length) {
pivot = data[pivot_iter];
bP();
setTimeout(sendAllLoop, 100);
return;
}
setTimeout(sendAllDone, 100);
return;
}
pivot_iter++;
if (pivot_iter < data.length) {
pivot = data[pivot_iter];
bP();
setTimeout(sendAllLoop, 10);
return;
}
setTimeout(sendAllDone, 100);
return;
}
function sendAll() {
if ($('select[name=fn]').val() == "") {
alert(unescape('Bitte Auswahl bei Dokument (Dateiname) treffen!'));
return;
}
if (!confirm(unescape('Wirklich fuer ' + $.grep(data, function (e) { return (typeof e.__done === 'undefined'); }).length + ' Adressaten generieren?')))
return;
count_s = 0;
count_f = 0;
pivot = data[0];
pivot_iter = 0;
bP();
window.scrollTo(0, 0);
$('#pdf_modal').show();
setTimeout(sendAllLoop, 100);
}
function abortAll() {
window.emergencyOff = true;
}
<? minEnd(); ?>
</script>
<?
include_once __DIR__ . "/footer.php";
?>
|