From d835aa84fe2b3c279fe177058d79e83457cea7af Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Wed, 3 Feb 2016 14:45:00 +0100 Subject: [PATCH] better checking when setting attr. value --- SeedDMS_Core/Core/inc.ClassAttribute.php | 69 ++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/SeedDMS_Core/Core/inc.ClassAttribute.php b/SeedDMS_Core/Core/inc.ClassAttribute.php index dd25e19e3..d091f7e81 100644 --- a/SeedDMS_Core/Core/inc.ClassAttribute.php +++ b/SeedDMS_Core/Core/inc.ClassAttribute.php @@ -109,7 +109,7 @@ class SeedDMS_Core_Attribute { /* {{{ */ /** * 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 * multi value attributes. * @@ -125,14 +125,67 @@ class SeedDMS_Core_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 */ - function setValue($value) { /* {{{*/ + function setValue($values) { /* {{{*/ $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)) { case "SeedDMS_Core_Document": if(trim($value) === '') @@ -480,6 +533,12 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */ /** * 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 */ function getValueSet() { /* {{{ */ @@ -496,7 +555,7 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */ if(strlen($this->_valueset) > 1) return explode($this->_valueset[0], substr($this->_valueset, 1)); else - return false; + return array(); } /* }}} */ /**