Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
4.27% |
5 / 117 |
|
33.33% |
4 / 12 |
CRAP | |
0.00% |
0 / 1 |
SeedDMS_Core_Object | |
4.27% |
5 / 117 |
|
33.33% |
4 / 12 |
2907.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setDMS | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDMS | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getID | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAttributes | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
90 | |||
getAttribute | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getAttributeValue | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
getAttributeValueAsArray | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getAttributeValueAsString | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
setAttributeValue | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
342 | |||
removeAttribute | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
132 |
1 | <?php |
2 | /** |
3 | * Implementation of an generic object in the document management system |
4 | * |
5 | * @category DMS |
6 | * @package SeedDMS_Core |
7 | * @license GPL2 |
8 | * @author Uwe Steinmann <uwe@steinmann.cx> |
9 | * @copyright Copyright (C) 2010-2012 Uwe Steinmann |
10 | * @version Release: @package_version@ |
11 | */ |
12 | |
13 | |
14 | /** |
15 | * Class to represent a generic object in the document management system |
16 | * |
17 | * This is the base class for generic objects in SeedDMS. |
18 | * |
19 | * @category DMS |
20 | * @package SeedDMS_Core |
21 | * @author Uwe Steinmann <uwe@steinmann.cx> |
22 | * @copyright Copyright (C) 2010-2012 Uwe Steinmann |
23 | * @version Release: @package_version@ |
24 | */ |
25 | class SeedDMS_Core_Object { /* {{{ */ |
26 | /** |
27 | * @var integer unique id of object |
28 | */ |
29 | protected $_id; |
30 | |
31 | /** |
32 | * @var array list of attributes |
33 | */ |
34 | protected $_attributes; |
35 | |
36 | /** |
37 | * @var SeedDMS_Core_DMS back reference to document management system |
38 | */ |
39 | public $_dms; |
40 | |
41 | /** |
42 | * SeedDMS_Core_Object constructor. |
43 | * @param $id |
44 | */ |
45 | function __construct($id) { /* {{{ */ |
46 | $this->_id = $id; |
47 | $this->_dms = null; |
48 | } /* }}} */ |
49 | |
50 | /** |
51 | * Check if this object is of a given type. |
52 | * |
53 | * This method must be implemened in the child class |
54 | * |
55 | * @param string $type type of object |
56 | */ |
57 | public function isType($type) {return false;} |
58 | |
59 | /** |
60 | * Set dms this object belongs to. |
61 | * |
62 | * Each object needs a reference to the dms it belongs to. It will be |
63 | * set when the object is created. |
64 | * The dms has a references to the currently logged in user |
65 | * and the database connection. |
66 | * |
67 | * @param SeedDMS_Core_DMS $dms reference to dms |
68 | */ |
69 | public function setDMS($dms) { /* {{{ */ |
70 | $this->_dms = $dms; |
71 | } /* }}} */ |
72 | |
73 | public function getDMS() { /* {{{ */ |
74 | return $this->_dms; |
75 | } /* }}} */ |
76 | |
77 | /** |
78 | * Return the internal id of the document |
79 | * |
80 | * @return integer id of document |
81 | */ |
82 | public function getID() { return $this->_id; } |
83 | |
84 | /** |
85 | * Returns all attributes set for the object |
86 | * |
87 | * @return array|bool |
88 | */ |
89 | public function getAttributes() { /* {{{ */ |
90 | if (!$this->_attributes) { |
91 | $db = $this->_dms->getDB(); |
92 | |
93 | switch(get_class($this)) { |
94 | case $this->_dms->getClassname('document'): |
95 | $queryStr = "SELECT a.* FROM `tblDocumentAttributes` a LEFT JOIN `tblAttributeDefinitions` b ON a.`attrdef`=b.`id` WHERE a.`document` = " . $this->_id." ORDER BY b.`name`"; |
96 | break; |
97 | case $this->_dms->getClassname('documentcontent'): |
98 | $queryStr = "SELECT a.* FROM `tblDocumentContentAttributes` a LEFT JOIN `tblAttributeDefinitions` b ON a.`attrdef`=b.`id` WHERE a.`content` = " . $this->_id." ORDER BY b.`name`"; |
99 | break; |
100 | case $this->_dms->getClassname('folder'): |
101 | $queryStr = "SELECT a.* FROM `tblFolderAttributes` a LEFT JOIN `tblAttributeDefinitions` b ON a.`attrdef`=b.`id` WHERE a.`folder` = " . $this->_id." ORDER BY b.`name`"; |
102 | break; |
103 | default: |
104 | return false; |
105 | } |
106 | $resArr = $db->getResultArray($queryStr); |
107 | if (is_bool($resArr) && !$resArr) return false; |
108 | |
109 | $this->_attributes = array(); |
110 | |
111 | foreach ($resArr as $row) { |
112 | $attrdef = $this->_dms->getAttributeDefinition($row['attrdef']); |
113 | $attr = new SeedDMS_Core_Attribute($row["id"], $this, $attrdef, $row["value"]); |
114 | $attr->setDMS($this->_dms); |
115 | $this->_attributes[$attrdef->getId()] = $attr; |
116 | } |
117 | } |
118 | return $this->_attributes; |
119 | |
120 | } /* }}} */ |
121 | |
122 | /** |
123 | * Returns an attribute of the object for the given attribute definition |
124 | * |
125 | * @param SeedDMS_Core_AttributeDefinition $attrdef |
126 | * @return array|string value of attritbute or false. The value is an array |
127 | * if the attribute is defined as multi value |
128 | */ |
129 | public function getAttribute($attrdef) { /* {{{ */ |
130 | if (!$this->_attributes) { |
131 | $this->getAttributes(); |
132 | } |
133 | |
134 | if (isset($this->_attributes[$attrdef->getId()])) { |
135 | return $this->_attributes[$attrdef->getId()]; |
136 | } else { |
137 | return false; |
138 | } |
139 | |
140 | } /* }}} */ |
141 | |
142 | /** |
143 | * Returns an attribute value of the object for the given attribute definition |
144 | * |
145 | * @param SeedDMS_Core_AttributeDefinition $attrdef |
146 | * @return array|string value of attritbute or false. The value is an array |
147 | * if the attribute is defined as multi value |
148 | */ |
149 | public function getAttributeValue($attrdef) { /* {{{ */ |
150 | if (!$this->_attributes) { |
151 | $this->getAttributes(); |
152 | } |
153 | |
154 | if (isset($this->_attributes[$attrdef->getId()])) { |
155 | $value = $this->_attributes[$attrdef->getId()]->getValue(); |
156 | if($attrdef->getMultipleValues()) { |
157 | $sep = substr($value, 0, 1); |
158 | $vsep = $attrdef->getValueSetSeparator(); |
159 | /* If the value doesn't start with the separator used in the value set, |
160 | * then assume that the value was not saved with a leading separator. |
161 | * This can happen, if the value was previously a single value from |
162 | * the value set and later turned into a multi value attribute. |
163 | */ |
164 | if($sep == $vsep) |
165 | return(explode($sep, substr($value, 1))); |
166 | else |
167 | return(array($value)); |
168 | } else { |
169 | return $this->_attributes[$attrdef->getId()]->getParsedValue(); |
170 | } |
171 | } else |
172 | return false; |
173 | |
174 | } /* }}} */ |
175 | |
176 | /** |
177 | * Returns an attribute value of the object for the given attribute definition |
178 | * |
179 | * This is a short cut for getAttribute($attrdef)->getValueAsArray() but |
180 | * first checks if the object has an attribute for the given attribute |
181 | * definition. |
182 | * |
183 | * @param SeedDMS_Core_AttributeDefinition $attrdef |
184 | * @return array|bool |
185 | * even if the attribute is not defined as multi value |
186 | */ |
187 | public function getAttributeValueAsArray($attrdef) { /* {{{ */ |
188 | if (!$this->_attributes) { |
189 | $this->getAttributes(); |
190 | } |
191 | |
192 | if (isset($this->_attributes[$attrdef->getId()])) { |
193 | return $this->_attributes[$attrdef->getId()]->getValueAsArray(); |
194 | } else |
195 | return false; |
196 | |
197 | } /* }}} */ |
198 | |
199 | /** |
200 | * Returns an attribute value of the object for the given attribute definition |
201 | * |
202 | * This is a short cut for getAttribute($attrdef)->getValueAsString() but |
203 | * first checks if the object has an attribute for the given attribute |
204 | * definition. |
205 | * |
206 | * @param SeedDMS_Core_AttributeDefinition $attrdef |
207 | * @return string value of attritbute or false. The value is always a string |
208 | * even if the attribute is defined as multi value |
209 | */ |
210 | public function getAttributeValueAsString($attrdef) { /* {{{ */ |
211 | if (!$this->_attributes) { |
212 | $this->getAttributes(); |
213 | } |
214 | |
215 | if (isset($this->_attributes[$attrdef->getId()])) { |
216 | return $this->_attributes[$attrdef->getId()]->getValue(); |
217 | } else |
218 | return false; |
219 | |
220 | } /* }}} */ |
221 | |
222 | /** |
223 | * Set an attribute of the object for the given attribute definition |
224 | * |
225 | * @param SeedDMS_Core_AttributeDefinition $attrdef definition of attribute |
226 | * @param array|string $value value of attribute, for multiple values this |
227 | * must be an array |
228 | * @return boolean true if operation was successful, otherwise false |
229 | */ |
230 | public function setAttributeValue($attrdef, $value) { /* {{{ */ |
231 | $db = $this->_dms->getDB(); |
232 | if (!$this->_attributes) { |
233 | $this->getAttributes(); |
234 | } |
235 | switch($attrdef->getType()) { |
236 | case SeedDMS_Core_AttributeDefinition::type_boolean: |
237 | $value = ($value === true || $value != '' || $value == 1) ? 1 : 0; |
238 | break; |
239 | } |
240 | if($attrdef->getMultipleValues() && is_array($value)) { |
241 | if(in_array($attrdef->getType(), array(SeedDMS_Core_AttributeDefinition::type_user, SeedDMS_Core_AttributeDefinition::type_group))) |
242 | $sep = ','; |
243 | else |
244 | $sep = substr($attrdef->getValueSet(), 0, 1); |
245 | $value = $sep.implode($sep, $value); |
246 | } |
247 | /* Handle the case if an attribute is not set already */ |
248 | if(!isset($this->_attributes[$attrdef->getId()])) { |
249 | switch(get_class($this)) { |
250 | case $this->_dms->getClassname('document'): |
251 | $tablename = 'tblDocumentAttributes'; |
252 | $queryStr = "INSERT INTO `tblDocumentAttributes` (`document`, `attrdef`, `value`) VALUES (".$this->_id.", ".$attrdef->getId().", ".$db->qstr($value).")"; |
253 | break; |
254 | case $this->_dms->getClassname('documentcontent'): |
255 | $tablename = 'tblDocumentContentAttributes'; |
256 | $queryStr = "INSERT INTO `tblDocumentContentAttributes` (`content`, `attrdef`, `value`) VALUES (".$this->_id.", ".$attrdef->getId().", ".$db->qstr($value).")"; |
257 | break; |
258 | case $this->_dms->getClassname('folder'): |
259 | $tablename = 'tblFolderAttributes'; |
260 | $queryStr = "INSERT INTO `tblFolderAttributes` (`folder`, `attrdef`, `value`) VALUES (".$this->_id.", ".$attrdef->getId().", ".$db->qstr($value).")"; |
261 | break; |
262 | default: |
263 | return false; |
264 | } |
265 | $res = $db->getResult($queryStr); |
266 | if (!$res) |
267 | return false; |
268 | |
269 | $attr = new SeedDMS_Core_Attribute($db->getInsertID($tablename), $this, $attrdef, $value); |
270 | $attr->setDMS($this->_dms); |
271 | $this->_attributes[$attrdef->getId()] = $attr; |
272 | |
273 | /* Check if 'onPostAddAttribute' callback is set */ |
274 | if(isset($this->_dms->callbacks['onPostAddAttribute'])) { |
275 | foreach($this->_dms->callbacks['onPostAddAttribute'] as $callback) { |
276 | if(!call_user_func($callback[0], $callback[1], $this, $attrdef, $value)) { |
277 | } |
278 | } |
279 | } |
280 | |
281 | return true; |
282 | } |
283 | |
284 | /* The attribute already exists. setValue() will either update or delete it. */ |
285 | $this->_attributes[$attrdef->getId()]->setValue($value); |
286 | |
287 | return true; |
288 | } /* }}} */ |
289 | |
290 | /** |
291 | * Remove an attribute of the object for the given attribute definition |
292 | * |
293 | * FIXME: shouldn't this rather be setAttributeValue() with an empty value? |
294 | * |
295 | * @param SeedDMS_Core_AttributeDefinition $attrdef |
296 | * @return boolean true if operation was successful, otherwise false |
297 | */ |
298 | public function removeAttribute($attrdef) { /* {{{ */ |
299 | $db = $this->_dms->getDB(); |
300 | if (!$this->_attributes) { |
301 | $this->getAttributes(); |
302 | } |
303 | if(isset($this->_attributes[$attrdef->getId()])) { |
304 | $oldvalue = $this->_attributes[$attrdef->getId()]->getValue(); |
305 | switch(get_class($this)) { |
306 | case $this->_dms->getClassname('document'): |
307 | $queryStr = "DELETE FROM `tblDocumentAttributes` WHERE `document`=".$this->_id." AND `attrdef`=".$attrdef->getId(); |
308 | break; |
309 | case $this->_dms->getClassname('documentcontent'): |
310 | $queryStr = "DELETE FROM `tblDocumentContentAttributes` WHERE `content`=".$this->_id." AND `attrdef`=".$attrdef->getId(); |
311 | break; |
312 | case $this->_dms->getClassname('folder'): |
313 | $queryStr = "DELETE FROM `tblFolderAttributes` WHERE `folder`=".$this->_id." AND `attrdef`=".$attrdef->getId(); |
314 | break; |
315 | default: |
316 | return false; |
317 | } |
318 | $res = $db->getResult($queryStr); |
319 | if (!$res) |
320 | return false; |
321 | |
322 | /* Check if 'onPostRemoveAttribute' callback is set */ |
323 | if(isset($this->_dms->callbacks['onPostRemoveAttribute'])) { |
324 | foreach($this->_dms->callbacks['onPostRemoveAttribute'] as $callback) { |
325 | if(!call_user_func($callback[0], $callback[1], $this, $attrdef, $oldvalue)) { |
326 | } |
327 | } |
328 | } |
329 | |
330 | unset($this->_attributes[$attrdef->getId()]); |
331 | } |
332 | return true; |
333 | } /* }}} */ |
334 | } /* }}} */ |