Filter data

[MAILPOET-3135]
This commit is contained in:
Pavel Dohnal
2021-03-03 14:02:41 +01:00
committed by Veljko V
parent 8bdcfebee8
commit 4481444282
2 changed files with 21 additions and 4 deletions

View File

@@ -22,9 +22,18 @@ class Logs {
} }
public function render() { public function render() {
$search = isset($_GET['search']) ? $_GET['search'] : null;
$from = isset($_GET['from']) ? $_GET['from'] : null;
$to = isset($_GET['to']) ? $_GET['to'] : null;
$dateFrom = (new Carbon())->subDays(7); $dateFrom = (new Carbon())->subDays(7);
$dateTo = new Carbon(); if (isset($from)) {
$logs = $this->logRepository->getLogs($dateFrom, $dateTo); $dateFrom = new Carbon($from);
}
$dateTo = null;
if (isset($to)) {
$dateTo = new Carbon($to);
}
$logs = $this->logRepository->getLogs($dateFrom, $dateTo, $search);
$data = ['logs' => []]; $data = ['logs' => []];
foreach ($logs as $log) { foreach ($logs as $log) {
$data['logs'][] = [ $data['logs'][] = [

View File

@@ -4,6 +4,7 @@ namespace MailPoet\Logging;
use MailPoet\Doctrine\Repository; use MailPoet\Doctrine\Repository;
use MailPoet\Entities\LogEntity; use MailPoet\Entities\LogEntity;
use MailPoet\Util\Helpers;
/** /**
* @extends Repository<LogEntity> * @extends Repository<LogEntity>
@@ -16,15 +17,16 @@ class LogRepository extends Repository {
/** /**
* @param \DateTimeInterface|null $dateFrom * @param \DateTimeInterface|null $dateFrom
* @param \DateTimeInterface|null $dateTo * @param \DateTimeInterface|null $dateTo
* @param string|null $search
* @return LogEntity[] * @return LogEntity[]
*/ */
public function getLogs(\DateTimeInterface $dateFrom = null, \DateTimeInterface $dateTo = null): array { public function getLogs(\DateTimeInterface $dateFrom = null, \DateTimeInterface $dateTo = null, string $search = null): array {
$query = $this->doctrineRepository->createQueryBuilder('l') $query = $this->doctrineRepository->createQueryBuilder('l')
->select('l'); ->select('l');
if ($dateFrom instanceof \DateTimeInterface) { if ($dateFrom instanceof \DateTimeInterface) {
$query $query
->where('l.createdAt > :dateFrom') ->andWhere('l.createdAt > :dateFrom')
->setParameter('dateFrom', $dateFrom->format('Y-m-d H:i:s')); ->setParameter('dateFrom', $dateFrom->format('Y-m-d H:i:s'));
} }
if ($dateTo instanceof \DateTimeInterface) { if ($dateTo instanceof \DateTimeInterface) {
@@ -32,6 +34,12 @@ class LogRepository extends Repository {
->andWhere('l.createdAt < :dateTo') ->andWhere('l.createdAt < :dateTo')
->setParameter('dateTo', $dateTo->format('Y-m-d H:i:s')); ->setParameter('dateTo', $dateTo->format('Y-m-d H:i:s'));
} }
if ($search) {
$search = Helpers::escapeSearch($search);
$query
->andWhere('l.name LIKE :search or l.message LIKE :search')
->setParameter('search', "%$search%");
}
$query->orderBy('l.createdAt', 'desc'); $query->orderBy('l.createdAt', 'desc');