associate
This commit is contained in:
@@ -33,17 +33,29 @@ if (isset($_POST['formclass']) && $_POST['formclass'] === 'Y') {
|
|||||||
$idassociateservice = (int) ($_POST['servicelist'] ?? 0);
|
$idassociateservice = (int) ($_POST['servicelist'] ?? 0);
|
||||||
|
|
||||||
if ($idmainservice > 0 && $idassociateservice > 0) {
|
if ($idmainservice > 0 && $idassociateservice > 0) {
|
||||||
$stmt = $pdo->prepare(
|
// Evita duplicati: inserisci solo se l'associazione non esiste già
|
||||||
"INSERT INTO associateclass (idmainservice, idassociateservice)
|
$stmtCheck = $pdo->prepare(
|
||||||
VALUES (:main, :assoc)"
|
"SELECT COUNT(*) FROM associateclass
|
||||||
|
WHERE idmainservice = :main AND idassociateservice = :assoc"
|
||||||
);
|
);
|
||||||
$ok = $stmt->execute([
|
$stmtCheck->execute([':main' => $idmainservice, ':assoc' => $idassociateservice]);
|
||||||
':main' => $idmainservice,
|
$alreadyExists = (int) $stmtCheck->fetchColumn() > 0;
|
||||||
':assoc' => $idassociateservice,
|
|
||||||
]);
|
if ($alreadyExists) {
|
||||||
$insertFeedback = $ok
|
$insertFeedback = ['type' => 'error', 'text' => 'Questa classe è già associata.'];
|
||||||
? ['type' => 'success', 'text' => 'Associazione aggiunta con successo.']
|
} else {
|
||||||
: ['type' => 'error', 'text' => 'Errore durante l\'associazione. Riprova.'];
|
$stmt = $pdo->prepare(
|
||||||
|
"INSERT INTO associateclass (idmainservice, idassociateservice)
|
||||||
|
VALUES (:main, :assoc)"
|
||||||
|
);
|
||||||
|
$ok = $stmt->execute([
|
||||||
|
':main' => $idmainservice,
|
||||||
|
':assoc' => $idassociateservice,
|
||||||
|
]);
|
||||||
|
$insertFeedback = $ok
|
||||||
|
? ['type' => 'success', 'text' => 'Associazione aggiunta con successo.']
|
||||||
|
: ['type' => 'error', 'text' => 'Errore durante l\'associazione. Riprova.'];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$insertFeedback = ['type' => 'error', 'text' => 'Seleziona una classe da associare.'];
|
$insertFeedback = ['type' => 'error', 'text' => 'Seleziona una classe da associare.'];
|
||||||
}
|
}
|
||||||
@@ -66,12 +78,21 @@ if ($idmain > 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------
|
/* -------------------------------------------------------------------------
|
||||||
* Elenco di tutte le classi (per il dropdown), escludendo la principale
|
* Elenco delle classi per il dropdown:
|
||||||
|
* esclude la principale E quelle già associate a questa principale.
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
$stmtAll = $pdo->prepare(
|
$stmtAll = $pdo->prepare(
|
||||||
"SELECT idservice, servicename FROM service WHERE idservice != :idmain ORDER BY servicename"
|
"SELECT idservice, servicename
|
||||||
|
FROM service
|
||||||
|
WHERE idservice != :idmain
|
||||||
|
AND idservice NOT IN (
|
||||||
|
SELECT idassociateservice
|
||||||
|
FROM associateclass
|
||||||
|
WHERE idmainservice = :idmain2
|
||||||
|
)
|
||||||
|
ORDER BY servicename"
|
||||||
);
|
);
|
||||||
$stmtAll->execute([':idmain' => $idmain]);
|
$stmtAll->execute([':idmain' => $idmain, ':idmain2' => $idmain]);
|
||||||
$allServices = $stmtAll->fetchAll();
|
$allServices = $stmtAll->fetchAll();
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------
|
/* -------------------------------------------------------------------------
|
||||||
@@ -251,6 +272,9 @@ $associatedClasses = $stmtAssoc->fetchAll();
|
|||||||
</option>
|
</option>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</select>
|
</select>
|
||||||
|
<?php if (empty($allServices)) : ?>
|
||||||
|
<small class="text-muted">Tutte le classi disponibili sono già state associate.</small>
|
||||||
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user