Blame view

php/classes/AmdaObjectMgr.php 9.65 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
	      foreach($obj as $key => $value) 
	      {
f60f0bd9   Benjamin Renard   Fix upload from s...
127
				if ($key != 'id' && $key != 'leaf' && $key != 'nodeType' && !is_array($value) && !is_object($value)) 
57f8eda0   Benjamin Renard   Fix some errors d...
128
				{
b1d50569   Elena.Budnik   formattage
129
130
131
					$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