Code

b76c1746546196267432b5d387aa57fc3c037b7a
[gosa.git] / gosa-plugins / goto-ng / admin / newConfigManagement / class_ConfigManagementDataModel.inc
1 <?php
3 class ConfigManagementDataModel
4 {
5     public $data = array();
6     public $typeToPath = array();
7     public $idToPath = array();
8     public $dnToPath = array();
9     public $config = NULL;
11     function __construct($config)
12     {
13         $this->config = &$config;
14         $this->data = array();
15         $this->data['linear'] = array();
16         $this->data['recursive'] = array();
17         $this->addItem('root', '','root', array());
18     }
20     function getDataModel()
21     {
22         return($this->data);
23     }
25     function moveItem($from, $to)
26     {
27         // Get source entries
28         $itemFrom = &$this->data['linear'][$from];
29         $parentFrom = &$this->data['linear'][$itemFrom['parentPath']];
31         // Extract 'to' informations out of the path. 
32         $name = preg_replace("/^.*\//","", $to);
33         $toParentPath = preg_replace("/\/[^\/]*$/","", $to);
34         $parentTo = &$this->data['linear'][$toParentPath];
35         $parentToDn = $parentTo['dn'];
36         $dn = rtrim("cn={$name},{$parentToDn}",',');
38         // Append the 'from' entry to the 'to' parent children. 
39         $parentTo['children'][$to] = $parentFrom['children'][$from];
40         $parentTo['children'][$to]['path']= $to;
41         $parentTo['children'][$to]['name']= $name;
42         $parentTo['children'][$to]['dn']= $dn;
44         // Unset the source path
45         unset($parentFrom['children'][$from]);
46         unset($this->data['linear'][$from]);
48         // Append the linear entry.
49         $this->data['linear'][$to] = &$parentTo['children'][$to];
51         // Update id->Path mapping
52         $id = $this->data['linear'][$to]['id'];
53         $this->idToPath[$id] = $to;
54         $this->dnToPath[$dn] = $to;
56     }
58     function removeItem($path)
59     {
60         $item = &$this->data['linear'][$path];
61         $parent = &$this->data['linear'][$item['parentPath']];
62         unset($parent['children'][$path]);
63         unset($this->data['linear'][$path]);
64     }
66     function addItem($type, $path, $name, $values = array(), $status = "")
67     {
68         if($path == ""){
69             $parentItem = &$this->data['recursive'];
70             $parentId = NULL;
71             $parentDn = $this->config->current['BASE'];
72         }elseif(isset($this->data['linear'][$path])){
73             $parentItem = &$this->data['linear'][$path]['children'];
74             $parentId = $this->data['linear'][$path]['id'];
75             $parentDn = $this->data['linear'][$path]['dn'];
76         }else{
77             trigger_error("Parent item ({$path}) does not exists!");
78             return(FALSE);
79         }
81         $this->currentID ++;
82         $entryPath = "{$path}/{$name}";
83         $entryDn  = rtrim("cn={$name},{$parentDn}",',');
84         $entryId =  $this->currentID;
85         
86         $entry['path'] = $entryPath;
87         $entry['dn'] = $entryDn;
88         $entry['id'] = $entryId;
89         $entry['parentId'] = $parentId;
90         $entry['name'] = $name;
91         $entry['path'] = $entryPath;
92         $entry['parentPath'] = $path;
93         $entry['type'] = $type;
94         $entry['status'] = $status;
95         $entry['values'] = $values;
96         $entry['children'] = array();
98         $parentItem[$entryPath] = &$entry;
99         $this->data['linear'][$entryPath] = &$parentItem[$entryPath];
101         $this->idToPath[$entryId] = $entryPath;
102         $this->dnToPath[$entryDn] = $entryPath;
104         $this->typeToPath[$type][$name] = $entryPath;
105     
106         return($entryId);
107     }
109     function pathToDn($path)
110     {
111         
112     }
114     function hasChildren($path)
115     {
116         if( $this->itemExistsByPath($path) && 
117             $this->data['linear'][$path]['children'] && 
118             count($this->data['linear'][$path]['children'] )){
119                 return(TRUE);
120         }
121         return(FALSE);
122     }
124     function itemExistsByPath($path)
125     {
126         return(isset($this->data['linear'][$path]));
127     }
128     
129     function getItemByPath($path)
130     {
131         if(isset($this->data['linear'][$path])){
132             return($this->data['linear'][$path]);
133         }
134         return(NULL);
135     }
137     function setItemStatus($path, $status)
138     {
139         if(!$this->itemExistsByPath($path)){
140             trigger_error("Invalid item path '{$path}'!");
141         }else{
142             $this->data['linear'][$path]['status'] = $status;
143         }
144     }
146     function setItemValues($path, $values)
147     {
148         if(!$this->itemExistsByPath($path)){
149             trigger_error("Invalid item path '{$path}'!");
150         }else{
151             $this->data['linear'][$path]['values'] = $values;
152         }
153     }
155     function getItemsByType($types)
156     {
157         $res = array();
158         foreach($this->data['linear'] as $path => $item){
159             if(in_array($item['type'],$types)) $res[] = $item;
160         }
161         return($res);
162     }
165     function getItemById($id)
166     {
167         $path = NULL;
168         if(isset($this->idToPath[$id])){
169             $path = $this->idToPath[$id];
170         }else{
171             return(NULL);
172         }
173         if(isset($this->data['linear'][$path])){
174             return($this->data['linear'][$path]);
175         }
176         return(NULL);
177     }
179     function getItemByDn($dn)
180     {
181         $path = NULL;
182         if(isset($this->dnToPath[$dn])){
183             $path = $this->dnToPath[$dn];
184         }else{
185             return(NULL);
186         }
187         if(isset($this->data['linear'][$path])){
188             return($this->data['linear'][$path]);
189         }
190         return(NULL);
191     }
194 ?>