File indexing completed on 2024-12-29 04:51:03

0001 /*
0002    SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003    SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 function fixAddress(content, node) {
0007     if (node.result.length != 1) {
0008         return;
0009     }
0010     var res = node.result[0];
0011     // location can be a string rather than a place object
0012     if (typeof res.reservationFor.location === "string") {
0013         var place = JsonLd.newObject("Place");
0014         place.name = res.reservationFor.location;
0015         res.reservationFor.location = place;
0016     }
0017 
0018     // streetAddress duplicates city and zip code without proper separation in
0019     // about half their emails...
0020     if (!res.reservationFor.location.address) {
0021         return res;
0022     }
0023     var addr = res.reservationFor.location.address;
0024     for (var i of [0, 1]) {
0025         if (addr.streetAddress.endsWith(addr.addressLocality)) {
0026             addr.streetAddress = addr.streetAddress.substr(0, addr.streetAddress.length - addr.addressLocality.length).trim();
0027         }
0028         if (addr.streetAddress.endsWith(addr.postalCode)) {
0029             addr.streetAddress = addr.streetAddress.substr(0, addr.streetAddress.length - addr.postalCode.length).trim();
0030         }
0031     }
0032     res.reservationFor.location.address = addr;
0033     return res;
0034 }
0035 
0036 function findBarcode(content, node) {
0037     if (node.result.length != 1) {
0038         return;
0039     }
0040     const pdfs = node.findChildNodes({ mimeType: "application/pdf", scope: "Descendants" });
0041     if (pdfs.length == 1) {
0042         const images = pdfs[0].findChildNodes({ mimeType: "internal/qimage", scope: "Descendants" });
0043         var barcode = undefined;
0044         for (const image of images) {
0045             if (!image.childNodes || image.childNodes.length != 1 || image.childNodes[0].mimeType != "text/plain") {
0046                 continue;
0047             }
0048             if (barcode && barcode != image.childNodes[0].content) {
0049                 return;
0050             }
0051             barcode = image.childNodes[0].content;
0052         }
0053         if (barcode) {
0054             var res = node.result[0];
0055             if (res.reservedTicket == undefined)
0056                 res.reservedTicket = JsonLd.newObject("Ticket");
0057             res.reservedTicket.ticketToken = "qrCode:" + barcode;
0058             return res;
0059         }
0060     }
0061 }
0062 
0063 function parsePdf(pdf, node, triggerNode) {
0064     const page = pdf.pages[triggerNode.location];
0065     let res = JsonLd.newEventReservation();
0066     res.reservationNumber = triggerNode.content.substr(0, 10);
0067     res.reservedTicket.ticketToken = 'qrCode:' + triggerNode.content;
0068     res.reservationFor.name = page.textInRect(0.0, 0.12, 1.0, 0.18);
0069     const multiDay = page.textInRect(0.0, 0.27, 1.0, 0.3).match(/(\d\d\.? \S+ \d{4}).*? (\d\d:\d\d).*?(\d\d\.? \S+ \d{4}).*(\d\d:\d\d)/);
0070     const oneDay = page.textInRect(0.0, 0.27, 1.0, 0.3).match(/(\d\d\.? \S+ \d{4}).*? (\d\d:\d\d).*?(\d\d:\d\d)/);
0071     res.reservationFor.startDate = JsonLd.toDateTime(oneDay[1] + ' ' + oneDay[2], ['dd MMMM yyyy hh:mm', 'dd. MMMM yyyy hh:mm'], ['en', 'de', 'fr']);
0072     res.reservationFor.endDate = JsonLd.toDateTime((multiDay ? multiDay[3] : oneDay[1]) + ' ' + oneDay[3], ['dd MMMM yyyy hh:mm', 'dd. MMMM yyyy hh:mm'], ['en', 'de', 'fr']);
0073     const addr = page.textInRect(0.0, 0.2, 1.0, 0.27).split(',');
0074     res.reservationFor.location.name = addr[0];
0075     res.reservationFor.location.address.addressCountry = addr[addr.length - 1];
0076     res.reservationFor.location.address.addressLocality = addr[addr.length - 2];
0077     res.reservationFor.location.address.streetAddress = addr[addr.length - 3];
0078     const name = page.text.match(/  Name\n.*  (.*)/);
0079     if (name) {
0080         res.underName.name = name[1];
0081     }
0082     return res;
0083 }