getConnection();
/**
* ws_lookup_options manager page
* Table expected:
* - id (AI)
* - category (varchar)
* - value (varchar)
* - label (varchar)
* - sort_order (int)
* - is_default (tinyint 0/1)
* - is_active (tinyint 0/1)
*/
// AJAX HANDLERS
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] == '1') {
header('Content-Type: application/json; charset=utf-8');
$action = $_POST['action'] ?? '';
try {
if ($action === 'add') {
$category = trim($_POST['category'] ?? '');
$value = trim($_POST['value'] ?? '');
$label = trim($_POST['label'] ?? '');
$sort_order = (int)($_POST['sort_order'] ?? 100);
$is_default = (int)($_POST['is_default'] ?? 0) ? 1 : 0;
$is_active = (int)($_POST['is_active'] ?? 1) ? 1 : 0;
if ($category === '' || $value === '' || $label === '') {
echo json_encode(['success' => false, 'message' => 'Compila category, value e label']);
exit;
}
$pdo->beginTransaction();
// If set as default, unset other defaults in same category (simple & reliable)
if ($is_default === 1) {
$stmt = $pdo->prepare("UPDATE ws_lookup_options SET is_default = 0 WHERE category = ?");
$stmt->execute([$category]);
}
$stmt = $pdo->prepare("
INSERT INTO ws_lookup_options
(category, value, label, sort_order, is_default, is_active)
VALUES (?, ?, ?, ?, ?, ?)
");
$stmt->execute([$category, $value, $label, $sort_order, $is_default, $is_active]);
$pdo->commit();
echo json_encode(['success' => true]);
exit;
}
if ($action === 'edit') {
$id = (int)($_POST['id'] ?? 0);
$category = trim($_POST['category'] ?? '');
$value = trim($_POST['value'] ?? '');
$label = trim($_POST['label'] ?? '');
$sort_order = (int)($_POST['sort_order'] ?? 100);
$is_default = (int)($_POST['is_default'] ?? 0) ? 1 : 0;
$is_active = (int)($_POST['is_active'] ?? 1) ? 1 : 0;
if ($id <= 0 || $category === '' || $value === '' || $label === '') {
echo json_encode(['success' => false, 'message' => 'Dati non validi']);
exit;
}
$pdo->beginTransaction();
// If set as default, unset other defaults in same category (excluding current)
if ($is_default === 1) {
$stmt = $pdo->prepare("UPDATE ws_lookup_options SET is_default = 0 WHERE category = ? AND id <> ?");
$stmt->execute([$category, $id]);
}
$stmt = $pdo->prepare("
UPDATE ws_lookup_options SET
category = ?,
value = ?,
label = ?,
sort_order = ?,
is_default = ?,
is_active = ?
WHERE id = ?
");
$stmt->execute([$category, $value, $label, $sort_order, $is_default, $is_active, $id]);
$pdo->commit();
echo json_encode(['success' => true]);
exit;
}
if ($action === 'delete') {
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'ID non valido']);
exit;
}
$stmt = $pdo->prepare("DELETE FROM ws_lookup_options WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['success' => true]);
exit;
}
echo json_encode(['success' => false, 'message' => 'Azione sconosciuta']);
exit;
} catch (Exception $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
exit;
}
}
// Categories list (static for now, easy to extend)
$categories = [
'marking' => 'Marchiatura',
'lubrication_type' => 'Lubrificazione',
'control_frequency_cut' => 'Frequenza controllo (taglio)',
'control_frequency_drawing' => 'Frequenza controllo disegno',
'control_frequency_jig' => 'Frequenza controllo in dima',
'control_mode_jig' => 'Modalità controllo in dima',
'box_type' => 'Tipo scatola',
'pallet_type' => 'Tipo bancale',
'requested_package_code' => 'Confezione richiesta',
];
// Selected category filter
$categoryFilter = trim($_GET['cat'] ?? '');
if ($categoryFilter !== '' && !array_key_exists($categoryFilter, $categories)) {
$categoryFilter = '';
}
// Load options
$params = [];
$sql = "
SELECT id, category, value, label, sort_order, is_default, is_active, created_at, updated_at
FROM ws_lookup_options
";
if ($categoryFilter !== '') {
$sql .= " WHERE category = ? ";
$params[] = $categoryFilter;
}
$sql .= " ORDER BY category ASC, sort_order ASC, label ASC, id ASC ";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$options = $stmt->fetchAll(PDO::FETCH_ASSOC);
function h($v)
{
return htmlspecialchars((string)$v, ENT_QUOTES);
}
?>
Lookup Worksheet
Categoria
| ID |
Category |
Value |
Label |
Ordine |
Default |
Attivo |
Azioni |
| = (int)$o['id'] ?> |
= h($o['category']) ?>
= h($categories[$o['category']] ?? '-') ?>
|
= h($o['value']) ?> |
= h($o['label']) ?> |
= (int)$o['sort_order'] ?> |
= (int)$o['is_default'] === 1 ? '✅' : '-' ?> |
= (int)$o['is_active'] === 1 ? '✅' : '⛔' ?> |
|
Suggerimento: imposta Default solo per 1 valore per categoria (questa pagina lo gestisce automaticamente).