1<?php
2
3class ilADTGroup extends ilADT
4{
5    protected $elements; // [array]
6
7    public function __clone()
8    {
9        if (is_array($this->elements)) {
10            foreach ($this->elements as $id => $element) {
11                $this->elements[$id] = clone $element;
12            }
13        }
14    }
15
16
17    // definition
18
19    protected function isValidDefinition(ilADTDefinition $a_def)
20    {
21        return ($a_def instanceof ilADTGroupDefinition);
22    }
23
24    protected function setDefinition(ilADTDefinition $a_def)
25    {
26        parent::setDefinition($a_def);
27
28        $this->elements = array();
29
30        foreach ($this->getDefinition()->getElements() as $name => $def) {
31            $this->addElement($name, $def);
32        }
33    }
34
35
36    // defaults
37
38    public function reset()
39    {
40        parent::reset();
41
42        $elements = $this->getElements();
43        if (is_array($elements)) {
44            foreach ($elements as $element) {
45                $element->reset();
46            }
47        }
48    }
49
50
51    // properties
52
53    protected function addElement($a_name, ilADTDefinition $a_def)
54    {
55        $this->elements[$a_name] = ilADTFactory::getInstance()->getInstanceByDefinition($a_def);
56    }
57
58    public function hasElement($a_name)
59    {
60        return array_key_exists($a_name, $this->elements);
61    }
62
63    public function getElement($a_name)
64    {
65        if ($this->hasElement($a_name)) {
66            return $this->elements[$a_name];
67        }
68    }
69
70    public function getElements()
71    {
72        return $this->elements;
73    }
74
75
76
77    // comparison
78
79    public function equals(ilADT $a_adt)
80    {
81        if ($this->getDefinition()->isComparableTo($a_adt)) {
82            return ($this->getCheckSum() == $a_adt->getCheckSum());
83        }
84    }
85
86    public function isLarger(ilADT $a_adt)
87    {
88        // return null?
89    }
90
91    public function isSmaller(ilADT $a_adt)
92    {
93        // return null?
94    }
95
96
97    // null
98
99    public function isNull()
100    {
101        return !sizeof($this->getElements());
102    }
103
104
105    // validation
106
107    public function getValidationErrorsByElements()
108    {
109        return (array) $this->validation_errors;
110    }
111
112    public function getValidationErrors()
113    {
114        return array_keys((array) $this->validation_errors);
115    }
116
117    protected function addElementValidationError($a_element_id, $a_error_code)
118    {
119        $this->validation_errors[(string) $a_error_code] = $a_element_id;
120    }
121
122    public function isValid()
123    {
124        $valid = parent::isValid();
125
126        if (!$this->isNull()) {
127            foreach ($this->getElements() as $element_id => $element) {
128                if (!$element->isValid()) {
129                    foreach ($element->getValidationErrors() as $error) {
130                        $this->addElementValidationError($element_id, $error);
131                    }
132                    $valid = false;
133                }
134            }
135        }
136
137        return $valid;
138    }
139
140    public function translateErrorCode($a_code)
141    {
142        if (isset($this->validation_errors[$a_code])) {
143            $element_id = $this->validation_errors[$a_code];
144            $element = $this->getElement($element_id);
145            if ($element) {
146                return $element->translateErrorCode($a_code);
147            }
148        }
149    }
150
151
152    // check
153
154    public function getCheckSum()
155    {
156        if (!$this->isNull()) {
157            $tmp = array();
158            foreach ($this->getElements() as $element) {
159                $tmp[] = $element->getCheckSum();
160            }
161            return md5(implode(",", $tmp));
162        }
163    }
164
165
166    // stdClass
167
168    public function exportStdClass()
169    {
170        $obj = new stdClass();
171        foreach ($this->getElements() as $id => $element) {
172            $obj->$id = $element->exportStdClass();
173        }
174        return $obj;
175    }
176
177    public function importStdClass($a_std)
178    {
179        if (is_object($a_std)) {
180            foreach ($this->getElements() as $id => $element) {
181                $element->importStdClass($a_std->$id);
182            }
183        }
184    }
185}
186