1<?php
2
3/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once("Services/Table/classes/class.ilTable2GUI.php");
6
7/**
8* TableGUI class for media object usages listing
9*
10* @author Alex Killing <alex.killing@gmx.de>
11* @version $Id$
12*
13* @ingroup ServicesMediaObjects
14*/
15class ilMediaObjectUsagesTableGUI extends ilTable2GUI
16{
17    /**
18     * @var ilCtrl
19     */
20    protected $ctrl;
21
22    /**
23     * @var ilAccessHandler
24     */
25    protected $access;
26
27    /**
28     * @var ilObjMediaObject
29     */
30    protected $media_object;
31
32    /**
33     * @var ilTree
34     */
35    protected $repo_tree;
36
37    /**
38    * Constructor
39    */
40    public function __construct(
41        $a_parent_obj,
42        $a_parent_cmd,
43        $a_media_object,
44        $a_include_hist = false
45    ) {
46        global $DIC;
47
48        $this->ctrl = $DIC->ctrl();
49        $this->lng = $DIC->language();
50        $this->access = $DIC->access();
51        $ilCtrl = $DIC->ctrl();
52        $lng = $DIC->language();
53
54        $this->repo_tree = $DIC->repositoryTree();
55
56        parent::__construct($a_parent_obj, $a_parent_cmd);
57        $this->media_object = $a_media_object;
58        $this->include_hist = $a_include_hist;
59        $this->addColumn($lng->txt("mob_object"));
60        $this->addColumn($this->lng->txt("type"));
61        //if ($a_include_hist) {
62            $this->addColumn($this->lng->txt("cont_versions"));
63        //}
64        //$this->setEnableHeader(false);
65        $this->setFormAction($ilCtrl->getFormAction($a_parent_obj));
66        $this->setRowTemplate("tpl.mob_usage_row.html", "Services/MediaObjects");
67        $this->getItems();
68        $this->setTitle($lng->txt("cont_mob_usages"));
69    }
70
71    /**
72    * Get items of current folder
73    */
74    public function getItems()
75    {
76        $usages = $this->media_object->getUsages($this->include_hist);
77
78        $clip_cnt = 0;
79        $to_del = array();
80        $agg_usages = array();
81        foreach ($usages as $k => $usage) {
82            $usage["trash"] = false;
83            if (is_int(strpos($usage["type"], ":"))) {
84                $us_arr = explode(":", $usage["type"]);
85
86                // try to figure out object id of pages
87                if ($us_arr[1] == "pg") {
88                    $page_obj = ilPageObjectFactory::getInstance($us_arr[0], $usage["id"]);
89                    $usage["page"] = $page_obj;
90                    $repo_tree = $this->repo_tree;
91                    $ref_ids = array_filter(ilObject::_getAllReferences($page_obj->getRepoObjId()), function($ref_id) use ($repo_tree) {
92                        return $repo_tree->isInTree($ref_id);
93                    });
94                    $usage["ref_ids"] = $ref_ids;
95                    if (count($ref_ids) == 0) {
96                        $usage["trash"] = true;
97                    }
98                }
99            }
100
101
102            if ($usage["type"] == "clip") {
103                $clip_cnt++;
104            } else {
105                if ($this->incl_hist || !$usage["trash"]) {
106                    if (empty($agg_usages[$usage["type"] . ":" . $usage["id"]])) {
107                        $agg_usages[$usage["type"] . ":" . $usage["id"]] = $usage;
108                    }
109                    $agg_usages[$usage["type"] . ":" . $usage["id"]]["versions"][] =
110                        ["hist_nr" => $usage["hist_nr"],
111                         "lang" => $usage["lang"]];
112                }
113            }
114        }
115
116        // usages in clipboards
117        if ($clip_cnt > 0) {
118            $agg_usages[] = array("type" => "clip", "cnt" => $clip_cnt);
119        }
120
121        $this->setData($agg_usages);
122    }
123
124    /**
125    * Standard Version of Fill Row. Most likely to
126    * be overwritten by derived class.
127    */
128    protected function fillRow($a_set)
129    {
130        $lng = $this->lng;
131        $ilCtrl = $this->ctrl;
132        $ilAccess = $this->access;
133
134        $usage = $a_set;
135
136        //var_dump($usage);
137
138        if (is_int(strpos($usage["type"], ":"))) {
139            $us_arr = explode(":", $usage["type"]);
140            $usage["type"] = $us_arr[1];
141            $cont_type = $us_arr[0];
142        }
143
144        include_once('./Services/Link/classes/class.ilLink.php');
145
146        switch ($usage["type"]) {
147            case "pg":
148                $page_obj = $usage["page"];
149
150                $item = array();
151
152                //$this->tpl->setVariable("TXT_OBJECT", $usage["type"].":".$usage["id"]);
153                switch ($cont_type) {
154                    case "lm":
155                        require_once("./Modules/LearningModule/classes/class.ilObjContentObject.php");
156                        require_once("./Modules/LearningModule/classes/class.ilObjLearningModule.php");
157                        require_once("./Modules/LearningModule/classes/class.ilLMObject.php");
158                        if (ilObject::_lookupType($page_obj->getParentId()) == "lm") {
159                            $lm_obj = new ilObjLearningModule($page_obj->getParentId(), false);
160                            $item["obj_type_txt"] = $this->lng->txt("obj_" . $cont_type);
161                            $item["obj_title"] = $lm_obj->getTitle();
162                            $item["sub_txt"] = $this->lng->txt("pg");
163                            $item["sub_title"] = ilLMObject::_lookupTitle($page_obj->getId());
164                            $ref_id = $this->getFirstWritableRefId($lm_obj->getId());
165                            if ($ref_id > 0) {
166                                $item["obj_link"] = ilLink::_getStaticLink($page_obj->getId() . "_" . $ref_id, "pg");
167                            }
168                        }
169                        break;
170
171                    case "wpg":
172                        require_once("./Modules/Wiki/classes/class.ilWikiPage.php");
173                        $item["obj_type_txt"] = $this->lng->txt("obj_wiki");
174                        $item["obj_title"] = ilObject::_lookupTitle($page_obj->getParentId());
175                        $item["sub_txt"] = $this->lng->txt("pg");
176                        $item["sub_title"] = ilWikiPage::lookupTitle($page_obj->getId());
177                        $ref_id = $this->getFirstWritableRefId($page_obj->getParentId());
178                        if ($ref_id > 0) {
179                            $item["obj_link"] = ilLink::_getStaticLink($ref_id, "wiki");
180                        }
181                        break;
182
183                    case "gdf":
184                        require_once("./Modules/Glossary/classes/class.ilGlossaryTerm.php");
185                        require_once("./Modules/Glossary/classes/class.ilGlossaryDefinition.php");
186                        $term_id = ilGlossaryDefinition::_lookupTermId($page_obj->getId());
187                        $glo_id = ilGlossaryTerm::_lookGlossaryId($term_id);
188                        $item["obj_type_txt"] = $this->lng->txt("obj_glo");
189                        $item["obj_title"] = ilObject::_lookupTitle($glo_id);
190                        $item["sub_txt"] = $this->lng->txt("cont_term");
191                        $item["sub_title"] = ilGlossaryTerm::_lookGlossaryTerm($term_id);
192                        $ref_id = $this->getFirstWritableRefId($page_obj->getParentId());
193                        if ($ref_id > 0) {
194                            $item["obj_link"] = ilLink::_getStaticLink($ref_id, "glo");
195                        }
196                        break;
197
198                    case "cont":
199                        $otype = ilObject::_lookupType($page_obj->getId());
200                        $item["obj_type_txt"] = $this->lng->txt("obj_" . $otype);
201                        $item["obj_title"] = ilObject::_lookupTitle($page_obj->getId());
202                        $ref_id = $this->getFirstWritableRefId($page_obj->getId());
203                        if ($ref_id > 0) {
204                            $item["obj_link"] = ilLink::_getStaticLink($ref_id, $otype);
205                        }
206                        break;
207
208                    default:
209                        include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
210                        $oid = ilObjMediaObject::getParentObjectIdForUsage($a_set);
211                        if ($oid > 0) {
212                            $type = ilObject::_lookupType($oid);
213                            $item["obj_type_txt"] = $this->lng->txt("obj_" . $type);
214                            $item["obj_title"] = ilObject::_lookupTitle($oid);
215                            $ref_id = $this->getFirstWritableRefId($oid);
216                            if ($ref_id > 0) {
217                                $item["obj_link"] = ilLink::_getStaticLink($ref_id, $type);
218                            }
219                        }
220                        break;
221                }
222
223                if ($usage["trash"]) {
224                    $item["obj_title"].= " (".$lng->txt("trash").")";
225                }
226
227                break;
228
229            case "mep":
230                $item["obj_type_txt"] = $this->lng->txt("obj_mep");
231                $item["obj_title"] = ilObject::_lookupTitle($usage["id"]);
232                $ref_id = $this->getFirstWritableRefId($usage["id"]);
233                if ($ref_id > 0) {
234                    $item["obj_link"] = ilLink::_getStaticLink($ref_id, "mep");
235                }
236                break;
237
238            case "map":
239                $item["obj_type_txt"] = $this->lng->txt("obj_mob");
240                $item["obj_title"] = ilObject::_lookupTitle($usage["id"]);
241                $item["sub_txt"] = $this->lng->txt("cont_link_area");
242                break;
243        }
244
245        // show versions
246        if (is_array($usage["versions"]) && is_object($usage["page"])) {
247            $ver = $sep = "";
248
249            if (count($usage["versions"]) > 5) {
250                $ver .= "..., ";
251                $cnt = count($usage["versions"]) - 5;
252                for ($i = 0; $i < $cnt; $i++) {
253                    unset($usage["versions"][$i]);
254                }
255            }
256            foreach ($usage["versions"] as $version) {
257                if ($version["hist_nr"] == 0) {
258                    $version["hist_nr"] = $this->lng->txt("cont_current_version");
259                }
260                $ver .= $sep . $version["hist_nr"];
261                if ($version["lang"] != "") {
262                    $ver.= "/".$version["lang"];
263                }
264                $sep = ", ";
265            }
266
267            $this->tpl->setCurrentBlock("versions");
268            $this->tpl->setVariable("VAL_VERSIONS", $ver);
269            $this->tpl->parseCurrentBlock();
270        } else {
271            $this->tpl->setCurrentBlock("versions");
272            $this->tpl->setVariable("VAL_VERSIONS", " ");
273            $this->tpl->parseCurrentBlock();
274        }
275
276        if ($item["obj_type_txt"] != "") {
277            $this->tpl->setVariable("VAL_TYPE", $item["obj_type_txt"]);
278        }
279
280        if ($usage["type"] != "clip") {
281            if ($item["obj_link"]) {
282                $this->tpl->setCurrentBlock("linked_item");
283                $this->tpl->setVariable("TXT_OBJECT", $item["obj_title"]);
284                $this->tpl->setVariable("HREF_LINK", $item["obj_link"]);
285                $this->tpl->parseCurrentBlock();
286            } else {
287                $this->tpl->setVariable("TXT_OBJECT_NO_LINK", $item["obj_title"]);
288            }
289
290            if ($item["sub_txt"] != "") {
291                $this->tpl->setVariable("SEP", ", ");
292                $this->tpl->setVariable("SUB_TXT", $item["sub_txt"]);
293                if ($item["sub_title"] != "") {
294                    $this->tpl->setVariable("SEP2", ": ");
295                    $this->tpl->setVariable("SUB_TITLE", $item["sub_title"]);
296                }
297            }
298        } else {
299            $this->tpl->setVariable("TXT_OBJECT_NO_LINK", $this->lng->txt("cont_users_have_mob_in_clip1") .
300                " " . $usage["cnt"] . " " . $this->lng->txt("cont_users_have_mob_in_clip2"));
301        }
302    }
303
304    public function getFirstWritableRefId($a_obj_id)
305    {
306        $ilAccess = $this->access;
307
308        $ref_ids = ilObject::_getAllReferences($a_obj_id);
309        foreach ($ref_ids as $ref_id) {
310            if ($ilAccess->checkAccess("write", "", $ref_id)) {
311                return $ref_id;
312            }
313        }
314        return 0;
315    }
316}
317