Skip to content
Snippets Groups Projects
Commit 2168e5ac authored by Fabian Galinski's avatar Fabian Galinski :pouting_cat:
Browse files

[FEATUR] Adds the possibility to choose categories for the news plugins

parent 265bc232
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ namespace SGalinski\SgNews\Controller;
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Controller that shows the latest news
......@@ -54,8 +55,17 @@ class LatestController extends AbstractController {
$limit = ((int) $this->settings['limit']);
$limit = ($limit < 1 ? 1 : $limit);
$latestNewsEntries = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategory(
$limit, FALSE, (int) $this->settings['categoryId']
$filterByCategories = FALSE;
$categoryUids = GeneralUtility::intExplode(',', $this->settings['categories']);
foreach ($categoryUids as $categoryUid) {
if ($categoryUid > 0) {
$filterByCategories = TRUE;
break;
}
}
$latestNewsEntries = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategories(
$limit, FALSE, ($filterByCategories ? $categoryUids : NULL)
);
$newsMetaData = [];
......
......@@ -53,31 +53,39 @@ class ListByCategoryController extends AbstractController {
* @return void
*/
public function indexAction() {
/** @var Category $selectedCategory */
$selectedCategoryId = (int) $GLOBALS['TSFE']->id;
$selectedCategory = $this->categoryRepository->findByUid($selectedCategoryId);
if (!$selectedCategory) {
return;
$filterByCategories = FALSE;
$categoryUids = GeneralUtility::intExplode(',', $this->settings['categories']);
foreach ($categoryUids as $categoryUid) {
if ($categoryUid > 0) {
$filterByCategories = TRUE;
break;
}
}
if (!$filterByCategories) {
$categoryUids = [(int) $GLOBALS['TSFE']->id];
}
$categories = [];
foreach ($categoryUids as $categoryUid) {
$categories[$categoryUid] = $this->categoryRepository->findByUid($categoryUid);
}
$offset = 0;
$newsPerPage = (int) $this->settings['newsLimitPerPage'];
$currentPageBrowserPage = (int) GeneralUtility::_GP('tx_pagebrowse_pi1')['page'];
if ($currentPageBrowserPage && $newsPerPage) {
$offset = $currentPageBrowserPage * $newsPerPage;
}
/** @noinspection PhpUndefinedMethodInspection */
$newsCount = $this->newsRepository->countByPid($selectedCategoryId);
$newsCount = $this->newsRepository->newsCountByCategories($categoryUids);
$numberOfPages = ceil($newsCount / $newsPerPage);
$news = $this->newsRepository->findAllSortedNewsByCategory($selectedCategoryId, $newsPerPage, $offset);
$news = $news->toArray();
$highlightedNews = NULL;
$news = $this->newsRepository->findAllSortedNewsByCategories($categoryUids, $newsPerPage, $offset);
$news = $news->toArray();
if ($currentPageBrowserPage <= 1) {
$highlightedNews = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategory(
1, FALSE, $selectedCategoryId
)->getFirst();
$highlightedNews = $this->newsRepository
->findLastUpdatedOrHighlightedNewsByCategories(1, FALSE, $categoryUids)->getFirst();
if ($highlightedNews && !in_array($highlightedNews, $news)) {
$news[] = $highlightedNews;
}
......@@ -86,7 +94,7 @@ class ListByCategoryController extends AbstractController {
$newsMetaData = $highlightedNewsMetaData = [];
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$data = $this->getMetaDataForNews($newsEntry, $selectedCategory);
$data = $this->getMetaDataForNews($newsEntry, $categories[$newsEntry->getPid()]);
if ($newsEntry === $highlightedNews) {
$highlightedNewsMetaData = $data;
continue;
......@@ -102,7 +110,7 @@ class ListByCategoryController extends AbstractController {
$this->view->assign('numberOfPages', $numberOfPages);
$this->view->assign('newsMetaData', $newsMetaData);
$this->view->assign('category', $selectedCategory);
$this->view->assign('categories', $categories);
$this->view->assign('highlightedNewsMetaData', $highlightedNewsMetaData);
}
}
......
......@@ -42,9 +42,9 @@ class NewsFeedController extends AbstractController {
* @return void
*/
public function indexAction() {
$news = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategory(10, FALSE);
$news = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategories(10, FALSE);
$this->view->assign('news', $news);
}
}
?>
\ No newline at end of file
?>
......@@ -70,13 +70,19 @@ class OverviewController extends AbstractController {
$newsByCategory = [];
$newsLimitPerCategory = (int) $this->settings['newsLimit'];
$this->categoryRepository->setDefaultOrderings(['sorting' => Query::ORDER_ASCENDING]);
$categories = $this->categoryRepository->findAll();
if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id);
} else {
$categories = $this->categoryRepository->findAll();
}
foreach ($categories as $category) {
/** @var Category $category */
$categoryId = $category->getUid();
$newsMetaData = [];
$news = $this->newsRepository->findAllSortedNewsByCategory($categoryId, $newsLimitPerCategory);
$news = $this->newsRepository->findAllSortedNewsByCategories([$categoryId], $newsLimitPerCategory);
foreach ($news as $newsEntry) {
/** @var News $newsEntry */
$data = $this->getMetaDataForNews($newsEntry, $category);
......@@ -93,7 +99,7 @@ class OverviewController extends AbstractController {
}
/** @var News $highlightedNews */
$highlightedNews = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategory(1, FALSE)->getFirst();
$highlightedNews = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategories(1, FALSE)->getFirst();
$category = $newsByCategory[$highlightedNews->getPid()]['category'];
$highlightedNewsMetaData = NULL;
if ($category) {
......@@ -126,10 +132,26 @@ class OverviewController extends AbstractController {
/** @noinspection PhpUndefinedMethodInspection */
$newsCount = $this->newsRepository->countAll();
$numberOfPages = ceil($newsCount / $newsPerPage);
$news = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategory($newsPerPage, FALSE, 0, $offset);
if ($this->settings['onlyNewsWithinThisPageSection']) {
/** @noinspection PhpUndefinedMethodInspection */
$categories = $this->categoryRepository->findByPid($GLOBALS['TSFE']->id);
$categoryIds = [];
foreach ($categories as $category) {
/** @var $category Category */
$categoryIds[] = $category->getUid();
}
$news = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategories(
$newsPerPage, FALSE, $categoryIds, $offset
);
} else {
$categories = $this->categoryRepository->findAll();
$news = $this->newsRepository->findLastUpdatedOrHighlightedNewsByCategories(
$newsPerPage, FALSE, NULL, $offset
);
}
$categoriesById = [];
$categories = $this->categoryRepository->findAll();
foreach ($categories as $category) {
/** @var $category Category */
$categoriesById[$category->getUid()] = $category;
......
......@@ -26,6 +26,7 @@ namespace SGalinski\SgNews\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Domain\Model\Category;
use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
......@@ -37,17 +38,17 @@ class NewsRepository extends AbstractRepository {
/**
* Method returns all news by category id sorted by the field lastUpdated.
*
* @param int $categoryId
* @param array $categoryIds NULL, if the category filter isn't applied, otherwise an array with the categories uid.
* @param int $limit
* @param int $offset
* @return QueryResult
*/
public function findAllSortedNewsByCategory($categoryId = 0, $limit = 0, $offset = 0) {
public function findAllSortedNewsByCategories($categoryIds = NULL, $limit = 0, $offset = 0) {
$query = $this->createQuery();
$categoryConstraint = NULL;
if ($categoryId > 0) {
$categoryConstraint = $query->equals('pid', $categoryId);
$categoryConstraint = [];
if ($categoryIds !== NULL && is_array($categoryIds)) {
$categoryConstraint[] = $query->in('pid', $categoryIds);
}
if ($limit > 0) {
......@@ -64,7 +65,24 @@ class NewsRepository extends AbstractRepository {
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
return $query->matching($categoryConstraint)->execute();
return $query->matching($query->logicalAnd($categoryConstraint))->execute();
}
/**
* Returns the count of all news within the given category ids.
*
* @param array $categoryIds NULL, if the category filter isn't applied, otherwise an array with the categories uid.
* @return int
*/
public function newsCountByCategories($categoryIds = NULL) {
$query = $this->createQuery();
$categoryConstraint = [];
if ($categoryIds !== NULL && is_array($categoryIds)) {
$categoryConstraint[] = $query->in('pid', $categoryIds);
}
return $query->matching($query->logicalAnd($categoryConstraint))->count();
}
/**
......@@ -72,17 +90,17 @@ class NewsRepository extends AbstractRepository {
*
* @param int $limit
* @param bool $onlyHighlighted
* @param int $categoryId 0 if the category filter isn't applied
* @param array $categoryIds NULL, if the category filter isn't applied, otherwise an array with the categories uid.
* @param int $offset
* @return QueryResult
*/
public function findLastUpdatedOrHighlightedNewsByCategory(
$limit = 1, $onlyHighlighted = FALSE, $categoryId = 0, $offset = 0
public function findLastUpdatedOrHighlightedNewsByCategories(
$limit = 1, $onlyHighlighted = FALSE, $categoryIds = NULL, $offset = 0
) {
$query = $this->createQuery();
$constraints = NULL;
if ($categoryId > 0) {
$constraints[] = $query->equals('pid', $categoryId);
if ($categoryIds !== NULL && is_array($categoryIds)) {
$constraints[] = $query->in('pid', $categoryIds);
}
if ($onlyHighlighted) {
......@@ -170,4 +188,4 @@ class NewsRepository extends AbstractRepository {
}
}
?>
\ No newline at end of file
?>
......@@ -21,6 +21,20 @@
</config>
</TCEforms>
</settings.limit>
<settings.categories>
<TCEforms>
<label>LLL:EXT:sg_news/Resources/Private/Language/locallang_db.xlf:plugin.overview.flexForm.categories</label>
<config>
<type>select</type>
<renderType>selectMultipleSideBySide</renderType>
<size>5</size>
<maxitems>99</maxitems>
<foreign_table>pages</foreign_table>
<foreign_table_where>AND doktype = 117 ORDER BY title</foreign_table_where>
</config>
</TCEforms>
</settings.categories>
</el>
</ROOT>
</main>
......
......@@ -21,8 +21,22 @@
</config>
</TCEforms>
</settings.newsLimitPerPage>
<settings.categories>
<TCEforms>
<label>LLL:EXT:sg_news/Resources/Private/Language/locallang_db.xlf:plugin.overview.flexForm.listByCategory.categories</label>
<config>
<type>select</type>
<renderType>selectMultipleSideBySide</renderType>
<size>5</size>
<maxitems>99</maxitems>
<foreign_table>pages</foreign_table>
<foreign_table_where>AND doktype = 117 ORDER BY title</foreign_table_where>
</config>
</TCEforms>
</settings.categories>
</el>
</ROOT>
</main>
</sheets>
</T3DataStructure>
\ No newline at end of file
</T3DataStructure>
......@@ -30,8 +30,17 @@
</config>
</TCEforms>
</settings.newsLimit>
<settings.onlyNewsWithinThisPageSection>
<TCEforms>
<label>LLL:EXT:sg_news/Resources/Private/Language/locallang_db.xlf:plugin.overview.flexForm.onlyNewsWithinThisPageSection</label>
<config>
<type>check</type>
<default>0</default>
</config>
</TCEforms>
</settings.onlyNewsWithinThisPageSection>
</el>
</ROOT>
</main>
</sheets>
</T3DataStructure>
\ No newline at end of file
</T3DataStructure>
......@@ -77,6 +77,10 @@
<source>Settings</source>
<target>Einstellungen</target>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.categories" approved="yes">
<source>Categories, if none is selected, then all will be displayed</source>
<target>Kategorien, wenn keine ausgewählt wird, dann werden alle angezeigt</target>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.groupByCategories" approved="yes">
<source>Group by categories</source>
<target>Nach Kategorien gruppieren</target>
......@@ -89,6 +93,14 @@
<source>News Limit per Page</source>
<target>News-Limit pro Seite</target>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.onlyNewsWithinThisPageSection" approved="yes">
<source>Only news within this page section</source>
<target>Nur News innerhalb dieser Seiten-Sektion</target>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.listByCategory.categories" approved="yes">
<source>Categories, if none is selected, then the this page is used as category</source>
<target>Kategorien, wenn keine ausgewählt wird, dann wird diese Seite als Kategorie genutzt</target>
</trans-unit>
</body>
</file>
</xliff>
\ No newline at end of file
</xliff>
......@@ -60,6 +60,9 @@
<trans-unit id="plugin.flexForm">
<source>Settings</source>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.categories">
<source>Categories, if none is selected, then all will be displayed</source>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.groupByCategories">
<source>Group by categories</source>
</trans-unit>
......@@ -69,6 +72,12 @@
<trans-unit id="plugin.overview.flexForm.newsLimitPerPage">
<source>News Limit per Page</source>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.onlyNewsWithinThisPageSection">
<source>Only news within this page section</source>
</trans-unit>
<trans-unit id="plugin.overview.flexForm.listByCategory.categories">
<source>Categories, if none is selected, then the this page is used as category</source>
</trans-unit>
</body>
</file>
</xliff>
\ No newline at end of file
</xliff>
......@@ -11,7 +11,9 @@
<div class="ce-textpic ce-left ce-above">
<div class="ce-bodytext">
<div class="spacer-60"></div>
<h2 style="text-align: center" class="tx-sgnews-category-title">{category.subtitleWithFallbackToTitle}</h2>
<f:for each="{categories}" as="category">
<h2 style="text-align: center" class="tx-sgnews-category-title">{category.subtitleWithFallbackToTitle}</h2>
</f:for>
<div class="spacer-60"></div>
</div>
</div>
......@@ -32,7 +34,7 @@
newsMetaData: highlightedNewsMetaData,
headerTag: '<h2>',
closingHeaderTag: '</h2>',
showCategory: 0
showCategory: 1
}" />
</li>
</f:if>
......@@ -42,7 +44,7 @@
newsMetaData: newsMetaDataEntry,
headerTag: '<h2>',
closingHeaderTag: '</h2>',
showCategory: 0
showCategory: 1
}" />
</li>
</f:for>
......
......@@ -19,7 +19,7 @@ $EM_CONF[$_EXTKEY] = array(
'modify_tables' => '',
'clearCacheOnLoad' => 0,
'lockType' => '',
'version' => '2.0.0',
'version' => '2.1.0',
'constraints' => array(
'depends' => array(
'typo3' => '7.6.0-7.6.99',
......
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