Newer
Older
<?php
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!
***************************************************************/
use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
/**
* News Repository
*/
class NewsRepository extends AbstractRepository {
/**
* Method returns all news by category id sorted by the field lastUpdated.
*
* @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 findAllSortedNewsByCategories($categoryIds = NULL, $limit = 0, $offset = 0) {
$categoryConstraint = [];
if ($categoryIds !== NULL && is_array($categoryIds)) {
$categoryConstraint[] = $query->in('pid', $categoryIds);
}
if ($limit > 0) {
$query->setLimit($limit);
}
if ($offset > 0) {
$query->setOffset($offset);
}
$query->setOrderings(
[
'lastUpdated' => QueryInterface::ORDER_DESCENDING,
'crdate' => QueryInterface::ORDER_DESCENDING,
]
);
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();
}
/**
* Method returns the last updated news by category id which is highlighted.
*
* @param int $limit
* @param bool $onlyHighlighted
* @param array $categoryIds NULL, if the category filter isn't applied, otherwise an array with the categories uid.
* @param int $offset
Fabian Galinski
committed
* @param bool $hideNeverHighlightedNews
public function findLastUpdatedOrHighlightedNewsByCategories(
Fabian Galinski
committed
$limit = 1, $onlyHighlighted = FALSE, $categoryIds = NULL, $offset = 0, $hideNeverHighlightedNews = FALSE
Stefan Galinski
committed
if ($categoryIds !== NULL && count($categoryIds)) {
$constraints[] = $query->in('pid', $categoryIds);
}
if ($onlyHighlighted) {
$constraints[] = $query->equals('tx_sgnews_highlighted', 1);
}
Fabian Galinski
committed
if ($hideNeverHighlightedNews) {
$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 ($constraints !== NULL) {
$constraints = $query->logicalAnd($constraints);
}
if ($limit > 0) {
$query->setLimit($limit);
}
if ($offset > 0) {
$query->setOffset($offset);
}
return $query->matching($constraints)->execute();
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}
/**
* 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
* @return QueryResult
*/
public function findNextNewsEntryFromCurrentNews(News $news) {
$query = $this->createQuery();
$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');
/** @noinspection PhpUndefinedMethodInspection */
return $query->statement($statement)->execute();
}
/**
* Method returns the previous News of the given news.
*
* @param News $news
* @return QueryResult
*/
public function findPreviousNewsEntryFromCurrentNews(News $news) {
$query = $this->createQuery();
$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');
/** @noinspection PhpUndefinedMethodInspection */
return $query->statement($statement)->execute();
}
}