1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Timeline for news
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 * @version $Id$
10 *
11 * @ilCtrl_Calls ilNewsTimelineGUI: ilLikeGUI, ilNoteGUI
12 *
13 * @ingroup ServicesNews
14 */
15class ilNewsTimelineGUI
16{
17    /**
18     * @var ilCtrl
19     */
20    protected $ctrl;
21
22    /**
23     * @var ilTemplate
24     */
25    protected $tpl;
26
27    /**
28     * @var int
29     */
30    protected $ref_id;
31
32    /**
33     * @var ilLanguage
34     */
35    protected $lng;
36
37    /**
38     * @var ilToolbarGUI
39     */
40    protected $toolbar;
41
42    /**
43     * @var ilObjUser
44     */
45    protected $user;
46
47    /**
48     * @var ilAccessHandler
49     */
50    protected $access;
51
52    /**
53     * @var int
54     */
55    protected static $items_per_load = 10;
56
57    /**
58     * @var bool
59     */
60    protected $user_edit_all = false;
61
62    /**
63     * Constructor
64     *
65     * @param int $a_ref_id reference id
66     */
67    protected function __construct($a_ref_id, $a_include_auto_entries)
68    {
69        global $DIC;
70
71        $this->ctrl = $DIC->ctrl();
72        $this->tpl = $DIC["tpl"];
73        $this->ref_id = $a_ref_id;
74        $this->lng = $DIC->language();
75        $this->toolbar = $DIC->toolbar();
76        $this->user = $DIC->user();
77        $this->include_auto_entries = $a_include_auto_entries;
78        $this->access = $DIC->access();
79
80        $this->news_id = (int) $_GET["news_id"];
81
82
83        $this->lng->loadLanguageModule("news");
84    }
85
86    /**
87     * Set user can edit other users postings
88     *
89     * @param bool $a_val user can edit all postings
90     */
91    public function setUserEditAll($a_val)
92    {
93        $this->user_edit_all = $a_val;
94    }
95
96    /**
97     * Get user can edit other users postings
98     *
99     * @return bool user can edit all postings
100     */
101    public function getUserEditAll()
102    {
103        return $this->user_edit_all;
104    }
105
106    /**
107     * Get instance
108     *
109     * @param int $a_ref_id reference id
110     * @return ilNewsTimelineGUI
111     */
112    public static function getInstance($a_ref_id, $a_include_auto_entries)
113    {
114        return new self($a_ref_id, $a_include_auto_entries);
115    }
116
117    /**
118     * Execute command
119     *
120     * @throws ilCtrlException
121     */
122    public function executeCommand()
123    {
124        $ctrl = $this->ctrl;
125
126        $next_class = $this->ctrl->getNextClass($this);
127        $cmd = $this->ctrl->getCmd("show");
128
129        switch ($next_class) {
130            case "illikegui":
131                $i = new ilNewsItem($this->news_id);
132                include_once("./Services/Like/classes/class.ilLikeFactoryGUI.php");
133                $likef = new ilLikeFactoryGUI();
134                $like_gui = $likef->widget(array($i->getContextObjId()));
135                $ctrl->saveParameter($this, "news_id");
136                $like_gui->setObject(
137                    $i->getContextObjId(),
138                    $i->getContextObjType(),
139                    $i->getContextSubObjId(),
140                    $i->getContextSubObjType(),
141                    $this->news_id
142                );
143                $ret = $ctrl->forwardCommand($like_gui);
144                break;
145
146            case "ilnotegui":
147                $i = new ilNewsItem($this->news_id);
148                $ctrl->saveParameter($this, "news_id");
149                $notes_obj_type = ($i->getContextSubObjType() == "")
150                    ? $i->getContextObjType()
151                    : $i->getContextSubObjType();
152                $note_gui = new ilNoteGUI(
153                    $i->getContextObjId(),
154                    $i->getContextSubObjId(),
155                    $notes_obj_type,
156                    false,
157                    $i->getId()
158                );
159                $ret = $ctrl->forwardCommand($note_gui);
160                break;
161
162            default:
163                if (in_array($cmd, array("show", "save", "update", "loadMore", "remove", "updateNewsItem"))) {
164                    $this->$cmd();
165                }
166        }
167    }
168
169    /**
170     * Show
171     *
172     * @param
173     * @return
174     */
175    public function show()
176    {
177        // toolbar
178        if ($this->access->checkAccess("news_add_news", "", $this->ref_id)) {
179            $b = ilLinkButton::getInstance();
180            $b->setCaption('news_add_news');
181            $b->setOnClick("return il.News.create();");
182            $b->setPrimary(true);
183            $this->toolbar->addButtonInstance($b);
184        }
185
186        include_once("./Services/News/classes/class.ilNewsItem.php");
187        $news_item = new ilNewsItem();
188        $news_item->setContextObjId($this->ctrl->getContextObjId());
189        $news_item->setContextObjType($this->ctrl->getContextObjType());
190
191        $news_data = $news_item->getNewsForRefId(
192            $this->ref_id,
193            false,
194            false,
195            0,
196            true,
197            false,
198            !$this->include_auto_entries,
199            false,
200            null,
201            self::$items_per_load
202        );
203
204        include_once("./Services/News/Timeline/classes/class.ilTimelineGUI.php");
205        include_once("./Services/News/classes/class.ilNewsTimelineItemGUI.php");
206        $timeline = ilTimelineGUI::getInstance();
207
208        // get like widget
209        $obj_ids = array_unique(array_map(function ($a) {
210            return $a["context_obj_id"];
211        }, $news_data));
212        include_once("./Services/Like/classes/class.ilLikeFactoryGUI.php");
213        $likef = new ilLikeFactoryGUI();
214        $like_gui = $likef->widget($obj_ids);
215
216        $js_items = array();
217        foreach ($news_data as $d) {
218            $news_item = new ilNewsItem($d["id"]);
219            $item = ilNewsTimelineItemGUI::getInstance($news_item, $d["ref_id"], $like_gui);
220            $item->setUserEditAll($this->getUserEditAll());
221            $timeline->addItem($item);
222            $js_items[$d["id"]] = array(
223                "id" => $d["id"],
224                "user_id" => $d["user_id"],
225                "title" => $d["title"],
226                "content" => $d["content"] . $d["content_long"],
227                "content_long" => "",
228                "priority" => $d["priority"],
229                "visibility" => $d["visibility"],
230                "content_type" => $d["content_type"],
231                "mob_id" => $d["mob_id"]
232            );
233        }
234
235        include_once("./Services/JSON/classes/class.ilJsonUtil.php");
236        $this->tpl->addOnloadCode("il.News.setItems(" . ilJsonUtil::encode($js_items) . ");");
237        $this->tpl->addOnloadCode("il.News.setAjaxUrl('" . $this->ctrl->getLinkTarget($this, "", "", true) . "');");
238
239        if (count($news_data) > 0) {
240            $ttpl = new ilTemplate("tpl.news_timeline.html", true, true, "Services/News");
241            $ttpl->setVariable("NEWS", $timeline->render());
242            $ttpl->setVariable("EDIT_MODAL", $this->getEditModal());
243            $ttpl->setVariable("DELETE_MODAL", $this->getDeleteModal());
244            $ttpl->setVariable("LOADER", ilUtil::getImagePath("loader.svg"));
245            $this->tpl->setContent($ttpl->get());
246        } else {
247            ilUtil::sendInfo($this->lng->txt("news_timline_add_entries_info"));
248            $this->tpl->setContent($this->getEditModal());
249        }
250
251        $this->lng->toJS("create");
252        $this->lng->toJS("edit");
253        $this->lng->toJS("update");
254        $this->lng->toJS("save");
255
256        $this->tpl->addJavaScript("./Services/News/js/News.js");
257        include_once("./Services/MediaObjects/classes/class.ilMediaPlayerGUI.php");
258        ilMediaPlayerGUI::initJavascript($this->tpl);
259    }
260
261    /**
262     * Load more
263     *
264     * @param
265     * @return
266     */
267    public function loadMore()
268    {
269        include_once("./Services/News/classes/class.ilNewsItem.php");
270        $news_item = new ilNewsItem();
271        $news_item->setContextObjId($this->ctrl->getContextObjId());
272        $news_item->setContextObjType($this->ctrl->getContextObjType());
273
274        $excluded = $_POST["rendered_news"];
275
276        $news_data = $news_item->getNewsForRefId(
277            $this->ref_id,
278            false,
279            false,
280            0,
281            true,
282            false,
283            !$this->include_auto_entries,
284            false,
285            null,
286            self::$items_per_load,
287            $excluded
288        );
289
290        include_once("./Services/News/Timeline/classes/class.ilTimelineGUI.php");
291        include_once("./Services/News/classes/class.ilNewsTimelineItemGUI.php");
292        $timeline = ilTimelineGUI::getInstance();
293
294        // get like widget
295        $obj_ids = array_unique(array_map(function ($a) {
296            return $a["context_obj_id"];
297        }, $news_data));
298        include_once("./Services/Like/classes/class.ilLikeFactoryGUI.php");
299        $likef = new ilLikeFactoryGUI();
300        $like_gui = $likef->widget($obj_ids);
301
302        $js_items = array();
303        foreach ($news_data as $d) {
304            $news_item = new ilNewsItem($d["id"]);
305            $item = ilNewsTimelineItemGUI::getInstance($news_item, $d["ref_id"], $like_gui);
306            $item->setUserEditAll($this->getUserEditAll());
307            $timeline->addItem($item);
308            $js_items[$d["id"]] = array(
309                "id" => $d["id"],
310                "user_id" => $d["user_id"],
311                "title" => $d["title"],
312                "content" => $d["content"] . $d["content_long"],
313                "content_long" => "",
314                "priority" => $d["priority"],
315                "visibility" => $d["visibility"],
316                "content_type" => $d["content_type"],
317                "mob_id" => $d["mob_id"]
318            );
319        }
320
321        include_once("./Services/JSON/classes/class.ilJsonUtil.php");
322        $obj = new stdClass();
323        $obj->data = $js_items;
324        $obj->html = $timeline->render(true);
325
326        echo ilJsonUtil::encode($obj);
327        exit;
328    }
329
330
331    /**
332     *
333     *
334     * @param
335     */
336    protected function updateNewsItem()
337    {
338        if ($_POST["news_action"] == "save") {
339            $this->save();
340            $this->ctrl->redirect($this, "show");
341        }
342        if ($_POST["news_action"] == "update") {
343            $this->update();
344            $this->ctrl->redirect($this, "show");
345        }
346    }
347
348
349    /**
350     * Save (ajax)
351     */
352    public function save()
353    {
354        include_once("./Services/News/classes/class.ilNewsItemGUI.php");
355        $form = ilNewsItemGUI::getEditForm(IL_FORM_CREATE, $this->ref_id);
356        if ($form->checkInput()) {
357            $news_item = new ilNewsItem();
358            $news_item->setTitle($form->getInput("news_title"));
359            $news_item->setContent($form->getInput("news_content"));
360            $news_item->setVisibility($form->getInput("news_visibility"));
361            include_once("./Services/News/classes/class.ilNewsItemGUI.php");
362            if (ilNewsItemGUI::isRteActivated()) {
363                $news_item->setContentHtml(true);
364            }
365            //$news_item->setContentLong($form->getInput("news_content_long"));
366            $news_item->setContentLong("");
367
368            $obj_id = ilObject::_lookupObjectId($this->ref_id);
369            $obj_type = ilObject::_lookupType($obj_id);
370            $news_item->setContextObjId($obj_id);
371            $news_item->setContextObjType($obj_type);
372            $news_item->setUserId($this->user->getId());
373
374            $media = $_FILES["media"];
375            if ($media["name"] != "") {
376                $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
377                $news_item->setMobId($mob->getId());
378            }
379
380            $news_set = new ilSetting("news");
381            if (!$news_set->get("enable_rss_for_internal")) {
382                $news_item->setVisibility("users");
383            }
384
385            $news_item->create();
386        }
387    }
388
389
390
391    /**
392     * Update (ajax)
393     */
394    public function update()
395    {
396        include_once("./Services/News/classes/class.ilNewsItemGUI.php");
397        $form = ilNewsItemGUI::getEditForm(IL_FORM_EDIT, $this->ref_id);
398        if ($form->checkInput()) {
399            $news_item = new ilNewsItem((int) $_POST["id"]);
400            $news_item->setTitle($form->getInput("news_title"));
401            $news_item->setContent($form->getInput("news_content"));
402            $news_item->setVisibility($form->getInput("news_visibility"));
403            //$news_item->setContentLong($form->getInput("news_content_long"));
404            include_once("./Services/News/classes/class.ilNewsItemGUI.php");
405            if (ilNewsItemGUI::isRteActivated()) {
406                $news_item->setContentHtml(true);
407            }
408            $news_item->setContentLong("");
409
410            $media = $_FILES["media"];
411            $old_mob_id = 0;
412
413            // delete old media object
414            if ($media["name"] != "" || $_POST["media_delete"] != "") {
415                if ($news_item->getMobId() > 0 && ilObject::_lookupType($news_item->getMobId()) == "mob") {
416                    $old_mob_id = $news_item->getMobId();
417                }
418                $news_item->setMobId(0);
419            }
420
421            if ($media["name"] != "") {
422                $mob = ilObjMediaObject::_saveTempFileAsMediaObject($media["name"], $media["tmp_name"], true);
423                $news_item->setMobId($mob->getId());
424            }
425
426            $obj_id = ilObject::_lookupObjectId($this->ref_id);
427
428            if ($news_item->getContextObjId() == $obj_id) {
429                $news_item->setUpdateUserId($this->user->getId());
430                $news_item->update();
431
432                if ($old_mob_id > 0) {
433                    include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
434                    $old_mob = new ilObjMediaObject($old_mob_id);
435                    $old_mob->delete();
436                }
437            }
438        }
439    }
440
441    /**
442     * Remove (ajax)
443     */
444    public function remove()
445    {
446        include_once("./Services/News/classes/class.ilNewsItemGUI.php");
447        $news_item = new ilNewsItem((int) $_POST["id"]);
448        if ($this->user->getId() == $news_item->getUserId() || $this->getUserEditAll()) {
449            $news_item->delete();
450        }
451        exit;
452    }
453
454    /**
455     * Get edit modal
456     *
457     * @return string modal html
458     */
459    protected function getEditModal()
460    {
461        include_once("./Services/UIComponent/Modal/classes/class.ilModalGUI.php");
462        $modal = ilModalGUI::getInstance();
463        $modal->setHeading($this->lng->txt("edit"));
464        $modal->setId("ilNewsEditModal");
465        $modal->setType(ilModalGUI::TYPE_LARGE);
466
467        include_once("./Services/News/classes/class.ilNewsItemGUI.php");
468        $form = ilNewsItemGUI::getEditForm(IL_FORM_EDIT, $this->ref_id);
469        $form->setShowTopButtons(false);
470        $form->setFormAction($this->ctrl->getFormAction($this));
471
472
473        //
474        $hi = new ilHiddenInputGUI("id");
475        $form->addItem($hi);
476        $act = new ilHiddenInputGUI("news_action");
477        $form->addItem($act);
478        $form->setId("news_edit_form");
479
480        $modal->setBody($form->getHTML());
481
482        return $modal->getHTML();
483    }
484
485    /**
486     * Get delete modal
487     *
488     * @return string modal html
489     */
490    protected function getDeleteModal()
491    {
492        include_once("./Services/UIComponent/Modal/classes/class.ilModalGUI.php");
493        $modal = ilModalGUI::getInstance();
494        $modal->setHeading($this->lng->txt("delete"));
495        $modal->setId("ilNewsDeleteModal");
496        $modal->setType(ilModalGUI::TYPE_LARGE);
497
498        require_once 'Services/UIComponent/Button/classes/class.ilSubmitButton.php';
499        $confirm = ilSubmitButton::getInstance();
500        $confirm->setCaption("delete");
501        $confirm->setId("news_btn_delete");
502        $modal->addButton($confirm);
503
504        $cancel = ilSubmitButton::getInstance();
505        $cancel->setCaption("cancel");
506        $cancel->setId("news_btn_cancel_delete");
507        $modal->addButton($cancel);
508
509        $modal->setBody("<p id='news_delete_news_title'></p>" .
510            $this->tpl->getMessageHTML($this->lng->txt("news_really_delete_news"), "question"));
511
512        return $modal->getHTML();
513    }
514}
515