fix specifying a condition, can NOT use "value", as it gets used as the widgets value, using "last" now

This commit is contained in:
ralf 2024-04-04 08:56:35 +02:00
parent 5853afd7ff
commit 13c68e92df
3 changed files with 15 additions and 11 deletions

View File

@ -379,9 +379,9 @@ class admin_customfields
if (!empty($content['cf_values'])) if (!empty($content['cf_values']))
{ {
$values = array(); $values = array();
if ($content['cf_type'] === 'serial' && !str_starts_with($content['cf_values'], 'value=')) if ($content['cf_type'] === 'serial' && !str_starts_with($content['cf_values'], 'last='))
{ {
$content['cf_values'] = 'value=' . $content['cf_values']; $content['cf_values'] = 'last=' . $content['cf_values'];
} }
if($content['cf_values'][0] === '@') if($content['cf_values'][0] === '@')
{ {
@ -411,7 +411,7 @@ class admin_customfields
$values[$var] = trim($value)==='' ? $var : $value; $values[$var] = trim($value)==='' ? $var : $value;
} }
} }
if ($content['cf_type'] === 'serial' && !preg_match(Api\Storage\Customfields::SERIAL_PREG, $values['value'])) if ($content['cf_type'] === 'serial' && !preg_match(Api\Storage\Customfields::SERIAL_PREG, $values['last']))
{ {
Api\Etemplate::set_validation_error('cf_values', lang('Invalid Format, must end in a group of digits e.g. %1 or %2', "'0000'", "'RE2024-0000'")); Api\Etemplate::set_validation_error('cf_values', lang('Invalid Format, must end in a group of digits e.g. %1 or %2', "'0000'", "'RE2024-0000'"));
break; break;

View File

@ -186,7 +186,7 @@ class Customfields extends Transformer
unset($fields[$key]); unset($fields[$key]);
} }
// Rmove fields for none private cutomfields when name refers to a single custom field // move fields for none private customfields when name refers to a single custom field
$matches = null; $matches = null;
if (($pos=strpos($form_name,$this->attrs['prefix'])) !== false && if (($pos=strpos($form_name,$this->attrs['prefix'])) !== false &&
preg_match($preg = '/'.$this->attrs['prefix'].'([^\]]+)/',$form_name,$matches) && !isset($fields[$name=$matches[1]])) preg_match($preg = '/'.$this->attrs['prefix'].'([^\]]+)/',$form_name,$matches) && !isset($fields[$name=$matches[1]]))
@ -570,7 +570,7 @@ class Customfields extends Transformer
// check if we have condition(s) beside the value, and they are NOT meet --> do NOT generate the serial // check if we have condition(s) beside the value, and they are NOT meet --> do NOT generate the serial
if (is_array($field['values']) && array_filter($field['values'], static function($val, $name) use ($content) if (is_array($field['values']) && array_filter($field['values'], static function($val, $name) use ($content)
{ {
return $name === 'value' ? false : $val != $content[$name]; return $name === 'last' ? false : $val != $content[$name];
}, ARRAY_FILTER_USE_BOTH)) }, ARRAY_FILTER_USE_BOTH))
{ {
continue; continue;

View File

@ -675,24 +675,28 @@ class Customfields implements \IteratorAggregate
} }
else else
{ {
$values = ['value' => $row['cf_values'] ?? null]; $values = null;
}
if (!is_array($values))
{
$values = ['last' => $values];
} }
// we increment the last digit-group // we increment the last digit-group
if (empty($values['value']) || !preg_match(self::SERIAL_PREG, $values['value'], $matches)) if (empty($values['last']) || !preg_match(self::SERIAL_PREG, $values['last'], $matches))
{ {
$values['value'] = 1; $values['last'] = 1;
} }
else else
{ {
$values['value'] = preg_replace(self::SERIAL_PREG, $values['last'] = preg_replace(self::SERIAL_PREG,
sprintf('%0'.strlen($matches[0]).'d', 1+(int)$matches[0]), $values['value']); sprintf('%0'.strlen($matches[0]).'d', 1+(int)$matches[0]), $values['last']);
} }
$row['cf_values'] = json_encode($values); $row['cf_values'] = json_encode($values);
break; break;
} }
if (isset($row) && self::$db->update(self::TABLE, $row, $where, __LINE__, __FILE__) && self::$db->transaction_commit()) if (isset($row) && self::$db->update(self::TABLE, $row, $where, __LINE__, __FILE__) && self::$db->transaction_commit())
{ {
return $values['value']; return $values['last'];
} }
self::$db->transaction_abort(); self::$db->transaction_abort();
throw new Api\Db\Exception("Could not generate serial number for custom-field #$id!"); throw new Api\Db\Exception("Could not generate serial number for custom-field #$id!");