Skip to content
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
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ services:
OpenDxp\Bundle\AdminBundle\Service\Element\EditLockService: ~

OpenDxp\Bundle\AdminBundle\Service\Email\UnusableRecipientDetector: ~
OpenDxp\Bundle\AdminBundle\Service\Email\EmailLogListingFactory: ~

OpenDxp\Bundle\AdminBundle\Service\Login\LoginPageService: ~

Expand Down
98 changes: 98 additions & 0 deletions public/js/opendxp/settings/email/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ opendxp.registerNS('opendxp.settings.email.log');
opendxp.settings.email.log = Class.create({

filterField: null,
exportPrepareUrl: null,
exportProcessUrl: null,
exportDownloadUrl: null,

exportConfirmThreshold: 1000,

initialize: function(document) {
this.document = document;
this.exportPrepareUrl = Routing.generate('opendxp_admin_email_exportemaillogs_prepare');
this.exportProcessUrl = Routing.generate('opendxp_admin_email_exportemaillogs');
this.exportDownloadUrl = Routing.generate('opendxp_admin_email_exportemaillogs_download');

this.filterField = new Ext.form.TextField({
width: 200,
Expand Down Expand Up @@ -461,11 +469,17 @@ opendxp.settings.email.log = Class.create({

this.pagingtoolbar = opendxp.helpers.grid.buildDefaultPagingToolbar(this.store);

this.selectionColumn = new Ext.selection.CheckboxModel();

var toolbar = Ext.create('Ext.Toolbar', {
cls: 'opendxp_main_toolbar',
items: [
'->',
{
text: t('export_csv'),
iconCls: 'opendxp_icon_export',
handler: this.doExport.bind(this)
}, '-', {
text: t('filter') + '/' + t('search'),
xtype: 'tbtext',
style: 'margin: 0 10px 0 0;'
Expand All @@ -477,6 +491,7 @@ opendxp.settings.email.log = Class.create({
frame: false,
store: this.store,
columns : gridColumns,
selModel: this.selectionColumn,
columnLines: true,
stripeRows: true,
border: true,
Expand All @@ -496,6 +511,89 @@ opendxp.settings.email.log = Class.create({
return this.grid;
},

doExport: function () {
var params = {};

if (this.document) {
params.documentId = this.document.id;
}

var selection = this.grid.getSelectionModel().getSelection();
if (selection.length > 0) {
params['ids[]'] = selection.map(function (record) {
return record.get('id');
});

this.exportPrepare(params);
return;
}

// the applied filter, not the raw input: the field only takes effect on ENTER
var filter = this.store.getProxy().extraParams.filter;
if (!filter) {
this.exportPrepare(params);
return;
}

Ext.MessageBox.confirm('', t('filter_active_message'), function (buttonValue) {
if (buttonValue === 'yes') {
params.filter = filter;
}

this.exportPrepare(params);
}.bind(this));
},

exportPrepare: function (params) {
Ext.Ajax.request({
url: this.exportPrepareUrl,
method: 'POST',
params: params,
success: function (response) {
var rdata = Ext.decode(response.responseText);

if (!rdata || !rdata.fileHandle) {
return;
}

if (rdata.total <= this.exportConfirmThreshold) {
this.exportProcess(params, rdata.fileHandle);
return;
}

var formatted = new Intl.NumberFormat(navigator.language).format(rdata.total);

Ext.Msg.confirm(t('warning'), sprintf(t('email_log_export_confirmation'), '<b>' + formatted + '</b>'), function (buttonValue) {
if (buttonValue === 'yes') {
this.exportProcess(params, rdata.fileHandle);
}
}.bind(this));
}.bind(this)
});
},

exportProcess: function (params, fileHandle) {
this.grid.setLoading(t('please_wait'));

Ext.Ajax.request({
url: this.exportProcessUrl,
method: 'POST',
params: Ext.applyIf({fileHandle: fileHandle}, params),
callback: function () {
this.grid.setLoading(false);
}.bind(this),
success: function (response) {
var rdata = Ext.decode(response.responseText);

if (!rdata || !rdata.success) {
return;
}

opendxp.helpers.download(Ext.urlAppend(this.exportDownloadUrl, Ext.Object.toQueryString({fileHandle: fileHandle})));
}.bind(this)
});
},

reload: function () {

this.grid.store.reload();
Expand Down
39 changes: 39 additions & 0 deletions src/Controller/Admin/EmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
use OpenDxp\Bundle\AdminBundle\Handler\Email\Blocklist\UpdateBlocklistEntry\UpdateBlocklistEntryHandler;
use OpenDxp\Bundle\AdminBundle\Handler\Email\BlocklistPayload;
use OpenDxp\Bundle\AdminBundle\Handler\Email\DeleteEmailLog\DeleteEmailLogHandler;
use OpenDxp\Bundle\AdminBundle\Handler\Email\DoEmailLogExport\DoEmailLogExportHandler;
use OpenDxp\Bundle\AdminBundle\Handler\Email\DoEmailLogExport\DoEmailLogExportPayload;
use OpenDxp\Bundle\AdminBundle\Handler\Email\PrepareEmailLogExport\PrepareEmailLogExportHandler;
use OpenDxp\Bundle\AdminBundle\Handler\Email\PrepareEmailLogExport\PrepareEmailLogExportPayload;
use OpenDxp\Bundle\AdminBundle\Handler\Email\GetBlocklist\GetBlocklistHandler;
use OpenDxp\Bundle\AdminBundle\Handler\Email\GetEmailLogs\GetEmailLogsHandler;
use OpenDxp\Bundle\AdminBundle\Handler\Email\GetEmailLogs\GetEmailLogsPayload;
Expand All @@ -38,7 +42,9 @@
use OpenDxp\Bundle\AdminBundle\Payload\Common\IdQueryPayload;
use OpenDxp\Bundle\AdminBundle\Security\AdminPermission;
use OpenDxp\Http\RequestHelper;
use OpenDxp\Bundle\AdminBundle\Service\Grid\GridExportService;
use OpenDxp\Security\CorePermission;
use RuntimeException;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -65,6 +71,39 @@ public function emailLogsAction(
return $this->apiJson($handler($payload));
}

#[IsGranted(CorePermission::Emails->value)]
#[Route('/export-email-logs/prepare', name: 'opendxp_admin_email_exportemaillogs_prepare', methods: ['POST'])]
public function prepareEmailLogExportAction(
PrepareEmailLogExportHandler $handler,
PrepareEmailLogExportPayload $payload,
): JsonResponse {
return $this->apiJson($handler($payload));
}

#[IsGranted(CorePermission::Emails->value)]
#[Route('/export-email-logs', name: 'opendxp_admin_email_exportemaillogs', methods: ['POST'])]
public function doEmailLogExportAction(
DoEmailLogExportHandler $handler,
DoEmailLogExportPayload $payload,
): JsonResponse {
$handler($payload);

return $this->apiOk();
}

#[IsGranted(CorePermission::Emails->value)]
#[Route('/export-email-logs/download', name: 'opendxp_admin_email_exportemaillogs_download', methods: ['GET'])]
public function downloadEmailLogExportAction(
GridExportService $gridExportService,
#[MapQueryParameter] ?string $fileHandle = null,
): Response {
try {
return $gridExportService->downloadCsvFile($fileHandle ?? '');
} catch (RuntimeException) {
throw $this->createNotFoundException('CSV file not found');
}
}

#[IsGranted(CorePermission::Emails->value)]
#[Route('/show-email-log', name: 'opendxp_admin_email_showemaillog', methods: ['GET'])]
public function showEmailLogAction(
Expand Down
123 changes: 123 additions & 0 deletions src/Handler/Email/DoEmailLogExport/DoEmailLogExportHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

/**
* OpenDXP
*
* This source file is licensed under the GNU General Public License version 3 (GPLv3).
*
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) OpenDXP (https://www.opendxp.io)
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
*/

namespace OpenDxp\Bundle\AdminBundle\Handler\Email\DoEmailLogExport;

use League\Flysystem\FilesystemException;
use OpenDxp\Bundle\AdminBundle\Exception\AdminOperationFailedException;
use OpenDxp\Bundle\AdminBundle\Service\Email\EmailLogListingFactory;
use OpenDxp\Bundle\AdminBundle\Service\Grid\GridExportService;
use OpenDxp\Logger;
use OpenDxp\Model\Tool\Email\Log;
use OpenDxp\Tool\Storage;
use RuntimeException;

final class DoEmailLogExportHandler
{
private const string DELIMITER = ';';

private const int PAGE_SIZE = 500;

/**
* The body columns are omitted: they are megabytes of markup
*/
private const array COLUMNS = [
'id',
'documentId',
'sentDate',
'from',
'replyTo',
'to',
'cc',
'bcc',
'subject',
'params',
'error',
];

public function __construct(
private readonly EmailLogListingFactory $listingFactory,
private readonly GridExportService $gridExportService,
) {
}

public function __invoke(DoEmailLogExportPayload $payload): void
{
$temp = tmpfile();

if ($temp === false) {
throw new RuntimeException('Unable to open a temporary file for the CSV export');
}

try {
fputcsv($temp, self::COLUMNS, self::DELIMITER, '"', '');

$offset = 0;

do {
$list = $this->listingFactory->create($payload->documentId, $payload->filter, $payload->ids);
$list->setLimit(self::PAGE_SIZE);
$list->setOffset($offset);

$entries = $list->getEmailLogs();

foreach ($entries as $entry) {
fputcsv($temp, $this->toRow($entry), self::DELIMITER, '"', '');
}

$offset += self::PAGE_SIZE;

} while (count($entries) === self::PAGE_SIZE);

rewind($temp);

Storage::get('temp')->writeStream(
$this->gridExportService->getCsvFile($payload->fileHandle),
$temp
);
} catch (FilesystemException $exception) {
Logger::err($exception->getMessage());

throw new AdminOperationFailedException(
sprintf('export file could not be written: %s', $payload->fileHandle)
);
} finally {
if (is_resource($temp)) {
fclose($temp);
}
}
}

/**
* @return array<int, string|int|null>
*/
private function toRow(Log $entry): array
{
return [
$entry->getId(),
$entry->getDocumentId(),
date('Y-m-d H:i:s', $entry->getSentDate()),
$entry->getFrom(),
$entry->getReplyTo(),
$entry->getTo(),
$entry->getCc(),
$entry->getBcc(),
$entry->getSubject(),
json_encode($entry->getParams(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
$entry->getError(),
];
}
}
44 changes: 44 additions & 0 deletions src/Handler/Email/DoEmailLogExport/DoEmailLogExportPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/**
* OpenDXP
*
* This source file is licensed under the GNU General Public License version 3 (GPLv3).
*
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) OpenDXP (https://www.opendxp.io)
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
*/

namespace OpenDxp\Bundle\AdminBundle\Handler\Email\DoEmailLogExport;

use OpenDxp\Bundle\AdminBundle\Payload\ExtJsPayloadInterface;
use Symfony\Component\HttpFoundation\Request;

final readonly class DoEmailLogExportPayload implements ExtJsPayloadInterface
{
/**
* @param int[] $ids
*/
public function __construct(
public readonly string $fileHandle,
public readonly ?int $documentId = null,
public readonly ?string $filter = null,
public readonly array $ids = [],
) {
}

public static function fromRequest(Request $request): static
{
return new static(
fileHandle: $request->request->getString('fileHandle'),
documentId: $request->request->has('documentId') ? (int) $request->request->getString('documentId') : null,
filter: $request->request->has('filter') ? $request->request->getString('filter') : null,
ids: array_map(intval(...), $request->request->all('ids')),
);
}
}
Loading
Loading