Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: logic for send logs #397

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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))
Expand All @@ -179,6 +200,8 @@ public function actionDownloadLogs()
$errors[] = $error;
Translations::$plugin->logHelper->log('['. __METHOD__ .'] ' . $error, Constants::LOG_LEVEL_ERROR);
}

$zipHasFiles = true;
}

// Close zip
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/templates/settings/send-logs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
{{ 'Download your log files and ' }}<a href="mailto:[email protected]?subject={{subject}}&body={{body}}">send to Acclaro support</a>
</div>
<span class="light italic" style="font-style:italic;">{{'Remember to attach the downloaded logs!'}}</span>

{{ 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',
}) }}
<div class="buttons">
<button type="submit" class="btn submit icon" data-icon="download" value="submit">{{ "Download logs"|t('app') }}</button>
</div>
Expand Down