1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2008 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");
25
26/**
27* ilPDTaggingBlockGUI displays personal tag cloud on personal desktop.
28*
29* @author Alex Killing <alex.killing@gmx.de>
30* @version $Id$
31*
32* @ilCtrl_IsCalledBy ilPDTaggingBlockGUI: ilColumnGUI
33*/
34class ilPDTaggingBlockGUI extends ilBlockGUI
35{
36    /**
37     * @var ilObjectDefinition
38     */
39    protected $obj_definition;
40
41    public static $block_type = "pdtag";
42
43    /**
44    * Constructor
45    */
46    public function __construct()
47    {
48        global $DIC;
49
50        $this->ctrl = $DIC->ctrl();
51        $this->lng = $DIC->language();
52        $this->user = $DIC->user();
53        $this->obj_definition = $DIC["objDefinition"];
54        $this->access = $DIC->access();
55        $lng = $DIC->language();
56
57        parent::__construct();
58
59        $lng->loadLanguageModule("tagging");
60
61        $this->setTitle($lng->txt("tagging_my_tags"));
62        $this->setEnableNumInfo(false);
63        $this->setLimit(99999);
64        $this->setAvailableDetailLevels(1, 0);
65    }
66
67    /**
68     * @inheritdoc
69     */
70    public function getBlockType() : string
71    {
72        return self::$block_type;
73    }
74
75    /**
76     * @inheritdoc
77     */
78    protected function isRepositoryObject() : bool
79    {
80        return false;
81    }
82
83
84    /**
85    * Get Screen Mode for current command.
86    */
87    public static function getScreenMode()
88    {
89        switch ($_GET["cmd"]) {
90            case "showResourcesForTag":
91                return IL_SCREEN_CENTER;
92                break;
93
94            default:
95                return IL_SCREEN_SIDE;
96                break;
97        }
98    }
99
100    /**
101    * execute command
102    */
103    public function executeCommand()
104    {
105        $ilCtrl = $this->ctrl;
106
107        $next_class = $ilCtrl->getNextClass();
108        $cmd = $ilCtrl->getCmd("getHTML");
109
110        return $this->$cmd();
111    }
112
113    public function getHTML()
114    {
115        // workaround to show details row
116        $this->setData(array("dummy"));
117
118        if ($this->getCurrentDetailLevel() == 0) {
119            return "";
120        } else {
121            return parent::getHTML();
122        }
123    }
124
125    /**
126    * Fill data section
127    */
128    public function fillDataSection()
129    {
130        $ilUser = $this->user;
131
132        include_once("./Services/Tagging/classes/class.ilTagging.php");
133        $this->tags = ilTagging::getTagsForUser($ilUser->getId(), 1000000);
134
135        if ($this->getCurrentDetailLevel() > 1 && (count($this->tags) > 0)) {
136            $this->setDataSection($this->getTagCloud());
137        } else {
138            if ($this->num_bookmarks == 0 && $this->num_folders == 0) {
139                $this->setEnableDetailRow(false);
140            }
141            $this->setDataSection($this->getOverview());
142        }
143    }
144
145    /**
146    * get tree bookmark list for personal desktop
147    */
148    public function getTagCloud()
149    {
150        $ilCtrl = $this->ctrl;
151
152        $showdetails = ($this->getCurrentDetailLevel() > 2);
153        $tpl = new ilTemplate(
154            "tpl.tag_cloud.html",
155            true,
156            true,
157            "Services/Tagging"
158        );
159        $max = 1;
160        foreach ($this->tags as $tag) {
161            $max = max($tag["cnt"], $max);
162        }
163        reset($this->tags);
164
165        foreach ($this->tags as $tag) {
166            $tpl->setCurrentBlock("linked_tag");
167            $ilCtrl->setParameter($this, "tag", rawurlencode($tag["tag"]));
168            $tpl->setVariable(
169                "HREF_TAG",
170                $ilCtrl->getLinkTarget($this, "showResourcesForTag")
171            );
172            $tpl->setVariable("TAG_TITLE", $tag["tag"]);
173            $tpl->setVariable(
174                "REL_CLASS",
175                ilTagging::getRelevanceClass($tag["cnt"], $max)
176            );
177            $tpl->parseCurrentBlock();
178        }
179        return $tpl->get();
180    }
181
182    /**
183    * List resources for tag
184    */
185    public function showResourcesForTag()
186    {
187        $lng = $this->lng;
188        $ilCtrl = $this->ctrl;
189        $ilUser = $this->user;
190        $objDefinition = $this->obj_definition;
191
192        $_GET["tag"] = str_replace("-->", "", $_GET["tag"]);
193
194        $tpl = new ilTemplate("tpl.resources_for_tag.html", true, true, "Services/Tagging");
195        include_once("./Services/PersonalDesktop/classes/class.ilPDContentBlockGUI.php");
196        $content_block = new ilPDContentBlockGUI();
197        $content_block->setColSpan(2);
198        $content_block->setTitle(sprintf(
199            $lng->txt("tagging_resources_for_tag"),
200            "<i>" . $_GET["tag"] . "</i>"
201        ));
202        $content_block->setImage(ilUtil::getImagePath("icon_tag.svg"));
203        $content_block->addHeaderCommand(
204            $ilCtrl->getParentReturn($this),
205            $lng->txt("selected_items_back")
206        );
207
208        // get resources
209        include_once("./Services/Tagging/classes/class.ilTagging.php");
210        $objs = ilTagging::getObjectsForTagAndUser($ilUser->getId(), $_GET["tag"]);
211
212        $unaccessible = false;
213        foreach ($objs as $key => $obj) {
214            $ref_ids = ilObject::_getAllReferences($obj["obj_id"]);
215            foreach ($ref_ids as $ref_id) {
216                $type = $obj["obj_type"];
217
218                if ($type == "") {
219                    $unaccessible = true;
220                    continue;
221                }
222
223                // get list gui class for each object type
224                if (empty($this->item_list_gui[$type])) {
225                    $class = $objDefinition->getClassName($type);
226                    $location = $objDefinition->getLocation($type);
227
228                    $full_class = "ilObj" . $class . "ListGUI";
229
230                    include_once($location . "/class." . $full_class . ".php");
231                    $this->item_list_gui[$type] = new $full_class();
232                    $this->item_list_gui[$type]->enableDelete(false);
233                    $this->item_list_gui[$type]->enablePath(true);
234                    $this->item_list_gui[$type]->enableCut(false);
235                    $this->item_list_gui[$type]->enableCopy(false);
236                    $this->item_list_gui[$type]->enableSubscribe(false);
237                    $this->item_list_gui[$type]->enableLink(false);
238                    $this->item_list_gui[$type]->enableIcon(true);
239                }
240                $html = $this->item_list_gui[$type]->getListItemHTML(
241                    $ref_id,
242                    $obj["obj_id"],
243                    ilObject::_lookupTitle($obj["obj_id"]),
244                    ilObject::_lookupDescription($obj["obj_id"])
245                );
246
247                if ($html != "") {
248                    $css = ($css != "tblrow1") ? "tblrow1" : "tblrow2";
249
250                    $tpl->setCurrentBlock("res_row");
251                    $tpl->setVariable("ROWCLASS", $css);
252                    $tpl->setVariable("RESOURCE_HTML", $html);
253                    $tpl->setVariable("ALT_TYPE", $lng->txt("obj_" . $type));
254                    $tpl->setVariable(
255                        "IMG_TYPE",
256                        ilUtil::getImagePath("icon_" . $type . ".svg")
257                    );
258                    $tpl->parseCurrentBlock();
259                } else {
260                    $unaccessible = true;
261                }
262            }
263        }
264
265        if ($unaccessible) {
266            $tpl->setCurrentBlock("no_access");
267            $tpl->setVariable("SOME_OBJ_WITHOUT_ACCESS", $lng->txt("tag_some_obj_tagged_without_access"));
268            $ilCtrl->saveParameter($this, "tag");
269            $tpl->setVariable("HREF_REMOVE_TAGS", $ilCtrl->getLinkTarget($this, "removeTagsWithoutAccess"));
270            $tpl->setVariable("REMOVE_TAGS", $lng->txt("tag_remove_tags_of_obj_without_access"));
271            $tpl->parseCurrentBlock();
272        }
273
274        $content_block->setContent($tpl->get());
275        //$content_block->setContent("test");
276
277        return $content_block->getHTML();
278    }
279
280    /**
281     * Remove tasg without access
282     */
283    public function removeTagsWithoutAccess()
284    {
285        $ilCtrl = $this->ctrl;
286        $ilAccess = $this->access;
287        $ilUser = $this->user;
288        $lng = $this->lng;
289
290        // get resources
291        include_once("./Services/Tagging/classes/class.ilTagging.php");
292        $objs = ilTagging::getObjectsForTagAndUser($ilUser->getId(), $_GET["tag"]);
293
294        foreach ($objs as $key => $obj) {
295            $ref_ids = ilObject::_getAllReferences($obj["obj_id"]);
296            if (count($ref_ids) == 0) {
297                $inaccessible = true;
298            } else {
299                $inaccessible = false;
300            }
301            foreach ($ref_ids as $ref_id) {
302                $type = $obj["obj_type"];
303
304                if ($type == "") {
305                    $inaccessible = true;
306                    continue;
307                }
308                if (!$ilAccess->checkAccess("visible", "", $ref_id) &&
309                    !$ilAccess->checkAccess("read", "", $ref_id) &&
310                    !$ilAccess->checkAccess("write", "", $ref_id)) {
311                    $inaccessible = true;
312                }
313                if ($inaccessible) {
314                    ilTagging::deleteTagOfObjectForUser($ilUser->getId(), $obj["obj_id"], $obj["obj_type"], $obj["sub_obj_id"], $obj["sub_obj_type"], $_GET["tag"]);
315                }
316            }
317        }
318
319        ilUtil::sendSuccess($lng->txt("tag_tags_deleted"), true);
320
321        $ilCtrl->returnToParent($this);
322    }
323
324
325    /**
326    * block footer
327    */
328    public function fillFooter()
329    {
330        $this->fillFooterLinks();
331        $this->tpl->setVariable("FCOLSPAN", $this->getColSpan());
332        if ($this->tpl->blockExists("block_footer")) {
333            $this->tpl->setCurrentBlock("block_footer");
334            $this->tpl->parseCurrentBlock();
335        }
336    }
337
338
339    /**
340    * Get overview.
341    */
342    public function getOverview()
343    {
344        $lng = $this->lng;
345
346        return '<div class="small">' . $lng->txt("tagging_tag_info") . "</div>";
347    }
348}
349