Skip to content
Snippets Groups Projects
Commit a7d90462 authored by Stefan Galinski's avatar Stefan Galinski :video_game:
Browse files

[FEATURE] Implement a sorting setting (values: date, positionInTree)

parent 082865de
No related branches found
No related tags found
No related merge requests found
Showing
with 193 additions and 159 deletions
......@@ -187,5 +187,3 @@ abstract class AbstractController extends ActionController {
];
}
}
?>
......@@ -61,7 +61,7 @@ class LatestController extends AbstractController {
}
$latestNewsEntries = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategories(
$limit, FALSE, $categoryUids, 0, TRUE
$limit, FALSE, $categoryUids, 0, TRUE, $this->settings['sortBy']
);
$newsMetaData = [];
......
......@@ -26,8 +26,8 @@ namespace SGalinski\SgNews\Controller;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Service\HeaderMetaDataService;
use SGalinski\SgNews\Domain\Model\News;
use SGalinski\SgNews\Service\HeaderMetaDataService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -81,7 +81,9 @@ class ListByCategoryController extends AbstractController {
$newsMetaData = [];
$headerSet = FALSE;
$news = $this->newsRepository->findAllSortedNewsByCategories($categoryUids, $newsPerPage, $offset)->toArray();
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryUids, $newsPerPage, $offset, $this->settings['sortBy']
)->toArray();
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$data = $this->getMetaDataForNews($newsEntry, $categories[$newsEntry->getPid()]);
......@@ -103,5 +105,3 @@ class ListByCategoryController extends AbstractController {
$this->view->assign('categories', $categories);
}
}
?>
......@@ -2,29 +2,29 @@
namespace SGalinski\SgNews\Controller;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Controller that handles the news feed rendering
......@@ -46,5 +46,3 @@ class NewsFeedController extends AbstractController {
$this->view->assign('news', $news);
}
}
?>
......@@ -69,7 +69,9 @@ class OverviewController extends AbstractController {
protected function highlightBestFitNews(array $categoryIds = NULL) {
/** @var News $highlightedNews */
$highlightedNews = $this->newsRepository
->findLastUpdatedOrHighlightedNewsByCategories(1, FALSE, $categoryIds)->getFirst();
->findLastUpdatedOrHighlightedNewsByCategories(
1, FALSE, $categoryIds, 0, FALSE, $this->settings['sortBy']
)->getFirst();
if (!$highlightedNews) {
return;
}
......@@ -119,7 +121,9 @@ class OverviewController extends AbstractController {
$categoryIds[] = $category->getUid();
$categoriesById[$categoryId] = $category;
$news = $this->newsRepository->findAllSortedNewsByCategories([$categoryId], $newsLimitPerCategory, $offset);
$news = $this->newsRepository->findAllSortedNewsByCategories(
[$categoryId], $newsLimitPerCategory, $offset, $this->settings['sortBy']
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
......@@ -141,7 +145,9 @@ class OverviewController extends AbstractController {
}
$allNews = [];
$news = $this->newsRepository->findAllSortedNewsByCategories($categoryIds, $newsLimitPerCategory, $offset);
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerCategory, $offset, $this->settings['sortBy']
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
......@@ -200,7 +206,9 @@ class OverviewController extends AbstractController {
}
$newsMetaData = [];
$news = $this->newsRepository->findAllSortedNewsByCategories($categoryIds, $newsPerPage, $offset);
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsPerPage, $offset, $this->settings['sortBy']
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$data = $this->getMetaDataForNews($newsEntry, $categoriesById[$newsEntry->getPid()]);
......
......@@ -143,5 +143,3 @@ class PageBrowserController extends ActionController {
return $this->uriBuilder->reset()->setUseCacheHash(FALSE)->uriFor('index', ['currentPage' => $page,]);
}
}
?>
......@@ -26,11 +26,9 @@ namespace SGalinski\SgNews\Controller;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Service\HeaderMetaDataService;
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use SGalinski\SgNews\Service\HeaderMetaDataService;
/**
* Controller that handles the news single view page
......@@ -73,24 +71,18 @@ class SingleViewController extends AbstractController {
return;
}
$similarNewsMetaData = [];
$similarNews = $this->newsRepository->findRandomNews(3, $news);
foreach ($similarNews as $similarNewsEntry) {
/** @var News $similarNewsEntry */
/** @var Category $category */
$category = $this->categoryRepository->findByUid($similarNewsEntry->getPid());
if (!$category) {
continue;
}
$similarNewsMetaData[] = $this->getMetaDataForNews($similarNewsEntry, $category);
}
/** @var $typoScriptController TypoScriptFrontendController */
$typoScriptController = $GLOBALS['TSFE'];
/** @var PageRenderer $pageRenderer */
$pageRenderer = $typoScriptController->getPageRenderer();
// $similarNewsMetaData = [];
// $similarNews = $this->newsRepository->findRandomNews(3, $news);
// foreach ($similarNews as $similarNewsEntry) {
// /** @var News $similarNewsEntry */
// /** @var Category $category */
// $category = $this->categoryRepository->findByUid($similarNewsEntry->getPid());
// if (!$category) {
// continue;
// }
//
// $similarNewsMetaData[] = $this->getMetaDataForNews($similarNewsEntry, $category);
// }
$newsAuthor = $news->getAuthorFrontendUser();
$newsMetaData = $this->getMetaDataForNews($news, $newsCategory);
......@@ -100,16 +92,19 @@ class SingleViewController extends AbstractController {
HeaderMetaDataService::addOgImageToHeader($newsMetaData['teaserImage']);
}
$previousNews = $this->newsRepository->findPreviousNewsEntryFromCurrentNews($news, $this->settings['sortBy'])
->getFirst();
$nextNews = $this->newsRepository->findNextNewsEntryFromCurrentNews($news, $this->settings['sortBy'])
->getFirst();
$this->view->assignMultiple(
[
'newsMetaData' => $newsMetaData,
'similarNewsMetaData' => $similarNewsMetaData,
'previousNews' => $this->newsRepository->findPreviousNewsEntryFromCurrentNews($news)->getFirst(),
'nextNews' => $this->newsRepository->findNextNewsEntryFromCurrentNews($news)->getFirst(),
// 'similarNewsMetaData' => $similarNewsMetaData,
'previousNews' => $previousNews,
'nextNews' => $nextNews,
'newsAuthor' => $newsAuthor,
]
);
}
}
?>
......@@ -2,34 +2,32 @@
namespace SGalinski\SgNews\Domain\Model;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Category
*/
class Category extends CategoryAndNews {
}
?>
......@@ -227,5 +227,3 @@ abstract class CategoryAndNews extends AbstractEntity {
$this->sorting = (int) $sorting;
}
}
?>
......@@ -240,5 +240,3 @@ class News extends CategoryAndNews {
return $this->authorFrontendUser;
}
}
?>
......@@ -2,34 +2,32 @@
namespace SGalinski\SgNews\Domain\Repository;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Category Repository
*/
class CategoryRepository extends AbstractRepository {
}
?>
......@@ -41,9 +41,10 @@ class NewsRepository extends AbstractRepository {
* @param array $categoryIds NULL, if the category filter isn't applied, otherwise an array with the categories uid.
* @param int $limit
* @param int $offset
* @param string $sortBy date or positionInTree
* @return QueryResultInterface
*/
public function findAllSortedNewsByCategories($categoryIds = NULL, $limit = 0, $offset = 0) {
public function findAllSortedNewsByCategories($categoryIds = NULL, $limit = 0, $offset = 0, $sortBy = 'date') {
$query = $this->createQuery();
$categoryConstraint = [];
......@@ -63,12 +64,23 @@ class NewsRepository extends AbstractRepository {
$query->setOffset($offset);
}
$query->setOrderings(
[
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
if ($sortBy === 'date') {
$query->setOrderings(
[
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
} else {
$query->setOrderings(
[
'sorting' => QueryInterface::ORDER_ASCENDING,
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
}
return $query->execute();
}
......@@ -97,10 +109,12 @@ class NewsRepository extends AbstractRepository {
* @param array $categoryIds NULL, if the category filter isn't applied, otherwise an array with the categories uid.
* @param int $offset
* @param bool $hideNeverHighlightedNews
* @param string $sortBy date or positionInTree
* @return QueryResult
*/
public function findLastUpdatedOrHighlightedNewsByCategories(
$limit = 1, $onlyHighlighted = FALSE, array $categoryIds = NULL, $offset = 0, $hideNeverHighlightedNews = FALSE
$limit = 1, $onlyHighlighted = FALSE, array $categoryIds = NULL,
$offset = 0, $hideNeverHighlightedNews = FALSE, $sortBy = 'date'
) {
$query = $this->createQuery();
$constraints = NULL;
......@@ -116,13 +130,24 @@ class NewsRepository extends AbstractRepository {
$constraints[] = $query->equals('tx_sgnews_never_highlighted', 0);
}
$query->setOrderings(
[
'tx_sgnews_highlighted' => QueryInterface::ORDER_DESCENDING,
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
if ($sortBy === 'date') {
$query->setOrderings(
[
'tx_sgnews_highlighted' => QueryInterface::ORDER_DESCENDING,
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
} else {
$query->setOrderings(
[
'tx_sgnews_highlighted' => QueryInterface::ORDER_DESCENDING,
'sorting' => QueryInterface::ORDER_ASCENDING,
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
}
if ($constraints !== NULL) {
$constraints = $query->logicalAnd($constraints);
......@@ -139,39 +164,46 @@ class NewsRepository extends AbstractRepository {
return $query->matching($constraints)->execute();
}
/**
* Method returns the random News of the given amount.
*
* @param int $limit
* @param News $excludeNews
* @return QueryResult
*/
public function findRandomNews($limit = 0, News $excludeNews = NULL) {
$query = $this->createQuery();
$excludeClause = ($excludeNews ? 'AND uid != ' . $excludeNews->getUid() : '');
$statement = 'SELECT * FROM pages WHERE doktype = 116 ' . $excludeClause .
$this->getEnableFieldsStatement('pages') . ' ORDER BY RAND()';
if ($limit > 0) {
$statement .= ' LIMIT ' . $limit;
}
/** @noinspection PhpUndefinedMethodInspection */
return $query->statement($statement)->execute();
}
// /**
// * Method returns the random News of the given amount.
// *
// * @param int $limit
// * @param News $excludeNews
// * @return QueryResult
// */
// public function findRandomNews($limit = 0, News $excludeNews = NULL) {
// $query = $this->createQuery();
// $excludeClause = ($excludeNews ? 'AND uid != ' . $excludeNews->getUid() : '');
// $statement = 'SELECT * FROM pages WHERE doktype = 116 ' . $excludeClause .
// $this->getEnableFieldsStatement('pages') . ' ORDER BY RAND()';
//
// if ($limit > 0) {
// $statement .= ' LIMIT ' . $limit;
// }
//
// /** @noinspection PhpUndefinedMethodInspection */
// return $query->statement($statement)->execute();
// }
/**
* Method returns the next News of the given news.
*
* @param News $news
* @param string $sortBy date or positionInTree
* @return QueryResult
*/
public function findNextNewsEntryFromCurrentNews(News $news) {
public function findNextNewsEntryFromCurrentNews(News $news, $sortBy = 'date') {
$query = $this->createQuery();
if ($sortBy === 'date') {
$orderBy = 'ORDER BY lastUpdated DESC, crdate DESC';
} else {
$orderBy = 'ORDER BY sorting ASC, lastUpdated DESC, crdate DESC';
}
$statement = 'SELECT * FROM pages WHERE uid =
(SELECT MIN(uid) FROM pages WHERE pid = ' . $news->getPid() . ' AND uid > ' . $news->getUid() .
$this->getEnableFieldsStatement('pages') . ' ORDER BY lastUpdated DESC, crdate DESC)' .
$this->getEnableFieldsStatement('pages') . ' ' . $orderBy . ')' .
$this->getEnableFieldsStatement('pages');
/** @noinspection PhpUndefinedMethodInspection */
......@@ -182,19 +214,24 @@ class NewsRepository extends AbstractRepository {
* Method returns the previous News of the given news.
*
* @param News $news
* @param string $sortBy date or positionInTree
* @return QueryResult
*/
public function findPreviousNewsEntryFromCurrentNews(News $news) {
public function findPreviousNewsEntryFromCurrentNews(News $news, $sortBy = 'date') {
$query = $this->createQuery();
if ($sortBy === 'date') {
$orderBy = 'ORDER BY lastUpdated DESC, crdate DESC';
} else {
$orderBy = 'ORDER BY sorting ASC, lastUpdated DESC, crdate DESC';
}
$statement = 'SELECT * FROM pages WHERE uid =
(SELECT MAX(uid) FROM pages WHERE pid = ' . $news->getPid() . ' AND uid < ' . $news->getUid() .
$this->getEnableFieldsStatement('pages') . ' ORDER BY lastUpdated DESC, crdate DESC)' .
$this->getEnableFieldsStatement('pages') . ' ' . $orderBy . ')' .
$this->getEnableFieldsStatement('pages');
/** @noinspection PhpUndefinedMethodInspection */
return $query->statement($statement)->execute();
}
}
?>
......@@ -55,9 +55,17 @@ plugin.tx_sgnews {
}
settings {
# Name of your site - required for the news.xml
siteName =
# Your rootpage id - required for the news.xml to retrieve the base path
rootpageId = 1
# News limit (can be usually set also inside the flexforms)
newsLimitPerPage = {$plugin.tx_sgnews.settings.newsLimitPerPage}
# How to sort the news in general (date, positionInTree)
sortBy = date
}
features {
......
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