ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilNewsTimelineGUI.php
Go to the documentation of this file.
1<?php
2
24
32{
33 protected \ILIAS\News\InternalGUIService $gui;
34 protected int $period = 0;
35 protected \ILIAS\News\Timeline\TimelineManager $manager;
36 protected \ILIAS\DI\UIServices $ui;
37 protected \ILIAS\Notes\Service $notes;
38 protected \ILIAS\HTTP\Services $http;
39 protected int $news_id;
40 protected bool $include_auto_entries;
41 protected ilCtrl $ctrl;
43 protected int $ref_id;
44 protected ilLanguage $lng;
46 protected ilObjUser $user;
48 protected static int $items_per_load = 20;
49 protected bool $user_edit_all = false;
51 protected bool $enable_add_news = true;
53
54 protected function __construct(
55 int $a_ref_id,
56 bool $a_include_auto_entries
57 ) {
58 global $DIC;
59
60 $this->ctrl = $DIC->ctrl();
61 $this->tpl = $DIC["tpl"];
62 $this->ref_id = $a_ref_id;
63 $this->lng = $DIC->language();
64 $this->toolbar = $DIC->toolbar();
65 $this->user = $DIC->user();
66 $this->include_auto_entries = $a_include_auto_entries;
67 $this->access = $DIC->access();
68 $this->http = $DIC->http();
69 $this->notes = $DIC->notes();
70
71 $this->std_request = $DIC->news()
72 ->internal()
73 ->gui()
74 ->standardRequest();
75
76 $this->news_id = $this->std_request->getNewsId();
77
78 $this->lng->loadLanguageModule("news");
79 $this->lng->loadLanguageModule("cont");
80 $this->ui = $DIC->ui();
81 $this->manager = $DIC->news()->internal()->domain()->timeline();
82 $this->gui = $DIC->news()->internal()->gui();
83 }
84
85 public function setEnableAddNews(bool $a_val): void
86 {
87 $this->enable_add_news = $a_val;
88 }
89
90 public function getEnableAddNews(): bool
91 {
93 }
94
95 public function setPeriod(int $a_val): void
96 {
97 $this->period = $a_val;
98 }
99
100 public function getPeriod(): int
101 {
102 return $this->period;
103 }
104
105
109 public function setUserEditAll(bool $a_val): void
110 {
111 $this->user_edit_all = $a_val;
112 }
113
117 public function getUserEditAll(): bool
118 {
120 }
121
122 public static function getInstance(
123 int $a_ref_id,
124 bool $a_include_auto_entries
126 return new self($a_ref_id, $a_include_auto_entries);
127 }
128
132 public function executeCommand(): void
133 {
134 $ctrl = $this->ctrl;
135
136 $next_class = $this->ctrl->getNextClass($this);
137 $cmd = $this->ctrl->getCmd("show");
138
139 switch ($next_class) {
140 case "illikegui":
141 $item = $this->manager->getNewsItem($this->news_id);
142 $likef = new ilLikeFactoryGUI();
143 $like_gui = $likef->widget([$item->getContextObjId()]);
144 $ctrl->saveParameter($this, "news_id");
145 $like_gui->setObject(
146 $item->getContextObjId(),
147 $item->getContextObjType(),
148 $item->getContextSubObjId(),
149 (string) $item->getContextSubObjType(),
150 $this->news_id
151 );
152 $ret = $ctrl->forwardCommand($like_gui);
153 break;
154
155 case strtolower(ilCommentGUI::class):
156 $item = $this->manager->getNewsItem($this->news_id);
157 $ctrl->saveParameter($this, "news_id");
158 $notes_obj_type = ($item->getContextSubObjType() === null)
159 ? $item->getContextObjType()
160 : $item->getContextSubObjType();
161 $comment_gui = $this->notes->gui()->getCommentsGUI(
162 $item->getContextObjId(),
163 $item->getContextSubObjId(),
164 $notes_obj_type,
165 $item->getId()
166 );
167 $comment_gui->setShowHeader(false);
168 $ret = $ctrl->forwardCommand($comment_gui);
169 break;
170
171 default:
172 if (in_array($cmd, ["show", "save", "update", "loadMore", "remove", "updateNewsItem", "downloadMob"])) {
173 $this->$cmd();
174 }
175 }
176 }
177
178 public function show(?ilPropertyFormGUI $form = null): void
179 {
180 $this->tpl->setContent($this->getHTML($form));
181 }
182
183 protected function readNewsData($excluded = []): void
184 {
185 $this->news_collection = $this->manager->getNewsData(
186 $this->ref_id,
187 $this->ctrl->getContextObjId(),
188 $this->ctrl->getContextObjType(),
189 $this->period,
190 $this->include_auto_entries,
191 self::$items_per_load,
192 $excluded
193 );
194 }
195
196 public function getHTML(?ilPropertyFormGUI $form = null): string
197 {
198 // toolbar
199 if ($this->getEnableAddNews() &&
200 $this->access->checkAccess("news_add_news", "", $this->ref_id)) {
201 $this->gui->button(
202 $this->lng->txt("news_add_news"),
203 "#"
204 )->onClick("return il.News.create();")->primary()->toToolbar(true, $this->toolbar);
205 }
206
207 $this->readNewsData();
208
209 $timeline = ilTimelineGUI::getInstance();
210
211 // get like widget
212 $obj_ids = array_unique($this->news_collection->pluck('context_obj_id'));
213 $likef = new ilLikeFactoryGUI();
214 $like_gui = $likef->widget($obj_ids);
215
216 $js_items = [];
217
219 foreach ($this->news_collection as $news_item) {
220 $item = ilNewsTimelineItemGUI::getInstance($news_item, $like_gui);
221 $item->setUserEditAll($this->getUserEditAll());
222 $timeline->addItem($item);
223 $js_items[$news_item->getId()] = [
224 "id" => $news_item->getId(),
225 "user_id" => $news_item->getUserId(),
226 "title" => $news_item->getTitle(),
227 "content" => $news_item->getContent() . $news_item->getContentLong(),
228 "content_long" => '',
229 "priority" => $news_item->getPriority(),
230 "visibility" => $news_item->getVisibility(),
231 "content_type" => $news_item->getContentType(),
232 "mob_id" => $news_item->getMobId()
233 ];
234 }
235
236 $this->tpl->addOnLoadCode("il.News.setItems(" . json_encode($js_items, JSON_THROW_ON_ERROR) . ");");
237 $this->tpl->addOnLoadCode("il.News.setAjaxUrl('" . $this->ctrl->getLinkTarget($this, "", "", true) . "');");
238
239 if (!$this->news_collection->isEmpty()) {
240 $ttpl = new ilTemplate("tpl.news_timeline.html", true, true, "components/ILIAS/News");
241 $ttpl->setVariable("NEWS", $timeline->render());
242 $this->renderDeleteModal($ttpl);
243 $this->renderEditModal($form, $ttpl);
244 $ttpl->setVariable("LOADER", ilUtil::getImagePath("media/loader.svg"));
245 $this->tpl->setContent($ttpl->get());
246 $html = $ttpl->get();
247 } else {
248 if ($this->getEnableAddNews()) {
249 $ttpl = new ilTemplate("tpl.news_timeline.html", true, true, "components/ILIAS/News");
250 $this->tpl->setOnScreenMessage('info', $this->lng->txt("news_timline_add_entries_info"));
251 $this->renderEditModal($form, $ttpl);
252 $this->tpl->setContent($ttpl->get());
253 $html = $ttpl->get();
254 } else {
255 $mess = $this->ui->factory()->messageBox()->info(
256 $this->lng->txt("news_timline_no_entries")
257 );
258 $html = $this->ui->renderer()->render($mess);
259 }
260 }
261
262 $this->lng->toJS("create");
263 $this->lng->toJS("edit");
264 $this->lng->toJS("update");
265 $this->lng->toJS("save");
266
267 $debug = false;
268 if ($debug) {
269 $this->tpl->addJavaScript("../components/ILIAS/News/resources/News.js");
270 } else {
271 $this->tpl->addJavaScript("assets/js/News.js");
272 }
273 $this->notes->gui()->initJavascript();
274 return $html;
275 }
276
277 public function loadMore(): void
278 {
279 $news_item = new ilNewsItem();
280 $news_item->setContextObjId($this->ctrl->getContextObjId());
281 $news_item->setContextObjType($this->ctrl->getContextObjType());
282
283 $excluded = $this->std_request->getRenderedNews();
284
285 $this->readNewsData($excluded);
286
287 $timeline = ilTimelineGUI::getInstance();
288
289 // get like widget
290 $obj_ids = array_unique($this->news_collection->pluck('context_obj_id'));
291 $likef = new ilLikeFactoryGUI();
292 $like_gui = $likef->widget($obj_ids);
293
294 $js_items = [];
295
297 foreach ($this->news_collection as $news_item) {
298 $item = ilNewsTimelineItemGUI::getInstance($news_item, $like_gui);
299 $item->setUserEditAll($this->getUserEditAll());
300 $timeline->addItem($item);
301 $js_items[$news_item->getId()] = [
302 "id" => $news_item->getId(),
303 "user_id" => $news_item->getUserId(),
304 "title" => $news_item->getTitle(),
305 "content" => $news_item->getContent() . $news_item->getContentLong(),
306 "content_long" => '',
307 "priority" => $news_item->getPriority(),
308 "visibility" => $news_item->getVisibility(),
309 "content_type" => $news_item->getContentType(),
310 "mob_id" => $news_item->getMobId()
311 ];
312 }
313
314 $obj = new stdClass();
315 $obj->data = $js_items;
316 $obj->html = $timeline->render(true);
317
318 $this->send(json_encode($obj, JSON_THROW_ON_ERROR));
319 }
320
324 protected function send(string $output): void
325 {
326 $this->http->saveResponse($this->http->response()->withBody(
327 Streams::ofString($output)
328 ));
329 $this->http->sendResponse();
330 $this->http->close();
331 }
332
333 protected function updateNewsItem(): void
334 {
335 if ($this->std_request->getNewsAction() === "save") {
336 $this->save();
337 }
338 if ($this->std_request->getNewsAction() === "update") {
339 $this->update();
340 }
341 }
342
343
344 // Save (ajax)
345 public function save(): void
346 {
348 if ($form->checkInput()) {
349 $news_item = new ilNewsItem();
350 $news_item->setTitle($form->getInput("news_title"));
351 $news_item->setContent($form->getInput("news_content"));
352 $news_item->setVisibility($form->getInput("news_visibility"));
354 $news_item->setContentHtml(true);
355 }
356 //$news_item->setContentLong($form->getInput("news_content_long"));
357 $news_item->setContentLong("");
358
359 $obj_id = ilObject::_lookupObjectId($this->ref_id);
360 $obj_type = ilObject::_lookupType($obj_id);
361 $news_item->setContextObjId($obj_id);
362 $news_item->setContextObjType($obj_type);
363 $news_item->setUserId($this->user->getId());
364
365 $media = $_FILES["media"];
366 if ($media["name"] != "") {
367 $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
368 $news_item->setMobId($mob->getId());
369 }
370
371 $news_set = new ilSetting("news");
372 if (!$news_set->get("enable_rss_for_internal")) {
373 $news_item->setVisibility("users");
374 }
375 $news_item->create();
376 $this->ctrl->redirect($this, "show");
377 } else {
378 $form->setValuesByPost();
379 $this->show($form);
380 $this->tpl->addOnLoadCode("il.News.create(true);");
381 }
382 }
383
384
385
386 // Update (ajax)
387 public function update(): void
388 {
390 if ($form->checkInput()) {
391 $news_item = new ilNewsItem($this->std_request->getId());
392 $news_item->setTitle($form->getInput("news_title"));
393 $news_item->setContent($form->getInput("news_content"));
394 $news_item->setVisibility($form->getInput("news_visibility"));
395 //$news_item->setContentLong($form->getInput("news_content_long"));
397 $news_item->setContentHtml(true);
398 }
399 $news_item->setContentLong("");
400
401 $media = $_FILES["media"];
402 $old_mob_id = 0;
403
404 // delete old media object
405 if ($media["name"] != "" || $this->std_request->getDeleteMedia() > 0) {
406 if ($news_item->getMobId() > 0 && ilObject::_lookupType($news_item->getMobId()) === "mob") {
407 $old_mob_id = $news_item->getMobId();
408 }
409 $news_item->setMobId(0);
410 }
411
412 if ($media["name"] != "") {
413 $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
414 $news_item->setMobId($mob->getId());
415 }
416
417 $obj_id = ilObject::_lookupObjectId($this->ref_id);
418
419 if ($news_item->getContextObjId() === $obj_id) {
420 $news_item->setUpdateUserId($this->user->getId());
421 $news_item->update();
422
423 if ($old_mob_id > 0) {
424 $old_mob = new ilObjMediaObject($old_mob_id);
425 $old_mob->delete();
426 }
427 }
428 $this->ctrl->redirect($this, "show");
429 } else {
430 $form->setValuesByPost();
431 $this->show($form);
432 $this->tpl->addOnLoadCode("il.News.edit(" . $this->std_request->getNewsId() . ", true);");
433 }
434 }
435
436 // Remove (ajax)
437 public function remove(): void
438 {
439 $news_item = new ilNewsItem($this->std_request->getNewsId());
440 if ($this->getUserEditAll() || $this->user->getId() === $news_item->getUserId()) {
441 $news_item->delete();
442 }
443 $this->send("");
444 }
445
446 protected function getEditModal($form = null): \ILIAS\UI\Component\Modal\Modal
447 {
448 if (is_null($form)) {
450 }
451 $form->setShowTopButtons(false);
452 $form->setFormAction($this->ctrl->getFormAction($this));
453
454
455 //
456 $hi = new ilHiddenInputGUI("id");
457 $form->addItem($hi);
458 $act = new ilHiddenInputGUI("news_action");
459 $form->addItem($act);
460 $form->setId("news_edit_form");
461
462 $modal = $this->gui->ui()->factory()->modal()->roundtrip(
463 $this->lng->txt("edit"),
464 $this->ui->factory()->legacy()->content($form->getHTML())
465 );
466
467 return $modal;
468 }
469
470 protected function renderDeleteModal(ilTemplate $tpl): void
471 {
472 $mbox = $this->gui->ui()->factory()->messageBox()->confirmation(
473 $this->lng->txt("news_really_delete_news")
474 );
475 $title = $this->gui->ui()->factory()->legacy()->content("<p id='news_delete_news_title'></p>");
476 $modal = $this->gui->modal($this->lng->txt("delete"))
477 ->content([$title, $mbox])
478 ->button($this->lng->txt("delete"), "#", false, "il.News.remove(); return false;");
479 $c = $modal->getTriggerButtonComponents("");
480 $tpl->setVariable("DELETE_MODAL", $this->gui->ui()->renderer()->render($c["modal"]));
481 $tpl->setVariable("SIGNAL_ID", $c["signal"]);
482 }
483
484 protected function renderEditModal(?ilPropertyFormGUI $form, ilTemplate $tpl): void
485 {
486 $edit_modal = $this->getEditModal($form);
487 $signal = $edit_modal->getShowSignal()->getId();
488 $close_signal = $edit_modal->getCloseSignal()->getId();
489 $tpl->setVariable("SHOW_EDIT_SIGNAL", $signal);
490 $tpl->setVariable("CLOSE_EDIT_SIGNAL", $close_signal);
491 $tpl->setVariable("EDIT_MODAL", $this->ui->renderer()->render($edit_modal));
492 }
493
494 protected function downloadMob(): void
495 {
496 $news_id = $this->std_request->getNewsId();
497 $news = new ilNewsItem($news_id);
498 $news->deliverMobFile("Standard", true);
499 }
500}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Optimized News Collection with memory-efficient data structures to support large news feeds.
News Item DTO for transfer of news items.
Definition: NewsItem.php:29
show()
returns the content of IniFile
Class ilCtrl provides processing control methods.
saveParameter(object $a_gui_obj, $a_parameter)
@inheritDoc
forwardCommand(object $a_gui_object)
@inheritDoc
This class represents a hidden form property in a property form.
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getEditForm(int $a_mode, int $a_ref_id)
A news item can be created by different sources.
ilGlobalTemplateInterface $tpl
show(?ilPropertyFormGUI $form=null)
ILIAS HTTP Services $http
renderDeleteModal(ilTemplate $tpl)
ILIAS News InternalGUIService $gui
ILIAS News Timeline TimelineManager $manager
static getInstance(int $a_ref_id, bool $a_include_auto_entries)
ILIAS DI UIServices $ui
ILIAS Notes Service $notes
setUserEditAll(bool $a_val)
Set user can edit other users postings.
NewsCollection $news_collection
getUserEditAll()
Get user can edit other users postings.
renderEditModal(?ilPropertyFormGUI $form, ilTemplate $tpl)
StandardGUIRequest $std_request
__construct(int $a_ref_id, bool $a_include_auto_entries)
static getInstance(NewsItem $news_item, ilLikeGUI $like_gui)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
User class.
static _lookupObjectId(int $ref_id)
static _lookupType(int $id, bool $reference=false)
This class represents a property form user interface.
ILIAS Setting Class.
special template class to simplify handling of ITX/PEAR
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
static http()
Fetches the global http state from ILIAS.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
catch(ilCmiXapiException $e) send($response)
Definition: xapitoken.php:97