Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add qr code management #1046

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
219 changes: 219 additions & 0 deletions admin/qrcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?php
/* Copyright (C) 2021-2023 EVARISK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024

*
* 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/>.
*/

/**
* \file admin/redirections.php
* \ingroup saturne
* \brief Saturne redirections page
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

est-ce que j'aurais copié mon fichier et fais des erreurs ? ça m'étonnerait

*/

// Load Saturne environment
if (file_exists('../saturne.main.inc.php')) {
require_once __DIR__ . '/../saturne.main.inc.php';
} elseif (file_exists('../../saturne.main.inc.php')) {
require_once __DIR__ . '/../../saturne.main.inc.php';
} else {
die('Include of saturne main fails');
}

// Get module parameters
$moduleName = GETPOST('module_name', 'alpha');
$moduleNameLowerCase = strtolower($moduleName);

// Load Dolibarr libraries
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT . '/includes/tecnickcom/tcpdf/tcpdf_barcodes_2d.php';

// Load Module libraries
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saturne Libraries

require_once __DIR__ . '/../lib/saturne.lib.php';
require_once __DIR__ . '/../class/saturneqrcode.class.php';

// Global variables definitions
global $conf, $db, $hookmanager, $langs, $user;

// Load translation files required by the page
saturne_load_langs(['admin']);

// Initialize view objects
$form = new Form($db);

// Get parameters
$action = GETPOST('action', 'alpha');
$url = GETPOST('url', 'alpha');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psr12


// Initialize Redirection Manager
$saturneQRCode = new SaturneQRCode($db);

// Security check - Protection if external user
$permissiontoread = $user->rights->saturne->adminpage->read;
saturne_check_access($permissiontoread);

/*
* Actions
*/

// Add a redirection
if ($action == 'add') {
if (dol_strlen($url) == 0) {
setEventMessage('URLToEncodeRequired', 'errors');
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
$saturneQRCode->url = $url;
$saturneQRCode->encoded_qr_code = $saturneQRCode->getQRCodeBase64($url);
$saturneQRCode->module_name = 'saturne';
$saturneQRCode->status = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psr12

$saturneQRCode->create($user);

setEventMessage('QRCodeCreated');
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}

if ($action == 'update') {
$saturneQRCode->fetch(GETPOST('id'));
$saturneQRCode->url = GETPOST('url');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tu l"as stocké, tu peux faire pareil pour id

$saturneQRCode->encoded_qr_code = $saturneQRCode->getQRCodeBase64($saturneQRCode->url);
$saturneQRCode->update($user);

setEventMessage('QRCodeUpdated');
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}

// Remove a redirection
if ($action == 'remove') {
$saturneQRCode->fetch(GETPOST('id'));
$saturneQRCode->delete($user, false, false);

setEventMessage('QRCodeRemoved');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

langs->trans

header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}

/*
* View
*/

$title = $langs->trans('ModuleSetup', 'Saturne');
$help_url = 'FR:Module_' . $moduleName;
Comment on lines +112 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psr12 + helpUrl


saturne_header(0, '', $title, $help_url);

print load_fiche_titre($title, '', 'title_setup');

// Configuration header
$preHead = $moduleNameLowerCase . '_admin_prepare_head';
$head = $preHead();
print dol_get_fiche_head($head, 'qrcode', $title, -1, $moduleNameLowerCase . '_color@' . $moduleNameLowerCase);
$QRCodes = $saturneQRCode->fetchAll();

print '<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 -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est totalement illégal de print comme ça du html (+remove commentaire)

</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>';

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>';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

espace en trop

print '<td class="center">' . $langs->trans('ModuleName') . '</td>';
print '<td class="center">' . $langs->trans('Actions') . '</td>';
print '</tr>';

if (is_array($QRCodes) && !empty($QRCodes)) {
foreach ($QRCodes as $QRCode) {
if ($action == 'edit' && $QRCode->id == GETPOST('id')) {
print '<tr class="oddeven" id="qrcode-'. $QRCode->id .'"><td>';
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="update">';
print '<input type="hidden" name="id" value="' . $QRCode->id . '">';
print '<input type="text" class="minwidth500" name="url" value="' . $QRCode->url . '">';
print '</td><td class="center">';
print '</td><td class="center">';
print '</td><td class="center">';
print '<input hidden class="qrcode-base64" value="'. $QRCode->encoded_qr_code .'">';
print '<button type="submit" class="butAction">' . $langs->trans('Save') . '</button>';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wpeo-button dans le doute

print '</td></tr>';
print '</form>';
} else {
print '<tr class="oddeven"><td>';
print $QRCode->url;
print '</td>';
print '<td class="right preview-qr-code">';
print '<input hidden class="qrcode-base64" value="'. $QRCode->encoded_qr_code .'">';
print img_picto($langs->trans("QRCodeGeneration"), 'fontawesome_fa-qrcode_fas_blue');
print ' ' . $form->textwithpicto('', $langs->trans('QRCodeGenerationTooltip'));
print '</td>';
print '</td><td class="center">';
print ucfirst($QRCode->module_name);
print '</td><td class="center">';

// Modify this section to use anchor tags for edit and delete actions
print '<a href="' . $_SERVER['PHP_SELF'] . '?module_name=' . $moduleName . '&action=edit&id=' . $QRCode->id . '#qrcode-'. $QRCode->id .'" class="edit-button">';
print img_picto($langs->trans('Edit'), 'edit');
print '</a> ';
// Form for Remove action using a form with token and a styled submit button
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '?module_name=' . $moduleName . '" style="display:inline;">';
print '<input type="hidden" name="token" value="' . newToken() . '">'; // Token for CSRF protection
print '<input type="hidden" name="action" value="remove">'; // Action to remove the QR code
print '<input type="hidden" name="id" value="' . $QRCode->id . '">'; // ID of the QR code to be removed
print '<button type="submit" class="" title="' . $langs->trans('Remove') . '">';
print '<i class="fas fa-trash-alt"></i>'; // Font Awesome icon for the delete action
print '</button>';
print '</form>';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trop de commentaires et un espace en trop et class="" vide


print '</td></tr>';
}
}
}


print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="add">';

print '<tr class="oddeven"><td>';
print '<input placeholder="'. $langs->trans('URLToEncode') .'" type="text" name="url" value="' . $url . '">';
print "&nbsp;" . $form->textwithpicto($langs->trans('Help'), $langs->trans('HowToUseURLToEncode'));
print '</td><td class="center">';
print '</td><td class="center">';
print '</td><td class="center">';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colspan

print '<input type="submit" class="button" value="' . $langs->trans('Add') . '">';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wpeo-button

print '</td></tr>';

print '</table>';
print '</form>';

print dol_get_fiche_end();
llxFooter();
$db->close();
?>
169 changes: 169 additions & 0 deletions class/saturneqrcode.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/* Copyright (C) 2021-2023 EVARISK <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024

*
* 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/>.
*/

/**
* \file class/saturneqrcode.class.php
* \ingroup saturne
* \brief This file is a CRUD class file for SaturneQRCode (Create/Read/Update/Delete).
*/

// 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
{
/**
* @var DoliDB Database handler
*/
public $db;

/**
* @var string Module name
*/
public $module = 'saturne';

/**
* @var string Element type of object
*/
public $element = 'saturne_qrcode';

/**
* @var string Name of table without prefix where object is stored This is also the key used for extrafields management
*/
public $table_element = 'saturne_qrcode';

/**
* @var int Does this object support multicompany module ?
* 0 = No test on entity, 1 = Test with field entity, 'field@table' = Test with link by field@table
*/
public $ismultientitymanaged = 1;

/**
* @var int Does object support extrafields ? 0 = No, 1 = Yes
*/
public $isextrafieldmanaged = 0;

/**
* @var string Last output from end job execution
*/
public $output = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kèsseussé ?


/**
* @var string Name of icon for certificate Must be a 'fa-xxx' fontawesome code (or 'fa-xxx_fa_color_size') or 'certificate@saturne' if picto is file 'img/object_certificatepng'
*/
public string $picto = 'fontawesome_fa-forward_fas_#d35968';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad picto (+ commentaire au dessus)


/**
* @var array Array with all fields and their property Do not use it as a static var It may be modified by constructor
*/
public $fields = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vérifier les positions (et quand même le reste des fields au cas ou)

'rowid' => ['type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'comment' => 'Id'],
'entity' => ['type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 0, 'index' => 1],
'date_creation' => ['type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 40, 'notnull' => 1, 'visible' => 0],
'tms' => ['type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 50, 'notnull' => 1, 'visible' => 0],
'import_key' => ['type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 60, 'notnull' => 0, 'visible' => 0, 'index' => 0],
'status' => ['type' => 'smallint', 'label' => 'Status', 'enabled' => 1, 'position' => 70, 'notnull' => 1, 'visible' => 2, 'default' => 0, 'index' => 1, 'validate' => 1, 'arrayofkeyval' => [0 => 'StatusDraft', 1 => 'ValidatePendingSignature', 2 => 'Expired', 3 => 'Archived']],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mauvais arrayofkeyval + si tu hard delete pas besoin de status

'module_name' => ['type' => 'varchar(128)', 'label' => 'ModuleName', 'enabled' => 1, 'position' => 90, 'notnull' => 0, 'visible' => 0],
'url' => ['type' => 'text', 'label' => 'Url', 'enabled' => 1, 'position' => 80, 'notnull' => 0, 'visible' => 0, 'index' => 0],
'encoded_qr_code' => ['type' => 'text', 'label' => 'EncodedData', 'enabled' => 1, 'position' => 90, 'notnull' => 0, 'visible' => 0, 'index' => 0],
'fk_user_creat' => ['type' => 'integer:User:user/class/userclassphp', 'label' => 'UserAuthor', 'picto' => 'user', 'enabled' => 1, 'position' => 220, 'notnull' => 1, 'visible' => 0, 'foreignkey' => 'userrowid'],
];

/**
* @var int ID
*/
public int $rowid;

/**
* @var int Entity
*/
public $entity;

/**
* @var int|string Creation date
*/
public $date_creation;

/**
* @var int|string Timestamp
*/
public $tms;

/**
* @var string Import key
*/
public $import_key;

/**
* @var int Status
*/
public $status;

/**
* @var string Module name
*/
public $module_name;

/**
* @var string URL
*/
public $url;

/**
* @var string QR Code encoded
*/
public $encoded_qr_code;

/**
* @var int User creator
*/
public $fk_user_creat;

/**
* Constructor
*
* @param DoliDb $db Database handler
* @param string $moduleNameLowerCase Module name
* @param string $objectType Object element type
*/
public function __construct(DoliDB $db, string $moduleNameLowerCase = 'saturne', string $objectType = 'saturne_qrcode')
{
parent::__construct($db, $moduleNameLowerCase, $objectType);
}

/**
* Get QR Code base64
*
* @param string $url URL to encode
*
* @return string Encoded QR Code
*/
public function getQRCodeBase64(string $url): string
{
// Create QR Code
$barcodeObject = new TCPDF2DBarcode($url, 'QRCODE,H');
$qrCodePng = $barcodeObject->getBarcodePngData(6, 6);
$qrCodeBase64 = 'data:image/png;base64,' . base64_encode($qrCodePng);

return $qrCodeBase64;
}
}

?>
Loading