ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilNewsItemGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once("./Services/News/classes/class.ilNewsItem.php");
6 
7 define("IL_FORM_EDIT", 0);
8 define("IL_FORM_CREATE", 1);
9 define("IL_FORM_RE_EDIT", 2);
10 define("IL_FORM_RE_CREATE", 3);
11 
21 {
25  protected $ctrl;
26 
30  protected $lng;
31 
35  protected $tabs;
36 
40  protected $user;
41 
45  protected $toolbar;
46 
47  protected $enable_edit = 0;
48  protected $context_obj_id;
49  protected $context_obj_type;
52  protected $form_edit_mode;
53 
54 
58  public function __construct()
59  {
60  global $DIC;
61 
62  $this->lng = $DIC->language();
63  $this->tabs = $DIC->tabs();
64  $this->user = $DIC->user();
65  $this->toolbar = $DIC->toolbar();
66  $ilCtrl = $DIC->ctrl();
67  $lng = $DIC->language();
68 
69  $this->ctrl = $ilCtrl;
70 
71  include_once("Services/News/classes/class.ilNewsItem.php");
72  if ($_GET["news_item_id"] > 0) {
73  $this->news_item = new ilNewsItem($_GET["news_item_id"]);
74  }
75 
76  $this->ctrl->saveParameter($this, array("news_item_id"));
77 
78  // Init EnableEdit.
79  $this->setEnableEdit(false);
80 
81  // Init Context.
82  $this->setContextObjId($ilCtrl->getContextObjId());
83  $this->setContextObjType($ilCtrl->getContextObjType());
84  $this->setContextSubObjId($ilCtrl->getContextSubObjId());
85  $this->setContextSubObjType($ilCtrl->getContextSubObjType());
86 
87  $lng->loadLanguageModule("news");
88 
89  $ilCtrl->saveParameter($this, "add_mode");
90  }
91 
97  public function getHTML()
98  {
99  $lng = $this->lng;
101 
102  $lng->LoadLanguageModule("news");
103 
104  return $this->getNewsForContextBlock();
105  }
106 
111  public function executeCommand()
112  {
114 
115  // check, if news item id belongs to context
116  if (is_object($this->news_item) && $this->news_item->getId() > 0
117  && ilNewsItem::_lookupContextObjId($this->news_item->getId()) != $this->getContextObjId()) {
118  throw new ilException("News ID does not match object context.");
119  }
120 
121 
122  // get next class and command
123  $next_class = $this->ctrl->getNextClass($this);
124  $cmd = $this->ctrl->getCmd();
125 
126  switch ($next_class) {
127  default:
128  $html = $this->$cmd();
129  break;
130  }
131 
132  return $html;
133  }
134 
140  public function setEnableEdit($a_enable_edit = 0)
141  {
142  $this->enable_edit = $a_enable_edit;
143  }
144 
150  public function getEnableEdit()
151  {
152  return $this->enable_edit;
153  }
154 
160  public function setContextObjId($a_context_obj_id)
161  {
162  $this->context_obj_id = $a_context_obj_id;
163  }
164 
170  public function getContextObjId()
171  {
172  return $this->context_obj_id;
173  }
174 
180  public function setContextObjType($a_context_obj_type)
181  {
182  $this->context_obj_type = $a_context_obj_type;
183  }
184 
190  public function getContextObjType()
191  {
193  }
194 
200  public function setContextSubObjId($a_context_sub_obj_id)
201  {
202  $this->context_sub_obj_id = $a_context_sub_obj_id;
203  }
204 
210  public function getContextSubObjId()
211  {
213  }
214 
220  public function setContextSubObjType($a_context_sub_obj_type)
221  {
222  $this->context_sub_obj_type = $a_context_sub_obj_type;
223  }
224 
230  public function getContextSubObjType()
231  {
233  }
234 
240  public function setFormEditMode($a_form_edit_mode)
241  {
242  $this->form_edit_mode = $a_form_edit_mode;
243  }
244 
250  public function getFormEditMode()
251  {
252  return $this->form_edit_mode;
253  }
254 
259  public function createNewsItem()
260  {
262  return $form->getHtml();
263  }
264 
269  public function editNewsItem()
270  {
272  $this->getValuesNewsItem($form);
273  return $form->getHtml();
274  }
275 
276 
283  protected function initFormNewsItem($a_mode)
284  {
285  $ilTabs = $this->tabs;
286 
287  $ilTabs->clearTargets();
288  $form = self::getEditForm($a_mode, (int) $_GET["ref_id"]);
289  $form->setFormAction($this->ctrl->getFormAction($this));
290 
291  return $form;
292  }
293 
300  public static function getEditForm($a_mode, $a_ref_id)
301  {
302  global $DIC;
303 
304  $lng = $DIC->language();
305 
306  $lng->loadLanguageModule("news");
307 
308  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
309 
310  $form = new ilPropertyFormGUI();
311 
312  // Property Title
313  $text_input = new ilTextInputGUI($lng->txt("news_news_item_title"), "news_title");
314  $text_input->setInfo("");
315  $text_input->setRequired(true);
316  $text_input->setMaxLength(200);
317  $form->addItem($text_input);
318 
319  // Property Content
320  $text_area = new ilTextAreaInputGUI($lng->txt("news_news_item_content"), "news_content");
321  $text_area->setInfo("");
322  $text_area->setRequired(false);
323  $text_area->setRows("4");
324  $text_area->setUseRte(true);
325  $form->addItem($text_area);
326 
327  // Property Visibility
328  $radio_group = new ilRadioGroupInputGUI($lng->txt("news_news_item_visibility"), "news_visibility");
329  $radio_option = new ilRadioOption($lng->txt("news_visibility_users"), "users");
330  $radio_group->addOption($radio_option);
331  $radio_option = new ilRadioOption($lng->txt("news_visibility_public"), "public");
332  $radio_group->addOption($radio_option);
333  $radio_group->setInfo($lng->txt("news_news_item_visibility_info"));
334  $radio_group->setRequired(false);
335  $radio_group->setValue("users");
336  $form->addItem($radio_group);
337 
338  // media
339  $media = new ilFileInputGUI($lng->txt('news_media'), 'media');
340  $media->setSuffixes(array("jpeg", "jpg", "png", "gif", "mp4", "mp3"));
341  $media->setRequired(false);
342  $media->setALlowDeletion(true);
343  $media->setValue(" ");
344  $form->addItem($media);
345 
346  // Property ContentLong
347  /*
348  $text_area = new ilTextAreaInputGUI($lng->txt("news_news_item_content_long"), "news_content_long");
349  $text_area->setInfo($lng->txt("news_news_item_content_long_info"));
350  $text_area->setRequired(false);
351  $text_area->setCols("40");
352  $text_area->setRows("8");
353  $text_area->setUseRte(true);
354  $form->addItem($text_area);*/
355 
356 
357  // save and cancel commands
358  if (in_array($a_mode, array(IL_FORM_CREATE,IL_FORM_RE_CREATE))) {
359  $form->addCommandButton("saveNewsItem", $lng->txt("save"), "news_btn_create");
360  $form->addCommandButton("cancelSaveNewsItem", $lng->txt("cancel"), "news_btn_cancel_create");
361  } else {
362  $form->addCommandButton("updateNewsItem", $lng->txt("save"), "news_btn_update");
363  $form->addCommandButton("cancelUpdateNewsItem", $lng->txt("cancel"), "news_btn_cancel_update");
364  }
365 
366  $form->setTitle($lng->txt("news_news_item_head"));
367 
368  $news_set = new ilSetting("news");
369  if (!$news_set->get("enable_rss_for_internal")) {
370  $form->removeItemByPostVar("news_visibility");
371  } else {
372  $nv = $form->getItemByPostVar("news_visibility");
373  if (is_object($nv)) {
374  $nv->setValue(ilNewsItem::_getDefaultVisibilityForRefId($a_ref_id));
375  }
376  }
377 
378  return $form;
379  }
380 
385  public function getValuesNewsItem(\ilPropertyFormGUI $a_form)
386  {
387  $values = array();
388 
389  $values["news_title"] = $this->news_item->getTitle();
390  $values["news_content"] = $this->news_item->getContent() . $this->news_item->getContentLong();
391  $values["news_visibility"] = $this->news_item->getVisibility();
392  //$values["news_content_long"] = $this->news_item->getContentLong();
393  $values["news_content_long"] = "";
394 
395  $a_form->setValuesByArray($values);
396 
397  if ($this->news_item->getMobId() > 0) {
398  $fi = $a_form->getItemByPostVar("media");
399  $fi->setValue(ilObject::_lookupTitle($this->news_item->getMobId()));
400  }
401  }
402 
407  public function saveNewsItem()
408  {
410 
411  if (!$this->getEnableEdit()) {
412  return;
413  }
414 
416  if ($form->checkInput()) {
417  $this->news_item = new ilNewsItem();
418  $this->news_item->setTitle($form->getInput("news_title"));
419  $this->news_item->setContent($form->getInput("news_content"));
420  $this->news_item->setVisibility($form->getInput("news_visibility"));
421 
422  // $data = $form->getInput('media');
423  // var_dump($data);
424 
425  $media = $_FILES["media"];
426  if ($media["name"] != "") {
427  $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
428  $this->news_item->setMobId($mob->getId());
429  }
430 
431 
432  $this->news_item->setContentLong("");
433  if (self::isRteActivated()) {
434  $this->news_item->setContentHtml(true);
435  }
436  //$this->news_item->setContentLong($form->getInput("news_content_long"));
437 
438  // changed
439  //$this->news_item->setContextObjId($this->ctrl->getContextObjId());
440  //$this->news_item->setContextObjType($this->ctrl->getContextObjType());
441  $this->news_item->setContextObjId($this->getContextObjId());
442  $this->news_item->setContextObjType($this->getContextObjType());
443  $this->news_item->setContextSubObjId($this->getContextSubObjId());
444  $this->news_item->setContextSubObjType($this->getContextSubObjType());
445  $this->news_item->setUserId($ilUser->getId());
446 
447  $news_set = new ilSetting("news");
448  if (!$news_set->get("enable_rss_for_internal")) {
449  $this->news_item->setVisibility("users");
450  }
451 
452  $this->news_item->create();
453  $this->exitSaveNewsItem();
454  } else {
455  $form->setValuesByPost();
456  return $form->getHtml();
457  }
458  }
459 
460  public function exitSaveNewsItem()
461  {
463 
464  if ($_GET["add_mode"] == "block") {
465  $ilCtrl->returnToParent($this);
466  } else {
467  $ilCtrl->redirect($this, "editNews");
468  }
469  }
470 
475  public function updateNewsItem()
476  {
478 
479  if (!$this->getEnableEdit()) {
480  return "";
481  }
482 
484  if ($form->checkInput()) {
485  $this->news_item->setUpdateUserId($ilUser->getId());
486  $this->news_item->setTitle($form->getInput("news_title"));
487  $this->news_item->setContent($form->getInput("news_content"));
488  $this->news_item->setVisibility($form->getInput("news_visibility"));
489  //$this->news_item->setContentLong($form->getInput("news_content_long"));
490  $this->news_item->setContentLong("");
491 
492  $media = $_FILES["media"];
493  $old_mob_id = 0;
494 
495  // delete old media object
496  if ($media["name"] != "" || $_POST["media_delete"] != "") {
497  if ($this->news_item->getMobId() > 0 && ilObject::_lookupType($this->news_item->getMobId()) == "mob") {
498  $old_mob_id = $this->news_item->getMobId();
499  }
500  $this->news_item->setMobId(0);
501  }
502 
503  if ($media["name"] != "") {
504  $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
505  $this->news_item->setMobId($mob->getId());
506  }
507 
508  if (self::isRteActivated()) {
509  $this->news_item->setContentHtml(true);
510  }
511  $this->news_item->update();
512 
513  if ($old_mob_id > 0) {
514  $old_mob = new ilObjMediaObject($old_mob_id);
515  $old_mob->delete();
516  }
517 
518  $this->exitUpdateNewsItem();
519  } else {
520  $form->setValuesByPost();
521  return $form->getHtml();
522  }
523  }
524 
525  public function exitUpdateNewsItem()
526  {
528 
529  $ilCtrl->redirect($this, "editNews");
530  }
531 
536  public function cancelUpdateNewsItem()
537  {
538  return $this->editNews();
539  }
540 
545  public function cancelSaveNewsItem()
546  {
548 
549  if ($_GET["add_mode"] == "block") {
550  $ilCtrl->returnToParent($this);
551  } else {
552  return $this->editNews();
553  }
554  }
555 
561  public function editNews()
562  {
563  $ilTabs = $this->tabs;
564  $ilToolbar = $this->toolbar;
565  $lng = $this->lng;
567 
568  $this->setTabs();
569 
570  $ilToolbar->addButton(
571  $lng->txt("news_add_news"),
572  $ilCtrl->getLinkTarget($this, "createNewsItem")
573  );
574 
575  if (!$this->getEnableEdit()) {
576  return;
577  }
578  return $this->getNewsForContextTable();
579  }
580 
584  public function cancelUpdate()
585  {
586  return $this->editNews();
587  }
588 
592  public function confirmDeletionNewsItems()
593  {
595  $lng = $this->lng;
596  $ilTabs = $this->tabs;
597 
598  if (!$this->getEnableEdit()) {
599  return;
600  }
601 
602  // check whether at least one item is selected
603  if (count($_POST["news_id"]) == 0) {
604  ilUtil::sendFailure($lng->txt("no_checkbox"));
605  return $this->editNews();
606  }
607 
608  $ilTabs->clearTargets();
609 
610  include_once("Services/Utilities/classes/class.ilConfirmationGUI.php");
611  $c_gui = new ilConfirmationGUI();
612 
613  // set confirm/cancel commands
614  $c_gui->setFormAction($ilCtrl->getFormAction($this, "deleteNewsItems"));
615  $c_gui->setHeaderText($lng->txt("info_delete_sure"));
616  $c_gui->setCancel($lng->txt("cancel"), "editNews");
617  $c_gui->setConfirm($lng->txt("confirm"), "deleteNewsItems");
618 
619  // add items to delete
620  foreach ($_POST["news_id"] as $news_id) {
621  $news = new ilNewsItem($news_id);
622  $c_gui->addItem("news_id[]", $news_id, $news->getTitle());
623  }
624 
625  return $c_gui->getHTML();
626  }
627 
631  public function deleteNewsItems()
632  {
633  if (!$this->getEnableEdit()) {
634  return;
635  }
636  // delete all selected news items
637  foreach ($_POST["news_id"] as $news_id) {
638  $news = new ilNewsItem($news_id);
639  $news->delete();
640  }
641 
642  return $this->editNews();
643  }
644 
649  public function getNewsForContextBlock()
650  {
651  $lng = $this->lng;
652 
653  include_once("Services/News/classes/class.ilNewsForContextBlockGUI.php");
654  $block_gui = new ilNewsForContextBlockGUI(get_class($this));
655 
656  $block_gui->setParentClass("ilinfoscreengui");
657  $block_gui->setParentCmd("showSummary");
658  $block_gui->setEnableEdit($this->getEnableEdit());
659 
660 
661  $news_item = new ilNewsItem();
662 
663  // changed
664  //$news_item->setContextObjId($this->ctrl->getContextObjId());
665  //$news_item->setContextObjType($this->ctrl->getContextObjType());
666  $news_item->setContextObjId($this->getContextObjId());
667  $news_item->setContextObjType($this->getContextObjType());
668  $news_item->setContextSubObjId($this->getContextSubObjId());
669  $news_item->setContextSubObjType($this->getContextSubObjType());
670 
671  $data = $news_item->queryNewsForContext();
672 
673  $block_gui->setTitle($lng->txt("news_block_news_for_context"));
674  $block_gui->setRowTemplate("tpl.block_row_news_for_context.html", "Services/News");
675  $block_gui->setData($data);
676 
677  return $block_gui->getHTML();
678  }
679 
680 
685  public function getNewsForContextTable()
686  {
687  $lng = $this->lng;
688 
689  $news_item = new ilNewsItem();
690  $news_item->setContextObjId($this->getContextObjId());
691  $news_item->setContextObjType($this->getContextObjType());
692  $news_item->setContextSubObjId($this->getContextSubObjId());
693  $news_item->setContextSubObjType($this->getContextSubObjType());
694 
695  $perm_ref_id = 0;
696  if (in_array($this->getContextObjType(), array("cat", "grp", "crs", "root"))) {
697  $data = $news_item->getNewsForRefId(
698  $_GET["ref_id"],
699  false,
700  false,
701  0,
702  true,
703  false,
704  true,
705  true
706  );
707  } else {
708  $perm_ref_id = $_GET["ref_id"];
709  if ($this->getContextSubObjId() > 0) {
710  $data = $news_item->queryNewsForContext(
711  false,
712  0,
713  "",
714  true,
715  true
716  );
717  } else {
718  $data = $news_item->queryNewsForContext();
719  }
720  }
721 
722  include_once("Services/News/classes/class.ilNewsForContextTableGUI.php");
723  $table_gui = new ilNewsForContextTableGUI($this, "getNewsForContextTable", $perm_ref_id);
724 
725  $table_gui->setTitle($lng->txt("news_table_news_for_context"));
726  $table_gui->setRowTemplate("tpl.table_row_news_for_context.html", "Services/News");
727  $table_gui->setData($data);
728 
729  $table_gui->setDefaultOrderField("creation_date");
730  $table_gui->setDefaultOrderDirection("desc");
731  $table_gui->addMultiCommand("confirmDeletionNewsItems", $lng->txt("delete"));
732  $table_gui->setTitle($lng->txt("news"));
733  $table_gui->setSelectAllCheckbox("news_id");
734 
735 
736  return $table_gui->getHTML();
737  }
738 
745  public function setTabs()
746  {
747  $ilTabs = $this->tabs;
749  $lng = $this->lng;
750 
751  $ilTabs->clearTargets();
752  $ilTabs->setBackTarget(
753  $lng->txt("back"),
754  $ilCtrl->getParentReturn($this)
755  );
756  }
757 
763  public static function isRteActivated()
764  {
765  include_once("./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php");
767  return false;
768  }
769  return true;
770  }
771 }
This class represents an option in a radio group.
getContextSubObjId()
Get ContextSubObjId.
getItemByPostVar($a_post_var)
Get Item by POST variable.
static isRteActivated()
Is Rte activated.
static _getRichTextEditor()
Returns the identifier for the Rich Text Editor.
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
getNewsForContextBlock()
BLOCK NewsForContext: Get block HTML.
$_GET["client_id"]
This class represents a file property in a property form.
editNews()
Edit news.
setContextObjId($a_context_obj_id)
Set ContextObjId.
getFormEditMode()
Get FormEditMode.
User Interface for NewsItem entities.
confirmDeletionNewsItems()
Confirmation Screen.
getContextObjId()
Get ContextObjId.
static _lookupTitle($a_id)
lookup object title
BlockGUI class for block NewsForContext.
saveNewsItem()
FORM NewsItem: Save NewsItem.
getEnableEdit()
Get EnableEdit.
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=true)
Create new media object and update page in db and return new media object.
getValuesNewsItem(\ilPropertyFormGUI $a_form)
FORM NewsItem: Get current values for NewsItem form.
setInfo($a_info)
Set Info.
getContextObjType()
Get ContextObjType.
user()
Definition: user.php:4
deleteNewsItems()
Delete news items.
global $ilCtrl
Definition: ilias.php:18
setInfo($a_info)
Set Information Text.
cancelUpdateNewsItem()
FORM NewsItem: Save NewsItem.
This class represents a property in a property form.
initFormNewsItem($a_mode)
FORM NewsItem: Init form.
if(isset($_POST['submit'])) $form
$values
setContextSubObjId($a_context_sub_obj_id)
Set ContextSubObjId.
getContextSubObjType()
Get ContextSubObjType.
This class represents a text property in a property form.
setContextSubObjType($a_context_sub_obj_type)
Set ContextSubObjType.
$ilUser
Definition: imgupload.php:18
createNewsItem()
FORM NewsItem: Create NewsItem.
Class ilObjMediaObject.
editNewsItem()
FORM NewsItem: Edit form.
const IL_FORM_EDIT
getNewsForContextTable()
TABLE NewsForContext: Get table HTML.
static _lookupType($a_id, $a_reference=false)
lookup object type
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
This class represents a text area property in a property form.
TableGUI class for table NewsForContext.
static _getDefaultVisibilityForRefId($a_ref_id)
Get default visibility for reference id.
static _lookupContextObjId($a_news_id)
Context Object ID.
cancelSaveNewsItem()
FORM NewsItem: Save NewsItem.
const IL_FORM_RE_CREATE
const IL_FORM_CREATE
setContextObjType($a_context_obj_type)
Set ContextObjType.
setEnableEdit($a_enable_edit=0)
Set EnableEdit.
static getEditForm($a_mode, $a_ref_id)
FORM NewsItem: Init form.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
$_POST["username"]
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
$html
Definition: example_001.php:87
setFormEditMode($a_form_edit_mode)
Set FormEditMode.
executeCommand()
Execute command.
cancelUpdate()
Cancel update.
Confirmation screen class.
__construct()
Constructor.
$data
Definition: bench.php:6
updateNewsItem()
FORM NewsItem: Save NewsItem.