1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24include_once("./Services/Block/classes/class.ilBlockGUI.php");
25include_once("./Services/Block/classes/class.ilExternalFeedBlockGUIGen.php");
26include_once("./Services/Feeds/classes/class.ilExternalFeed.php");
27
28/**
29* BlockGUI class for external feed block on the personal desktop.
30* Within the repository ilExternalFeedBlockGUI is used.
31* is used.
32*
33* @author Alex Killing <alex.killing@gmx.de>
34* @version $Id$
35*
36* @ilCtrl_IsCalledBy ilPDExternalFeedBlockGUI: ilColumnGUI
37* @ingroup ServicesFeeds
38*/
39class ilPDExternalFeedBlockGUI extends ilExternalFeedBlockGUIGen
40{
41    /**
42     * @var ilSetting
43     */
44    protected $settings;
45
46    public static $block_type = "pdfeed";
47
48    /**
49    * Constructor
50    */
51    public function __construct()
52    {
53        global $DIC;
54
55        $this->ctrl = $DIC->ctrl();
56        $this->lng = $DIC->language();
57        $this->user = $DIC->user();
58        $this->access = $DIC->access();
59        $this->settings = $DIC->settings();
60        $lng = $DIC->language();
61
62        parent::__construct();
63
64        $lng->loadLanguageModule("feed");
65
66        $this->setLimit(5);
67        $this->setRowTemplate("tpl.block_external_feed_row.html", "Services/Feeds");
68    }
69
70    /**
71     * @inheritdoc
72     */
73    public function getBlockType() : string
74    {
75        return self::$block_type;
76    }
77
78    /**
79     * @inheritdoc
80     */
81    protected function isRepositoryObject() : bool
82    {
83        return false;
84    }
85
86    /**
87    * Get Screen Mode for current command.
88    */
89    public static function getScreenMode()
90    {
91        global $DIC;
92
93        $ilCtrl = $DIC->ctrl();
94
95        switch ($ilCtrl->getCmd()) {
96            case "create":
97            case "edit":
98            case "saveFeedBlock":
99            case "updateFeedBlock":
100            case "editFeedBlock":
101            case "showFeedItem":
102            case "confirmDeleteFeedBlock":
103                return IL_SCREEN_CENTER;
104                break;
105
106            default:
107                return IL_SCREEN_SIDE;
108                break;
109        }
110    }
111
112    /**
113    * Do most of the initialisation.
114    */
115    public function setBlock($a_block)
116    {
117        $ilCtrl = $this->ctrl;
118
119        // init block
120        $this->feed_block = $a_block;
121        $this->setTitle($this->feed_block->getTitle());
122        $this->setBlockId($this->feed_block->getId());
123
124        // get feed object
125        include_once("./Services/Feeds/classes/class.ilExternalFeed.php");
126        $this->feed = new ilExternalFeed();
127        $this->feed->setUrl($this->feed_block->getFeedUrl());
128
129        // init details
130        $this->setAvailableDetailLevels(2);
131
132        $ilCtrl->setParameter($this, "block_id", $this->feed_block->getId());
133    }
134
135    /**
136    * execute command
137    */
138    public function executeCommand()
139    {
140        $ilCtrl = $this->ctrl;
141
142        $next_class = $ilCtrl->getNextClass();
143        $cmd = $ilCtrl->getCmd("getHTML");
144
145        switch ($next_class) {
146            default:
147                return $this->$cmd();
148        }
149    }
150
151    /**
152    * Fill data section
153    */
154    public function fillDataSection()
155    {
156        if ($this->getDynamic()) {
157            $this->setDataSection($this->getDynamicReload());
158        } elseif ($this->getCurrentDetailLevel() > 1 && count($this->getData()) > 0) {
159            parent::fillDataSection();
160        } else {
161            $this->setDataSection($this->getOverview());
162        }
163    }
164
165    /**
166    * Get block HTML code.
167    */
168    public function getHTML()
169    {
170        $ilCtrl = $this->ctrl;
171        $lng = $this->lng;
172        $ilUser = $this->user;
173        $ilSetting = $this->settings;
174
175
176        if ($ilSetting->get("block_limit_pdfeed") == 0) {
177            return "";
178        }
179
180        if ($this->getCurrentDetailLevel() == 0) {
181            return "";
182        }
183
184
185        // if no dynamic reload
186        if (!$this->getDynamic()) {
187            $this->feed->fetch();
188            $this->setData($this->feed->getItems());
189        }
190
191        $ilCtrl->setParameter(
192            $this,
193            "external_feed_block_id",
194            $this->getBlockId()
195        );
196        $this->addBlockCommand(
197            $ilCtrl->getLinkTarget(
198                $this,
199                "editFeedBlock"
200            ),
201            $lng->txt("edit")
202        );
203        $this->addBlockCommand(
204            $ilCtrl->getLinkTarget(
205                $this,
206                "confirmDeleteFeedBlock"
207            ),
208            $lng->txt("delete")
209        );
210        $ilCtrl->setParameter($this, "external_feed_block_id", "");
211
212        // JS enabler
213        $add = "";
214        if ($_SESSION["il_feed_js"] == "n" ||
215            ($ilUser->getPref("il_feed_js") == "n" && $_SESSION["il_feed_js"] != "y")) {
216            $add = $this->getJSEnabler();
217        }
218
219        return parent::getHTML() . $add;
220    }
221
222    public function getDynamic()
223    {
224        $ilCtrl = $this->ctrl;
225        $ilUser = $this->user;
226
227        if ($ilCtrl->getCmdClass() != "ilcolumngui" && $ilCtrl->getCmd() != "enableJS") {
228            $sess_feed_js = "";
229            if (isset($_SESSION["il_feed_js"])) {
230                $sess_feed_js = $_SESSION["il_feed_js"];
231            }
232            if ($sess_feed_js != "n" &&
233                ($ilUser->getPref("il_feed_js") != "n" || $sess_feed_js == "y")) {
234                // do not get feed dynamically, if cache hit is given.
235                if (!$this->feed->checkCacheHit()) {
236                    return true;
237                }
238            }
239        }
240
241        return false;
242    }
243
244    public function getDynamicReload()
245    {
246        $ilCtrl = $this->ctrl;
247        $lng = $this->lng;
248
249        $ilCtrl->setParameterByClass(
250            "ilcolumngui",
251            "block_id",
252            "block_pdfeed_" . $this->getBlockId()
253        );
254
255        $rel_tpl = new ilTemplate("tpl.dynamic_reload.html", true, true, "Services/Feeds");
256        $rel_tpl->setVariable("TXT_LOADING", $lng->txt("feed_loading_feed"));
257        $rel_tpl->setVariable("BLOCK_ID", "block_pdfeed_" . $this->getBlockId());
258        $rel_tpl->setVariable(
259            "TARGET",
260            $ilCtrl->getLinkTargetByClass("ilcolumngui", "updateBlock", "", true)
261        );
262
263        // no JS
264        $rel_tpl->setVariable("TXT_FEED_CLICK_HERE", $lng->txt("feed_no_js_click_here"));
265        $rel_tpl->setVariable(
266            "TARGET_NO_JS",
267            $ilCtrl->getLinkTargetByClass("ilpdexternalfeedblockgui", "disableJS")
268        );
269
270        return $rel_tpl->get();
271    }
272
273    public function getJSEnabler()
274    {
275        $ilCtrl = $this->ctrl;
276
277        $ilCtrl->setParameterByClass(
278            "ilcolumngui",
279            "block_id",
280            "block_pdfeed_" . $this->getBlockId()
281        );
282
283        $rel_tpl = new ilTemplate("tpl.js_enabler.html", true, true, "Services/Feeds");
284        $rel_tpl->setVariable("BLOCK_ID", "block_pdfeed_" . $this->getBlockId());
285        $rel_tpl->setVariable(
286            "TARGET",
287            $ilCtrl->getLinkTargetByClass("ilpdexternalfeedblockgui", "enableJS", true)
288        );
289
290        return $rel_tpl->get();
291    }
292
293
294    public function disableJS()
295    {
296        $ilCtrl = $this->ctrl;
297        $ilUser = $this->user;
298
299        $_SESSION["il_feed_js"] = "n";
300        $ilUser->writePref("il_feed_js", "n");
301        $ilCtrl->redirectByClass("ilpersonaldesktopgui", "show");
302    }
303
304    public function enableJS()
305    {
306        $ilUser = $this->user;
307
308        $_SESSION["il_feed_js"] = "y";
309        $ilUser->writePref("il_feed_js", "y");
310        echo $this->getHTML();
311        exit;
312    }
313
314    /**
315    * Fill feed item row
316    */
317    public function fillRow($item)
318    {
319        $ilCtrl = $this->ctrl;
320
321        $ilCtrl->setParameter($this, "feed_item_id", $item->getId());
322        $this->tpl->setVariable("VAL_TITLE", $item->getTitle());
323        $this->tpl->setVariable(
324            "HREF_SHOW",
325            $ilCtrl->getLinkTarget($this, "showFeedItem")
326        );
327        $ilCtrl->setParameter($this, "feed_item_id", "");
328    }
329
330    /**
331    * Get overview.
332    */
333    public function getOverview()
334    {
335        $lng = $this->lng;
336
337        $this->setEnableNumInfo(false);
338        return '<div class="small">' . ((int) count($this->getData())) . " " . $lng->txt("feed_feed_items") . "</div>";
339    }
340
341    /**
342    * Show Feed Item
343    */
344    public function showFeedItem()
345    {
346        $lng = $this->lng;
347        $ilCtrl = $this->ctrl;
348
349        include_once("./Services/News/classes/class.ilNewsItem.php");
350
351        $this->feed->fetch();
352        foreach ($this->feed->getItems() as $item) {
353            if ($item->getId() == $_GET["feed_item_id"]) {
354                $c_item = $item;
355                break;
356            }
357        }
358
359        $tpl = new ilTemplate("tpl.show_feed_item.html", true, true, "Services/Feeds");
360
361        if (is_object($c_item)) {
362            if (trim($c_item->getSummary()) != "") {		// summary
363                $tpl->setCurrentBlock("content");
364                $tpl->setVariable("VAL_CONTENT", $c_item->getSummary());
365                $tpl->parseCurrentBlock();
366            }
367            if (trim($c_item->getDate()) != "" || trim($c_item->getAuthor()) != "") {		// date
368                $tpl->setCurrentBlock("date_author");
369                if (trim($c_item->getAuthor()) != "") {
370                    $tpl->setVariable("VAL_AUTHOR", $c_item->getAuthor() . " - ");
371                }
372                $tpl->setVariable("VAL_DATE", $c_item->getDate());
373                $tpl->parseCurrentBlock();
374            }
375
376            if (trim($c_item->getLink()) != "") {		// link
377                $tpl->setCurrentBlock("plink");
378                $tpl->setVariable("HREF_LINK", $c_item->getLink());
379                $tpl->setVariable("TXT_LINK", $lng->txt("feed_open_source_page"));
380                $tpl->parseCurrentBlock();
381            }
382            $tpl->setVariable("VAL_TITLE", $c_item->getTitle());			// title
383        }
384
385        include_once("./Services/PersonalDesktop/classes/class.ilPDContentBlockGUI.php");
386        $content_block = new ilPDContentBlockGUI();
387        $content_block->setContent($tpl->get());
388        $content_block->setTitle($this->getTitle());
389        $content_block->setImage(ilUtil::getImagePath("icon_feed.svg"));
390        $content_block->addHeaderCommand(
391            $ilCtrl->getParentReturn($this),
392            $lng->txt("selected_items_back")
393        );
394
395        return $content_block->getHTML();
396    }
397
398    /**
399    * Create Form for Block.
400    */
401    public function create()
402    {
403        return $this->createFeedBlock();
404    }
405
406    /**
407    * FORM FeedBlock: Init form. (We need to overwrite, because Generator
408    * does not know FeedUrl Inputs yet.
409    *
410    * @param	int	$a_mode	Form Edit Mode (IL_FORM_EDIT | IL_FORM_CREATE)
411    */
412    public function initFormFeedBlock($a_mode)
413    {
414        $lng = $this->lng;
415
416        $lng->loadLanguageModule("block");
417
418        require_once("Services/Form/classes/class.ilPropertyFormGUI.php");
419
420        $this->form_gui = new ilPropertyFormGUI();
421
422        // Property Title
423        $text_input = new ilTextInputGUI($lng->txt("block_feed_block_title"), "block_title");
424        $text_input->setInfo("");
425        $text_input->setRequired(true);
426        $text_input->setMaxLength(200);
427        $this->form_gui->addItem($text_input);
428
429        // Property FeedUrl
430        $text_input = new ilFeedUrlInputGUI($lng->txt("block_feed_block_feed_url"), "block_feed_url");
431        $text_input->setInfo($lng->txt("block_feed_block_feed_url_info"));
432        $text_input->setRequired(true);
433        $text_input->setMaxLength(250);
434        $this->form_gui->addItem($text_input);
435
436
437        // save and cancel commands
438        if (in_array($a_mode, array(IL_FORM_CREATE,IL_FORM_RE_CREATE))) {
439            $this->form_gui->addCommandButton("saveFeedBlock", $lng->txt("save"));
440            $this->form_gui->addCommandButton("cancelSaveFeedBlock", $lng->txt("cancel"));
441        } else {
442            $this->form_gui->addCommandButton("updateFeedBlock", $lng->txt("save"));
443            $this->form_gui->addCommandButton("cancelUpdateFeedBlock", $lng->txt("cancel"));
444        }
445
446        $this->form_gui->setTitle($lng->txt("block_feed_block_head"));
447        $this->form_gui->setFormAction($this->ctrl->getFormAction($this));
448
449        $this->prepareFormFeedBlock($this->form_gui);
450    }
451
452    /**
453    * FORM FeedBlock: Prepare Saving of FeedBlock.
454    *
455    * @param	object	$a_feed_block	FeedBlock object.
456    */
457    public function prepareSaveFeedBlock(&$a_feed_block)
458    {
459        $ilCtrl = $this->ctrl;
460
461        $a_feed_block->setContextObjId($ilCtrl->getContextObjId());
462        $a_feed_block->setContextObjType($ilCtrl->getContextObjType());
463        $a_feed_block->setType("pdfeed");
464    }
465
466    /**
467    * Confirmation of feed block deletion
468    */
469    public function confirmDeleteFeedBlock()
470    {
471        $ilCtrl = $this->ctrl;
472        $lng = $this->lng;
473
474        include_once("Services/Utilities/classes/class.ilConfirmationGUI.php");
475        $c_gui = new ilConfirmationGUI();
476
477        // set confirm/cancel commands
478        $c_gui->setFormAction($ilCtrl->getFormAction($this, "deleteFeedBlock"));
479        $c_gui->setHeaderText($lng->txt("info_delete_sure"));
480        $c_gui->setCancel($lng->txt("cancel"), "exitDeleteFeedBlock");
481        $c_gui->setConfirm($lng->txt("confirm"), "deleteFeedBlock");
482
483        // add items to delete
484        $c_gui->addItem(
485            "external_feed_block_id",
486            $this->feed_block->getId(),
487            $this->feed_block->getTitle(),
488            ilUtil::getImagePath("icon_feed.svg")
489        );
490
491        return $c_gui->getHTML();
492    }
493
494    /**
495    * Cancel deletion of feed block
496    */
497    public function exitDeleteFeedBlock()
498    {
499        $ilCtrl = $this->ctrl;
500
501        $ilCtrl->returnToParent($this);
502    }
503
504    /**
505    * Delete feed block
506    */
507    public function deleteFeedBlock()
508    {
509        $ilCtrl = $this->ctrl;
510
511        $this->feed_block->delete();
512        $ilCtrl->returnToParent($this);
513    }
514}
515