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 $obj_ids = array_map(fn($context) => $context->getObjId(), $contexts);
77 $query =
"SELECT DISTINCT (context_obj_id) AS obj_id FROM il_news_item WHERE ";
81 $query .=
" AND creation_date >= %s";
87 $query .=
" AND creation_date >= %s";
88 $values[] = $criteria->
getStartDate()->format(
'Y-m-d H:i:s');
92 $result = $this->db->queryF($query, $types, $values);
93 $needed_obj_ids = array_column($this->db->fetchAll($result),
'obj_id',
'obj_id');
95 return array_filter($contexts, fn($context) => isset($needed_obj_ids[$context->getObjId()]));
104 public function loadLazyItems(array $news_ids, array $group_context_types): array
106 if (empty($news_ids)) {
112 $additional_obj_ids = [];
114 foreach ($this->db->fetchAll($result) as $row) {
115 $news_item = $this->
factory->newsItem($row);
117 if (in_array($news_item->getContextObjType(), $group_context_types)) {
118 $additional_obj_ids[] = $news_item->getContextObjId();
121 $news_items[] = $news_item;
124 if (empty($additional_obj_ids)) {
132 $result = $this->db->query($query);
136 array_map(fn($row) => $this->
factory->newsItem($row), $this->db->fetchAll($result))
143 SELECT il_news_item.*,
145 (SELECT ref_id FROM object_reference WHERE object_reference.obj_id = il_news_item.context_obj_id LIMIT 1),
150 if ($news_ids !==
null) {
162 if (empty($contexts)) {
166 $obj_ids = array_map(fn($context) => $context->getObjId(), $contexts);
167 $result = $this->db->queryF(...$this->
buildBatchQuery($obj_ids, $criteria));
172 while ($row = $this->db->fetchAssoc($result)) {
173 $items[] = $this->
factory->newsItem($row);
174 $user_read[$row[
'id']] = isset($row[
'user_read']) && $row[
'user_read'] !== 0;
179 $collection->setUserReadStatus($criteria->
getReadUserId(), $user_read);
190 if (empty($contexts)) {
194 $obj_ids = array_map(fn($context) => $context->getObjId(), $contexts);
195 $result = $this->db->queryF(...$this->
buildBatchQuery($obj_ids, $criteria,
true));
199 while ($row = $this->db->fetchAssoc($result)) {
200 $items[] = $row[
'id'];
201 $user_read[$row[
'id']] = isset($row[
'user_read']) && $row[
'user_read'] !== 0;
206 $collection->setUserReadStatus($criteria->
getReadUserId(), $user_read);
219 foreach ($contexts as $context) {
220 $context_map[$context->getObjId()] = $context;
224 $query =
"SELECT context_obj_id, count(context_obj_id) as count FROM il_news_item WHERE {$in_clause} GROUP BY context_obj_id";
225 $result = $this->db->query($query);
228 foreach ($this->db->fetchAll($result) as $row) {
230 $context_map[$row[
'context_obj_id']],
245 $columns = [
'il_news_item.id'];
249 'COALESCE((SELECT ref_id FROM object_reference WHERE object_reference.obj_id = il_news_item.context_obj_id LIMIT 1), 0) AS ref_id'
255 throw new \InvalidArgumentException(
"Read user id is required for read status");
258 $columns[] =
'il_news_read.user_id AS user_read';
259 $joins .=
'LEFT JOIN il_news_read ON il_news_item.id = il_news_read.news_id AND il_news_read.user_id = %s ';
265 $query =
"SELECT " . join(
', ', $columns) .
" FROM il_news_item {$joins} WHERE "
269 $query .=
" AND creation_date >= %s";
275 $query .=
" AND creation_date >= %s";
276 $values[] = $criteria->
getStartDate()->format(
'Y-m-d H:i:s');
281 $query .=
" AND priority = 1 AND content_type = 'text'";
286 $query .=
" AND n.priority {$operator} %s";
292 $query .=
" AND visibility = '" .
NEWS_PUBLIC .
"'";
295 $query .=
" ORDER BY creation_date DESC";
297 return [$query, $types, $values];
303 if (is_numeric($time_period) && $time_period > 0) {
304 return date(
'Y-m-d H:i:s', time() - ($time_period * 24 * 60 * 60));
308 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)
filterContext(array $contexts, NewsCriteria $criteria)
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)