19declare(strict_types=1);
39 protected readonly
Factory $factory
45 $query =
"SELECT * FROM il_news_item WHERE id = %s";
48 return $result->numRows()
49 ? $this->
factory->newsItem($this->db->fetchAssoc($result))
59 if (empty($news_ids)) {
64 return array_map(fn($row) => $this->
factory->newsItem($row), $this->db->fetchAll($result));
73 public function loadLazyItems(array $news_ids, array $group_context_types): array
75 if (empty($news_ids)) {
81 $additional_obj_ids = [];
83 foreach ($this->db->fetchAll($result) as $row) {
84 $news_item = $this->
factory->newsItem($row);
86 if (in_array($news_item->getContextObjType(), $group_context_types)) {
87 $additional_obj_ids[] = $news_item->getContextObjId();
90 $news_items[] = $news_item;
93 if (empty($additional_obj_ids)) {
101 $result = $this->db->query($query);
105 array_map(fn($row) => $this->
factory->newsItem($row), $this->db->fetchAll($result))
112 SELECT il_news_item.*,
114 (SELECT ref_id FROM object_reference WHERE object_reference.obj_id = il_news_item.context_obj_id LIMIT 1),
119 if ($news_ids !==
null) {
131 if (empty($contexts)) {
136 $result = $this->db->queryF(...$this->
buildBatchQuery($obj_ids, $criteria));
141 while ($row = $this->db->fetchAssoc($result)) {
142 $items[] = $this->
factory->newsItem($row);
143 $user_read[$row[
'id']] = isset($row[
'user_read']) && $row[
'user_read'] !== 0;
148 $collection->setUserReadStatus($criteria->
getReadUserId(), $user_read);
159 if (empty($contexts)) {
164 $result = $this->db->queryF(...$this->
buildBatchQuery($obj_ids, $criteria,
true));
168 while ($row = $this->db->fetchAssoc($result)) {
169 $items[] = $row[
'id'];
170 $user_read[$row[
'id']] = isset($row[
'user_read']) && $row[
'user_read'] !== 0;
175 $collection->setUserReadStatus($criteria->
getReadUserId(), $user_read);
193 $query =
"SELECT context_obj_id, count(context_obj_id) as count FROM il_news_item WHERE {$in_clause} GROUP BY context_obj_id";
194 $result = $this->db->query($query);
197 foreach ($this->db->fetchAll($result) as $row) {
199 $context_map[$row[
'context_obj_id']],
214 $columns = [
'il_news_item.id'];
218 'COALESCE((SELECT ref_id FROM object_reference WHERE object_reference.obj_id = il_news_item.context_obj_id LIMIT 1), 0) AS ref_id'
224 throw new \InvalidArgumentException(
"Read user id is required for read status");
227 $columns[] =
'il_news_read.user_id AS user_read';
228 $joins .=
'LEFT JOIN il_news_read ON il_news_item.id = il_news_read.news_id AND il_news_read.user_id = %s ';
234 $query =
"SELECT " . join(
', ', $columns) .
" FROM il_news_item {$joins} WHERE "
238 $query .=
" AND creation_date >= %s";
244 $query .=
" AND creation_date >= %s";
245 $values[] = $criteria->
getStartDate()->format(
'Y-m-d H:i:s');
250 $query .=
" AND priority = 1 AND content_type = 'text'";
255 $query .=
" AND n.priority {$operator} %s";
261 $query .=
" AND visibility = '" .
NEWS_PUBLIC .
"'";
264 $query .=
" ORDER BY creation_date DESC";
266 return [$query, $types, $values];
272 if (is_numeric($time_period) && $time_period > 0) {
273 return date(
'Y-m-d H:i:s', time() - ($time_period * 24 * 60 * 60));
277 if (preg_match(
"/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/", $time_period)) {
Factory for creating News DTOs from database results (arrays)
This class is a special implementation of a NewsCollection that is designed to load the complete News...
Optimized News Collection with memory-efficient data structures to support large news feeds.
News Context DTO represents a context where news items can be associated with.
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.
News Item DTO for transfer of news items.
News Repository provides basic CRUD operations and optimized database access for news operations with...
buildFindQuery(?array $news_ids=null)
countByContextsBatch(array $contexts)
findByContextsBatchLazy(array $contexts, NewsCriteria $criteria)
__construct(protected readonly \ilDBInterface $db, protected readonly Factory $factory)
static parseTimePeriod(string|int $time_period)
findByIds(array $news_ids)
loadLazyItems(array $news_ids, array $group_context_types)
buildBatchQuery(array $obj_ids, NewsCriteria $criteria, bool $only_id=false)
findByContextsBatch(array $contexts, NewsCriteria $criteria)