public function insert($value) { $_value = $value; if (($len = strlen($this->_prefix)) > 0) { if (strpos($_value, $this->_prefix) !== 0) { throw new Tinebase_Exception_UnexpectedValue('prefix missing'); } $_value = substr($_value, $len); } $_value = ltrim($_value, '0'); $orgLen = strlen($_value); $trimLen = strlen($_value); if ($orgLen !== $trimLen) { if ($trimLen >= $this->_zerofill || $orgLen > $this->_zerofill) { throw new Tinebase_Exception_UnexpectedValue('improper format: to many leading zeros "' . $value . '"'); } if ($orgLen < $this->_zerofill) { throw new Tinebase_Exception_UnexpectedValue('improper format: not enough leading zeros "' . $value . '"'); } if (0 === $trimLen) { $_value = '0'; } } $intValue = intval($_value); if ($_value !== '' . $intValue) { throw new Tinebase_Exception_UnexpectedValue('improper format: wrong prefix or non digit found where none should be "' . $value . '"'); } return parent::insert($intValue); }
protected function _setAutoincrementValues(Tinebase_Record_Interface $_record, Tinebase_Record_Interface $_oldRecord = null) { $className = get_class($_record); $configuration = $_record->getConfiguration(); if (null === $configuration) { return; } if (!method_exists($configuration, 'getAutoincrementFields')) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' CLass has no getAutoincrementFields(): ' . get_class($configuration)); } return; } foreach ($configuration->getAutoincrementFields() as $fieldDef) { $createNewValue = false; $checkValue = false; $freeOldValue = false; $numberable = null; // if new record field is not set and if there is no old record, we assign a new value if (!isset($_record->{$fieldDef['fieldName']})) { if (null === $_oldRecord) { $createNewValue = true; } } else { // if new record field is set to empty string, we assign a new value if (empty($_record->{$fieldDef['fieldName']})) { $createNewValue = true; // if new record field is populated and it differs from the old record value, we need to check the value } elseif (null !== $_oldRecord) { if ($_record->{$fieldDef['fieldName']} != $_oldRecord->{$fieldDef['fieldName']}) { $checkValue = true; } // if new record field is populated and there is no old record, we need to check the value } else { $checkValue = true; } } if (true === $checkValue || true === $createNewValue) { $numberable = Tinebase_Numberable::getNumberable($className, $fieldDef['fieldName'], $fieldDef); } if (true === $checkValue) { if (false === $numberable->insert($_record->{$fieldDef['fieldName']})) { // if the check failed and we have an old value, we keep on using the old value if (null !== $_oldRecord && !empty($_oldRecord->{$fieldDef['fieldName']})) { $_record->{$fieldDef['fieldName']} = $_oldRecord->{$fieldDef['fieldName']}; // else we create a new one } else { $createNewValue = true; } } else { $freeOldValue = true; } } if (true === $createNewValue) { $_record->{$fieldDef['fieldName']} = $numberable->getNext(); if (null !== $_oldRecord && !empty($_oldRecord->{$fieldDef['fieldName']})) { $freeOldValue = true; } } if (true === $freeOldValue) { $numberable->free($_oldRecord->{$fieldDef['fieldName']}); } } }