better checking when setting attr. value

This commit is contained in:
Uwe Steinmann 2016-02-03 14:45:00 +01:00
parent 0c8768a09e
commit d835aa84fe

View File

@ -109,7 +109,7 @@ class SeedDMS_Core_Attribute { /* {{{ */
/** /**
* Return attribute values as an array * Return attribute values as an array
* *
* This function returns the attribute value as an array. Such an array * This function returns the attribute value as an array. The array
* has one element for non multi value attributes and n elements for * has one element for non multi value attributes and n elements for
* multi value attributes. * multi value attributes.
* *
@ -125,14 +125,67 @@ class SeedDMS_Core_Attribute { /* {{{ */
/** /**
* Set a value of an attribute * Set a value of an attribute
* The attribute is deleted completely if the value is the empty string
* *
* @param string $value value to be set * The attribute is completely deleted if the value is an empty string
* or empty array. An array of values is only allowed if the attribute may
* have multiple values. If an array is passed and the attribute may
* have only a single value, then the first element of the array will
* be taken.
*
* @param string $values value as string or array to be set
* @return boolean true if operation was successfull, otherwise false * @return boolean true if operation was successfull, otherwise false
*/ */
function setValue($value) { /* {{{*/ function setValue($values) { /* {{{*/
$db = $this->_dms->getDB(); $db = $this->_dms->getDB();
if($this->_attrdef->getMultipleValues()) {
/* Multiple values without a value set is not allowed */
if(!$valuesetstr = $this->_attrdef->getValueSet())
return false;
$valueset = $this->_attrdef->getValueSetAsArray();
if(is_array($values)) {
if($values) {
$error = false;
foreach($values as $v) {
if(!in_array($v, $valueset)) { $error = true; break; }
}
if($error)
return false;
$valuesetstr = $this->_attrdef->getValueSet();
$value = $valuesetstr[0].implode($valuesetstr[0], $values);
} else {
$value = '';
}
} else {
if($values) {
if($valuesetstr[0] != $values[0])
$values = explode($valuesetstr[0], $values);
else
$values = explode($valuesetstr[0], substr($values, 1));
$error = false;
foreach($values as $v) {
if(!in_array($v, $valueset)) { $error = true; break; }
}
if($error)
return false;
$value = $valuesetstr[0].implode($valuesetstr[0], $values);
} else {
$value = $values;
}
}
} else {
if(is_array($values)) {
if($values)
$value = $values[0];
else
$value = '';
} else {
$value = $values;
}
}
switch(get_class($this->_obj)) { switch(get_class($this->_obj)) {
case "SeedDMS_Core_Document": case "SeedDMS_Core_Document":
if(trim($value) === '') if(trim($value) === '')
@ -480,6 +533,12 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
/** /**
* Get the value set as saved in the database * Get the value set as saved in the database
* *
* This is a string containing the list of valueѕ separated by a
* delimiter which also precedes the whole string, e.g. '|Yes|No'
*
* Use {@link SeedDMS_Core_AttributeDefinition::getValueSetAsArray()}
* for a list of values returned as an array.
*
* @return string value set * @return string value set
*/ */
function getValueSet() { /* {{{ */ function getValueSet() { /* {{{ */
@ -496,7 +555,7 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
if(strlen($this->_valueset) > 1) if(strlen($this->_valueset) > 1)
return explode($this->_valueset[0], substr($this->_valueset, 1)); return explode($this->_valueset[0], substr($this->_valueset, 1));
else else
return false; return array();
} /* }}} */ } /* }}} */
/** /**