supplierlink3/admin/setup.php

493 lines
19 KiB
PHP
Executable file

<?php
/* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2025 Eduard Wisch <data@data-it-solution.de>
*
* 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 supplierlink3/admin/setup.php
* \ingroup supplierlink3
* \brief SupplierLink3 setup page.
*/
// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
$i--;
$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
$res = @include "../../main.inc.php";
}
if (!$res && file_exists("../../../main.inc.php")) {
$res = @include "../../../main.inc.php";
}
if (!$res) {
die("Include of main fails");
}
// Libraries
require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
require_once '../lib/supplierlink3.lib.php';
// Translations
$langs->loadLangs(array("admin", "supplierlink3@supplierlink3"));
// Access control
if (!$user->admin) {
accessforbidden();
}
// Parameters
$action = GETPOST('action', 'aZ09');
/*
* Actions
*/
// Toggle display settings
if ($action == 'set_enable_ordercard') {
$value = GETPOST('value', 'int');
dolibarr_set_const($db, 'SUPPLIERLINK3_ENABLE_ORDERCARD', $value, 'chaine', 0, '', $conf->entity);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
if ($action == 'set_enable_propalcard') {
$value = GETPOST('value', 'int');
dolibarr_set_const($db, 'SUPPLIERLINK3_ENABLE_PROPALCARD', $value, 'chaine', 0, '', $conf->entity);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
if ($action == 'set_enable_supplierorder') {
$value = GETPOST('value', 'int');
dolibarr_set_const($db, 'SUPPLIERLINK3_ENABLE_SUPPLIERORDER', $value, 'chaine', 0, '', $conf->entity);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
if ($action == 'set_enable_replenish') {
$value = GETPOST('value', 'int');
dolibarr_set_const($db, 'SUPPLIERLINK3_ENABLE_REPLENISH', $value, 'chaine', 0, '', $conf->entity);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
if ($action == 'set_enable_productcard') {
$value = GETPOST('value', 'int');
dolibarr_set_const($db, 'SUPPLIERLINK3_ENABLE_PRODUCTCARD', $value, 'chaine', 0, '', $conf->entity);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
// Save icon settings
if ($action == 'save_icons') {
$shopIcon = GETPOST('shop_icon', 'alpha');
$stockIcon = GETPOST('stock_icon', 'alpha');
// Validate FontAwesome class format
if (!empty($shopIcon) && preg_match('/^fa[srb]?\s+fa-[\w-]+$/', $shopIcon)) {
dolibarr_set_const($db, 'SUPPLIERLINK3_SHOP_ICON', $shopIcon, 'chaine', 0, '', $conf->entity);
} elseif (empty($shopIcon)) {
dolibarr_del_const($db, 'SUPPLIERLINK3_SHOP_ICON', $conf->entity);
}
if (!empty($stockIcon) && preg_match('/^fa[srb]?\s+fa-[\w-]+$/', $stockIcon)) {
dolibarr_set_const($db, 'SUPPLIERLINK3_STOCK_ICON', $stockIcon, 'chaine', 0, '', $conf->entity);
} elseif (empty($stockIcon)) {
dolibarr_del_const($db, 'SUPPLIERLINK3_STOCK_ICON', $conf->entity);
}
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
// Debug mode
if ($action == 'setdebug') {
$value = GETPOST('value', 'int');
dolibarr_set_const($db, 'SUPPLIERLINK3_DEBUG_MODE', $value, 'chaine', 0, '', $conf->entity);
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
if ($action == 'cleardebuglog') {
$logFile = '/tmp/supplierlink3_debug.log';
if (file_exists($logFile)) {
unlink($logFile);
setEventMessages($langs->trans('SL3_DebugLogCleared'), null, 'mesgs');
}
header("Location: ".$_SERVER["PHP_SELF"]);
exit;
}
/*
* View
*/
$form = new Form($db);
$title = $langs->trans("SupplierLink3Setup");
llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-supplierlink3 page-admin');
// Subheader
$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($title, $linkback, 'title_setup');
// Configuration header
$head = supplierlink3AdminPrepareHead();
print dol_get_fiche_head($head, 'settings', $title, -1, "supplierlink3@supplierlink3");
print '<span class="opacitymedium">'.$langs->trans('SupplierLink3Description').'</span><br><br>';
// =============================================================================
// DISPLAY SETTINGS
// =============================================================================
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th colspan="3">'.$langs->trans('SL3_DisplaySettings').'</th>';
print '</tr>';
print '<tr class="oddeven">';
print '<td colspan="3" class="opacitymedium">'.$langs->trans('SL3_DisplaySettingsDesc').'</td>';
print '</tr>';
// Customer Orders (ordercard)
$enableOrderCard = getDolGlobalInt('SUPPLIERLINK3_ENABLE_ORDERCARD', 1);
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_EnableOrderCard').'</td>';
print '<td class="center" width="80">';
if ($enableOrderCard) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_ordercard&token='.newToken().'&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a>';
} else {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_ordercard&token='.newToken().'&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_EnableOrderCardDesc').'</td>';
print '</tr>';
// Proposals (propalcard)
$enablePropalCard = getDolGlobalInt('SUPPLIERLINK3_ENABLE_PROPALCARD', 1);
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_EnablePropalCard').'</td>';
print '<td class="center">';
if ($enablePropalCard) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_propalcard&token='.newToken().'&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a>';
} else {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_propalcard&token='.newToken().'&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_EnablePropalCardDesc').'</td>';
print '</tr>';
// Supplier Orders (ordersuppliercard)
$enableSupplierOrder = getDolGlobalInt('SUPPLIERLINK3_ENABLE_SUPPLIERORDER', 1);
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_EnableSupplierOrder').'</td>';
print '<td class="center">';
if ($enableSupplierOrder) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_supplierorder&token='.newToken().'&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a>';
} else {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_supplierorder&token='.newToken().'&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_EnableSupplierOrderDesc').'</td>';
print '</tr>';
// Stock Replenishment (stockreplenishlist)
$enableReplenish = getDolGlobalInt('SUPPLIERLINK3_ENABLE_REPLENISH', 1);
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_EnableReplenish').'</td>';
print '<td class="center">';
if ($enableReplenish) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_replenish&token='.newToken().'&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a>';
} else {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_replenish&token='.newToken().'&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_EnableReplenishDesc').'</td>';
print '</tr>';
// Product Card (productcard)
$enableProductCard = getDolGlobalInt('SUPPLIERLINK3_ENABLE_PRODUCTCARD', 1);
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_EnableProductCard').'</td>';
print '<td class="center">';
if ($enableProductCard) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_productcard&token='.newToken().'&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a>';
} else {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=set_enable_productcard&token='.newToken().'&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_EnableProductCardDesc').'</td>';
print '</tr>';
print '</table>';
print '<br>';
// =============================================================================
// ICON SETTINGS
// =============================================================================
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="save_icons">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th colspan="3">'.$langs->trans('SL3_IconSettings').'</th>';
print '</tr>';
print '<tr class="oddeven">';
print '<td colspan="3" class="opacitymedium">'.$langs->trans('SL3_IconSettingsDesc').'</td>';
print '</tr>';
// Shop Icon
$shopIcon = getDolGlobalString('SUPPLIERLINK3_SHOP_ICON', 'fas fa-store');
print '<tr class="oddeven">';
print '<td width="30%">'.$langs->trans('SL3_ShopIcon').'</td>';
print '<td>';
print '<input type="text" name="shop_icon" value="'.dol_escape_htmltag($shopIcon).'" class="flat minwidth200" placeholder="fas fa-store">';
print ' <span class="sl3-icon-preview" style="font-size: 16px; margin-left: 10px;"><i class="'.$shopIcon.'" style="color: #0077b6;"></i></span>';
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_ShopIconDesc').'</td>';
print '</tr>';
// Stock Icon (optional, for future use)
$stockIcon = getDolGlobalString('SUPPLIERLINK3_STOCK_ICON', '');
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_StockIcon').'</td>';
print '<td>';
print '<input type="text" name="stock_icon" value="'.dol_escape_htmltag($stockIcon).'" class="flat minwidth200" placeholder="fas fa-box">';
if (!empty($stockIcon)) {
print ' <span class="sl3-icon-preview" style="font-size: 16px; margin-left: 10px;"><i class="'.$stockIcon.'"></i></span>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_StockIconDesc').'</td>';
print '</tr>';
// Icon preview row
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_IconPreview').'</td>';
print '<td colspan="2">';
print '<div style="display: flex; gap: 20px; align-items: center; padding: 10px; background: #f9f9f9; border-radius: 4px;">';
// Preview: Shop link with stock badge
print '<div style="display: inline-flex; align-items: center;">';
print '<span style="display: inline-block; width: 28px; text-align: center;">';
print '<a href="#" style="color: #0077b6; font-size: 14px;"><i class="'.$shopIcon.'"></i></a>';
print '</span>';
print '<span style="display: inline-block; min-width: 55px; text-align: right;">';
print '<span class="badge badge-success" style="background-color: #28a745; color: #fff;">42</span>';
print '</span>';
print '</div>';
print '<div style="display: inline-flex; align-items: center;">';
print '<span style="display: inline-block; width: 28px; text-align: center;">';
print '<a href="#" style="color: #0077b6; font-size: 14px;"><i class="'.$shopIcon.'"></i><i class="fas fa-caret-down" style="font-size:8px;margin-left:2px;"></i></a>';
print '</span>';
print '<span style="display: inline-block; min-width: 55px; text-align: right;">';
print '<span class="badge badge-warning" style="background-color: #fd7e14; color: #fff;">8</span>';
print '</span>';
print '</div>';
print '<div style="display: inline-flex; align-items: center;">';
print '<span style="display: inline-block; width: 28px; text-align: center;"></span>';
print '<span style="display: inline-block; min-width: 55px; text-align: right;">';
print '<span class="badge badge-danger" style="background-color: #dc3545; color: #fff;">0</span>';
print '</span>';
print '</div>';
print '</div>';
print '</td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td colspan="3" class="center">';
print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
print '</td>';
print '</tr>';
print '</table>';
print '</form>';
print '<br>';
// =============================================================================
// DEBUG SETTINGS
// =============================================================================
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th colspan="3">'.$langs->trans('SL3_DebugSettings').'</th>';
print '</tr>';
// Debug-Modus (default: OFF)
$debugMode = getDolGlobalInt('SUPPLIERLINK3_DEBUG_MODE', 0);
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_DebugMode').'</td>';
print '<td class="center" width="80">';
if ($debugMode) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdebug&token='.newToken().'&value=0">';
print img_picto($langs->trans("Activated"), 'switch_on');
print '</a>';
} else {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdebug&token='.newToken().'&value=1">';
print img_picto($langs->trans("Disabled"), 'switch_off');
print '</a>';
}
print '</td>';
print '<td class="opacitymedium">'.$langs->trans('SL3_DebugModeDesc').'</td>';
print '</tr>';
// Debug-Log anzeigen
$logFile = '/tmp/supplierlink3_debug.log';
print '<tr class="oddeven">';
print '<td>'.$langs->trans('SL3_DebugLog').'</td>';
print '<td colspan="2">';
if (file_exists($logFile)) {
$logContent = file_get_contents($logFile);
$logSize = filesize($logFile);
print '<strong>'.$langs->trans('SL3_DebugLogFile').':</strong> '.$logFile.' ('.$langs->trans('SL3_DebugLogSize').': '.round($logSize/1024, 2).' KB)<br>';
print '<a class="button" href="'.$_SERVER["PHP_SELF"].'?action=cleardebuglog&token='.newToken().'">'.$langs->trans('SL3_DebugLogClear').'</a>';
print '<br><br>';
print '<strong>'.$langs->trans('SL3_DebugLogLastEntries').':</strong><br>';
print '<textarea readonly style="width:100%; height:200px; font-family:monospace; font-size:11px;">';
// Zeige letzte 100 Zeilen
$lines = explode("\n", $logContent);
$lastLines = array_slice($lines, -100);
print htmlspecialchars(implode("\n", $lastLines));
print '</textarea>';
} else {
print '<span class="opacitymedium">'.$langs->trans('SL3_DebugLogEmpty').'</span>';
}
print '</td>';
print '</tr>';
print '</table>';
print '<br>';
// =============================================================================
// REGISTERED HOOKS
// =============================================================================
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th colspan="3">'.$langs->trans('SL3_RegisteredHooks').'</th>';
print '</tr>';
$hooks = array(
'ordersuppliercard' => $langs->trans('SL3_HookOrderSupplier'),
'ordercard' => $langs->trans('SL3_HookOrderCard'),
'propalcard' => $langs->trans('SL3_HookPropalCard'),
'productcard' => $langs->trans('SL3_HookProductCard'),
'productpricecard' => $langs->trans('SL3_HookProductPrice'),
'stockreplenishlist' => $langs->trans('SL3_HookReplenish'),
);
// Check which hooks are enabled
$enabledHooks = array(
'ordersuppliercard' => getDolGlobalInt('SUPPLIERLINK3_ENABLE_SUPPLIERORDER', 1),
'ordercard' => getDolGlobalInt('SUPPLIERLINK3_ENABLE_ORDERCARD', 1),
'propalcard' => getDolGlobalInt('SUPPLIERLINK3_ENABLE_PROPALCARD', 1),
'productcard' => getDolGlobalInt('SUPPLIERLINK3_ENABLE_PRODUCTCARD', 1),
'productpricecard' => getDolGlobalInt('SUPPLIERLINK3_ENABLE_PRODUCTCARD', 1),
'stockreplenishlist' => getDolGlobalInt('SUPPLIERLINK3_ENABLE_REPLENISH', 1),
);
foreach ($hooks as $hook => $desc) {
$isEnabled = $enabledHooks[$hook] ?? 1;
print '<tr class="oddeven">';
print '<td width="200"><code>'.$hook.'</code></td>';
print '<td>'.$desc.'</td>';
print '<td class="center" width="60">';
if ($isEnabled) {
print '<span class="badge badge-status4">'.$langs->trans("Activated").'</span>';
} else {
print '<span class="badge badge-status8">'.$langs->trans("Disabled").'</span>';
}
print '</td>';
print '</tr>';
}
print '</table>';
// Page end
print dol_get_fiche_end();
// JavaScript for live icon preview
print '<script>
$(document).ready(function() {
$("input[name=\'shop_icon\']").on("input", function() {
var iconClass = $(this).val();
$(this).siblings(".sl3-icon-preview").find("i").attr("class", iconClass);
});
$("input[name=\'stock_icon\']").on("input", function() {
var iconClass = $(this).val();
var $preview = $(this).siblings(".sl3-icon-preview");
if ($preview.length === 0) {
$(this).after(\' <span class="sl3-icon-preview" style="font-size: 16px; margin-left: 10px;"><i class="\' + iconClass + \'"></i></span>\');
} else {
$preview.find("i").attr("class", iconClass);
}
});
});
</script>';
llxFooter();
$db->close();