Blame view

php/classes/AmdaObjectMgr.php 9.61 KB
16035364   Benjamin Renard   First commit
1
2
3
4
<?php
/**
 * @class AmdaObjectMgr 
 * @version $Id: AmdaObjectMgr.php 2891 2015-04-30 11:31:51Z elena $
16035364   Benjamin Renard   First commit
5
6
 */

b1d50569   Elena.Budnik   formattage
7
8
class AmdaObjectMgr
{
fdf1413b   Elena.Budnik   TT download
9
10
11
12
13
14
15
16
17
18
19
20
	protected $xmlName, $xp;      
	protected $attributes, $optionalAttributes;       
	protected $contentRootId, $objTagName;
	protected $descFileName, $resFileName;
	protected $id_prefix;
	protected $contentRootTag;
	protected $obj, $id;
	protected $types;

	public $contentDom; 
	public $objectDom;

b1d50569   Elena.Budnik   formattage
21
22
	protected function __construct($xmlFile) 
	{
fdf1413b   Elena.Budnik   TT download
23
		// content XML
16035364   Benjamin Renard   First commit
24
25
26
27
28
29
		$this->xmlName = USERWSDIR."/".$xmlFile;
		$this->contentDom = new DomDocument("1.0");
		$this->contentDom->preserveWhiteSpace = false;
		$this->contentDom->formatOutput = true;

		if (file_exists($this->xmlName)) {
fdf1413b   Elena.Budnik   TT download
30
31
			$this->contentDom -> load($this->xmlName);	 
			$this->xp = new domxpath($this->contentDom); 
16035364   Benjamin Renard   First commit
32
		}
fdf1413b   Elena.Budnik   TT download
33
34
		// object desc XML
		//TODO not used in RequestMgr
16035364   Benjamin Renard   First commit
35
36
37
		$this->objectDom = new DomDocument("1.0"); 
		$this->objectDom->preserveWhiteSpace = false;
		$this->objectDom->formatOutput = true;
b1d50569   Elena.Budnik   formattage
38
	}
16035364   Benjamin Renard   First commit
39
   
b1d50569   Elena.Budnik   formattage
40
41
	private function getNewId() 
	{                   
fdf1413b   Elena.Budnik   TT download
42
43
		//  Get all ID attributes
		$elements = $this->xp->query("//".$this->objTagName."/@xml:id");
16035364   Benjamin Renard   First commit
44

fdf1413b   Elena.Budnik   TT download
45
		// Now find New Valid ID  
b1d50569   Elena.Budnik   formattage
46
47
48
49
		if ($elements->length  > 0) 
		{
			if($elements->length > 0) for ($i = 0; $i < $elements->length; $i++) 
			{
fdf1413b   Elena.Budnik   TT download
50
51
				$id =  explode('_',$elements->item($i)->nodeValue);
				$idList[] = $id[1]; 
16035364   Benjamin Renard   First commit
52
53
54
55
			}

			sort($idList);

b1d50569   Elena.Budnik   formattage
56
57
58
59
			for ($i = 0; $i < count($idList); $i++) 
			{
				if ($idList[$i] > $i) 
				{ 
fdf1413b   Elena.Budnik   TT download
60
61
					$newID = $i;
					break;
16035364   Benjamin Renard   First commit
62
				}  
fdf1413b   Elena.Budnik   TT download
63
64
				$newID = $i+1;  
			}                 
16035364   Benjamin Renard   First commit
65
		  } else
b1d50569   Elena.Budnik   formattage
66
67
			$newID = 0;
			
fdf1413b   Elena.Budnik   TT download
68
69
		return $newID;
	}
16035364   Benjamin Renard   First commit
70

b1d50569   Elena.Budnik   formattage
71
72
	protected function setId() 
	{ 	
fdf1413b   Elena.Budnik   TT download
73
		$id_ = $this->getNewId();
16035364   Benjamin Renard   First commit
74
75
		if ($id_ === false) return false;
		$this->id = $this->id_prefix.$id_;
fdf1413b   Elena.Budnik   TT download
76
		
16035364   Benjamin Renard   First commit
77
78
79
		return $this->id;         
	} 
   
b1d50569   Elena.Budnik   formattage
80
81
	protected function objectExistsByName($name)
	{	
fdf1413b   Elena.Budnik   TT download
82
		$this->obj = $this->xp->query("//".$this->objTagName."[@name='".$name."']");		 
16035364   Benjamin Renard   First commit
83
		if ($this->obj->length != 0) return true;
fdf1413b   Elena.Budnik   TT download
84
		
16035364   Benjamin Renard   First commit
85
86
87
		return false;
	} 

b1d50569   Elena.Budnik   formattage
88
89
	protected function getObjectIdByName($name) 
	{	
16035364   Benjamin Renard   First commit
90
		$this->obj = $this->xp->query("//".$this->objTagName."[@name='".$name."']");
fdf1413b   Elena.Budnik   TT download
91
92
93
94
		if ($this->obj->length == 0) return false; 
		$id = $this->obj->item(0)->getAttribute('xml:id');
		if ($id) return $id;
		
16035364   Benjamin Renard   First commit
95
96
97
98
		return false;           
	}
	
	protected function folderExistsByName($name){
fdf1413b   Elena.Budnik   TT download
99
100
	
		$this->obj = $this->xp->query("//".$this->contentRootTag."/folder[@name='".$name."']");		 
16035364   Benjamin Renard   First commit
101
		if ($this->obj->length != 0) return true;
fdf1413b   Elena.Budnik   TT download
102
		
16035364   Benjamin Renard   First commit
103
104
105
		return false;
	}  

b1d50569   Elena.Budnik   formattage
106
107
	protected function objectExistsById($id)
	{ 	
16035364   Benjamin Renard   First commit
108
109
		$this->obj = $this->contentDom->getElementById($id); 
		if ($this->obj != null) return $this->obj;
fdf1413b   Elena.Budnik   TT download
110
		
16035364   Benjamin Renard   First commit
111
		return false;
fdf1413b   Elena.Budnik   TT download
112
	} 
16035364   Benjamin Renard   First commit
113
      
fdf1413b   Elena.Budnik   TT download
114
115
	protected function createObjectResource(){}
	protected function renameInResource(){}
16035364   Benjamin Renard   First commit
116
117
118
119

/*
*  Write Object into desc file
*/
b1d50569   Elena.Budnik   formattage
120
121
	protected function createObjectDescription($obj)
	{	
fdf1413b   Elena.Budnik   TT download
122
123
		$root = $this->objectDom->createElement($this->objTagName); 
		$root->setAttribute('xml:id',$this->id);
16035364   Benjamin Renard   First commit
124
          
b1d50569   Elena.Budnik   formattage
125
126
127
128
129
130
131
	      foreach($obj as $key => $value) 
	      {
				if ($key != 'id' && $key != 'leaf' && $key != 'nodeType') 
				{	
					$node =  $this->objectDom->createElement($key,htmlspecialchars($value)); 
					$root -> appendChild($node);
				}
16035364   Benjamin Renard   First commit
132
	      }      
fdf1413b   Elena.Budnik   TT download
133
		// add Optional Attributes if they are undefined	      
b1d50569   Elena.Budnik   formattage
134
135
136
137
		foreach ($this->optionalAttributes as $key => $value)
		{
			if ($root->getElementsByTagName($key)->length == 0) 
			{
fdf1413b   Elena.Budnik   TT download
138
139
140
				$node =  $this->objectDom->createElement($key,htmlspecialchars($value));  
				$root -> appendChild($node);
			}
b1d50569   Elena.Budnik   formattage
141
		}
16035364   Benjamin Renard   First commit
142
 
fdf1413b   Elena.Budnik   TT download
143
144
145
146
		$this->objectDom->appendChild($root);
		$this->objectDom->save($this->descFileName);  
	}
	
16035364   Benjamin Renard   First commit
147
148
149
/*
*    Just Save  Content XML
*/
b1d50569   Elena.Budnik   formattage
150
151
152
	protected function saveContent() 
	{	
		$this->contentDom->save($this->xmlName);
fdf1413b   Elena.Budnik   TT download
153
	}
16035364   Benjamin Renard   First commit
154
155
156
157
    
/*
* Add Object to Content XML
*/
b1d50569   Elena.Budnik   formattage
158
159
	protected function addToContent($obj, $folder) 
	{              
fdf1413b   Elena.Budnik   TT download
160
		$folderToAdd = null;
16035364   Benjamin Renard   First commit
161
162

		$objList = $this->contentDom->getElementById($this->contentRootId); 		
fdf1413b   Elena.Budnik   TT download
163
164
165
166
		$newObj = $this->contentDom->createElement($this->objTagName);
		$newObj->setAttribute('xml:id',$this->id);
		// object to mapped array
		$obj_arr = (array)$obj;
b1d50569   Elena.Budnik   formattage
167
168
		foreach ($this->attributes as $key => $value) 
		{
fdf1413b   Elena.Budnik   TT download
169
170
171
172
173
174
175
176
177
			$newObj->setAttribute($key, $obj_arr[$key]);	 
		}              
		if ($folder != null)  
			$folderToAdd = $this->contentDom->getElementById($folder);

		if ($folderToAdd) 
			$folderToAdd -> appendChild($newObj);
		else 
			$objList -> appendChild($newObj);		  
16035364   Benjamin Renard   First commit
178
179

		$this->saveContent();
fdf1413b   Elena.Budnik   TT download
180
	}
16035364   Benjamin Renard   First commit
181
182
     
/*
fdf1413b   Elena.Budnik   TT download
183
*    Delete Object From Content XML
16035364   Benjamin Renard   First commit
184
*/
b1d50569   Elena.Budnik   formattage
185
186
	protected function deleteFromContent($obj) 
	{	
fdf1413b   Elena.Budnik   TT download
187
188
		$objList = $obj -> parentNode; // $this->contentDom->getElementById($this->contentRootId);
		$objList -> removeChild($obj);
16035364   Benjamin Renard   First commit
189
		$this->saveContent();
fdf1413b   Elena.Budnik   TT download
190
	}
16035364   Benjamin Renard   First commit
191
192

/*
fdf1413b   Elena.Budnik   TT download
193
*    Create Folder
16035364   Benjamin Renard   First commit
194
*/
b1d50569   Elena.Budnik   formattage
195
196
	protected function createFolder($obj)
	{	
fdf1413b   Elena.Budnik   TT download
197
198
199
200
201
202
203
204
205
206
		if ($this -> folderExistsByName($obj->name)) 
			return array('error' => NAME_EXISTS);
		$newFolder = $this->contentDom->createElement('folder');
		$id = $obj->name."_".$this->objTagName;
		$newFolder -> setAttribute('xml:id',$id); 
		$newFolder -> setAttribute('name',$obj->name);
		$objList = $this->contentDom->getElementById($obj->parent);
		$objList -> appendChild($newFolder);
		$this -> saveContent();
		
16035364   Benjamin Renard   First commit
207
		return array('id' => $id);          
fdf1413b   Elena.Budnik   TT download
208
209
	}
	
16035364   Benjamin Renard   First commit
210
211
212
/*
*         Get Folder of the object 
*/
b1d50569   Elena.Budnik   formattage
213
214
	protected function getObjectFolder($id) 
	{
fdf1413b   Elena.Budnik   TT download
215
216
217
218
219
		if (!($obj = $this->objectExistsById($id))) 
			return "NO_SUCH_ID";
		if ($obj->parentNode->tagName == 'folder') 
			return $obj->parentNode->getAttribute('xml:id');
			
16035364   Benjamin Renard   First commit
220
		return null;
fdf1413b   Elena.Budnik   TT download
221
	}
16035364   Benjamin Renard   First commit
222
         
b1d50569   Elena.Budnik   formattage
223
224
	protected function setAlias($chain)
	{
16035364   Benjamin Renard   First commit
225
226
227
		$aliasMgr = new AliasMgr();		 
		$listeAlias = $aliasMgr->getList();

b1d50569   Elena.Budnik   formattage
228
229
		foreach($listeAlias as $alias) 
		{
16035364   Benjamin Renard   First commit
230
231
			$chain = $aliasMgr->substrParamAlias($chain, $alias->getAttribute("xml:id"),$alias->getAttribute("name"));
		}
16035364   Benjamin Renard   First commit
232
		return $chain;
fdf1413b   Elena.Budnik   TT download
233
	}
16035364   Benjamin Renard   First commit
234

b1d50569   Elena.Budnik   formattage
235
236
	protected function resetAlias($chain) 
	{
16035364   Benjamin Renard   First commit
237
238
239
		$aliasMgr = new AliasMgr();		 
		$listeAlias = $aliasMgr->getList();

b1d50569   Elena.Budnik   formattage
240
241
		foreach($listeAlias as $alias) 
		{
16035364   Benjamin Renard   First commit
242
243
			$chain = $aliasMgr->substrAliasParam($chain, $alias->getAttribute("xml:id"),$alias->getAttribute("name"));
		}
16035364   Benjamin Renard   First commit
244
245
246
		return $chain;
	}	
	
b1d50569   Elena.Budnik   formattage
247
248
	protected function createDom() 
	{		 
16035364   Benjamin Renard   First commit
249
		$rootElement = $this->contentDom->createElement('ws');
b1d50569   Elena.Budnik   formattage
250
251
		foreach ($this->types as $type) 
		{
fdf1413b   Elena.Budnik   TT download
252
253
254
255
256
			$contentId = $type.'-treeRootNode';
			$contentTag = $type.'List';
			$typeElement = $this->contentDom->createElement($contentTag);
			$typeElement->setAttribute('xml:id', $contentId);
			$rootElement->appendChild($typeElement);
16035364   Benjamin Renard   First commit
257
		}
fdf1413b   Elena.Budnik   TT download
258
259
		
		$this->contentDom->appendChild($rootElement);
16035364   Benjamin Renard   First commit
260
		$this->contentDom->save($this->xmlName);
fdf1413b   Elena.Budnik   TT download
261
	}
16035364   Benjamin Renard   First commit
262
263
264
265
266
	
/*
*        Create Parameter[TT...]/Folder
*        create object itself, add it to content DOM
*/   
b1d50569   Elena.Budnik   formattage
267
268
	function createObject($p, $folder)
	{
fdf1413b   Elena.Budnik   TT download
269
270
271
272
273
274
		if ($p -> leaf) 
			return $this->createParameter($p, $folder);
		//      else return $this->createFolder($p);
		//TODO check if this is possible?
		else 
			return array('error' => 'createFolder should be called from RENAME');
fdf1413b   Elena.Budnik   TT download
275
	}
16035364   Benjamin Renard   First commit
276
277
278
279

/*
*          Rename Parameter[TT...]/Folder
*/
b1d50569   Elena.Budnik   formattage
280
281
282
283
	function renameObject($p)
	{
		if (!($objToRename = $this -> objectExistsById($p->id))) 
		{
fdf1413b   Elena.Budnik   TT download
284
285
286
287
288
289
290
291
			// NO SUCH ID:  leaf -> error;
			if ($p->leaf) 
				return array('error' => NO_SUCH_ID);
			// NO SUCH ID:  folder  -> create  
			return  $this -> createFolder($p); 			    
		}
		
		// object was just DD in the tree : move tag in xml
b1d50569   Elena.Budnik   formattage
292
293
		if ($p -> name == $p -> old_name) 
		{
fdf1413b   Elena.Budnik   TT download
294
295
296
297
298
299
300
301
302
303
304
305
306
307
			if (!($parentNode = $this->contentDom->getElementById($p -> parent))) 
				return array('error' => NO_SUCH_PARENT_ID);   
			$parentNode -> appendChild($objToRename);  
			$this->saveContent();
			
			return array('id' => $p->id);
		}

		//TODO  CHECK: With some PHP versions setAttribute ADD attribute
		if ($objToRename->hasAttribute('name')) 
			$objToRename->removeAttribute('name');
			
		$objToRename->setAttribute('name',$p->name);
		
b1d50569   Elena.Budnik   formattage
308
309
		if (!$p->leaf) 
		{		 
fdf1413b   Elena.Budnik   TT download
310
311
312
			$objToRename->removeAttribute('xml:id');
			$objToRename->setAttribute('xml:id', $p->name.'_'.$this->objTagName);
		}
16035364   Benjamin Renard   First commit
313
		else  
fdf1413b   Elena.Budnik   TT download
314
			$this -> renameInResource($p->name, $p->id); 
16035364   Benjamin Renard   First commit
315
316

		 $this->saveContent();
fdf1413b   Elena.Budnik   TT download
317
		 
16035364   Benjamin Renard   First commit
318
319
320
321
322
		 return array('id' => $p->id);		
	}
   
/*
*          Delete Parameter[TT...]/Folder
fdf1413b   Elena.Budnik   TT download
323
*    	     Delete object itself, delete from contentDOM, mark as undefined in depending objects		
16035364   Benjamin Renard   First commit
324
*/
b1d50569   Elena.Budnik   formattage
325
326
	function deleteObject($p)
	{
fdf1413b   Elena.Budnik   TT download
327
328
329
330
331
332
333
334
335
		if ($p->leaf) {       
			//   if Parameter[TT...] - delete resources first
			$isDeleted = $this->deleteParameter($p->id);
			if (!($objToDelete = $this -> objectExistsById($p->id))) 
				return array('error' => NO_SUCH_ID);		                         
		}
		else {
			if (!($objToDelete = $this -> objectExistsById($p->id))) 
				return array('error' => NO_SUCH_ID); 
16035364   Benjamin Renard   First commit
336
			$folderToDelete = $objToDelete->getElementsByTagName($this->objTagName);
fdf1413b   Elena.Budnik   TT download
337
			// delete all parameters[TT..] in folder(s)
16035364   Benjamin Renard   First commit
338
			foreach ($folderToDelete as $obj) {
fdf1413b   Elena.Budnik   TT download
339
340
341
342
343
344
345
346
347
348
349
350
				$id = $obj->getAttribute('xml:id');
				$this->deleteParameter($id);
			}
		}
		
		$this -> deleteFromContent($objToDelete);

		if ($isDeleted) 
			return array('id' => $p->id, 'maskDeleted' => true); 
		else 
			return array('id' => $p->id);
	}
16035364   Benjamin Renard   First commit
351
352
353

/*
*         Modify Parameter[TT...]/Folder
fdf1413b   Elena.Budnik   TT download
354
*/      
b1d50569   Elena.Budnik   formattage
355
356
357
358
	function modifyObject($p)
	{ 	
		if ($this->renameOnly($p)) 
		{ 
16035364   Benjamin Renard   First commit
359
			$p->leaf = 1;
fdf1413b   Elena.Budnik   TT download
360
361
362
363
364
365
366
367
			return $this->renameObject($p);
		} 
		
		$folder = $this->getObjectFolder($p->id);
		$this->deleteObject($p);	
		
		return $this->createObject($p, $folder);
	} 
16035364   Benjamin Renard   First commit
368
369

      
b1d50569   Elena.Budnik   formattage
370
371
	function validNameObject($p)
	{	
fdf1413b   Elena.Budnik   TT download
372
373
374
375
376
377
		if (!isset($p->name) || ($p->name == ""))
			return array('valid' => false, 'error' => 'Name is required');
		if ($p->leaf)
			$alreadyExist = $this -> objectExistsByName($p->name, $this->objTagName);
		else
			$alreadyExist = $this -> folderExistsByName($p->name);
b1d50569   Elena.Budnik   formattage
378
			
fdf1413b   Elena.Budnik   TT download
379
380
381
382
383
		if ($alreadyExist)
			return array('valid' => false, 'error' => 'This name already exists in this subtree!');
			
		return array('valid' => true);
	}
16035364   Benjamin Renard   First commit
384
         
fdf1413b   Elena.Budnik   TT download
385
	function getObject($id){}
16035364   Benjamin Renard   First commit
386
387
}
?>