1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2001 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
24
25/**
26* Meta Data class (element meta_data)
27*
28* @package ilias-core
29* @version $Id$
30*/
31include_once 'class.ilMDBase.php';
32
33class ilMDMetaMetadata extends ilMDBase
34{
35    public function getPossibleSubelements()
36    {
37        $subs['Identifier'] = 'meta_identifier';
38        $subs['Contribute'] = 'meta_contribute';
39
40        return $subs;
41    }
42
43
44    // SUBELEMENTS
45    public function &getIdentifierIds()
46    {
47        include_once 'Services/MetaData/classes/class.ilMDIdentifier.php';
48
49        return ilMDIdentifier::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_meta_data');
50    }
51    public function &getIdentifier($a_identifier_id)
52    {
53        include_once 'Services/MetaData/classes/class.ilMDIdentifier.php';
54
55        if (!$a_identifier_id) {
56            return false;
57        }
58        $ide = new ilMDIdentifier();
59        $ide->setMetaId($a_identifier_id);
60
61        return $ide;
62    }
63    public function &addIdentifier()
64    {
65        include_once 'Services/MetaData/classes/class.ilMDIdentifier.php';
66
67        $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId(), $this->getObjType());
68        $ide->setParentId($this->getMetaId());
69        $ide->setParentType('meta_meta_data');
70
71        return $ide;
72    }
73
74    public function &getContributeIds()
75    {
76        include_once 'Services/MetaData/classes/class.ilMDContribute.php';
77
78        return ilMDContribute::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_meta_data');
79    }
80    public function &getContribute($a_contribute_id)
81    {
82        include_once 'Services/MetaData/classes/class.ilMDContribute.php';
83
84        if (!$a_contribute_id) {
85            return false;
86        }
87        $con = new ilMDContribute();
88        $con->setMetaId($a_contribute_id);
89
90        return $con;
91    }
92    public function &addContribute()
93    {
94        include_once 'Services/MetaData/classes/class.ilMDContribute.php';
95
96        $con = new ilMDContribute($this->getRBACId(), $this->getObjId(), $this->getObjType());
97        $con->setParentId($this->getMetaId());
98        $con->setParentType('meta_meta_data');
99
100        return $con;
101    }
102
103
104
105    // SET/GET
106    public function setMetaDataScheme($a_val)
107    {
108        $this->meta_data_scheme = $a_val;
109    }
110    public function getMetaDataScheme()
111    {
112        // Fixed attribute
113        return 'LOM v 1.0';
114    }
115    public function setLanguage(&$lng_obj)
116    {
117        if (is_object($lng_obj)) {
118            $this->language = $lng_obj;
119        }
120    }
121    public function &getLanguage()
122    {
123        return is_object($this->language) ? $this->language : false;
124    }
125    public function getLanguageCode()
126    {
127        return is_object($this->language) ? $this->language->getLanguageCode() : false;
128    }
129
130
131    public function save()
132    {
133        global $DIC;
134
135        $ilDB = $DIC['ilDB'];
136
137        $fields = $this->__getFields();
138        $fields['meta_meta_data_id'] = array('integer',$next_id = $ilDB->nextId('il_meta_meta_data'));
139
140        if ($this->db->insert('il_meta_meta_data', $fields)) {
141            $this->setMetaId($next_id);
142            return $this->getMetaId();
143        }
144        return false;
145    }
146
147    public function update()
148    {
149        global $DIC;
150
151        $ilDB = $DIC['ilDB'];
152
153        if ($this->getMetaId()) {
154            if ($this->db->update(
155                'il_meta_meta_data',
156                $this->__getFields(),
157                array("meta_meta_data_id" => array('integer',$this->getMetaId()))
158            )) {
159                return true;
160            }
161        }
162        return false;
163    }
164
165    public function delete()
166    {
167        global $DIC;
168
169        $ilDB = $DIC['ilDB'];
170
171        if ($this->getMetaId()) {
172            $query = "DELETE FROM il_meta_meta_data " .
173                "WHERE meta_meta_data_id = " . $ilDB->quote($this->getMetaId(), 'integer');
174            $res = $ilDB->manipulate($query);
175
176
177            foreach ($this->getIdentifierIds() as $id) {
178                $ide = $this->getIdentifier($id);
179                $ide->delete();
180            }
181
182            foreach ($this->getContributeIds() as $id) {
183                $con = $this->getContribute($id);
184                $con->delete();
185            }
186            return true;
187        }
188
189        return false;
190    }
191
192
193    public function __getFields()
194    {
195        return array('rbac_id' => array('integer',$this->getRBACId()),
196                     'obj_id' => array('integer',$this->getObjId()),
197                     'obj_type' => array('text',$this->getObjType()),
198                     'meta_data_scheme' => array('text',$this->getMetaDataScheme()),
199                     'language' => array('text',$this->getLanguageCode()));
200    }
201
202    public function read()
203    {
204        global $DIC;
205
206        $ilDB = $DIC['ilDB'];
207
208        include_once 'Services/MetaData/classes/class.ilMDLanguageItem.php';
209
210
211        if ($this->getMetaId()) {
212            $query = "SELECT * FROM il_meta_meta_data " .
213                "WHERE meta_meta_data_id = " . $ilDB->quote($this->getMetaId(), 'integer');
214
215
216            $res = $this->db->query($query);
217            while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
218                $this->setRBACId($row->rbac_id);
219                $this->setObjId($row->obj_id);
220                $this->setObjType($row->obj_type);
221                $this->setMetaDataScheme($row->meta_data_scheme);
222                $this->setLanguage(new ilMDLanguageItem($row->language));
223            }
224            return true;
225        }
226        return false;
227    }
228
229    /*
230     * XML Export of all meta data
231     * @param object (xml writer) see class.ilMD2XML.php
232     *
233     */
234    public function toXML(&$writer)
235    {
236        if ($this->getMetaDataScheme()) {
237            $attr['MetadataScheme'] = $this->getMetaDataScheme();
238        }
239        if ($this->getLanguageCode()) {
240            $attr['Language'] = $this->getLanguageCode();
241        }
242        $writer->xmlStartTag('Meta-Metadata', $attr ? $attr : null);
243
244        // ELEMENT IDENTIFIER
245        $identifiers = $this->getIdentifierIds();
246        foreach ($identifiers as $id) {
247            $ide = &$this->getIdentifier($id);
248            $ide->toXML($writer);
249        }
250        if (!count($identifiers)) {
251            include_once 'Services/Metadata/classes/class.ilMDIdentifier.php';
252            $ide = new ilMDIdentifier($this->getRBACId(), $this->getObjId());
253            $ide->toXML($writer);
254        }
255
256        // ELEMETN Contribute
257        $contributes = $this->getContributeIds();
258        foreach ($contributes as $id) {
259            $con = &$this->getContribute($id);
260            $con->toXML($writer);
261        }
262        if (!count($contributes)) {
263            include_once 'Services/MetaData/classes/class.ilMDContribute.php';
264            $con = new ilMDContribute($this->getRBACId(), $this->getObjId());
265            $con->toXML($writer);
266        }
267
268        $writer->xmlEndTag('Meta-Metadata');
269    }
270
271    // STATIC
272    public static function _getId($a_rbac_id, $a_obj_id)
273    {
274        global $DIC;
275
276        $ilDB = $DIC['ilDB'];
277
278        $query = "SELECT meta_meta_data_id FROM il_meta_meta_data " .
279            "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
280            "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
281
282        $res = $ilDB->query($query);
283        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
284            return $row->meta_meta_data_id;
285        }
286        return false;
287    }
288}
289