167 lines
5.0 KiB
PHP
167 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* This file is developed by evoWeb.
|
|
*
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, either version 2
|
|
* of the License, or any later version.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Evoweb\EwBloggy\Domain\Repository;
|
|
|
|
use DateMalformedStringException;
|
|
use DateTimeImmutable;
|
|
use Evoweb\EwBloggy\Constants;
|
|
use Evoweb\EwBloggy\Domain\Model\Post;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
|
|
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
|
|
|
/**
|
|
* A post repository
|
|
*
|
|
* @extends Repository<Post>
|
|
*/
|
|
class PostRepository extends Repository
|
|
{
|
|
protected array $defaultConstraints = [];
|
|
|
|
public function initializeObject(): void
|
|
{
|
|
$query = $this->createQuery();
|
|
$this->defaultConstraints[] = $query->equals('doktype', Constants::DOKTYPE_BLOG_POST);
|
|
if ($this->getRequest()->getAttribute('language')?->getLanguageId() === 0) {
|
|
$this->defaultConstraints[] = $query->logicalOr(
|
|
$query->equals('l18n_cfg', 0),
|
|
$query->equals('l18n_cfg', 2)
|
|
);
|
|
} else {
|
|
try {
|
|
$this->defaultConstraints[] = $query->lessThan('l18n_cfg', 2);
|
|
} catch (InvalidQueryException) {
|
|
}
|
|
}
|
|
|
|
$this->defaultOrderings = [
|
|
'publish_date' => QueryInterface::ORDER_DESCENDING,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return QueryResultInterface<Post>
|
|
*/
|
|
public function findAll(): QueryResultInterface
|
|
{
|
|
return $this->getFindAllQuery()->execute();
|
|
}
|
|
|
|
/**
|
|
* @return QueryResultInterface<Post>
|
|
*/
|
|
public function findAllWithLimit(int $limit): QueryResultInterface
|
|
{
|
|
$query = $this->getFindAllQuery();
|
|
$query->setLimit($limit);
|
|
|
|
return $query->execute();
|
|
}
|
|
|
|
public function findMonthsAndYearsWithPosts(): array
|
|
{
|
|
$query = $this->createQuery();
|
|
$constraints = $this->defaultConstraints;
|
|
|
|
try {
|
|
$constraints[] = $query->greaterThan('crdateMonth', 0);
|
|
$constraints[] = $query->greaterThan('crdateYear', 0);
|
|
} catch (InvalidQueryException) {
|
|
}
|
|
|
|
$query->matching($query->logicalAnd(...$constraints));
|
|
$posts = $query->execute(true);
|
|
|
|
$result = [];
|
|
$currentIndex = -1;
|
|
$currentYear = null;
|
|
$currentMonth = null;
|
|
foreach ($posts as $post) {
|
|
$year = $post['crdate_year'];
|
|
$month = $post['crdate_month'];
|
|
if ($currentYear !== $year || $currentMonth !== $month) {
|
|
$currentIndex++;
|
|
$currentYear = $year;
|
|
$currentMonth = $month;
|
|
$result[$currentIndex] = [
|
|
'year' => $currentYear,
|
|
'month' => $currentMonth,
|
|
'count' => 1
|
|
];
|
|
} else {
|
|
$result[$currentIndex]['count']++;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @return QueryResultInterface<Post>
|
|
*/
|
|
public function findByMonthAndYear(int $year, ?int $month = null): QueryResultInterface
|
|
{
|
|
$query = $this->createQuery();
|
|
$constraints = $this->defaultConstraints;
|
|
|
|
try {
|
|
if ($month !== null) {
|
|
$startDate = new DateTimeImmutable(sprintf('%d-%d-1 00:00:00', $year, $month));
|
|
$endDate = new DateTimeImmutable(
|
|
sprintf('%d-%d-%d 23:59:59', $year, $month, (int)$startDate->format('t'))
|
|
);
|
|
} else {
|
|
$startDate = new DateTimeImmutable(sprintf('%d-1-1 00:00:00', $year));
|
|
$endDate = new DateTimeImmutable(sprintf('%d-12-31 23:59:59', $year));
|
|
}
|
|
$constraints[] = $query->greaterThanOrEqual('publish_date', $startDate->getTimestamp());
|
|
$constraints[] = $query->lessThanOrEqual('publish_date', $endDate->getTimestamp());
|
|
} catch (InvalidQueryException | DateMalformedStringException) {
|
|
}
|
|
|
|
return $query->matching($query->logicalAnd(...$constraints))->execute();
|
|
}
|
|
|
|
/**
|
|
* @return QueryInterface<Post>
|
|
*/
|
|
protected function getFindAllQuery(): QueryInterface
|
|
{
|
|
$query = $this->createQuery();
|
|
$constraints = $this->defaultConstraints;
|
|
|
|
try {
|
|
$constraints[] = $query->logicalOr(
|
|
$query->equals('archiveDate', 0),
|
|
$query->greaterThanOrEqual('archiveDate', time())
|
|
);
|
|
} catch (InvalidQueryException) {
|
|
}
|
|
|
|
$query->matching($query->logicalAnd(...$constraints));
|
|
|
|
return $query;
|
|
}
|
|
|
|
protected function getRequest(): ?ServerRequestInterface
|
|
{
|
|
return $GLOBALS['TYPO3_REQUEST'];
|
|
}
|
|
}
|