From a7d90462f50fa4b6620cadb36db7aec3176b9517 Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Tue, 26 Jul 2016 20:14:23 +0200
Subject: [PATCH] [FEATURE] Implement a sorting setting (values: date,
 positionInTree)

---
 Classes/Controller/AbstractController.php     |   2 -
 Classes/Controller/LatestController.php       |   2 +-
 .../Controller/ListByCategoryController.php   |   8 +-
 Classes/Controller/NewsFeedController.php     |  48 ++++---
 Classes/Controller/OverviewController.php     |  16 ++-
 Classes/Controller/PageBrowserController.php  |   2 -
 Classes/Controller/SingleViewController.php   |  47 ++++---
 Classes/Domain/Model/Category.php             |  48 ++++---
 Classes/Domain/Model/CategoryAndNews.php      |   2 -
 Classes/Domain/Model/News.php                 |   2 -
 .../Domain/Repository/CategoryRepository.php  |  48 ++++---
 Classes/Domain/Repository/NewsRepository.php  | 119 ++++++++++++------
 Configuration/TypoScript/Frontend/setup.txt   |   8 ++
 13 files changed, 193 insertions(+), 159 deletions(-)

diff --git a/Classes/Controller/AbstractController.php b/Classes/Controller/AbstractController.php
index 2e5efc7..da22512 100644
--- a/Classes/Controller/AbstractController.php
+++ b/Classes/Controller/AbstractController.php
@@ -187,5 +187,3 @@ abstract class AbstractController extends ActionController {
 		];
 	}
 }
-
-?>
diff --git a/Classes/Controller/LatestController.php b/Classes/Controller/LatestController.php
index 1cd7fda..4a8ad8e 100644
--- a/Classes/Controller/LatestController.php
+++ b/Classes/Controller/LatestController.php
@@ -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 = [];
diff --git a/Classes/Controller/ListByCategoryController.php b/Classes/Controller/ListByCategoryController.php
index 98f43db..c7ff9e6 100644
--- a/Classes/Controller/ListByCategoryController.php
+++ b/Classes/Controller/ListByCategoryController.php
@@ -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);
 	}
 }
-
-?>
diff --git a/Classes/Controller/NewsFeedController.php b/Classes/Controller/NewsFeedController.php
index 47bf50b..1c130c4 100644
--- a/Classes/Controller/NewsFeedController.php
+++ b/Classes/Controller/NewsFeedController.php
@@ -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);
 	}
 }
-
-?>
diff --git a/Classes/Controller/OverviewController.php b/Classes/Controller/OverviewController.php
index 3fd10ca..e0a2775 100644
--- a/Classes/Controller/OverviewController.php
+++ b/Classes/Controller/OverviewController.php
@@ -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()]);
diff --git a/Classes/Controller/PageBrowserController.php b/Classes/Controller/PageBrowserController.php
index 83df43c..5f3371c 100644
--- a/Classes/Controller/PageBrowserController.php
+++ b/Classes/Controller/PageBrowserController.php
@@ -143,5 +143,3 @@ class PageBrowserController extends ActionController {
 		return $this->uriBuilder->reset()->setUseCacheHash(FALSE)->uriFor('index', ['currentPage' => $page,]);
 	}
 }
-
-?>
diff --git a/Classes/Controller/SingleViewController.php b/Classes/Controller/SingleViewController.php
index 1e89a61..fb748fa 100644
--- a/Classes/Controller/SingleViewController.php
+++ b/Classes/Controller/SingleViewController.php
@@ -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,
 			]
 		);
 	}
 }
-
-?>
diff --git a/Classes/Domain/Model/Category.php b/Classes/Domain/Model/Category.php
index f30ad32..5ab70c4 100644
--- a/Classes/Domain/Model/Category.php
+++ b/Classes/Domain/Model/Category.php
@@ -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 {
 }
-
-?>
diff --git a/Classes/Domain/Model/CategoryAndNews.php b/Classes/Domain/Model/CategoryAndNews.php
index e14b071..d4d4f03 100644
--- a/Classes/Domain/Model/CategoryAndNews.php
+++ b/Classes/Domain/Model/CategoryAndNews.php
@@ -227,5 +227,3 @@ abstract class CategoryAndNews extends AbstractEntity {
 		$this->sorting = (int) $sorting;
 	}
 }
-
-?>
diff --git a/Classes/Domain/Model/News.php b/Classes/Domain/Model/News.php
index 6d54485..51c56a4 100644
--- a/Classes/Domain/Model/News.php
+++ b/Classes/Domain/Model/News.php
@@ -240,5 +240,3 @@ class News extends CategoryAndNews {
 		return $this->authorFrontendUser;
 	}
 }
-
-?>
diff --git a/Classes/Domain/Repository/CategoryRepository.php b/Classes/Domain/Repository/CategoryRepository.php
index 4680cf3..41af146 100644
--- a/Classes/Domain/Repository/CategoryRepository.php
+++ b/Classes/Domain/Repository/CategoryRepository.php
@@ -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 {
 }
-
-?>
diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php
index 1fdbf4e..eb43556 100644
--- a/Classes/Domain/Repository/NewsRepository.php
+++ b/Classes/Domain/Repository/NewsRepository.php
@@ -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();
 	}
 }
-
-?>
diff --git a/Configuration/TypoScript/Frontend/setup.txt b/Configuration/TypoScript/Frontend/setup.txt
index 5d219fd..e4ccb4f 100644
--- a/Configuration/TypoScript/Frontend/setup.txt
+++ b/Configuration/TypoScript/Frontend/setup.txt
@@ -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 {
-- 
GitLab