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