1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilOrgUnitImporter
6 *
7 * @author  Oskar Truffer <ot@studer-raimann.ch>
8 * @author  Martin Studer <ms@studer-raimann.ch>
9 * @author  Fabian Schmid <fs@studer-raimann.ch>
10 */
11class ilOrgUnitImporter extends ilXmlImporter
12{
13
14    /**
15     * @var  array lang_var => language variable, import_id => the reference or import id, depending on the ou_id_type
16     */
17    public $errors = [];
18    /**
19     * @var  array lang_var => language variable, import_id => the reference or import id, depending on the ou_id_type
20     */
21    public $warnings = [];
22    /**
23     * @var array keys in {updated, edited, deleted}
24     */
25    public $stats;
26
27
28    /**
29     * @param $id
30     * @param $type
31     *
32     * @return bool|int
33     */
34    protected function buildRef($id, $type)
35    {
36        if ($type == 'reference_id') {
37            if (!ilObjOrgUnit::_exists($id, true)) {
38                return false;
39            }
40
41            return $id;
42        } elseif ($type == 'external_id') {
43            $obj_id = ilObject::_lookupObjIdByImportId($id);
44
45            if (!ilObject::_hasUntrashedReference($obj_id)) {
46                return false;
47            }
48
49            $ref_ids = ilObject::_getAllReferences($obj_id);
50
51            if (!count($ref_ids)) {
52                return false;
53            }
54
55            foreach ($ref_ids as $ref_id) {
56                if (!ilObject::_isInTrash($ref_id)) {
57                    return $ref_id;
58                }
59            }
60            return false;
61        } else {
62            return false;
63        }
64    }
65
66    /**
67     * @param string $external_id
68     * @return bool
69     */
70    public function hasMoreThanOneMatch($external_id)
71    {
72        global $DIC;
73
74        $ilDB = $DIC->database();
75
76        $query = "SELECT * FROM object_data " .
77            "INNER JOIN object_reference as ref on ref.obj_id = object_data.obj_id and ref.deleted is null ".
78            'WHERE object_data.type = "orgu" and import_id = ' . $ilDB->quote($external_id, "text") . " " .
79            "ORDER BY create_date DESC";
80        $res = $ilDB->query($query);
81
82        if ($ilDB->numRows($res) > 1) {
83            return true;
84        } else {
85            return false;
86        }
87    }
88
89
90
91    /**
92     * @return bool
93     */
94    public function hasErrors()
95    {
96        return count($this->errors) != 0;
97    }
98
99
100    /**
101     * @return bool
102     */
103    public function hasWarnings()
104    {
105        return count($this->warnings) != 0;
106    }
107
108
109    /**
110     * @param      $lang_var
111     * @param      $import_id
112     * @param null $action
113     */
114    public function addWarning($lang_var, $import_id, $action = null)
115    {
116        $this->warnings[] = array( 'lang_var' => $lang_var, 'import_id' => $import_id, 'action' => $action );
117    }
118
119
120    /**
121     * @param      $lang_var
122     * @param      $import_id
123     * @param null $action
124     */
125    public function addError($lang_var, $import_id, $action = null)
126    {
127        $this->errors[] = array( 'lang_var' => $lang_var, 'import_id' => $import_id, 'action' => $action );
128    }
129
130
131    /**
132     * @return array
133     */
134    public function getErrors()
135    {
136        return $this->errors;
137    }
138
139
140    /**
141     * @return array
142     */
143    public function getWarnings()
144    {
145        return $this->warnings;
146    }
147
148
149    /**
150     * @return array
151     */
152    public function getStats()
153    {
154        return $this->stats;
155    }
156
157
158    /**
159     * @param $a_entity
160     * @param $a_id
161     * @param $a_xml
162     * @param $a_mapping ilImportMapping
163     *
164     * @return string|void
165     *
166     * @deprecated
167     */
168    public function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
169    {
170        $container_mappings = $a_mapping->getMappingsOfEntity("Services/Container", "objs");
171        foreach ($container_mappings as $old => $new) {
172            echo ilObject2::_lookupType($new);
173            if (ilObject2::_lookupType($new) == "orgu") {
174                $a_mapping->addMapping("Modules/OrgUnit", "orgu", $old, $new);
175            }
176        }
177    }
178}
179