Newer
Older
<?php
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!
***************************************************************/
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Service\HeaderMetaDataService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
/**
* Controller that handles the overview page of categories and their news
*/
class OverviewController extends AbstractController {
/**
* @inject
* @var \SGalinski\SgNews\Domain\Repository\CategoryRepository
*/
protected $categoryRepository;
/**
* @inject
* @var \SGalinski\SgNews\Domain\Repository\TagRepository
*/
protected $tagRepository;
/**
* @inject
* @var \SGalinski\SgNews\Domain\Repository\NewsRepository
*/
protected $newsRepository;
/**
* Renders the news overview
*
* @return void
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
switch ((int) $this->settings['groupBy']) {
case 1:
$this->overviewWithCategories();
break;
case 2:
$this->overviewWithTags();
break;
default:
$this->forward('overviewWithoutCategories');
break;
/**
* Highlights the best fitting news in the meta data of the page
*
* @param array $categoryIds
* @param array $tagIds
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
protected function highlightBestFitNews(array $categoryIds = NULL, array $tagIds = NULL) {
/** @var News $highlightedNews */
$highlightedNews = $this->newsRepository
->findLastUpdatedOrHighlightedNewsByCategories(
1, FALSE, $categoryIds, 0, FALSE, $this->settings['sortBy'], $tagIds
)->getFirst();
if (!$highlightedNews) {
return;
}
/** @var Category $category */
$category = $this->categoryRepository->findByUid($highlightedNews->getPid());
$highlightedNewsMetaData = NULL;
if ($category) {
$highlightedNewsMetaData = $this->getMetaDataForNews($highlightedNews, $category);
}
if ($highlightedNewsMetaData['image']) {
HeaderMetaDataService::addOgImageToHeader($highlightedNewsMetaData['image']);
} elseif ($highlightedNewsMetaData['teaserImage']) {
HeaderMetaDataService::addOgImageToHeader($highlightedNewsMetaData['teaserImage']);
}
}
/**
* Renders the news overview grouped by categories
*
* @return void
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
protected function overviewWithCategories() {
$newsLimitPerCategory = (int) $this->settings['newsLimit'];
$this->categoryRepository->setDefaultOrderings(['sorting' => Query::ORDER_ASCENDING]);
$offset = 0;
$currentPageBrowserPage = (int) GeneralUtility::_GP('tx_sgnews_pagebrowser')['currentPage'];
if ($currentPageBrowserPage && $newsLimitPerCategory) {
$offset = $currentPageBrowserPage * $newsLimitPerCategory;
}
if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id);
} else {
$categories = $this->categoryRepository->findAll();
}
$categoryIds = [];
$categoriesById = [];
$newsMetaData = [];
/** @var $category Category */
$categoryIds[] = $category->getUid();
$categoriesById[$categoryId] = $category;
$news = $this->newsRepository->findAllSortedNewsByCategories(
[$categoryId], $newsLimitPerCategory, $offset, $this->settings['sortBy']
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
$category = $categoriesById[$categoryId];
$newsMetaData[$categoryId][] = $data;
$newsByCategory = [];
foreach ($categoriesById as $categoryId => $category) {
/** @var $category Category */
'record' => $category,
'recordId' => $categoryId,
'recordType' => 'category',
'newsMetaData' => $newsMetaData[$categoryId]
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsLimitPerCategory, $offset, $this->settings['sortBy']
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
$category = $categoriesById[$categoryId];
$data = $this->getMetaDataForNews($newsEntry, $category);
$allNews[] = $data;
}
$this->highlightBestFitNews($categoryIds);
$newsCount = $this->newsRepository->newsCountByCategories($categoryIds);
$numberOfPages = ($newsLimitPerCategory <= 0 ? 0 : ceil($newsCount / $newsLimitPerCategory));
$this->view->assign('numberOfPages', $numberOfPages);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
$this->view->assign('newsItems', $newsByCategory);
$this->view->assign('groupBy', 'category');
$this->view->assign('allNews', $allNews);
}
/**
* Renders the news overview grouped by tags
*
* @return void
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
protected function overviewWithTags() {
$newsLimitPerTag = (int) $this->settings['newsLimit'];
$this->tagRepository->setDefaultOrderings(['sorting' => Query::ORDER_ASCENDING]);
$offset = 0;
$currentPageBrowserPage = (int) GeneralUtility::_GP('tx_sgnews_pagebrowser')['currentPage'];
if ($currentPageBrowserPage && $newsLimitPerTag) {
$offset = $currentPageBrowserPage * $newsLimitPerTag;
}
$tagPid = (int) $this->settings['tagPid'];
if(!$tagPid){
$tagPid = $GLOBALS['TSFE']->id;
}
if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */
$tags = $this->tagRepository->findByPid($tagPid);
} else {
$tags = $this->tagRepository->findAll();
}
$tagIds = [];
$tagsById = [];
$categoriesById = [];
$newsMetaData = [];
foreach ($tags as $tag) {
/** @var $tag Tag */
$tagId = $tag->getUid();
$tagIds[] = $tagId;
$tagsById[$tagId] = $tag;
$news = $this->newsRepository->findAllSortedNewsByCategories(
NULL, $newsLimitPerTag, $offset, $this->settings['sortBy'], [$tagId]
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
if (!isset($categoriesById[$categoryId])) {
$categoriesById[$categoryId] = $this->categoryRepository->findByUid($categoryId);
}
$category = $categoriesById[$categoryId];
$data = $this->getMetaDataForNews($newsEntry, $category);
$newsMetaData[$tagId][] = $data;
}
}
$newsByTag = [];
foreach ($tagsById as $tagId => $tag) {
/** @var $category Category */
$newsByTag[$tagId] = [
'record' => $tag,
'recordId' => $tagId,
'recordType' => 'tag',
'newsMetaData' => $newsMetaData[$tagId]
];
}
$allNews = [];
$news = $this->newsRepository->findAllSortedNewsByCategories(
NULL, $newsLimitPerTag, $offset, $this->settings['sortBy'], $tagIds
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$categoryId = $newsEntry->getPid();
if (!isset($categoriesById[$categoryId])) {
$categoriesById[$categoryId] = $this->categoryRepository->findByUid($categoryId);
}
$category = $categoriesById[$categoryId];
$data = $this->getMetaDataForNews($newsEntry, $category);
$allNews[] = $data;
}
$this->highlightBestFitNews([], $tagIds);
$newsCount = $this->newsRepository->newsCountByCategories([], $tagIds);
$numberOfPages = ($newsLimitPerTag <= 0 ? 0 : ceil($newsCount / $newsLimitPerTag));
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsItems', $newsByTag);
$this->view->assign('groupBy', 'tag');
$this->view->assign('allNews', $allNews);
/**
* Renders the news in a paginated list
*
* @return void
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
protected function overviewWithoutCategoriesAction() {
$offset = 0;
$newsPerPage = (int) $this->settings['newsLimit'];
$currentPageBrowserPage = (int) GeneralUtility::_GP('tx_sgnews_pagebrowser')['currentPage'];
if ($currentPageBrowserPage && $newsPerPage) {
$offset = ($currentPageBrowserPage * $newsPerPage) - 1;
if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id);
$categoryIds = [];
foreach ($categories as $category) {
/** @var $category Category */
$categoryId = $category->getUid();
$categoryIds[] = $categoryId;
$categoriesById[$categoryId] = $category;
}
$newsCount = $this->newsRepository->newsCountByCategories($categoryIds);
} else {
$newsCount = $this->newsRepository->countAll();
$categoryIds = NULL;
$categoriesById = [];
$categories = $this->categoryRepository->findAll();
foreach ($categories as $category) {
/** @var $category Category */
$categoriesById[$category->getUid()] = $category;
}
}
$newsMetaData = [];
$news = $this->newsRepository->findAllSortedNewsByCategories(
$categoryIds, $newsPerPage, $offset, $this->settings['sortBy']
);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$data = $this->getMetaDataForNews($newsEntry, $categoriesById[$newsEntry->getPid()]);
$newsMetaData[] = $data;
}
$this->highlightBestFitNews($categoryIds);
$numberOfPages = ($newsPerPage <= 0 ? 0 : ceil($newsCount / $newsPerPage));
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsMetaData', $newsMetaData);
}
}