ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilNewsItemGUI.php
Go to the documentation of this file.
1 <?php
2 
21 
28 {
29  public const FORM_EDIT = 0;
30  public const FORM_CREATE = 1;
31  public const FORM_RE_EDIT = 2;
32  public const FORM_RE_CREATE = 2;
34  protected ?ilNewsItem $news_item;
35 
36  protected ilCtrl $ctrl;
37  protected ilLanguage $lng;
38  protected ilTabsGUI $tabs;
39  protected ilObjUser $user;
41 
42  protected bool $enable_edit = false;
43  protected int $context_obj_id = 0;
44  protected string $context_obj_type = "";
45  protected int $context_sub_obj_id = 0;
46  protected string $context_sub_obj_type = "";
47  protected int $form_edit_mode;
48  protected int $requested_ref_id;
49  protected int $requested_news_item_id;
50  protected string $add_mode;
53 
54  public function __construct()
55  {
56  global $DIC;
57  $this->main_tpl = $DIC->ui()->mainTemplate();
58 
59  $this->lng = $DIC->language();
60  $this->tabs = $DIC->tabs();
61  $this->user = $DIC->user();
62  $this->toolbar = $DIC->toolbar();
63  $ilCtrl = $DIC->ctrl();
64  $lng = $DIC->language();
65 
66  $this->ctrl = $ilCtrl;
67 
68  $params = $DIC->http()->request()->getQueryParams();
69  $this->requested_ref_id = (int) ($params["ref_id"] ?? 0);
70  $this->requested_news_item_id = (int) ($params["news_item_id"] ?? 0);
71  $this->add_mode = (string) ($params["add_mode"] ?? "");
72  $this->news_access = new NewsAccess($this->requested_ref_id);
73 
74  $this->std_request = $DIC->news()
75  ->internal()
76  ->gui()
77  ->standardRequest();
78 
79  if ($this->requested_news_item_id > 0) {
80  $this->news_item = new ilNewsItem($this->requested_news_item_id);
81  }
82 
83  $this->ctrl->saveParameter($this, ["news_item_id"]);
84 
85  // Init EnableEdit.
86  $this->setEnableEdit(false);
87 
88  // Init Context.
89  $this->setContextObjId($ilCtrl->getContextObjId());
90  $this->setContextObjType($ilCtrl->getContextObjType());
91  //$this->setContextSubObjId($ilCtrl->getContextSubObjId());
92  //$this->setContextSubObjType($ilCtrl->getContextSubObjType());
93 
94  $lng->loadLanguageModule("news");
95 
96  $ilCtrl->saveParameter($this, "add_mode");
97  }
98 
99  public function getHTML(): string
100  {
101  $lng = $this->lng;
102  $lng->loadLanguageModule("news");
103  return $this->getNewsForContextBlock();
104  }
105 
106  public function executeCommand(): string
107  {
108  // check, if news item id belongs to context
109  if (isset($this->news_item) && $this->news_item->getId() > 0
110  && ilNewsItem::_lookupContextObjId($this->news_item->getId()) !== $this->getContextObjId()) {
111  throw new ilException("News ID does not match object context.");
112  }
113 
114 
115  // get next class and command
116  $next_class = $this->ctrl->getNextClass($this);
117  $cmd = $this->ctrl->getCmd();
118 
119  switch ($next_class) {
120  default:
121  $html = $this->$cmd();
122  break;
123  }
124 
125  return $html;
126  }
127 
128  public function setEnableEdit(bool $a_enable_edit = false): void
129  {
130  $this->enable_edit = $a_enable_edit;
131  }
132 
133  public function getEnableEdit(): bool
134  {
135  return $this->enable_edit;
136  }
137 
138  public function setContextObjId(int $a_context_obj_id): void
139  {
140  $this->context_obj_id = $a_context_obj_id;
141  }
142 
143  public function getContextObjId(): int
144  {
145  return $this->context_obj_id;
146  }
147 
148  public function setContextObjType(string $a_context_obj_type): void
149  {
150  $this->context_obj_type = $a_context_obj_type;
151  }
152 
153  public function getContextObjType(): string
154  {
156  }
157 
158  public function setContextSubObjId(int $a_context_sub_obj_id): void
159  {
160  $this->context_sub_obj_id = $a_context_sub_obj_id;
161  }
162 
163  public function getContextSubObjId(): int
164  {
166  }
167 
168  public function setContextSubObjType(string $a_context_sub_obj_type): void
169  {
170  $this->context_sub_obj_type = $a_context_sub_obj_type;
171  }
172 
173  public function getContextSubObjType(): string
174  {
176  }
177 
178  public function createNewsItem(): string
179  {
180  $form = $this->initFormNewsItem(self::FORM_CREATE);
181  return $form->getHTML();
182  }
183 
184  public function editNewsItem(): string
185  {
186  $form = $this->initFormNewsItem(self::FORM_EDIT);
187  $this->getValuesNewsItem($form);
188  return $form->getHTML();
189  }
190 
191  protected function initFormNewsItem(int $a_mode): ilPropertyFormGUI
192  {
193  $ilTabs = $this->tabs;
194 
195  $ilTabs->clearTargets();
196  $form = self::getEditForm($a_mode, $this->requested_ref_id);
197  $form->setFormAction($this->ctrl->getFormAction($this));
198 
199  return $form;
200  }
201 
202  public static function getEditForm(
203  int $a_mode,
204  int $a_ref_id
205  ): ilPropertyFormGUI {
206  global $DIC;
207 
208  $lng = $DIC->language();
209 
210  $lng->loadLanguageModule("news");
211 
212  $form = new ilPropertyFormGUI();
213 
214  // Property Title
215  $text_input = new ilTextInputGUI($lng->txt("news_news_item_title"), "news_title");
216  $text_input->setInfo("");
217  $text_input->setRequired(true);
218  $text_input->setMaxLength(200);
219  $form->addItem($text_input);
220 
221  // Property Content
222  $text_area = new ilTextAreaInputGUI($lng->txt("news_news_item_content"), "news_content");
223  $text_area->setInfo("");
224  $text_area->setRequired(false);
225  $text_area->setRows(4);
226  $form->addItem($text_area);
227 
228  // Property Visibility
229  $radio_group = new ilRadioGroupInputGUI($lng->txt("news_news_item_visibility"), "news_visibility");
230  $radio_option = new ilRadioOption($lng->txt("news_visibility_users"), "users");
231  $radio_group->addOption($radio_option);
232  $radio_option = new ilRadioOption($lng->txt("news_visibility_public"), "public");
233  $radio_group->addOption($radio_option);
234  $radio_group->setInfo($lng->txt("news_news_item_visibility_info"));
235  $radio_group->setRequired(false);
236  $radio_group->setValue("users");
237  $form->addItem($radio_group);
238 
239  // media
240  $media = new ilFileInputGUI($lng->txt('news_media'), 'media');
241  $media->setSuffixes(["jpeg", "jpg", "png", "gif", "mp4", "mp3", "pdf"]);
242  $media->setRequired(false);
243  $media->setAllowDeletion(true);
244  $media->setValue(" ");
245  $form->addItem($media);
246 
247  // save and cancel commands
248  if (in_array($a_mode, [self::FORM_CREATE, self::FORM_RE_CREATE])) {
249  $form->addCommandButton("saveNewsItem", $lng->txt("save"), "news_btn_create");
250  $form->addCommandButton("cancelSaveNewsItem", $lng->txt("cancel"), "news_btn_cancel_create");
251  } else {
252  $form->addCommandButton("updateNewsItem", $lng->txt("save"), "news_btn_update");
253  $form->addCommandButton("cancelUpdateNewsItem", $lng->txt("cancel"), "news_btn_cancel_update");
254  }
255 
256  $form->setTitle($lng->txt("news_news_item_head"));
257 
258  $news_set = new ilSetting("news");
259  if (!$news_set->get("enable_rss_for_internal")) {
260  $form->removeItemByPostVar("news_visibility");
261  } else {
262  $nv = $form->getItemByPostVar("news_visibility");
263  if (is_object($nv)) {
264  $nv->setValue(ilNewsItem::_getDefaultVisibilityForRefId($a_ref_id));
265  }
266  }
267 
268  return $form;
269  }
270 
271  // FORM NewsItem: Get current values for NewsItem form.
272  public function getValuesNewsItem(ilPropertyFormGUI $a_form): void
273  {
274  $values = [];
275 
276  $values["news_title"] = $this->news_item->getTitle();
277  $values["news_content"] = $this->news_item->getContent() . $this->news_item->getContentLong();
278  $values["news_visibility"] = $this->news_item->getVisibility();
279  //$values["news_content_long"] = $this->news_item->getContentLong();
280  $values["news_content_long"] = "";
281 
282  $a_form->setValuesByArray($values);
283 
284  if ($this->news_item->getMobId() > 0) {
285  $fi = $a_form->getItemByPostVar("media");
286  $fi->setValue(ilObject::_lookupTitle($this->news_item->getMobId()));
287  }
288  }
289 
290  // FORM NewsItem: Save NewsItem.
291  public function saveNewsItem(): string
292  {
293  $ilUser = $this->user;
294 
295  if (!$this->news_access->canAdd()) {
296  return "";
297  }
298 
299  $form = $this->initFormNewsItem(self::FORM_CREATE);
300  if ($form->checkInput()) {
301  $this->news_item = new ilNewsItem();
302  $this->news_item->setTitle($form->getInput("news_title"));
303  $this->news_item->setContent($form->getInput("news_content"));
304  $this->news_item->setVisibility($form->getInput("news_visibility"));
305 
306  $media = $_FILES["media"];
307  if ($media["name"] != "") {
308  $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
309  $this->news_item->setMobId($mob->getId());
310  }
311 
312 
313  $this->news_item->setContentLong("");
314  if (self::isRteActivated()) {
315  $this->news_item->setContentHtml(true);
316  }
317 
318  // changed
319  $this->news_item->setContextObjId($this->getContextObjId());
320  $this->news_item->setContextObjType($this->getContextObjType());
321  $this->news_item->setContextSubObjId($this->getContextSubObjId());
322  $this->news_item->setContextSubObjType($this->getContextSubObjType());
323  $this->news_item->setUserId($ilUser->getId());
324 
325  $news_set = new ilSetting("news");
326  if (!$news_set->get("enable_rss_for_internal")) {
327  $this->news_item->setVisibility("users");
328  }
329 
330  $this->news_item->create();
331  $this->exitSaveNewsItem();
332  } else {
333  $form->setValuesByPost();
334  return $form->getHTML();
335  }
336  return "";
337  }
338 
339  public function exitSaveNewsItem(): void
340  {
341  $ilCtrl = $this->ctrl;
342 
343  if ($this->add_mode === "block") {
344  $ilCtrl->returnToParent($this);
345  } else {
346  $ilCtrl->redirect($this, "editNews");
347  }
348  }
349 
350  public function updateNewsItem(): string
351  {
352  $ilUser = $this->user;
353 
354  if (!$this->news_access->canEdit($this->news_item)) {
355  return "";
356  }
357 
358  $form = $this->initFormNewsItem(self::FORM_EDIT);
359  if ($form->checkInput()) {
360  $this->news_item->setUpdateUserId($ilUser->getId());
361  $this->news_item->setTitle($form->getInput("news_title"));
362  $this->news_item->setContent($form->getInput("news_content"));
363  $this->news_item->setVisibility($form->getInput("news_visibility"));
364  //$this->news_item->setContentLong($form->getInput("news_content_long"));
365  $this->news_item->setContentLong("");
366 
367  $media = $_FILES["media"];
368  $old_mob_id = 0;
369 
370  // delete old media object
371  $media_delete = $this->std_request->getDeleteMedia();
372  if ($media["name"] != "" || $media_delete != "") {
373  if ($this->news_item->getMobId() > 0 && ilObject::_lookupType($this->news_item->getMobId()) === "mob") {
374  $old_mob_id = $this->news_item->getMobId();
375  }
376  $this->news_item->setMobId(0);
377  }
378 
379  if ($media["name"] != "") {
380  $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
381  $this->news_item->setMobId($mob->getId());
382  }
383 
384  if (self::isRteActivated()) {
385  $this->news_item->setContentHtml(true);
386  }
387  $this->news_item->update();
388 
389  if ($old_mob_id > 0) {
390  $old_mob = new ilObjMediaObject($old_mob_id);
391  $old_mob->delete();
392  }
393 
394  $this->exitUpdateNewsItem();
395  } else {
396  $form->setValuesByPost();
397  return $form->getHTML();
398  }
399  return "";
400  }
401 
402  public function exitUpdateNewsItem(): void
403  {
404  $ilCtrl = $this->ctrl;
405 
406  $ilCtrl->redirect($this, "editNews");
407  }
408 
409  public function cancelUpdateNewsItem(): string
410  {
411  return $this->editNews();
412  }
413 
414  public function cancelSaveNewsItem(): string
415  {
416  $ilCtrl = $this->ctrl;
417 
418  if ($this->add_mode === "block") {
419  $ilCtrl->returnToParent($this);
420  } else {
421  return $this->editNews();
422  }
423  return "";
424  }
425 
426  public function editNews(): string
427  {
428  $ilToolbar = $this->toolbar;
429  $lng = $this->lng;
430  $ilCtrl = $this->ctrl;
431 
432  $this->setTabs();
433  if (!$this->news_access->canAccessManageOverview()) {
434  return "";
435  }
436 
437  if ($this->news_access->canAdd()) {
438  $ilToolbar->addButton(
439  $lng->txt("news_add_news"),
440  $ilCtrl->getLinkTarget($this, "createNewsItem")
441  );
442  }
443 
444  return $this->getNewsForContextTable();
445  }
446 
447  public function cancelUpdate(): string
448  {
449  return $this->editNews();
450  }
451 
452  public function confirmDeletionNewsItems(): string
453  {
454  $ilCtrl = $this->ctrl;
455  $lng = $this->lng;
456  $ilTabs = $this->tabs;
457 
458  if (!$this->news_access->canAccessManageOverview()) {
459  return "";
460  }
461 
462  // check whether at least one item is selected
463  if (count($this->std_request->getNewsIds()) === 0) {
464  $this->main_tpl->setOnScreenMessage('failure', $lng->txt("no_checkbox"));
465  return $this->editNews();
466  }
467 
468  $ilTabs->clearTargets();
469 
470  $c_gui = new ilConfirmationGUI();
471 
472  // set confirm/cancel commands
473  $c_gui->setFormAction($ilCtrl->getFormAction($this, "deleteNewsItems"));
474  $c_gui->setHeaderText($lng->txt("info_delete_sure"));
475  $c_gui->setCancel($lng->txt("cancel"), "editNews");
476  $c_gui->setConfirm($lng->txt("confirm"), "deleteNewsItems");
477 
478  // add items to delete
479  foreach ($this->std_request->getNewsIds() as $news_id) {
480  $news = new ilNewsItem($news_id);
481  if ($this->news_access->canDelete($news)) {
482  $c_gui->addItem("news_id[]", $news_id, $news->getTitle());
483  }
484  }
485 
486  return $c_gui->getHTML();
487  }
488 
489  public function deleteNewsItems(): string
490  {
491  if (!$this->news_access->canAccessManageOverview()) {
492  return "";
493  }
494  // delete all selected news items
495  foreach ($this->std_request->getNewsIds() as $news_id) {
496  $news = new ilNewsItem($news_id);
497  if ($this->news_access->canDelete($news)) {
498  $news->delete();
499  }
500  }
501 
502  return $this->editNews();
503  }
504 
505  public function getNewsForContextBlock(): string
506  {
507  $lng = $this->lng;
508 
509  $block_gui = new ilNewsForContextBlockGUI();
510 
511  $block_gui->setEnableEdit($this->getEnableEdit());
512 
513 
514  $news_item = new ilNewsItem();
515 
516  // changed
517  $news_item->setContextObjId($this->getContextObjId());
518  $news_item->setContextObjType($this->getContextObjType());
519  $news_item->setContextSubObjId($this->getContextSubObjId());
520  $news_item->setContextSubObjType($this->getContextSubObjType());
521 
522  $data = $news_item->queryNewsForContext();
523 
524  $block_gui->setTitle($lng->txt("news_block_news_for_context"));
525  $block_gui->setRowTemplate("tpl.block_row_news_for_context.html", "components/ILIAS/News");
526  $block_gui->setData($data);
527 
528  return $block_gui->getHTML();
529  }
530 
531 
532  public function getNewsForContextTable(): string
533  {
534  $lng = $this->lng;
535 
536  $news_item = new ilNewsItem();
537  $news_item->setContextObjId($this->getContextObjId());
538  $news_item->setContextObjType($this->getContextObjType());
539  $news_item->setContextSubObjId($this->getContextSubObjId());
540  $news_item->setContextSubObjType($this->getContextSubObjType());
541 
542  $perm_ref_id = 0;
543  if (in_array($this->getContextObjType(), ["cat", "grp", "crs", "root"])) {
544  $data = $news_item->getNewsForRefId(
545  $this->requested_ref_id,
546  false,
547  false,
548  0,
549  true,
550  false,
551  true,
552  true
553  );
554  } else {
555  $perm_ref_id = $this->requested_ref_id;
556  if ($this->getContextSubObjId() > 0) {
557  $data = $news_item->queryNewsForContext(
558  false,
559  0,
560  "",
561  true,
562  true
563  );
564  } else {
565  $data = $news_item->queryNewsForContext();
566  }
567  }
568 
569  $table_gui = new ilNewsForContextTableGUI($this, "getNewsForContextTable", $perm_ref_id);
570 
571  $table_gui->setTitle($lng->txt("news_table_news_for_context"));
572  $table_gui->setRowTemplate("tpl.table_row_news_for_context.html", "components/ILIAS/News");
573  $table_gui->setData($data);
574 
575  $table_gui->setDefaultOrderField("creation_date");
576  $table_gui->setDefaultOrderDirection("desc");
577  $table_gui->addMultiCommand("confirmDeletionNewsItems", $lng->txt("delete"));
578  $table_gui->setTitle($lng->txt("news"));
579  $table_gui->setSelectAllCheckbox("news_id");
580 
581 
582  return $table_gui->getHTML();
583  }
584 
585  public function setTabs(): void
586  {
587  $ilTabs = $this->tabs;
588  $ilCtrl = $this->ctrl;
589  $lng = $this->lng;
590 
591  $ilTabs->clearTargets();
592  $ilTabs->setBackTarget(
593  $lng->txt("back"),
594  (string) $ilCtrl->getParentReturn($this)
595  );
596  }
597 
598  public static function isRteActivated(): bool
599  {
600  return false;
601  }
602 }
This class represents an option in a radio group.
static _getDefaultVisibilityForRefId(int $a_ref_id)
Get default visibility for reference id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
getItemByPostVar(string $a_post_var)
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
This class represents a file property in a property form.
setInfo(string $a_info)
setContextSubObjId(int $a_context_sub_obj_id)
ilGlobalTemplateInterface $main_tpl
User Interface for NewsItem entities.
setContextObjType(string $a_context_obj_type)
setSuffixes(array $a_suffixes)
loadLanguageModule(string $a_module)
Load language module.
BlockGUI class for block NewsForContext.
getValuesNewsItem(ilPropertyFormGUI $a_form)
setContextSubObjType(string $a_context_sub_obj_type)
setContextSubObjType(?string $a_context_sub_obj_type)
This class represents a property in a property form.
queryNewsForContext(bool $a_for_rss_use=false, $a_time_period=0, string $a_starting_date="", bool $a_no_auto_generated=false, bool $a_oldest_first=false, int $a_limit=0, array $a_exclude=[])
Query news for a context.
static _lookupTitle(int $obj_id)
redirect(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false)
static getEditForm(int $a_mode, int $a_ref_id)
global $DIC
Definition: shib_login.php:22
setContextObjId(int $a_context_obj_id)
setContextObjType(string $a_context_obj_type)
clearTargets()
clear all targets
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
setEnableEdit(bool $a_enable_edit=false)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
setContextObjId(int $a_context_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A news item can be created by different sources.
ilToolbarGUI $toolbar
initFormNewsItem(int $a_mode)
This class represents a text area property in a property form.
TableGUI class for table NewsForContext.
getNewsForRefId(int $a_ref_id, bool $a_only_public=false, bool $a_stopnesting=false, $a_time_period=0, bool $a_prevent_aggregation=true, bool $a_forum_group_sequences=false, bool $a_no_auto_generated=false, bool $a_ignore_date_filter=false, ?int $a_user_id=null, int $a_limit=0, array $a_excluded=[])
Get News For Ref Id.
NewsAccess $news_access
returnToParent(object $a_gui_obj, ?string $a_anchor=null)
static _lookupType(int $id, bool $reference=false)
static _lookupContextObjId(int $a_news_id)
Context Object ID.
setContextSubObjId(int $a_context_sub_obj_id)
StandardGUIRequest $std_request