yogiboook_new/public/userarea/get_product_classes.php

100 lines
2.9 KiB
PHP

<?php
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Verifica che iduserlogin sia definito
if (!isset($iduserlogin)) {
http_response_code(400);
echo json_encode(['error' => 'ID utente non definito']);
exit;
}
// Recupera i dati della scuola in base all'utente loggato
$stmt = $pdo->prepare("
SELECT id, name, website, email, phone, description, address_street, address_city, address_postal_code, address_province, address_country, logo, status
FROM schools
WHERE owner_id = ?
");
$stmt->execute([$iduserlogin]);
$school = $stmt->fetch();
if (!$school) {
http_response_code(404);
echo json_encode(['error' => 'Nessuna scuola trovata per l\'utente loggato']);
exit;
}
$school_id = $school['id'];
$product_id = $_GET['product_id'] ?? 0;
$variation_id = $_GET['variation_id'] ?? 0;
if ($product_id <= 0) {
http_response_code(400);
echo json_encode(['error' => 'ID prodotto non valido']);
exit;
}
// Recupera i dettagli del prodotto (inclusi is_full_access e auto_propagate_to_order)
$stmt = $pdo->prepare("
SELECT id, is_full_access, auto_propagate_to_order
FROM products
WHERE id = ? AND school_id = ?
");
$stmt->execute([$product_id, $school_id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$product) {
http_response_code(404);
echo json_encode(['error' => 'Prodotto non trovato o non autorizzato']);
exit;
}
// Inizializza l'array di risposta con i dettagli del prodotto
$response = [
'is_full_access' => $product['is_full_access'],
'auto_propagate_to_order' => $product['auto_propagate_to_order'],
'class_types' => []
];
// Se variation_id è specificato, recupera i dettagli della variazione
if ($variation_id > 0) {
$stmt = $pdo->prepare("
SELECT id, auto_propagate_to_order
FROM product_variations
WHERE id = ? AND product_id = ?
");
$stmt->execute([$variation_id, $product_id]);
$variation = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$variation) {
http_response_code(404);
echo json_encode(['error' => 'Variazione non trovata o non autorizzata']);
exit;
}
// Sovrascrivi auto_propagate_to_order con il valore della variazione
$response['auto_propagate_to_order'] = $variation['auto_propagate_to_order'];
}
// Recupera le classi associate
if ($variation_id > 0) {
$stmt = $pdo->prepare("
SELECT class_type_id
FROM product_class_types
WHERE product_id = ? AND variation_id = ?
");
$stmt->execute([$product_id, $variation_id]);
} else {
$stmt = $pdo->prepare("
SELECT class_type_id
FROM product_class_types
WHERE product_id = ? AND variation_id IS NULL
");
$stmt->execute([$product_id]);
}
$class_types = $stmt->fetchAll(PDO::FETCH_COLUMN);
$response['class_types'] = $class_types;
header('Content-Type: application/json');
echo json_encode($response);
exit;