ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
PostingList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Blog\Posting;
22
24
26{
28 protected ?array $postings = null;
29
30 public function __construct(
31 protected int $obj_id,
32 protected PostingManager $posting_manager,
33 protected \ILIAS\Blog\Settings\Settings $settings,
34 protected PageManagerInterface $page_manager,
35 protected bool $include_inactive = true
36 ) {
37 }
38
42 public function getPostingsForView(
43 int $author_id = 0,
44 string $keyword = "",
45 string $month = ""
46 ): array {
47 if ($author_id > 0) {
48 return $this->getByAuthor($author_id);
49 }
50 if ($keyword !== "") {
51 return $this->getByKeyword($keyword);
52 }
53
54 $max = $this->settings->getOverviewPostings();
55 if ($month === "" && $max > 0) {
56 $list_items = [];
57 $all_items = $this->getPostingsGroupedByMonth();
58 foreach ($all_items as $postings) {
59 foreach ($postings as $id => $item) {
60 $list_items[$id] = $item;
61 if (count($list_items) >= $max) {
62 break(2);
63 }
64 }
65 }
66 return $list_items;
67 }
68
69 return $this->getByMonth($month);
70 }
71
75 protected function getPostings(): array
76 {
77 if ($this->postings === null) {
78 $this->postings = $this->posting_manager->getAllPostings($this->obj_id);
79 if (!$this->include_inactive) {
80 $this->postings = array_filter($this->postings, function (Posting $posting) {
81 return $posting->isActive();
82 });
83 }
84 }
85 return $this->postings;
86 }
87
91 public function getByMonth(string $month): array
92 {
93 $res = [];
94 foreach ($this->getPostings() as $posting) {
95 if (substr($posting->getCreated()->get(IL_CAL_DATE), 0, 7) === $month) {
96 $res[$posting->getId()] = $posting;
97 }
98 }
99 return $res;
100 }
101
105 public function getByAuthor(int $author_id): array
106 {
107 $res = [];
108 foreach ($this->getPostings() as $posting) {
109 if ($posting->getAuthor() === $author_id) {
110 $res[$posting->getId()] = $posting;
111 continue;
112 }
113 foreach ($this->page_manager->getPageContributors("blp", $posting->getId()) as $editor) {
114 if ((int) $editor["user_id"] === $author_id) {
115 $res[$posting->getId()] = $posting;
116 break;
117 }
118 }
119 }
120 return $res;
121 }
122
126 public function getByKeyword(string $keyword): array
127 {
128 $res = [];
129 foreach ($this->getPostings() as $posting) {
130 if (in_array(
131 $keyword,
132 $this->posting_manager->getKeywords($this->obj_id, $posting->getId()),
133 true
134 )) {
135 $res[$posting->getId()] = $posting;
136 }
137 }
138 return $res;
139 }
140
144 public function getPostingsGroupedByMonth(): array
145 {
146 $items = [];
147 foreach ($this->getPostings() as $posting) {
148 $month = substr($posting->getCreated()->get(IL_CAL_DATE), 0, 7);
149 $items[$month][$posting->getId()] = $posting;
150 }
151 return $items;
152 }
153
154 public function prepareViewState(
155 string $month,
156 ?int $author
158 if ($author && !$this->hasAuthorPostings($author)) {
159 $author = null;
160 }
161
162 $items = $this->getPostingsGroupedByMonth();
163 if ($items && (!$month || !isset($items[$month]) || $items[$month] === [])) {
164 $month = (string) array_key_first($items);
165 }
166
167 return new PostingViewState($month, $author);
168 }
169
173 public function getMonthsWithPostings(): array
174 {
175 $months = [];
176 foreach ($this->getPostings() as $posting) {
177 $month = substr($posting->getCreated()->get(IL_CAL_DATE), 0, 7);
178 if (!in_array($month, $months, true)) {
179 $months[] = $month;
180 }
181 }
182 rsort($months);
183 return $months;
184 }
185
189 public function getAuthors(): array
190 {
191 $authors = [];
192 foreach ($this->getPostings() as $posting) {
193 $author_id = $posting->getAuthor();
194 if ($author_id > 0 && !in_array($author_id, $authors, true)) {
195 $authors[] = $author_id;
196 }
197
198 foreach ($this->page_manager->getPageContributors("blp", $posting->getId()) as $editor) {
199 $editor_id = (int) $editor["user_id"];
200 if ($editor_id > 0 && !in_array($editor_id, $authors, true)) {
201 $authors[] = $editor_id;
202 }
203 }
204 }
205 return $authors;
206 }
207
208 public function hasAuthorPostings(int $user_id): bool
209 {
210 foreach ($this->getPostings() as $posting) {
211 if ($posting->getAuthor() === $user_id) {
212 return true;
213 }
214 foreach ($this->page_manager->getPageContributors("blp", $posting->getId()) as $editor) {
215 if ((int) $editor["user_id"] === $user_id) {
216 return true;
217 }
218 }
219 }
220 return false;
221 }
222}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$settings
Definition: index.php:44
__construct(protected int $obj_id, protected PostingManager $posting_manager, protected \ILIAS\Blog\Settings\Settings $settings, protected PageManagerInterface $page_manager, protected bool $include_inactive=true)
Definition: PostingList.php:30
prepareViewState(string $month, ?int $author)
getPostingsForView(int $author_id=0, string $keyword="", string $month="")
Definition: PostingList.php:42
const IL_CAL_DATE
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
foreach($_POST as $key=> $value) $res
if(!file_exists('../ilias.ini.php'))