Blame view

php/classes/SharedObjectTreeFile.php 7.99 KB
169f14d2   Benjamin Renard   Add shared object...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
 * @class SharedObjectTreeFile
 * @version 
 * 
 */

class SharedObjectTreeFile  {
	public static $FILE_NAME = "SharedObjectTree.xml";
	
	private $doc = null;
	
	public static $ROOT_NODE_NAME = "shared";
	public static $LIST_NODE_NAME_SUFFIX = "List";
	
	function __construct() {
		$this->init();
	}
	
	public function update($object_type, $object_mgr) {
		//Get available objects
		$objects = $object_mgr->scanObjects();
		
		//Update related folders and objects
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			$listNode = $this->createObjectListNode($object_type);
		
		$lastObjectId = $listNode->getAttribute("lastObjectId");
		
		foreach ($objects as $folder_name => $folder_objects) {
			$folderNode = $this->getObjectFolderNode($object_type, $folder_name);
			
			if (!isset($folderNode))
				$folderNode = $this->createObjectFolderNode($object_type, $folder_name);
			
			foreach ($folder_objects as $object_id) {
				$objectNode = $this->getObjectNode($object_type, $folder_name, $object_id);
				
				if (!isset($objectNode))
					$objectNode = $this->createObjectNode($object_type, $folder_name, $object_id);
				
				if (!isset($objectNode))
					continue;
				
				$description = $object_mgr->getDescription($folder_name, $object_id);
				
				foreach ($description as $key => $value)
					$objectNode->setAttribute($key, $value);
			}
		}
c1c7a99c   Benjamin Renard   Add new functions...
53
		$this->doc->save($this->getFilePath());
169f14d2   Benjamin Renard   Add shared object...
54
55
		
		//Remove obsolete folders and objects
c1c7a99c   Benjamin Renard   Add new functions...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		$folders = $this->getFolders($object_type);
		foreach ($folders as $folder) {
			$folderNode = $this->getObjectFolderNode($object_type, $folder["name"]);
			if (!isset($folderNode)) {
				continue;
			}
			if (!array_key_exists($folder["name"], $objects)) {
				$folderNode->parentNode->removeChild($folderNode);
			}
			else {
				$objectNodes = $folderNode->getElementsByTagName($object_type);
				$to_remove = array();
				foreach ($objectNodes as $objectNode) {
					$idAtt = $objectNode->getAttribute('xml:id');
					if (!in_array($idAtt, $objects[$folder["name"]])) {
						$to_remove[] = $objectNode;
					}
				}
				foreach ($to_remove as $node) {
					$folderNode->removeChild($node);
				}
			}
		}
169f14d2   Benjamin Renard   Add shared object...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
		
		$this->doc->save($this->getFilePath());
		
		return array('success' => TRUE);
	}
	
	public function reserveNewId($object_type) {
		$listNode = $this->getObjectListNode($object_type);
	
		if (!isset($listNode))
			return NULL;
	
		$lastObjectId = $listNode->getAttribute("lastObjectId");
	
		if ($lastObjectId == "")
			$lastObjectId = 'shared' . $object_type . '_0';
		else {
			$res = sscanf($lastObjectId , 'shared' . $object_type . '_%d');
			$lastObjectId = 'shared' . $object_type . '_' . ($res[0] + 1);
		}
	
		$listNode->setAttribute("lastObjectId", $lastObjectId);
	
		$this->doc->save($this->getFilePath());
	
		return $lastObjectId;
	}
	
	public function getFolders($object_type) {
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			return array();
		
		$folderNodes = $listNode->getElementsByTagName("folder");
		
		$folders = array();
		foreach ($folderNodes as $folderNode) {
91dee2fd   Benjamin Renard   Fix bug with the ...
117
118
119
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
169f14d2   Benjamin Renard   Add shared object...
120
				continue;
91dee2fd   Benjamin Renard   Fix bug with the ...
121
			$folders[] = array('id' => $idAtt, 'name' => $nameAtt);
169f14d2   Benjamin Renard   Add shared object...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		}
		
		return $folders;
	}
	
	public function isNameAlreadyUsed($object_type, $name) {
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			return FALSE;
		
		$folderNodes = $listNode->getElementsByTagName("folder");
		
		foreach ($folderNodes as $folderNode) {
			$objectNodes = $folderNode->getElementsByTagName($object_type);
			foreach ($objectNodes as $objectNode) {
				if ($objectNode->getAttribute('name') == $name)
					return TRUE;
			}
		}
		
		return FALSE;
	}
	
	public function getFolderNameById($object_type, $folder_id) {
		$listNode = $this->getObjectListNode($object_type);
	
		if (!isset($listNode))
			return "";
	
		$folderNodes = $listNode->getElementsByTagName("folder");
	
		foreach ($folderNodes as $folderNode) {
91dee2fd   Benjamin Renard   Fix bug with the ...
155
156
157
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
169f14d2   Benjamin Renard   Add shared object...
158
				continue;
91dee2fd   Benjamin Renard   Fix bug with the ...
159
			if ($folder_id != $idAtt)
169f14d2   Benjamin Renard   Add shared object...
160
				continue;
91dee2fd   Benjamin Renard   Fix bug with the ...
161
			return $nameAtt;
169f14d2   Benjamin Renard   Add shared object...
162
163
164
165
166
167
168
		}
	
		return FALSE;
	}
	
	public function getFolderIdByObjectId($object_type, $object_id) {
		$listNode = $this->getObjectListNode($object_type);
169f14d2   Benjamin Renard   Add shared object...
169
170
		if (!isset($listNode))
			return "";
11bb1a8f   Benjamin Renard   Fix #5379
171

169f14d2   Benjamin Renard   Add shared object...
172
		$folderNodes = $listNode->getElementsByTagName("folder");
169f14d2   Benjamin Renard   Add shared object...
173
		foreach ($folderNodes as $folderNode) {
91dee2fd   Benjamin Renard   Fix bug with the ...
174
175
176
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
169f14d2   Benjamin Renard   Add shared object...
177
178
179
180
181
				continue;
			
			$objectNodes = $folderNode->getElementsByTagName($object_type);
			
			foreach ($objectNodes as $objectNode) {
11bb1a8f   Benjamin Renard   Fix #5379
182
				if ($objectNode->getAttribute('xml:id') == $object_id)
91dee2fd   Benjamin Renard   Fix bug with the ...
183
					return $idAtt;
169f14d2   Benjamin Renard   Add shared object...
184
185
			}
		}
169f14d2   Benjamin Renard   Add shared object...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
		return "";
	}
	
	private function init() {
		if (!file_exists($this->getFilePath()))
			$this->createFile();
		else
			$this->loadFile();
	}
	
	public function getFilePath() {
		
		return SHAREDPATH . "/" . self::$FILE_NAME;
	}
	
	private function createFile() {
		$this->doc = new DOMDocument("1.0", "UTF-8");
		$this->doc->preserveWhiteSpace = false;
		$this->doc->formatOutput = true;
		
		$xmlNode = $this->doc->createElement("shared");
		$this->doc->appendChild($xmlNode);
		
		$this->doc->save($this->getFilePath());
		chgrp($this->getFilePath(), APACHE_USER);
		chmod($this->getFilePath(), 0775);
	}
	
	private function loadFile() {
		$this->doc = new DOMDocument("1.0", "UTF-8");
		$this->doc->preserveWhiteSpace = false;
		$this->doc->formatOutput = true;
		
		$this->doc->load($this->getFilePath());
	}
	
	private function getObjectListNode($object_type) {
		$nodeName = $object_type . "List";
		$listNodes = $this->doc->documentElement->getElementsByTagName($nodeName);
		
		if (count($listNodes) == 0) 
			return NULL;
		
		return $listNodes->item(0);
	}
	
	private function createObjectListNode($object_type) {
		$nodeName = $object_type . "List";
		
		$listNode = $this->doc->createElement($nodeName);
		$listNode->setAttribute("xml:id", "shared" . $object_type . "-treeRootNode");
		$this->doc->documentElement->appendChild($listNode);
		$this->doc->save($this->getFilePath());
		return $listNode;
	}
	
	private function getObjectFolderNode($object_type, $folder_name) {
		$listNode = $this->getObjectListNode($object_type);
		if (!isset($listNode))
			return NULL;
		
		$folderNodes = $listNode->getElementsByTagName("folder");
		
		foreach ($folderNodes as $folderNode) {
			if ($folderNode->getAttribute('name') == $folder_name)
				return $folderNode;
		}
		
		return NULL;
	}
	
	private function createObjectFolderNode($object_type, $folder_name) {
		$listNode = $this->getObjectListNode($object_type);
		if (!isset($listNode))
			return NULL;
		
		$folderNode = $this->doc->createElement("folder");
		$folderNode->setAttribute("xml:id", $folder_name . '_' . $object_type);
		$folderNode->setAttribute("name", $folder_name);
		
		$listNode->appendChild($folderNode);
		$this->doc->save($this->getFilePath());
		return $folderNode;
	}
	
	private function getObjectNode($object_type, $folder_name, $object_id) {
		$folderNode = $this->getObjectFolderNode($object_type, $folder_name);
		
		if (!isset($folderNode))
			return NULL;
		
		$objectNodes = $folderNode->getElementsByTagName($object_type);
		
		foreach ($objectNodes as $objectNode) {
			if ($objectNode->getAttribute('xml:id') == $object_id)
				return $objectNode;
		}
		
		return NULL;
	}
	
	private function createObjectNode($object_type, $folder_name, $object_id) {
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			return NULL;
		
		$folderNode = $this->getObjectFolderNode($object_type, $folder_name);
		
		if (!isset($folderNode))
			return NULL;
		
		$objectNode = $this->doc->createElement($object_type);
		$objectNode->setAttribute("xml:id", $object_id);
		
		$folderNode->appendChild($objectNode);
		
		$this->doc->save($this->getFilePath());
		
		return $objectNode;
	}
}

11bb1a8f   Benjamin Renard   Fix #5379
309
?>