Blame view

php/classes/SharedObjectTreeFile.php 7.23 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?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);
			}
		}
		
		
		//Remove obsolete folders and objects
		
		$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 ...
94
95
96
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
169f14d2   Benjamin Renard   Add shared object...
97
				continue;
91dee2fd   Benjamin Renard   Fix bug with the ...
98
			$folders[] = array('id' => $idAtt, 'name' => $nameAtt);
169f14d2   Benjamin Renard   Add shared object...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		}
		
		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 ...
132
133
134
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
169f14d2   Benjamin Renard   Add shared object...
135
				continue;
91dee2fd   Benjamin Renard   Fix bug with the ...
136
			if ($folder_id != $idAtt)
169f14d2   Benjamin Renard   Add shared object...
137
				continue;
91dee2fd   Benjamin Renard   Fix bug with the ...
138
			return $nameAtt;
169f14d2   Benjamin Renard   Add shared object...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		}
	
		return FALSE;
	}
	
	public function getFolderIdByObjectId($object_type, $object_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 ...
153
154
155
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
169f14d2   Benjamin Renard   Add shared object...
156
157
158
159
160
				continue;
			
			$objectNodes = $folderNode->getElementsByTagName($object_type);
			
			foreach ($objectNodes as $objectNode) {
91dee2fd   Benjamin Renard   Fix bug with the ...
161
162
				if ($idAtt == $object_id)
					return $idAtt;
169f14d2   Benjamin Renard   Add shared object...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
			}
		}

		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;
	}
}

?>