1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/DataSet/classes/class.ilDataSet.php");
5
6/**
7 * Media Pool Data set class
8 *
9 * This class implements the following entities:
10 * - mob: object data
11 * - mob_media_item: data from table media_item
12 * - mob_mi_map_area: data from a table map_area
13 * - mob_mi_parameter: data from a table mob_parameter
14 *
15 * @author Alex Killing <alex.killing@gmx.de>
16 * @version $Id$
17 * @ingroup ingroup ServicesMediaObjects
18 */
19class ilMediaObjectDataSet extends ilDataSet
20{
21    protected $use_previous_import_ids = false;
22
23    /**
24     * Constructor
25     */
26    public function __construct()
27    {
28        parent::__construct();
29        $this->mob_log = ilLoggerFactory::getLogger('mob');
30    }
31
32    /**
33     * Set use previous import ids
34     *
35     * @param bool $a_val use previous import ids
36     */
37    public function setUsePreviousImportIds($a_val)
38    {
39        $this->use_previous_import_ids = $a_val;
40    }
41
42    /**
43     * Get use previous import ids
44     *
45     * @return bool use previous import ids
46     */
47    public function getUsePreviousImportIds()
48    {
49        return $this->use_previous_import_ids;
50    }
51
52    /**
53     * Get supported versions
54     *
55     * @param
56     * @return
57     */
58    public function getSupportedVersions()
59    {
60        return array("5.1.0", "4.3.0", "4.1.0");
61    }
62
63    /**
64     * Get xml namespace
65     *
66     * @param
67     * @return
68     */
69    public function getXmlNamespace($a_entity, $a_schema_version)
70    {
71        return "http://www.ilias.de/xml/Services/MediaObject/" . $a_entity;
72    }
73
74    /**
75     * Get field types for entity
76     *
77     * @param
78     * @return
79     */
80    protected function getTypes($a_entity, $a_version)
81    {
82        // mob
83        if ($a_entity == "mob") {
84            switch ($a_version) {
85                case "4.1.0":
86                case "4.3.0":
87                    return array(
88                        "Id" => "integer",
89                        "Title" => "text",
90                        "Description" => "text",
91                        "Dir" => "directory"
92                    );
93
94                case "5.1.0":
95                    return array(
96                        "Id" => "integer",
97                        "Title" => "text",
98                        "Description" => "text",
99                        "Dir" => "directory",
100                        "ImportId" => "text"
101                    );
102            }
103        }
104
105        // media item
106        if ($a_entity == "mob_media_item") {
107            switch ($a_version) {
108                case "4.1.0":
109                    return array(
110                        "Id" => "integer",
111                        "MobId" => "integer",
112                        "Width" => "integer",
113                        "Height" => "integer",
114                        "Halign" => "text",
115                        "Caption" => "text",
116                        "Nr" => "integer",
117                        "Purpose" => "text",
118                        "Location" => "text",
119                        "LocationType" => "text",
120                        "Format" => "text",
121                        "TextRepresentation" => "text"
122                    );
123
124                case "4.3.0":
125                case "5.1.0":
126                    return array(
127                        "Id" => "integer",
128                        "MobId" => "integer",
129                        "Width" => "integer",
130                        "Height" => "integer",
131                        "Halign" => "text",
132                        "Caption" => "text",
133                        "Nr" => "integer",
134                        "Purpose" => "text",
135                        "Location" => "text",
136                        "LocationType" => "text",
137                        "Format" => "text",
138                        "TextRepresentation" => "text"
139                    );
140            }
141        }
142
143        // map areas
144        if ($a_entity == "mob_mi_map_area") {
145            switch ($a_version) {
146                case "4.1.0":
147                case "4.3.0":
148                case "5.1.0":
149                        return array(
150                            "MiId" => "integer",
151                            "Nr" => "integer",
152                            "Shape" => "text",
153                            "Coords" => "text",
154                            "LinkType" => "text",
155                            "Title" => "text",
156                            "Href" => "text",
157                            "Target" => "text",
158                            "Type" => "text",
159                            "TargetFrame" => "text",
160                            "HighlightMode" => "text",
161                            "HighlightText" => "text"
162                        );
163            }
164        }
165
166        // media item parameter
167        if ($a_entity == "mob_mi_parameter") {
168            switch ($a_version) {
169                case "4.1.0":
170                case "4.3.0":
171                case "5.1.0":
172                        return array(
173                            "MiId" => "integer",
174                            "Name" => "text",
175                            "Value" => "text"
176                        );
177            }
178        }
179    }
180
181    /**
182     * Read data
183     *
184     * @param
185     * @return
186     */
187    public function readData($a_entity, $a_version, $a_ids, $a_field = "")
188    {
189        $ilDB = $this->db;
190
191        if (!is_array($a_ids)) {
192            $a_ids = array($a_ids);
193        }
194
195        // mob
196        if ($a_entity == "mob") {
197            $this->data = array();
198
199            switch ($a_version) {
200                case "4.1.0":
201                case "4.3.0":
202                    foreach ($a_ids as $mob_id) {
203                        if (ilObject::_lookupType($mob_id) == "mob") {
204                            $this->data[] = array("Id" => $mob_id,
205                                "Title" => ilObject::_lookupTitle($mob_id),
206                                "Description" => ilObject::_lookupDescription($mob_id)
207                            );
208                        }
209                    }
210                    break;
211
212                case "5.1.0":
213                    foreach ($a_ids as $mob_id) {
214                        if (ilObject::_lookupType($mob_id) == "mob") {
215                            $this->data[] = array("Id" => $mob_id,
216                                "Title" => ilObject::_lookupTitle($mob_id),
217                                "Description" => ilObject::_lookupDescription($mob_id),
218                                "ImportId" => ilObject::_lookupImportId($mob_id)
219                            );
220                        }
221                    }
222                    break;
223            }
224        }
225
226        // media item
227        if ($a_entity == "mob_media_item") {
228            switch ($a_version) {
229                case "4.1.0":
230                    $this->getDirectDataFromQuery("SELECT id, mob_id, width, height, halign," .
231                        "caption, nr, purpose, location, location_type, format, text_representation" .
232                        " FROM media_item WHERE " .
233                        $ilDB->in("mob_id", $a_ids, false, "integer"));
234                    break;
235
236                case "4.3.0":
237                case "5.1.0":
238                    $this->getDirectDataFromQuery("SELECT id, mob_id, width, height, halign," .
239                        "caption, nr, purpose, location, location_type, format, text_representation" .
240                        " FROM media_item WHERE " .
241                        $ilDB->in("mob_id", $a_ids, false, "integer"));
242                    break;
243            }
244        }
245
246
247        // media item map area
248        if ($a_entity == "mob_mi_map_area") {
249            switch ($a_version) {
250                case "4.1.0":
251                case "4.3.0":
252                case "5.1.0":
253                    foreach ($this->getDirectDataFromQuery("SELECT item_id mi_id, nr" .
254                        " ,shape, coords, link_type, title, href, target, type, target_frame, " .
255                        " highlight_mode, highlight_class" .
256                        " FROM map_area " .
257                        " WHERE " .
258                        $ilDB->in("item_id", $a_ids, false, "integer") .
259                        " ORDER BY nr", true, false) as $r) {
260                        $r["Target"] = ilUtil::insertInstIntoID($r["Target"]);
261
262                        // see ilPageObject::insertInstIntoIDs
263                        if ($r["Type"] == "RepositoryItem") {
264                            $id_arr = explode("_", $r["Target"]);
265                            $ref_id = $id_arr[3];
266                            $obj_id = ilObject::_lookupObjId($id_arr[3]);
267
268                            $otype = ilObject::_lookupType($obj_id);
269                            if ($obj_id > 0) {
270                                $id = $otype . "_" . $obj_id . "_" . $ref_id;
271                                $r["Target"] = "il_" . $id_arr[1] . "_" . $id;
272                            }
273                        }
274
275                        $this->data[] = $r;
276                    }
277                    break;
278            }
279        }
280
281        // media item parameter
282        if ($a_entity == "mob_mi_parameter") {
283            switch ($a_version) {
284                case "4.1.0":
285                case "4.3.0":
286                case "5.1.0":
287                    $this->getDirectDataFromQuery("SELECT med_item_id mi_id, name, value" .
288                        " FROM mob_parameter " .
289                        " WHERE " .
290                        $ilDB->in("med_item_id", $a_ids, false, "integer"));
291                    break;
292            }
293        }
294    }
295
296    /**
297     * Determine the dependent sets of data
298     */
299    protected function getDependencies($a_entity, $a_version, $a_rec, $a_ids)
300    {
301        switch ($a_entity) {
302            case "mob":
303                return array(
304                    "mob_media_item" => array("ids" => $a_rec["Id"])
305                );
306
307            case "mob_media_item":
308                return array(
309                    "mob_mi_map_area" => array("ids" => $a_rec["Id"]),
310                    "mob_mi_parameter" => array("ids" => $a_rec["Id"])
311                );
312        }
313        return false;
314    }
315
316    /**
317     * Get xml record
318     *
319     * @param
320     * @return
321     */
322    public function getXmlRecord($a_entity, $a_version, $a_set)
323    {
324        if ($a_entity == "mob") {
325            include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
326            $dir = ilObjMediaObject::_getDirectory($a_set["Id"]);
327            $a_set["Dir"] = $dir;
328        }
329
330        return $a_set;
331    }
332
333    /**
334     * Import record
335     *
336     * @param
337     * @return
338     */
339    public function importRecord($a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version)
340    {
341        //echo $a_entity;
342        //var_dump($a_rec);
343
344        switch ($a_entity) {
345            case "mob":
346
347//var_dump($a_rec);
348
349                include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
350                $newObj = new ilObjMediaObject();
351                $newObj->setType("mob");
352                $newObj->setTitle($a_rec["Title"]);
353                $newObj->setDescription($a_rec["Description"]);
354                // on translation re-import we are interested in the "previous" import id
355                if ($this->getUsePreviousImportIds()) {
356                    $newObj->setImportId($a_rec["ImportId"]);
357                } else {
358                    $newObj->setImportId("il_" . $this->getCurrentInstallationId() . "_mob_" . $a_rec["Id"]);
359                }
360                $newObj->create();
361                $newObj->createDirectory();
362                ilObjMediaObject::_createThumbnailDirectory($newObj->getId());
363                $this->current_mob = $newObj;
364
365                $dir = str_replace("..", "", $a_rec["Dir"]);
366                if ($dir != "" && $this->getImportDirectory() != "") {
367                    $source_dir = $this->getImportDirectory() . "/" . $dir;
368                    $target_dir = $dir = ilObjMediaObject::_getDirectory($newObj->getId());
369                    $this->mob_log->debug("s:-$source_dir-,t:-$target_dir-");
370                    ilUtil::rCopy($source_dir, $target_dir);
371                    ilObjMediaObject::renameExecutables($target_dir);
372                    include_once("./Services/MediaObjects/classes/class.ilMediaSvgSanitizer.php");
373                    ilMediaSvgSanitizer::sanitizeDir($target_dir);	// see #20339
374                }
375
376                $a_mapping->addMapping("Services/MediaObjects", "mob", $a_rec["Id"], $newObj->getId());
377//echo "<br>++add++"."0:".$a_rec["Id"].":mob+0:".$newObj->getId().":mob"."+";
378                $a_mapping->addMapping(
379                    "Services/MetaData",
380                    "md",
381                    "0:" . $a_rec["Id"] . ":mob",
382                    "0:" . $newObj->getId() . ":mob"
383                );
384                break;
385
386            case "mob_media_item":
387
388                // determine parent mob
389                include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
390                $mob_id = (int) $a_mapping->getMapping("Services/MediaObjects", "mob", $a_rec["MobId"]);
391                if (is_object($this->current_mob) && $this->current_mob->getId() == $mob_id) {
392                    $mob = $this->current_mob;
393                } else {
394                    $mob = new ilObjMediaObject($mob_id);
395                }
396
397                include_once("./Services/MediaObjects/classes/class.ilMediaItem.php");
398                $newObj = new ilMediaItem();
399                $newObj->setMobId($mob_id);
400                $newObj->setWidth($a_rec["Width"]);
401                $newObj->setHeight($a_rec["Height"]);
402                $newObj->setCaption($a_rec["Caption"]);
403                $newObj->setNr($a_rec["Nr"]);
404                $newObj->setPurpose($a_rec["Purpose"]);
405                $newObj->setLocation($a_rec["Location"]);
406                $newObj->setLocationType($a_rec["LocationType"]);
407                $newObj->setFormat($a_rec["Format"]);
408                $newObj->setTextRepresentation($a_rec["TextRepresentation"]);
409                $newObj->create();
410                $this->current_media_item = $newObj;
411
412                $a_mapping->addMapping("Services/MediaObjects", "mob_media_item", $a_rec["Id"], $newObj->getId());
413
414                break;
415
416            case "mob_mi_parameter":
417
418                // get media item
419                include_once("./Services/MediaObjects/classes/class.ilMediaItem.php");
420                $med_id = (int) $a_mapping->getMapping("Services/MediaObjects", "mob_media_item", $a_rec["MiId"]);
421                if (is_object($this->current_media_item) && $this->current_media_item->getId() == $med_id) {
422                    $med = $this->current_media_item;
423                } else {
424                    $med = new ilMediaItem($med_id);
425                }
426                $med->writeParameter($a_rec["Name"], $a_rec["Value"]);
427
428                break;
429
430            case "mob_mi_map_area":
431                // get media item
432                include_once("./Services/MediaObjects/classes/class.ilMediaItem.php");
433                $med_id = (int) $a_mapping->getMapping("Services/MediaObjects", "mob_media_item", $a_rec["MiId"]);
434                if (is_object($this->current_media_item) && $this->current_media_item->getId() == $med_id) {
435                    $med = $this->current_media_item;
436                } else {
437                    $med = new ilMediaItem($med_id);
438                }
439
440                include_once("./Services/MediaObjects/classes/class.ilMapArea.php");
441                $map_area = new ilMapArea();
442                $map_area->setItemId($med_id);
443                $map_area->setNr($a_rec["Nr"]);
444                $map_area->setShape($a_rec["Shape"]);
445                $map_area->setCoords($a_rec["Coords"]);
446                $map_area->setLinkType($a_rec["LinkType"]);
447                $map_area->setTitle($a_rec["Title"]);
448                $map_area->setHref($a_rec["Href"]);
449                $map_area->setTarget($a_rec["Target"]);
450                $map_area->setType($a_rec["Type"]);
451                $map_area->setTargetFrame($a_rec["TargetFrame"]);
452                $map_area->setHighlightMode($a_rec["HighlightMode"]);
453                $map_area->setHighlightClass($a_rec["HighlightClass"]);
454                $map_area->create();
455
456                break;
457        }
458    }
459}
460