Skip to content
Snippets Groups Projects
Verified Commit 21b1fb6d authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[TASK] Add comments to the findRelated functionality

parent ec1b1a90
No related branches found
No related tags found
1 merge request!41[TASK] Add related news by tag or category
......@@ -518,6 +518,7 @@ class NewsRepository extends AbstractRepository {
public function findRelated(News $news, int $limit = 0) {
$connection = $this->getConnection();
$qb = $connection->createQueryBuilder();
// We need to build the constraint for the tags/categories
$constraints = [];
$tags = $news->getTags();
if ($tags->count() > 0) {
......@@ -533,6 +534,7 @@ class NewsRepository extends AbstractRepository {
$constraints[] = $qb->expr()->eq('pid', $news->getPid());
}
// here we fetch the lastUpdated of the $limit amount of news with newer lastUpdated dates
$result = $qb->select('lastUpdated')
->from('pages')
->where(
......@@ -541,10 +543,11 @@ class NewsRepository extends AbstractRepository {
$qb->expr()->andX(...$constraints)
)
->setMaxResults($limit)
->orderBy('lastUpdated', 'asc')
->orderBy('lastUpdated', 'desc')
->execute();
$max = $result->fetchOne();
$newest = $result->fetchOne();
// Here we fetch the lastUpdated of the $limit amount of news with older lastUpdated dates
$result = $qb->select('lastUpdated')
->from('pages')
->where(
......@@ -553,9 +556,9 @@ class NewsRepository extends AbstractRepository {
$qb->expr()->andX(...$constraints)
)
->setMaxResults($limit)
->orderBy('lastUpdated', 'desc')
->orderBy('lastUpdated', 'asc')
->execute();
$min = $result->fetchOne();
$oldest = $result->fetchOne();
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(FALSE);
......@@ -563,13 +566,17 @@ class NewsRepository extends AbstractRepository {
'crdate' => QueryInterface::ORDER_DESCENDING
]);
$constraints = [];
if ($max) {
$constraints[] = $query->lessThan('lastUpdated', $max);
if ($newest) {
$constraints[] = $query->lessThanOrEqual('lastUpdated', $newest);
}
if ($min) {
$constraints[] = $query->greaterThan('lastUpdated', $min);
if ($oldest) {
$constraints[] = $query->greaterThanOrEqual('lastUpdated', $oldest);
}
// Now we fetch the $limit amount of news via extbase query and limit them to the newest and oldest date
// remember, we fetched the oldest and newest date from the $limit amount of news newer and older then
// the given news. If we limit the result of the following query to $limit, we get $limit amount of news
// "around" the given news, where newer news are preferred due to the ordering.
$tags = $news->getTags();
if ($tags->count() > 0) {
foreach ($tags as $tag) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment