1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Single news timeline item
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 * @version $Id$
10 * @ingroup ServicesNews
11 */
12class ilNewsTimelineItemGUI implements ilTimelineItemInt
13{
14    /**
15     * @var ilLanguage
16     */
17    protected $lng;
18
19    /**
20     * @var ilNewsItem
21     */
22    protected $news_item;
23
24    /**
25     * @var ilObjectDefinition
26     */
27    protected $obj_def;
28
29    /**
30     * @var ilObjUser
31     */
32    protected $user;
33
34    /**
35     * @var bool
36     */
37    protected $user_edit_all;
38
39    /**
40     * Ref ID of news item
41     *
42     * @var int
43     */
44    protected $news_item_ref_id;
45
46    /**
47     * Ref id of timeline container
48     * @var int
49     */
50    protected $ref_id;
51
52    /**
53     * @var ilCtrl
54     */
55    protected $ctrl;
56
57    /**
58     * @var \ilLikeGUI
59     */
60    protected $like_gui;
61
62    /**
63     * Constructor
64     *
65     * @param ilNewsItem $a_news_item
66     * @param $a_news_ref_id
67     * @param ilLikeGUI $a_like_gui
68     */
69    protected function __construct(ilNewsItem $a_news_item, $a_news_ref_id, \ilLikeGUI $a_like_gui)
70    {
71        global $DIC;
72
73        $this->like_gui = $a_like_gui;
74
75        $this->lng = $DIC->language();
76        $this->ctrl = $DIC->ctrl();
77        $this->setNewsItem($a_news_item);
78        $this->user = $DIC->user();
79        $this->obj_def = $DIC["objDefinition"];
80        $this->news_item_ref_id = $a_news_ref_id;
81
82        $this->ref_id = (int) $_GET["ref_id"];
83    }
84
85    /**
86     * Get instance
87     *
88     * @param ilNewsItem $a_news_item news item
89     * @return ilNewsTimelineItemGUI
90     */
91    public static function getInstance(ilNewsItem $a_news_item, $a_news_ref_id, \ilLikeGUI $a_like_gui)
92    {
93        return new self($a_news_item, $a_news_ref_id, $a_like_gui);
94    }
95
96
97    /**
98     * Set news item
99     *
100     * @param ilNewsItem $a_val news item
101     */
102    public function setNewsItem(ilNewsItem $a_val)
103    {
104        $this->news_item = $a_val;
105    }
106
107    /**
108     * Get news item
109     *
110     * @return ilNewsItem news item
111     */
112    public function getNewsItem()
113    {
114        return $this->news_item;
115    }
116
117    /**
118     * Set user can edit other users postings
119     *
120     * @param bool $a_val user can edit all postings
121     */
122    public function setUserEditAll($a_val)
123    {
124        $this->user_edit_all = $a_val;
125    }
126
127    /**
128     * Get user can edit other users postings
129     *
130     * @return bool user can edit all postings
131     */
132    public function getUserEditAll()
133    {
134        return $this->user_edit_all;
135    }
136
137    /**
138     * @inheritdoc
139     */
140    public function getDateTime()
141    {
142        $i = $this->getNewsItem();
143        return new ilDateTime($i->getCreationDate(), IL_CAL_DATETIME);
144    }
145
146
147    /**
148     * @inheritdoc
149     */
150    public function render()
151    {
152        $i = $this->getNewsItem();
153        $tpl = new ilTemplate("tpl.timeline_item.html", true, true, "Services/News");
154
155        include_once("./Services/News/classes/class.ilNewsRendererFactory.php");
156        $news_renderer = ilNewsRendererFactory::getRenderer($i->getContextObjType());
157        $news_renderer->setLanguage($this->lng->getLangKey());
158        $news_renderer->setNewsItem($i, $this->news_item_ref_id);
159
160        $obj_id = $i->getContextObjId();
161
162        // edited?
163        if ($i->getCreationDate() != $i->getUpdateDate()) {
164            $tpl->setCurrentBlock("edited");
165            $update_date = new ilDateTime($i->getUpdateDate(), IL_CAL_DATETIME);
166            $tpl->setVariable("TXT_EDITED", $this->lng->txt("cont_news_edited"));
167            if ($i->getUpdateUserId() > 0 && ($i->getUpdateUserId() != $i->getUserId())) {
168                include_once("./Services/User/classes/class.ilUserUtil.php");
169                $tpl->setVariable("TXT_USR_EDITED", ilUserUtil::getNamePresentation(
170                    $i->getUpdateUserId(),
171                    false,
172                    true,
173                    $this->ctrl->getLinkTargetByClass("ilnewstimelinegui")
174                ) . " - ");
175            }
176            include_once("./Services/Calendar/classes/class.ilDatePresentation.php");
177            $tpl->setVariable("TIME_EDITED", ilDatePresentation::formatDate($update_date));
178            $tpl->parseCurrentBlock();
179        }
180
181
182        // context object link
183        include_once("./Services/Link/classes/class.ilLink.php");
184        if ($this->news_item_ref_id > 0 && $this->ref_id != $this->news_item_ref_id) {
185            $tpl->setCurrentBlock("object");
186            $tpl->setVariable("OBJ_TITLE", ilObject::_lookupTitle($obj_id));
187            $tpl->setVariable("OBJ_IMG", ilObject::_getIcon($obj_id));
188            $tpl->setVariable("OBJ_HREF", $news_renderer->getObjectLink());
189            $tpl->parseCurrentBlock();
190        }
191
192        // media
193        if ($i->getMobId() > 0 && ilObject::_exists($i->getMobId())) {
194            $media = $this->renderMedia($i);
195            $tpl->setCurrentBlock("player");
196            $tpl->setVariable("PLAYER", $media);
197            $tpl->parseCurrentBlock();
198        }
199
200        $tpl->setVariable("USER_IMAGE", ilObjUser::_getPersonalPicturePath($i->getUserId(), "small"));
201        $tpl->setVariable(
202            "TITLE",
203            ilNewsItem::determineNewsTitle($i->getContextObjType(), $i->getTitle(), $i->getContentIsLangVar())
204        );
205
206        // content
207        $tpl->setVariable("CONTENT", $news_renderer->getTimelineContent());
208
209        include_once("./Services/User/classes/class.ilUserUtil.php");
210        $tpl->setVariable("TXT_USR", ilUserUtil::getNamePresentation(
211            $i->getUserId(),
212            false,
213            true,
214            $this->ctrl->getLinkTargetByClass("ilnewstimelinegui")
215        ));
216
217        include_once("./Services/Calendar/classes/class.ilDatePresentation.php");
218        $tpl->setVariable("TIME", ilDatePresentation::formatDate($this->getDateTime()));
219
220        // actions
221        include_once("Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php");
222        $list = new ilAdvancedSelectionListGUI();
223        $list->setListTitle("");
224        $list->setId("news_tl_act_" . $i->getId());
225        //$list->setSelectionHeaderClass("small");
226        //$list->setItemLinkClass("xsmall");
227        //$list->setLinksMode("il_ContainerItemCommand2");
228        $list->setHeaderIcon(ilAdvancedSelectionListGUI::DOWN_ARROW_DARK);
229        $list->setUseImages(false);
230
231        if ($i->getPriority() == 1 && ($i->getUserId() == $this->user->getId() || $this->getUserEditAll())) {
232            $list->addItem(
233                $this->lng->txt("edit"),
234                "",
235                "",
236                "",
237                "",
238                "",
239                "",
240                false,
241                "il.News.edit(" . $i->getId() . ");"
242            );
243            $list->addItem(
244                $this->lng->txt("delete"),
245                "",
246                "",
247                "",
248                "",
249                "",
250                "",
251                false,
252                "il.News.delete(" . $i->getId() . ");"
253            );
254        }
255
256        $news_renderer->addTimelineActions($list);
257
258        $tpl->setVariable("ACTIONS", $list->getHTML());
259
260        return $tpl->get();
261    }
262
263    /**
264     * Render media
265     *
266     * @param
267     * @return
268     */
269    protected function renderMedia(ilNewsItem $i)
270    {
271        global $DIC;
272
273        $media_path = $this->getMediaPath($i);
274        $mime = ilObjMediaObject::getMimeType($media_path);
275
276        $ui_factory = $DIC->ui()->factory();
277        $ui_renderer = $DIC->ui()->renderer();
278
279        if (in_array($mime, array("image/jpeg", "image/svg+xml", "image/gif", "image/png"))) {
280            $item_id = "il-news-modal-img-" . $i->getId();
281            $title = basename($media_path);
282            $image = $ui_renderer->render($ui_factory->image()->responsive($media_path, $title));
283
284            $img_tpl = new ilTemplate("tpl.news_timeline_image_file.html", true, true, "Services/News");
285            $img_tpl->setVariable("ITEM_ID", $item_id);
286            $img_tpl->setVariable("IMAGE", $image);
287
288            $html = $img_tpl->get();
289        } elseif (in_array($mime, array("audio/mpeg", "audio/ogg", "video/mp4", "video/x-flv", "video/webm"))) {
290            $mp = new ilMediaPlayerGUI();
291            $mp->setFile($media_path);
292            $html = $mp->getMediaPlayerHtml();
293
294        } else {
295            // download?
296            $html = "";
297        }
298        return $html;
299    }
300
301    /**
302     * Render media
303     *
304     * @param ilNewsItem
305     * @return string
306     */
307    protected function renderMediaModal(ilNewsItem $i)
308    {
309        global $DIC;
310
311        $media_path = $this->getMediaPath($i);
312        $mime = ilObjMediaObject::getMimeType($media_path);
313
314        $ui_factory = $DIC->ui()->factory();
315        $ui_renderer = $DIC->ui()->renderer();
316
317        $modal_html = "";
318
319        if (in_array($mime, array("image/jpeg", "image/svg+xml", "image/gif", "image/png"))) {
320            $title = basename($media_path);
321            $item_id = "il-news-modal-img-" . $i->getId();
322            $image = $ui_renderer->render($ui_factory->image()->responsive($media_path, $title));
323            $modal = ilModalGUI::getInstance();
324            $modal->setId($item_id);
325            $modal->setType(ilModalGUI::TYPE_LARGE);
326            $modal->setBody($image);
327            $modal->setHeading($title);
328            $modal_html = $modal->getHTML();
329        }
330        return $modal_html;
331    }
332
333
334    /**
335     * Render footer
336     * @throws ilCtrlException
337     */
338    public function renderFooter()
339    {
340        $i = $this->getNewsItem();
341
342        // like
343        $this->ctrl->setParameterByClass("ilnewstimelinegui", "news_id", $i->getId());
344        $this->like_gui->setObject(
345            $i->getContextObjId(),
346            $i->getContextObjType(),
347            $i->getContextSubObjId(),
348            $i->getContextSubObjType(),
349            $i->getId()
350        );
351        $html = $this->ctrl->getHTML($this->like_gui);
352
353        // comments
354        $notes_obj_type = ($i->getContextSubObjType() == "")
355            ? $i->getContextObjType()
356            : $i->getContextSubObjType();
357        $note_gui = new ilNoteGUI(
358            $i->getContextObjId(),
359            $i->getContextSubObjId(),
360            $notes_obj_type,
361            false,
362            $i->getId()
363        );
364        $note_gui->setDefaultCommand("getWidget");
365
366        //ilNoteGUI::getListCommentsJSCall($this->ajax_hash, $redraw_js)
367        $html .= $this->ctrl->getHTML($note_gui);
368
369        $this->ctrl->setParameterByClass("ilnewstimelinegui", "news_id", $_GET["news_id"]);
370
371        return $html . $this->renderMediaModal($i);
372    }
373
374    /**
375     * @param ilNewsItem $i
376     * @return string
377     */
378    protected function getMediaPath(ilNewsItem $i)
379    {
380        $media_path = "";
381        if ($i->getMobId() > 0) {
382            include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
383            include_once("./Services/MediaObjects/classes/class.ilMediaPlayerGUI.php");
384            $mob = new ilObjMediaObject($i->getMobId());
385            $med = $mob->getMediaItem("Standard");
386            if (strcasecmp("Reference", $med->getLocationType()) == 0) {
387                $media_path = $med->getLocation();
388            } else {
389                $media_path = ilObjMediaObject::_getURL($mob->getId()) . "/" . $med->getLocation();
390            }
391        }
392        return $media_path;
393    }
394}
395