1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/DataSet/classes/class.ilDataSet.php");
5
6/**
7 * News data set class
8 *
9 * @author Alex Killing <alex.killing@gmx.de>
10 * @version $Id$
11 * @ingroup ingroup ServicesNews
12 */
13class ilNewsDataSet extends ilDataSet
14{
15    /**
16     * Get supported versions
17     *
18     * @param
19     * @return
20     */
21    public function getSupportedVersions()
22    {
23        return array("5.4.0", "4.1.0");
24    }
25
26    /**
27     * Get xml namespace
28     *
29     * @param
30     * @return
31     */
32    public function getXmlNamespace($a_entity, $a_schema_version)
33    {
34        return "http://www.ilias.de/xml/Services/News/" . $a_entity;
35    }
36
37    /**
38     * Get field types for entity
39     *
40     * @param
41     * @return
42     */
43    protected function getTypes($a_entity, $a_version)
44    {
45        if ($a_entity == "news") {
46            switch ($a_version) {
47                case "4.1.0":
48                case "5.4.0":
49                    return array(
50                        "Id" => "integer",
51                        "Title" => "text",
52                        "Content" => "text",
53                        "Priority" => "integer",
54                        "ContextObjId" => "integer",
55                        "ContextObjType" => "text",
56                        "ContextSubObjId" => "integer",
57                        "ContextSubObjType" => "text",
58                        "ContentType" => "text",
59                        "Visibility" => "text",
60                        "ContentLong" => "text",
61                        "ContentIsLangVar" => "integer",
62                        "MobId" => "integer",
63                        "Playtime" => "text"
64                        );
65            }
66        }
67        if ($a_entity == "news_settings") {
68            switch ($a_version) {
69                case "5.4.0":
70                    return array(
71                        "ObjId" => "integer",
72                        "PublicFeed" => "integer",
73                        "DefaultVisibility" => "text",
74                        "KeepRssMin" => "integer",
75                        "HideNewsPerDate" => "integer",
76                        "HideNewsDate" => "text",
77                        "PublicNotifications" => "integer"
78                    );
79            }
80        }
81    }
82
83    /**
84     * Read data
85     *
86     * @param
87     * @return
88     */
89    public function readData($a_entity, $a_version, $a_ids, $a_field = "")
90    {
91        $ilDB = $this->db;
92
93        if (!is_array($a_ids)) {
94            $a_ids = array($a_ids);
95        }
96
97        if ($a_entity == "news") {
98            switch ($a_version) {
99                case "4.1.0":
100                case "5.4.0":
101                    $this->getDirectDataFromQuery("SELECT id, title, content, priority," .
102                        " context_obj_id, context_obj_type, context_sub_obj_id, context_sub_obj_type, " .
103                        " content_type, visibility, content_long, content_is_lang_var, mob_id, playtime" .
104                        " FROM il_news_item " .
105                        "WHERE " .
106                        $ilDB->in("id", $a_ids, false, "integer"));
107                    break;
108            }
109        }
110
111        if ($a_entity == "news_settings") {
112            switch ($a_version) {
113                case "5.4.0":
114                    foreach ($a_ids as $obj_id) {
115                        $this->data[$obj_id]["ObjId"] = $obj_id;
116                        $this->data[$obj_id]["PublicFeed"] = ilBlockSetting::_lookup("news", "public_feed", 0, $obj_id);
117                        $this->data[$obj_id]["KeepRssMin"] = (int) ilBlockSetting::_lookup("news", "keep_rss_min", 0, $obj_id);
118                        $this->data[$obj_id]["DefaultVisibility"] = ilBlockSetting::_lookup("news", "default_visibility", 0, $obj_id);
119                        $this->data[$obj_id]["HideNewsPerDate"] = (int) ilBlockSetting::_lookup("news", "hide_news_per_date", 0, $obj_id);
120                        $this->data[$obj_id]["HideNewsDate"] = ilBlockSetting::_lookup("news", "hide_news_date", 0, $obj_id);
121                        $this->data[$obj_id]["PublicNotifications"] = (int) ilBlockSetting::_lookup("news", "public_notifications", 0, $obj_id);
122                    }
123                    break;
124            }
125        }
126
127    }
128
129    /**
130     * Determine the dependent sets of data
131     */
132    protected function getDependencies($a_entity, $a_version, $a_rec, $a_ids)
133    {
134        return false;
135    }
136
137
138    /**
139     * Import record
140     *
141     * @param
142     * @return
143     */
144    public function importRecord($a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version)
145    {
146        //echo $a_entity;
147        //var_dump($a_rec);
148
149        switch ($a_entity) {
150            case "news":
151                $mob_id = null;
152                if ($a_rec["MobId"] > 0) {
153                    $mob_id = $a_mapping->getMapping("Services/MediaObjects", "mob", $a_rec["MobId"]);
154                }
155                $c = (int) $a_rec["ContextObjId"] . ":" . $a_rec["ContextObjType"] . ":" . (int) $a_rec["ContextSubObjId"] .
156                    ":" . $a_rec["ContextSubObjType"];
157                $context = $a_mapping->getMapping("Services/News", "news_context", $c);
158                $context = explode(":", $context);
159//var_dump($c);
160//var_dump($a_mapping->mappings["Services/News"]["news_context"]);
161                include_once("./Services/News/classes/class.ilNewsItem.php");
162                $newObj = new ilNewsItem();
163                $newObj->setTitle($a_rec["Title"]);
164                $newObj->setContent($a_rec["Content"]);
165                $newObj->setPriority($a_rec["Priority"]);
166                $newObj->setContextObjId($context[0]);
167                $newObj->setContextObjType($context[1]);
168                $newObj->setContextSubObjId($context[2]);
169                $newObj->setContextSubObjType($context[3]);
170                $newObj->setContentType($a_rec["ContentType"]);
171                $newObj->setVisibility($a_rec["Visibility"]);
172                $newObj->setContentLong($a_rec["ContentLong"]);
173                $newObj->setContentIsLangVar($a_rec["ContentIsLangVar"]);
174                $newObj->setMobId($mob_id);
175                $newObj->setPlaytime($a_rec["Playtime"]);
176                $newObj->create();
177                $a_mapping->addMapping("Services/News", "news", $a_rec["Id"], $newObj->getId());
178                break;
179
180            case "news_settings":
181
182                $dummy_dataset = new ilObjectDataSet();
183                $new_obj_id = $dummy_dataset->getNewObjId($a_mapping,  $a_rec["ObjId"]);
184
185                if ($new_obj_id > 0 && $a_schema_version == "5.4.0") {
186                    foreach ([
187                        "public_feed" => "PublicFeed",
188                        "keep_rss_min" => "KeepRssMin",
189                        "default_visibility" => "DefaultVisibility",
190                        "hide_news_per_date" => "HideNewsPerDate",
191                        "hide_news_date" => "HideNewsDate",
192                        "public_notifications" => "PublicNotifications"
193                         ] as $set => $field) {
194                        ilBlockSetting::_write(
195                            "news",
196                            $set,
197                            $a_rec[$field],
198                            0,
199                            $new_obj_id
200                        );
201                    }
202                }
203                break;
204
205        }
206    }
207}
208