Skip to content
Snippets Groups Projects
To find the state of this project's repository at the time of any of these versions, check out the tags.
NewsByAuthorController.php 3.10 KiB
<?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\News;
use SGalinski\SgNews\Domain\Repository\AuthorRepository;
use SGalinski\SgNews\Domain\Repository\CategoryRepository;
use SGalinski\SgNews\Domain\Repository\NewsRepository;
use SGalinski\SgNews\Service\HeaderMetaDataService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Controller that handles the news single view page
 */
class NewsByAuthorController extends AbstractController {
	/**
	 * Renders the news author list.
	 *
	 * @param int $authorId
	 * @return void
	 * @throws \InvalidArgumentException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	 */
	public function listAction($authorId) {
		if ($authorId) {
			$newsAuthorsIds[] = $authorId;
		} else {
			$newsAuthorsIds = GeneralUtility::intExplode(',', $this->settings['newsAuthors']);
			if (\count($newsAuthorsIds) <= 0) {
				return;
			}
		}

		$authors = [];
		$authorRepository = $this->objectManager->get(AuthorRepository::class);
		foreach ($newsAuthorsIds as $newsAuthorsId) {
			$author = $authorRepository->findByUid($newsAuthorsId);
			if (!$author) {
				continue;
			}

			$authors[] = $author;
		}

		if (\count($authors) <= 0) {
			return;
		}

		$newsRepository = $this->objectManager->get(NewsRepository::class);
		$news = $newsRepository->findAllByNewsAuthor($newsAuthorsIds);
		if (\count($news) <= 0) {
			return;
		}

		$categories = $newsMetaData = [];
		$categoryRepository = $this->objectManager->get(CategoryRepository::class);
		$excludedNewsIds = GeneralUtility::intExplode(',', $this->settings['excludedNews']);
		foreach ($news as $newsEntry) {
			/** @var News $newsEntry */
			if (in_array($newsEntry->getUid(), $excludedNewsIds, TRUE)) {
				continue;
			}

			$categoryId = $newsEntry->getPid();
			if (!isset($categories[$categoryId])) {
				$category = $categoryRepository->findByUid($categoryId);
				if (!$category) {
					continue;
				}

				$categories[$categoryId] = $category;
			}

			$newsMetaData[] = $this->getMetaDataForNews($newsEntry, $categories[$categoryId]);
		}

		$this->view->assign('newsMetaData', $newsMetaData);
		$this->view->assign('authors', $authors);
	}
}