Skip to content

Commit

Permalink
#1045 [QRCode] add: qr code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
theodaviddd committed Aug 6, 2024
1 parent b4edbc7 commit f84423d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 4 deletions.
3 changes: 2 additions & 1 deletion admin/qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>' . $langs->trans('URL') . '</td>';
print '<td class="center">' . $langs->trans('QR Code') . '</td>';
print '<td class="text-blank
">' . $langs->trans('QR Code') . '</td>';
print '<td class="center">' . $langs->trans('ModuleName') . '</td>';
print '<td class="center">' . $langs->trans('Action') . '</td>';
print '</tr>';
Expand Down
5 changes: 4 additions & 1 deletion class/saturneqrcode.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
* \brief This file is a CRUD class file for SaturneQRCode (Create/Read/Update/Delete).
*/

// Load Saturne libraries.
// Load Saturne libraries
require_once __DIR__ . '/saturneobject.class.php';

// Load QRCode library
require_once DOL_DOCUMENT_ROOT . '/includes/tecnickcom/tcpdf/tcpdf_barcodes_2d.php';

class SaturneQRCode extends SaturneObject
{
/**
Expand Down
85 changes: 85 additions & 0 deletions js/modules/qrcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* Copyright (C) 2024 EVARISK <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
*
* Library javascript to enable Browser notifications
*/

/**
* \file js/modules/qrcode.js
* \ingroup saturne
* \brief JavaScript qrcode file for module Saturne
*/

/**
* Init qrcode JS
*
* @since 1.2.0
* @version 1.2.0
*/
window.saturne.qrcode = {};

/**
* QR Code init
*
* @since 1.2.0
* @version 1.2.0
*
* @return {void}
*/
window.saturne.qrcode.init = function() {
window.saturne.qrcode.event();
};

/**
* QR Code event
*
* @since 1.2.0
* @version 1.2.0
*
* @return {void}
*/
window.saturne.qrcode.event = function() {
$(document).on('click', '.preview-qr-code', window.saturne.qrcode.previewQRCode);
};


// Fonction pour afficher le QR code dans une modal
window.saturne.qrcode.previewQRCode = function() {
// Obtenir l'image du QR code à partir des données de l'élément
let QRCodeBase64 = $(this).find('.qrcode-base64').val();

// Créer un élément d'image
const img = document.createElement('img');
img.src = QRCodeBase64;
img.alt = 'QR Code';
img.style.maxWidth = '100%';

// Insérer l'image dans le conteneur désigné
const pdfPreview = document.getElementById('pdfPreview');
pdfPreview.innerHTML = ''; // Vider le conteneur d'abord
pdfPreview.appendChild(img);

// Afficher la modal
$('#pdfModal').addClass('modal-active');

// Ajouter un bouton de téléchargement
const downloadBtn = document.getElementById('downloadBtn');
downloadBtn.onclick = function() {
const a = document.createElement('a');
a.href = QRCodeBase64;
a.download = 'QRCode.png';
a.click();
};
};
2 changes: 1 addition & 1 deletion js/saturne.min.js

Large diffs are not rendered by default.

50 changes: 49 additions & 1 deletion lib/documents.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@
*/
function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, string $urlsource, $genallowed, int $delallowed = 0, string $modelselected = '', int $allowgenifempty = 1, int $forcenomultilang = 0, int $notused = 0, int $noform = 0, string $param = '', string $title = '', string $buttonlabel = '', string $codelang = '', string $morepicto = '', $object = null, int $hideifempty = 0, string $removeaction = 'remove_file', int $active = 1, string $tooltiptext = '', string $sortfield = '', string $sortorder = ''): string
{
global $conf, $db, $form, $hookmanager, $langs;
global $conf, $db, $form, $hookmanager, $langs, $user;

if (!is_object($form)) {
$form = new Form($db);
}

require_once __DIR__ . '/../class/saturneqrcode.class.php';
$QRCode = new SaturneQRCode($db);

include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';

// Add entity in $param if not already exists
Expand Down Expand Up @@ -288,6 +291,7 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= $form->textwithpicto($langs->trans('Help'), $htmltooltip, 1, 0);
$out .= '</span>';
}
$out .= '</th><th>';
$out .= '</th>';

if (!empty($hookmanager->hooks['formfile'])) {
Expand All @@ -308,6 +312,7 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= '<td></td>';
$out .= get_document_title_search('date', 'Date', 'right');
$out .= '<td></td>';
$out .= '<td></td>';
$out .= '<td class="right ">';
$out .= '<i class="saturne-search-button fas fa-search" style="cursor: pointer;"></i>';
$out .= '&nbsp;&nbsp;&nbsp;';
Expand All @@ -320,6 +325,7 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= get_document_title_field($sortfield, $sortorder, 'Size', true, 'right');
$out .= get_document_title_field($sortfield, $sortorder, 'Date', true, 'right');
$out .= get_document_title_field($sortfield, $sortorder, 'PDF', false, 'right');
$out .= get_document_title_field($sortfield, $sortorder, 'QRCode', false, 'right');
$out .= get_document_title_field($sortfield, $sortorder, 'Action', false, 'right');
$out .= '</tr>';

Expand Down Expand Up @@ -353,6 +359,28 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= '<table class="noborder centpercent" id="' . $modulepart . '_table">' . "\n";
}

?>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pdf-lib.min.js"></script>

<?php

$out .= '<div id="pdfModal" class="wpeo-modal">
<div class="modal-container">
<div class="modal-header">
<h2>QR Code</h2>
</div>
<div class="modal-content" style="display: flex; justify-content: center">
<div id="pdfPreview">
<!-- Le PDF sera affiché ici dans un iframe -->
</div>
</div>
<div class="modal-footer">
<button id="downloadBtn" style="margin-top: 10px;"><i class="fas fa-download fa-2x"></i></button>
</div>
</div>
</div>';


// Loop on each file found
if (is_array($fileList)) {
foreach ($fileList as $file) {
Expand Down Expand Up @@ -425,6 +453,26 @@ function saturne_show_documents(string $modulepart, $modulesubdir, $filedir, str
$out .= '</td>';
}

//Show QR Code
$documentQRCode = $QRCode->fetchAll('', '', 0, 0, ['url' => $documenturl . '?modulepart=' . $modulepart . '&amp;file=' . urlencode($relativepath) . ($param ? '&' . $param : '')]);
if (is_array($documentQRCode) && !empty($documentQRCode)) {
$documentQRCode = array_shift($documentQRCode);
} else {
$QRCode->url = $documenturl . '?modulepart=' . $modulepart . '&amp;file=' . urlencode($relativepath) . ($param ? '&' . $param : '');
$QRCode->encoded_qr_code = $QRCode->getQRCodeBase64($QRCode->url);
$QRCode->status = 1;
$QRCode->module_name = $modulepart;
$QRCode->create($user);
$documentQRCode = $QRCode;

}

$out .= '<td class="right preview-qr-code">';
$out .= '<input hidden class="qrcode-base64" value="'. $documentQRCode->encoded_qr_code .'">';
$out .= img_picto($langs->trans("QRCodeGeneration"), 'fontawesome_fa-qrcode_fas_blue');
$out .= ' ' . $form->textwithpicto('', $langs->trans('QRCodeGenerationTooltip'));
$out .= '</td>';

if ($delallowed || $morepicto) {
$out .= '<td class="right nowraponall">';
if ($delallowed) {
Expand Down

0 comments on commit f84423d

Please sign in to comment.