diff --git a/app/Controllers/OrderController.php b/app/Controllers/OrderController.php index 4e54cd5..5617c1d 100644 --- a/app/Controllers/OrderController.php +++ b/app/Controllers/OrderController.php @@ -116,6 +116,7 @@ class OrderController $conn = \App\Config\Database::getInstance()->getConnection(); + $conn->beginTransaction(); try { // Create Order $sql = "INSERT INTO orders (title, type, content, received_at) VALUES (:title, :type, :content, :received_at)"; @@ -149,10 +150,13 @@ class OrderController 'type' => $typeLabel ], $count); + $conn->commit(); + $_SESSION['flash_success'] = "Ordem criada com sucesso! $count domínios processados."; View::redirect('/admin/orders'); } catch (\Exception $e) { + $conn->rollBack(); $_SESSION['flash_error'] = "Erro ao processar ordem: " . $e->getMessage(); View::redirect('/admin/orders/create'); } @@ -216,4 +220,36 @@ class OrderController readfile($filePath); exit; } + + public function uploadAttachments($id) + { + $orderModel = new \App\Models\Order(); + $order = $orderModel->find($id); + + if (!$order) { + $_SESSION['flash_error'] = "Ordem não encontrada."; + View::redirect('/admin/orders'); + return; + } + + try { + if (isset($_FILES['new_attachments']) && !empty($_FILES['new_attachments']['name'][0])) { + $attachments = $_FILES['new_attachments']; + $attachmentService = new AttachmentService(); + $savedCount = $attachmentService->storeFiles((int) $id, $attachments); + + if ($savedCount > 0) { + $_SESSION['flash_success'] = "{$savedCount} anexo(s) adicionado(s) com sucesso à ordem!"; + } else { + $_SESSION['flash_error'] = "Nenhum arquivo válido foi recebido."; + } + } else { + $_SESSION['flash_error'] = "Nenhum arquivo foi selecionado."; + } + } catch (\Exception $e) { + $_SESSION['flash_error'] = "Erro de upload: " . $e->getMessage(); + } + + View::redirect('/admin/orders/view/' . $id); + } } diff --git a/app/Services/AttachmentService.php b/app/Services/AttachmentService.php index febc364..42ff531 100644 --- a/app/Services/AttachmentService.php +++ b/app/Services/AttachmentService.php @@ -22,8 +22,23 @@ class AttachmentService 'text/plain' => 'txt', ]; - /** Tamanho máximo por arquivo: 20 MB */ - private const MAX_SIZE = 20 * 1024 * 1024; + /** + * Retorna o tamanho máximo de upload, lendo do PHP ini. + */ + private function getMaxSize(): int + { + $val = ini_get('upload_max_filesize'); + if (empty($val)) return 20 * 1024 * 1024; + $val = trim($val); + $last = strtolower($val[strlen($val)-1]); + $val = (int)$val; + switch($last) { + case 'g': $val *= 1024; + case 'm': $val *= 1024; + case 'k': $val *= 1024; + } + return $val > 0 ? $val : 20 * 1024 * 1024; + } /** * Raiz do diretório de uploads (fora do public/). @@ -81,8 +96,10 @@ class AttachmentService throw new \Exception("Erro no upload do arquivo '{$file['name']}': código {$file['error']}"); } - if ($file['size'] > self::MAX_SIZE) { - throw new \Exception("O arquivo '{$file['name']}' excede o tamanho máximo permitido de 20 MB."); + $maxSize = $this->getMaxSize(); + if ($file['size'] > $maxSize) { + $maxSizeMB = floor($maxSize / (1024 * 1024)); + throw new \Exception("O arquivo '{$file['name']}' excede o tamanho máximo permitido de {$maxSizeMB} MB."); } // Detecta o tipo MIME real do arquivo (não confiar só no header HTTP) diff --git a/app/Services/OrderProcessor.php b/app/Services/OrderProcessor.php index 5283d24..86a4eaa 100644 --- a/app/Services/OrderProcessor.php +++ b/app/Services/OrderProcessor.php @@ -36,7 +36,10 @@ class OrderProcessor $domains = array_unique($domains); // Remove duplicates in batch - $conn->beginTransaction(); + $inTransaction = $conn->inTransaction(); + if (!$inTransaction) { + $conn->beginTransaction(); + } try { $stmtCheck = $conn->prepare("SELECT id FROM domains WHERE name = :name"); @@ -77,11 +80,15 @@ class OrderProcessor ]); } - $conn->commit(); + if (!$inTransaction) { + $conn->commit(); + } return count($domains); } catch (\Exception $e) { - $conn->rollBack(); + if (!$inTransaction) { + $conn->rollBack(); + } throw $e; } } diff --git a/app/routes.php b/app/routes.php index 72d7e70..62bdcdf 100644 --- a/app/routes.php +++ b/app/routes.php @@ -87,6 +87,9 @@ $router->addMiddleware(\App\Middleware\AdminMiddleware::class); $router->get('/admin/orders/attachments/{id}/download', [\App\Controllers\OrderController::class, 'downloadAttachment']); $router->addMiddleware(\App\Middleware\AdminMiddleware::class); +$router->post('/admin/orders/attachments/upload/{id}', [\App\Controllers\OrderController::class, 'uploadAttachments']); +$router->addMiddleware(\App\Middleware\AdminMiddleware::class); + // Settings $router->get('/admin/settings', [\App\Controllers\SettingsController::class, 'index']); $router->addMiddleware(\App\Middleware\AdminMiddleware::class); diff --git a/resources/views/admin/orders/view.php b/resources/views/admin/orders/view.php index 4606c5d..2430809 100644 --- a/resources/views/admin/orders/view.php +++ b/resources/views/admin/orders/view.php @@ -29,7 +29,6 @@ -
@@ -37,6 +36,8 @@

Anexos ()

+ + + +
+ Nenhum anexo salvo para esta ordem. +
+ + +
+
+ + +
+
-