1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once("./Services/News/classes/class.ilNewsItem.php");
6
7define("IL_FORM_EDIT", 0);
8define("IL_FORM_CREATE", 1);
9define("IL_FORM_RE_EDIT", 2);
10define("IL_FORM_RE_CREATE", 3);
11
12/**
13 * User Interface for NewsItem entities.
14 *
15 * @author Alex Killing <alex.killing@gmx.de>
16 * @version $Id$
17 *
18 * @ingroup ServicesNews
19 */
20class ilNewsItemGUI
21{
22    /**
23     * @var ilCtrl
24     */
25    protected $ctrl;
26
27    /**
28     * @var ilLanguage
29     */
30    protected $lng;
31
32    /**
33     * @var ilTabsGUI
34     */
35    protected $tabs;
36
37    /**
38     * @var ilObjUser
39     */
40    protected $user;
41
42    /**
43     * @var ilToolbarGUI
44     */
45    protected $toolbar;
46
47    protected $enable_edit = 0;
48    protected $context_obj_id;
49    protected $context_obj_type;
50    protected $context_sub_obj_id;
51    protected $context_sub_obj_type;
52    protected $form_edit_mode;
53
54
55    /**
56     * Constructor
57     */
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
92    /**
93     * Get html
94     *
95     * @return string	html
96     */
97    public function getHTML()
98    {
99        $lng = $this->lng;
100        $ilCtrl = $this->ctrl;
101
102        $lng->LoadLanguageModule("news");
103
104        return $this->getNewsForContextBlock();
105    }
106
107    /**
108     * Execute command.
109     *
110     */
111    public function executeCommand()
112    {
113        $ilCtrl = $this->ctrl;
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
135    /**
136     * Set EnableEdit.
137     *
138     * @param	boolean	$a_enable_edit	Edit mode on/off
139     */
140    public function setEnableEdit($a_enable_edit = 0)
141    {
142        $this->enable_edit = $a_enable_edit;
143    }
144
145    /**
146     * Get EnableEdit.
147     *
148     * @return	boolean	Edit mode on/off
149     */
150    public function getEnableEdit()
151    {
152        return $this->enable_edit;
153    }
154
155    /**
156     * Set ContextObjId.
157     *
158     * @param	int	$a_context_obj_id
159     */
160    public function setContextObjId($a_context_obj_id)
161    {
162        $this->context_obj_id = $a_context_obj_id;
163    }
164
165    /**
166     * Get ContextObjId.
167     *
168     * @return	int
169     */
170    public function getContextObjId()
171    {
172        return $this->context_obj_id;
173    }
174
175    /**
176     * Set ContextObjType.
177     *
178     * @param	int	$a_context_obj_type
179     */
180    public function setContextObjType($a_context_obj_type)
181    {
182        $this->context_obj_type = $a_context_obj_type;
183    }
184
185    /**
186     * Get ContextObjType.
187     *
188     * @return	int
189     */
190    public function getContextObjType()
191    {
192        return $this->context_obj_type;
193    }
194
195    /**
196     * Set ContextSubObjId.
197     *
198     * @param	int	$a_context_sub_obj_id
199     */
200    public function setContextSubObjId($a_context_sub_obj_id)
201    {
202        $this->context_sub_obj_id = $a_context_sub_obj_id;
203    }
204
205    /**
206     * Get ContextSubObjId.
207     *
208     * @return	int
209     */
210    public function getContextSubObjId()
211    {
212        return $this->context_sub_obj_id;
213    }
214
215    /**
216     * Set ContextSubObjType.
217     *
218     * @param	int	$a_context_sub_obj_type
219     */
220    public function setContextSubObjType($a_context_sub_obj_type)
221    {
222        $this->context_sub_obj_type = $a_context_sub_obj_type;
223    }
224
225    /**
226     * Get ContextSubObjType.
227     *
228     * @return	int
229     */
230    public function getContextSubObjType()
231    {
232        return $this->context_sub_obj_type;
233    }
234
235    /**
236     * Set FormEditMode.
237     *
238     * @param	int	$a_form_edit_mode	Form Edit Mode (IL_FORM_EDIT | IL_FORM_CREATE | IL_FORM_RE_EDIT | IL_FORM_RE_CREATE)
239     */
240    public function setFormEditMode($a_form_edit_mode)
241    {
242        $this->form_edit_mode = $a_form_edit_mode;
243    }
244
245    /**
246     * Get FormEditMode.
247     *
248     * @return	int	Form Edit Mode (IL_FORM_EDIT | IL_FORM_CREATE | IL_FORM_RE_EDIT | IL_FORM_RE_CREATE)
249     */
250    public function getFormEditMode()
251    {
252        return $this->form_edit_mode;
253    }
254
255    /**
256     * FORM NewsItem: Create NewsItem.
257     *
258     */
259    public function createNewsItem()
260    {
261        $form = $this->initFormNewsItem(IL_FORM_CREATE);
262        return $form->getHtml();
263    }
264
265    /**
266     * FORM NewsItem: Edit form.
267     *
268     */
269    public function editNewsItem()
270    {
271        $form = $this->initFormNewsItem(IL_FORM_EDIT);
272        $this->getValuesNewsItem($form);
273        return $form->getHtml();
274    }
275
276
277    /**
278     * FORM NewsItem: Init form.
279     *
280     * @param int $a_mode	Form Edit Mode (IL_FORM_EDIT | IL_FORM_CREATE)
281     * @return ilPropertyFormGUI form
282     */
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
294    /**
295     * FORM NewsItem: Init form.
296     *
297     * @param	int	$a_mode	Form Edit Mode (IL_FORM_EDIT | IL_FORM_CREATE)
298     * @return ilPropertyFormGUI form
299     */
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
381    /**
382     * FORM NewsItem: Get current values for NewsItem form.
383     *
384     */
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
403    /**
404     * FORM NewsItem: Save NewsItem.
405     *
406     */
407    public function saveNewsItem()
408    {
409        $ilUser = $this->user;
410
411        if (!$this->getEnableEdit()) {
412            return;
413        }
414
415        $form = $this->initFormNewsItem(IL_FORM_CREATE);
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    {
462        $ilCtrl = $this->ctrl;
463
464        if ($_GET["add_mode"] == "block") {
465            $ilCtrl->returnToParent($this);
466        } else {
467            $ilCtrl->redirect($this, "editNews");
468        }
469    }
470
471    /**
472    * FORM NewsItem: Save NewsItem.
473    *
474    */
475    public function updateNewsItem()
476    {
477        $ilUser = $this->user;
478
479        if (!$this->getEnableEdit()) {
480            return "";
481        }
482
483        $form = $this->initFormNewsItem(IL_FORM_EDIT);
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    {
527        $ilCtrl = $this->ctrl;
528
529        $ilCtrl->redirect($this, "editNews");
530    }
531
532    /**
533    * FORM NewsItem: Save NewsItem.
534    *
535    */
536    public function cancelUpdateNewsItem()
537    {
538        return $this->editNews();
539    }
540
541    /**
542    * FORM NewsItem: Save NewsItem.
543    *
544    */
545    public function cancelSaveNewsItem()
546    {
547        $ilCtrl = $this->ctrl;
548
549        if ($_GET["add_mode"] == "block") {
550            $ilCtrl->returnToParent($this);
551        } else {
552            return $this->editNews();
553        }
554    }
555
556    /**
557     * Edit news
558     *
559     * @return html
560     */
561    public function editNews()
562    {
563        $ilTabs = $this->tabs;
564        $ilToolbar = $this->toolbar;
565        $lng = $this->lng;
566        $ilCtrl = $this->ctrl;
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
581    /**
582     * Cancel update
583     */
584    public function cancelUpdate()
585    {
586        return $this->editNews();
587    }
588
589    /**
590    * Confirmation Screen.
591    */
592    public function confirmDeletionNewsItems()
593    {
594        $ilCtrl = $this->ctrl;
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
628    /**
629    * Delete news items.
630    */
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
645    /**
646     * BLOCK NewsForContext: Get block HTML.
647     *
648     */
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
681    /**
682     * TABLE NewsForContext: Get table HTML.
683     *
684     */
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
739    /**
740     * Set tabs
741     *
742     * @param
743     * @return
744     */
745    public function setTabs()
746    {
747        $ilTabs = $this->tabs;
748        $ilCtrl = $this->ctrl;
749        $lng = $this->lng;
750
751        $ilTabs->clearTargets();
752        $ilTabs->setBackTarget(
753            $lng->txt("back"),
754            $ilCtrl->getParentReturn($this)
755        );
756    }
757
758    /**
759     * Is Rte activated
760     *
761     * @return bool
762     */
763    public static function isRteActivated()
764    {
765        include_once("./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php");
766        if (ilObjAdvancedEditing::_getRichTextEditor() == "") {
767            return false;
768        }
769        return true;
770    }
771}
772