Skip to content

Commit

Permalink
Merge pull request #397 from AcclaroInc/Task/496/update-send-logs-logic
Browse files Browse the repository at this point in the history
Update: logic for send logs
  • Loading branch information
shnsumit authored Sep 29, 2022
2 parents cd6374a + 8155910 commit 34ddc92
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit 34ddc92

Please sign in to comment.