summaryrefslogtreecommitdiff
path: root/pdf.php
diff options
context:
space:
mode:
Diffstat (limited to 'pdf.php')
-rw-r--r--pdf.php516
1 files changed, 516 insertions, 0 deletions
diff --git a/pdf.php b/pdf.php
new file mode 100644
index 0000000..7c912ea
--- /dev/null
+++ b/pdf.php
@@ -0,0 +1,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&uuml;r <?=$num?> Datens&auml;tze</span>
+<? } else { ?>
+<span style="font-size: .5em;">f&uuml;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("\"", "&quot;", $name)?>"
+ data-subject="<?=str_replace("\"", "&quot;", $subject)?>"
+ data-date="<?=str_replace("\"", "&quot;", $date)?>"
+ data-text="<?=str_replace("\"", "&quot;", $text)?>"
+ data-fn="<?=str_replace("\"", "&quot;", $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> &Uuml;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&ouml;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(/&quot;/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";
+?>