From 8155910b06a42c15c302477c07e06f70fbde2303 Mon Sep 17 00:00:00 2001 From: Bhupesh Pandey Date: Fri, 23 Sep 2022 15:08:53 +0530 Subject: [PATCH] Update: logic for send logs --- src/controllers/SettingsController.php | 25 +++++++++++++++++++++++++ src/templates/settings/send-logs.twig | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 93aa06b6..6dd34056 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -148,10 +148,27 @@ public function actionDeleteAllOrders() public function actionDownloadLogs() { $this->requireLogin(); + $this->requirePostRequest(); + if (!Translations::$plugin->userRepository->userHasAccess('translations:settings')) { return $this->redirect(Constants::URL_TRANSLATIONS, 302, true); } + $requestedDate = Craft::$app->getRequest()->getBodyParam('requestedDate'); + + $requestedDate = is_array($requestedDate) ? $requestedDate['date'] ?? null : $requestedDate; + + if (!$requestedDate) { + return $this->asFailure("Something went wrong with date"); + } + + $requestedDate = \DateTime::createFromFormat('n/j/Y', $requestedDate)->format('Y-m-d'); + $errors = \DateTime::getLastErrors(); + + if (($errors['warning_count'] + $errors['error_count']) > 0) { + return $this->asFailure("Please select a valid date"); + } + $zipName = 'logs'; $zipDest = Craft::$app->path->getTempPath().'/'. $zipName .'_'.time().'.' . Constants::FILE_FORMAT_ZIP; $errors = array(); @@ -169,8 +186,12 @@ public function actionDownloadLogs() } $logFiles = array_diff(scandir(Craft::$app->path->getLogPath()), array('.', '..')); + $zipHasFiles = false; foreach ($logFiles as $key => $file) { + // Skip if file is of some other date than requested + if (!str_contains($file, $requestedDate)) continue; + $file_contents = file_get_contents(Craft::$app->path->getLogPath() .'/'. $file); if (!$zip->addFromString($file, $file_contents)) @@ -179,6 +200,8 @@ public function actionDownloadLogs() $errors[] = $error; Translations::$plugin->logHelper->log('['. __METHOD__ .'] ' . $error, Constants::LOG_LEVEL_ERROR); } + + $zipHasFiles = true; } // Close zip @@ -189,6 +212,8 @@ public function actionDownloadLogs() return $errors; } + if (!$zipHasFiles) return $this->asSuccess("No logs found for selected date"); + if (!is_file($zipDest) || !Path::ensurePathIsContained($zipDest)) { throw new NotFoundHttpException(Craft::t('app', 'Invalid file name: {filename}', [ 'filename' => $zipDest diff --git a/src/templates/settings/send-logs.twig b/src/templates/settings/send-logs.twig index b9e06054..0862cf3f 100644 --- a/src/templates/settings/send-logs.twig +++ b/src/templates/settings/send-logs.twig @@ -28,6 +28,15 @@ {{ 'Download your log files and ' }}send to Acclaro support {{'Remember to attach the downloaded logs!'}} + + {{ forms.dateField({ + label: 'Logs will be downloaded for selected date'|t, + value: "now"|date('m/d/Y'), + name: 'requestedDate', + id: 'requestedDate', + size: 20, + placeholder: 'Optional', + }) }}