ppeasy/public/searchengine/ajaxsearch.php

115 lines
6.2 KiB
PHP

<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
//Including Database configuration file.
include "../db.php";
$iduserlog = $_SESSION["iduserlogin"];
$idcompany = $_SESSION["compid"];
//Getting value of "search" variable from "script.js".
if (isset($_POST['articlepartvalue'])) {
// Recupera e decodifica il valore inviato
$Name = urldecode($_POST['articlepartvalue']);
// Verifica se l'utente vuole mostrare le parti nascoste
$showHidden = isset($_POST['showHidden']) && $_POST['showHidden'] === 'true' ? true : false;
// Prepara la query con prepared statement
$Query = "SELECT
MIN(identificationparts.ididentificationparts) AS id,
identificationparts.article_identificationparts,
identificationparts.description_identificationparts,
identificationparts.material_identificationparts,
MAX(identificationparts.color_identificationparts) AS color_identificationparts,
MAX(identificationparts.component_identificationparts) AS component_identificationparts,
MAX(identificationparts.cmcreportnumber_identificationparts) AS cmcreportnumber_identificationparts,
MAX(identificationparts.cmcreportdate_identificationparts) AS cmcreportdate_identificationparts,
MAX(identificationparts.thirdlabreportnumber_identificationparts) AS thirdlabreportnumber_identificationparts,
MAX(identificationparts.thirdlabreportdate_identificationparts) AS thirdlabreportdate_identificationparts,
MAX(identificationparts.reportof) AS reportof,
MAX(identificationparts.kindoftest) AS kindoftest,
MAX(identificationparts.partsidnumber) AS partsidnumber,
MAX(identificationparts.arttypeid) AS arttypeid,
MAX(identificationparts.hide) AS hide,
MAX(`trf-details`.idtrfdetails) AS idtrfdetails,
`trf-details`.idcompany
FROM
identificationparts
LEFT JOIN
`trf-details`
ON
`trf-details`.idtrfdetails = identificationparts.idtrfdetails
WHERE
identificationparts.article_identificationparts LIKE ?
AND `trf-details`.idcompany = ?
" . ($showHidden ? "" : "AND identificationparts.hide = 'N'") . "
GROUP BY
identificationparts.article_identificationparts,
identificationparts.description_identificationparts,
identificationparts.material_identificationparts
LIMIT 30";
// Prepara lo statement
$stmt = $conn->prepare($Query);
if ($stmt === false) {
echo "Errore nella preparazione della query: " . $conn->error;
exit;
}
// Aggiungi wildcard per LIKE e parametri
$search = "%$Name%";
$stmt->bind_param("si", $search, $idcompany);
// Esegui la query
$stmt->execute();
$result = $stmt->get_result();
// Inizia la lista HTML
echo '<ul>';
// Colori alternati
$bgcolor = "#f2f2f2";
// Estrai i risultati
while ($Result = $result->fetch_assoc()) {
// Alterna il colore di sfondo
$bgcolor = ($bgcolor == "#f2f2f2") ? "#e6f7ff" : "#f2f2f2";
?>
<li style="cursor: pointer; background-color: <?php echo $bgcolor; ?>; border: 1px solid #ccc; border-radius: 5px; padding: 10px; margin-bottom: 5px; position: relative;">
<span onclick='fill(
<?php echo json_encode($Result['article_identificationparts'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['material_identificationparts'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['color_identificationparts'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['description_identificationparts'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['partsidnumber'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['arttypeid'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['cmcreportnumber_identificationparts'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['cmcreportdate_identificationparts'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['reportof'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>,
<?php echo json_encode($Result['kindoftest'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>
)' style="text-decoration: none; color: #333; display: block;">
<?php echo htmlspecialchars($Result['article_identificationparts'], ENT_QUOTES, 'UTF-8'); ?> -
<?php echo htmlspecialchars($Result['material_identificationparts'], ENT_QUOTES, 'UTF-8'); ?> -
<?php echo htmlspecialchars($Result['color_identificationparts'], ENT_QUOTES, 'UTF-8'); ?> -
[<?php echo htmlspecialchars($Result['description_identificationparts'], ENT_QUOTES, 'UTF-8'); ?>]
<?php if (!empty($Result['cmcreportnumber_identificationparts'])) { ?>
- Rep. <?php echo htmlspecialchars($Result['cmcreportnumber_identificationparts'], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
<?php if (!empty($Result['cmcreportdate_identificationparts'])) { ?>
- Date: <?php echo htmlspecialchars($Result['cmcreportdate_identificationparts'], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
</span>
<button id="toggle-btn-<?php echo $Result['id']; ?>" onclick="toggleHide(<?php echo $Result['id']; ?>, '<?php echo $Result['hide'] === 'N' ? 'Y' : 'N'; ?>', this)"
style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); padding: 2px 5px; border: none; background: none; cursor: pointer;">
<i class="fas <?php echo $Result['hide'] === 'N' ? 'fa-eye' : 'fa-eye-slash'; ?>" style="color: <?php echo $Result['hide'] === 'N' ? 'green' : 'red'; ?>;"></i>
</button>
</li>
<?php
}
echo '</ul>';
// Chiudi lo statement e la connessione
$stmt->close();
}
?>