versao aprimorada 2

This commit is contained in:
Flatlogic Bot 2025-11-20 22:14:33 +00:00
parent 8be2551b13
commit d7824332a6
3 changed files with 60 additions and 5 deletions

View File

@ -10,6 +10,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$voltage = trim($_POST['voltage'] ?? null);
$phase_neutral_ground = trim($_POST['phase_neutral_ground'] ?? null);
$breaker_output = trim($_POST['breaker_output'] ?? null);
$image_path = null;
if (empty($client_name) || empty($address) || empty($technician_name)) {
$_SESSION['message'] = 'Please fill all required fields.';
@ -18,12 +19,44 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
exit;
}
// Handle file upload
if (isset($_FILES['installation_image']) && $_FILES['installation_image']['error'] === UPLOAD_ERR_OK) {
$upload_dir = __DIR__ . '/assets/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0775, true);
}
$file_tmp_path = $_FILES['installation_image']['tmp_name'];
$file_name = $_FILES['installation_image']['name'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
if (in_array($file_ext, $allowed_ext)) {
$new_file_name = time() . '_' . basename($file_name);
$dest_path = $upload_dir . $new_file_name;
if (move_uploaded_file($file_tmp_path, $dest_path)) {
$image_path = 'assets/uploads/' . $new_file_name;
} else {
$_SESSION['message'] = 'Failed to move uploaded file.';
$_SESSION['message_type'] = 'danger';
header('Location: index.php');
exit;
}
} else {
$_SESSION['message'] = 'Invalid file type. Only JPG, JPEG, PNG, and GIF are allowed.';
$_SESSION['message_type'] = 'danger';
header('Location: index.php');
exit;
}
}
try {
$pdo = db();
$stmt = $pdo->prepare(
"INSERT INTO installations (client_name, address, technician_name, observations, voltage, phase_neutral_ground, breaker_output) VALUES (?, ?, ?, ?, ?, ?, ?)"
"INSERT INTO installations (client_name, address, technician_name, observations, voltage, phase_neutral_ground, breaker_output, image_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([$client_name, $address, $technician_name, $observations, $voltage, $phase_neutral_ground, $breaker_output]);
$stmt->execute([$client_name, $address, $technician_name, $observations, $voltage, $phase_neutral_ground, $breaker_output, $image_path]);
$_SESSION['message'] = 'Installation registered successfully!';
$_SESSION['message_type'] = 'success';
@ -35,4 +68,4 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
header('Location: index.php');
exit;
exit;

View File

@ -0,0 +1 @@
ALTER TABLE installations ADD COLUMN image_path VARCHAR(255) NULL DEFAULT NULL;

View File

@ -169,9 +169,13 @@ try {
<table class="table table-hover">
<thead>
<tr>
<th>Image</th>
<th>Client</th>
<th>Address</th>
<th>Technician</th>
<th>Voltage</th>
<th>Phase/Neutral</th>
<th>Breaker</th>
<th>Date</th>
<th>Actions</th>
</tr>
@ -179,14 +183,26 @@ try {
<tbody>
<?php if (empty($installations) && !isset($db_error)): ?>
<tr>
<td colspan="5" class="text-center">No installations found.</td>
<td colspan="9" class="text-center">No installations found.</td>
</tr>
<?php else: ?>
<?php foreach ($installations as $inst): ?>
<tr>
<td>
<?php if (!empty($inst['image_path'])): ?>
<a href="<?php echo htmlspecialchars($inst['image_path']); ?>" target="_blank">
<img src="<?php echo htmlspecialchars($inst['image_path']); ?>" alt="Installation Image" style="width: 50px; height: 50px; object-fit: cover;">
</a>
<?php else: ?>
N/A
<?php endif; ?>
</td>
<td><?php echo htmlspecialchars($inst['client_name']); ?></td>
<td><?php echo htmlspecialchars($inst['address']); ?></td>
<td><?php echo htmlspecialchars($inst['technician_name']); ?></td>
<td><?php echo htmlspecialchars($inst['voltage'] ?? 'N/A'); ?></td>
<td><?php echo htmlspecialchars($inst['phase_neutral_ground'] ?? 'N/A'); ?></td>
<td><?php echo htmlspecialchars($inst['breaker_output'] ?? 'N/A'); ?></td>
<td><?php echo date("d/m/Y", strtotime($inst['created_at'])); ?></td>
<td>
<a href="#" class="btn btn-sm btn-outline-secondary"><i class="bi bi-pencil-square"></i></a>
@ -212,7 +228,7 @@ try {
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="add_installation.php" method="POST">
<form action="add_installation.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="client_name" class="form-label">Client Name</label>
<input type="text" class="form-control" id="client_name" name="client_name" required>
@ -230,6 +246,11 @@ try {
<textarea class="form-control" id="observations" name="observations" rows="3"></textarea>
</div>
<div class="mb-3">
<label for="installation_image" class="form-label">Installation Image</label>
<input class="form-control" type="file" id="installation_image" name="installation_image" accept="image/*">
</div>
<hr>
<h6 class="mb-3">Electrical Measurements</h6>
<div class="row">