versao 3 aprimorada
This commit is contained in:
parent
d7824332a6
commit
0e6b109372
@ -10,7 +10,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$voltage = trim($_POST['voltage'] ?? null);
|
$voltage = trim($_POST['voltage'] ?? null);
|
||||||
$phase_neutral_ground = trim($_POST['phase_neutral_ground'] ?? null);
|
$phase_neutral_ground = trim($_POST['phase_neutral_ground'] ?? null);
|
||||||
$breaker_output = trim($_POST['breaker_output'] ?? null);
|
$breaker_output = trim($_POST['breaker_output'] ?? null);
|
||||||
$image_path = null;
|
|
||||||
|
|
||||||
if (empty($client_name) || empty($address) || empty($technician_name)) {
|
if (empty($client_name) || empty($address) || empty($technician_name)) {
|
||||||
$_SESSION['message'] = 'Please fill all required fields.';
|
$_SESSION['message'] = 'Please fill all required fields.';
|
||||||
@ -19,53 +18,61 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle file upload
|
$pdo = db();
|
||||||
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 {
|
try {
|
||||||
$pdo = db();
|
$pdo->beginTransaction();
|
||||||
$stmt = $pdo->prepare(
|
|
||||||
"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, $image_path]);
|
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare(
|
||||||
|
"INSERT INTO installations (client_name, address, technician_name, observations, voltage, phase_neutral_ground, breaker_output) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||||
|
);
|
||||||
|
$stmt->execute([$client_name, $address, $technician_name, $observations, $voltage, $phase_neutral_ground, $breaker_output]);
|
||||||
|
$installation_id = $pdo->lastInsertId();
|
||||||
|
|
||||||
|
// Handle multiple file uploads
|
||||||
|
if (isset($_FILES['images'])) {
|
||||||
|
$upload_dir = __DIR__ . '/assets/uploads/';
|
||||||
|
if (!is_dir($upload_dir)) {
|
||||||
|
mkdir($upload_dir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
|
||||||
|
|
||||||
|
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
|
||||||
|
if ($_FILES['images']['error'][$key] === UPLOAD_ERR_OK) {
|
||||||
|
$file_name = $_FILES['images']['name'][$key];
|
||||||
|
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
|
||||||
|
|
||||||
|
if (in_array($file_ext, $allowed_ext)) {
|
||||||
|
$new_file_name = time() . '_' . uniqid() . '_' . basename($file_name);
|
||||||
|
$dest_path = $upload_dir . $new_file_name;
|
||||||
|
|
||||||
|
if (move_uploaded_file($tmp_name, $dest_path)) {
|
||||||
|
$image_path = 'assets/uploads/' . $new_file_name;
|
||||||
|
$img_stmt = $pdo->prepare("INSERT INTO installation_images (installation_id, image_path) VALUES (?, ?)");
|
||||||
|
$img_stmt->execute([$installation_id, $image_path]);
|
||||||
|
} else {
|
||||||
|
throw new Exception('Failed to move uploaded file: ' . $file_name);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Exception('Invalid file type: ' . $file_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdo->commit();
|
||||||
$_SESSION['message'] = 'Installation registered successfully!';
|
$_SESSION['message'] = 'Installation registered successfully!';
|
||||||
$_SESSION['message_type'] = 'success';
|
$_SESSION['message_type'] = 'success';
|
||||||
} catch (PDOException $e) {
|
|
||||||
error_log("Database error: " . $e->getMessage());
|
} catch (Exception $e) {
|
||||||
$_SESSION['message'] = 'Failed to register installation. Please try again.';
|
if ($pdo->inTransaction()) {
|
||||||
|
$pdo->rollBack();
|
||||||
|
}
|
||||||
|
error_log("Upload/Database error: " . $e->getMessage());
|
||||||
|
$_SESSION['message'] = 'Failed to register installation: ' . $e->getMessage();
|
||||||
$_SESSION['message_type'] = 'danger';
|
$_SESSION['message_type'] = 'danger';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
header('Location: index.php');
|
header('Location: index.php');
|
||||||
exit;
|
exit;
|
||||||
|
|||||||
8
db/migrations/004_add_multiple_images_support.sql
Normal file
8
db/migrations/004_add_multiple_images_support.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS `installation_images` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`installation_id` INT NOT NULL,
|
||||||
|
`image_path` VARCHAR(255) NOT NULL,
|
||||||
|
FOREIGN KEY (`installation_id`) REFERENCES `installations`(`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
ALTER TABLE `installations` DROP COLUMN `image_path`;
|
||||||
15
index.php
15
index.php
@ -187,11 +187,16 @@ try {
|
|||||||
</tr>
|
</tr>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<?php foreach ($installations as $inst): ?>
|
<?php foreach ($installations as $inst): ?>
|
||||||
|
<?php
|
||||||
|
$img_stmt = $pdo->prepare("SELECT image_path FROM installation_images WHERE installation_id = ? ORDER BY id ASC LIMIT 1");
|
||||||
|
$img_stmt->execute([$inst['id']]);
|
||||||
|
$first_image = $img_stmt->fetchColumn();
|
||||||
|
?>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<?php if (!empty($inst['image_path'])): ?>
|
<?php if ($first_image): ?>
|
||||||
<a href="<?php echo htmlspecialchars($inst['image_path']); ?>" target="_blank">
|
<a href="<?php echo htmlspecialchars($first_image); ?>" target="_blank">
|
||||||
<img src="<?php echo htmlspecialchars($inst['image_path']); ?>" alt="Installation Image" style="width: 50px; height: 50px; object-fit: cover;">
|
<img src="<?php echo htmlspecialchars($first_image); ?>" alt="Installation Image" style="width: 50px; height: 50px; object-fit: cover;">
|
||||||
</a>
|
</a>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
N/A
|
N/A
|
||||||
@ -247,8 +252,8 @@ try {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="installation_image" class="form-label">Installation Image</label>
|
<label for="images" class="form-label">Installation Images</label>
|
||||||
<input class="form-control" type="file" id="installation_image" name="installation_image" accept="image/*">
|
<input class="form-control" type="file" id="images" name="images[]" accept="image/*" multiple>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user