From cd03264ab44b8ffccc726d43ccb07cb12e4ca3db Mon Sep 17 00:00:00 2001 From: Alexei Karpenko Date: Fri, 16 Jan 2026 13:09:11 +0100 Subject: [PATCH] Fehler: added fehlerupdate for adding missing fehler from config files to database --- .../controllers/system/fehler/CLI_Manager.php | 57 ++++ .../controllers/system/fehler/Manager.php | 46 +++ application/libraries/FehlerUpdateLib.php | 304 ++++++++++++++++++ application/libraries/IssuesLib.php | 2 +- .../issues/PlausicheckProducerLib.php | 19 +- application/models/system/Fehler_model.php | 3 +- 6 files changed, 421 insertions(+), 10 deletions(-) create mode 100644 application/controllers/system/fehler/CLI_Manager.php create mode 100644 application/controllers/system/fehler/Manager.php create mode 100644 application/libraries/FehlerUpdateLib.php diff --git a/application/controllers/system/fehler/CLI_Manager.php b/application/controllers/system/fehler/CLI_Manager.php new file mode 100644 index 000000000..0a510d25d --- /dev/null +++ b/application/controllers/system/fehler/CLI_Manager.php @@ -0,0 +1,57 @@ +. + */ + +if (!defined("BASEPATH")) exit("No direct script access allowed"); + +class CLI_Manager extends CLI_Controller +{ + /** + * + */ + public function __construct() + { + parent::__construct(); + + // Load libraries + $this->load->library('FehlerUpdateLib'); + } + + /** + * + */ + public function installAll() + { + $this->fehlerupdatelib->installAll(); + } + + /** + * + */ + public function installFromCore() + { + $this->fehlerupdatelib->installFromCore(); + } + + /** + * + */ + public function installFrom($fehlerConfigDirectory) + { + $this->fehlerupdatelib->installFrom(urldecode($fehlerConfigDirectory)); + } +} \ No newline at end of file diff --git a/application/controllers/system/fehler/Manager.php b/application/controllers/system/fehler/Manager.php new file mode 100644 index 000000000..832108dcf --- /dev/null +++ b/application/controllers/system/fehler/Manager.php @@ -0,0 +1,46 @@ + 'admin:rw', + 'installFromCore' => 'admin:rw' + ) + ); + + // Load libraries + $this->load->library('FehlerUpdateLib'); + } + + /** + * + */ + public function installAll() + { + $this->fehlerupdatelib->installAll(); + } + + /** + * + */ + public function installFromCore() + { + $this->fehlerupdatelib->installFromCore(); + } + + /** + * + */ + public function installFrom($fehlerConfigDirectory) + { + $this->fehlerupdatelib->installFrom($fehlerConfigDirectory); + } +} \ No newline at end of file diff --git a/application/libraries/FehlerUpdateLib.php b/application/libraries/FehlerUpdateLib.php new file mode 100644 index 000000000..a969bffba --- /dev/null +++ b/application/libraries/FehlerUpdateLib.php @@ -0,0 +1,304 @@ +. + */ + +if (! defined('BASEPATH')) exit('No direct script access allowed'); + +class FehlerUpdateLib +{ + // Who adds phrases into the database + //const INSERT_BY = 'FehlerUpdate'; + + const CONFIG_DIRECTORY = 'config'; + const CONFIG_FEHLER_NAME = 'fehler'; + const CONFIG_FEHLER_INDEX = 'fehler'; + + const TYPE_STRING = 'string'; + const TYPE_ARRAY = 'array'; + + //~ // Array elements names + const FEHLERCODE = 'fehlercode'; + const FEHLER_KURZBZ = 'fehler_kurzbz'; + const FEHLERTEXT = 'fehlertext'; + const FEHLERTYP_KURZBZ = 'fehlertyp_kurzbz'; + const APP = 'app'; + + // structure of a fehler + // type default: string + const FEHLER_ATTRIBUTES = [ + self::FEHLERCODE => ['required' => true], + self::FEHLER_KURZBZ => ['required' => true], + self::FEHLERTEXT => ['required' => true], + self::FEHLERTYP_KURZBZ => ['required' => false], + self::APP => ['required' => true, 'types' => [self::TYPE_STRING, self::TYPE_ARRAY]] + ]; + + private $_ci; // Code igniter instance + + /** + * Loads parser library + */ + public function __construct() + { + $this->_ci =& get_instance(); + + // Loads EPrintfLib + $this->_ci->load->library('EPrintfLib'); + + // Loads the PhraseModel + $this->_ci->load->model('system/Fehler_model', 'FehlerModel'); + } + + // ----------------------------------------------------------------------------------------------------------------- + // Public methods + + + /** + * + * @param + * @return object success or error + */ + public function installAll() + { + $this->installFromCore(); + + $this->_ci->load->library('ExtensionsLib'); + + // load fehler entries of extensions + $extensions = $this->_ci->extensionslib->getInstalledExtensions(); + + if (hasData($extensions)) + { + $extensionArray = array(); + + $extensionsData = getData($extensions); + + foreach ($extensionsData as $ext) + { + $configFilePath = ExtensionsLib::EXTENSIONS_DIR_NAME.'/'.$ext->name.'/'.FehlerUpdateLib::CONFIG_FEHLER_NAME; + $this->installFrom($configFilePath); + } + } + + } + + /** + * Install fehler from the core + */ + public function installFromCore() + { + $this->_installFehler(); + } + + /** + * Install fehler from the given path + */ + public function installFrom($fehlerConfigDirectory) + { + $this->_installFehler($fehlerConfigDirectory); + } + + + // ----------------------------------------------------------------------------------------------------------------- + // Private methods + + /** + * Install fehler from the given directory + */ + private function _installFehler($fehlerConfigDirectory = null) + { + // check that fehler config file exists + $fehlerConfigDirectory = isset($fehlerConfigDirectory) ? $fehlerConfigDirectory : self::CONFIG_FEHLER_NAME; + + $configFilename = APPPATH.self::CONFIG_DIRECTORY.'/'.$fehlerConfigDirectory.'.php'; + + if (!file_exists($configFilename)) + { + return; + } + + // Load Fehler Entries + $this->_ci->load->config($fehlerConfigDirectory); + $configArray = $this->_ci->config->item(self::CONFIG_FEHLER_INDEX); + + if (!isset($configArray) || !is_array($configArray)) // check if fehler config entries could be loaded + { + $this->_ci->eprintflib->printError( + 'Fehler config array could not be loaded, directory '.$fehlerConfigDirectory.' index '.self::CONFIG_FEHLER_INDEX + ); + return; + } + + $this->_ci->eprintflib->printInfo('------------------------------------------------------------------------------------------'); + $this->_ci->eprintflib->printInfo('Fehler installation started, directory '.$fehlerConfigDirectory); + + foreach ($configArray as $idx => $configEntry) + { + // create fehler from config entry + $createFehlerResult = $this->_createFehlerFromEntry($configEntry); + + // write error if creation failed + if (isError($createFehlerResult)) + { + $this->_ci->eprintflib->printError( + getError($createFehlerResult).', directory'.$fehlerConfigDirectory.', index '.$idx + ); + } + elseif (hasData($createFehlerResult)) + { + // add fehler to db + $addFehlerResult = $this->_addFehler(getData($createFehlerResult)); + + if (isError($addFehlerResult)) + { + $this->_ci->eprintflib->printError( + getError($addFehlerResult).', directory'.$fehlerConfigDirectory.', index '.$idx + ); + } + } + } + + $this->_ci->eprintflib->printInfo('Fehler installation ended'); + $this->_ci->eprintflib->printInfo('------------------------------------------------------------------------------------------'); + } + + /** + * Add a new fehler to the database + */ + private function _addFehler($fehler) + { + // Checks if the fehler already exists in the database + $this->_ci->FehlerModel->addSelect(self::FEHLERCODE.', '.self::FEHLER_KURZBZ); + $this->_ci->FehlerModel->db->where(self::FEHLERCODE.' = ', $fehler[self::FEHLERCODE]); + $this->_ci->FehlerModel->db->or_where(self::FEHLER_KURZBZ.' = ', $fehler[self::FEHLER_KURZBZ]); + $fehlerResult = $this->_ci->FehlerModel->load(); + + // If an error occurred then return the error itself + if (isError($fehlerResult)) return $fehlerResult; + + // if fehler has been found + if (hasData($fehlerResult)) + { + $foundFehler = getData($fehlerResult)[0]; + + // check if fehlercode - fehler kurzbz combination is correct + if ($foundFehler->{self::FEHLERCODE} != $fehler[self::FEHLERCODE] || $foundFehler->{self::FEHLER_KURZBZ} != $fehler[self::FEHLER_KURZBZ]) + { + return error("Wrong fehlercode - fehler kurzbz combination: ".$fehler[self::FEHLERCODE].", ".$fehler[self::FEHLER_KURZBZ]); + } + + $this->_ci->eprintflib->printMessage("Fehler ".$fehler[self::FEHLERCODE]." already exists in database"); + return success($fehler[self::FEHLERCODE]); + } + + // no fehler has been found + + // hanlde apps + if (isset($fehler[self::APP])) + { + $apps = $fehler[self::APP]; + if (is_string($apps)) $apps = [$apps]; + //unset($fehler[self::APP]); + $fehler[self::APP] = $apps[0]; + + //~ foreach ($apps as $app) + //~ { + //~ // TODO add entry for each app + //~ foreach ($_ as $_) + //~ { + + //~ } + + //~ } + } + + // Then add the fehler to the database + $fehlerInsertResult = $this->_ci->FehlerModel->insert( + $fehler + // TODO: add insertamum? + //~ array( + //~ 'insertamum' => 'NOW()', + //~ 'insertvon' => self::INSERT_BY + //~ ) + ); + + // If an error occurred then return the error itself + if (isError($fehlerInsertResult)) return $fehlerInsertResult; + + // Prints info about the new added fehler + $this->_ci->eprintflib->printMessage( + sprintf( + 'A new fehler has been added into the database: '. + 'fehlercode => %s | fehler_kurzbz => %s | fehlertyp => %s', + $fehler[self::FEHLERCODE], + $fehler[self::FEHLER_KURZBZ], + $fehler[self::FEHLERTYP_KURZBZ] + ) + ); + + // If here then no blocking errors occurred + return success(); + } + + /** + * Create an array with fehler data from config entry + */ + private function _createFehlerFromEntry($configEntry) + { + $fehler = []; + foreach (self::FEHLER_ATTRIBUTES as $attributeName => $attributeConditions) + { + $required = isset($attributeConditions['required']) && $attributeConditions['required']; + if ($required && !isset($configEntry[$attributeName])) + { + return error('attribute'.$attributeName.' is missing'); + } + + $attributeValue = $configEntry[$attributeName]; + $validType = false; + if (isset($attributeConditions['types']) && is_array($attributeConditions['types'])) + { + foreach ($attributeConditions['types'] as $type) + { + switch ($type) + { + case self::TYPE_STRING: + if (is_string($attributeValue) || is_null($attributeValue)) $validType = true; + break; + case self::TYPE_ARRAY: + if (is_array($attributeValue) && !($required && isEmptyArray($attributeValue))) $validType = true; + break; + //~ default: + //~ if (is_string($configEntry[$attributeName]) || is_null($configEntry[$attributeName])) $validType = true; + } + } + } + else + { + $validType = is_string($attributeValue) || is_null($attributeValue); + } + + if (!$validType) + { + return error('attribute'.$attributeName.' has invalid type'); + } + + $fehler[$attributeName] = $configEntry[$attributeName]; + } + return success($fehler); + } +} diff --git a/application/libraries/IssuesLib.php b/application/libraries/IssuesLib.php index f38303b3c..a5369d7e2 100644 --- a/application/libraries/IssuesLib.php +++ b/application/libraries/IssuesLib.php @@ -231,7 +231,7 @@ class IssuesLib return error("Person_id or oe_kurzbz must be set."); // get fehlertextVorlage and replace it with params - $fehlerRes = $this->_ci->FehlerModel->load($fehlercode); + $fehlerRes = $this->_ci->FehlerModel->loadWhere(['fehlercode' => $fehlercode]); if (hasData($fehlerRes)) { diff --git a/application/libraries/issues/PlausicheckProducerLib.php b/application/libraries/issues/PlausicheckProducerLib.php index 85ecd3577..0cd76962a 100644 --- a/application/libraries/issues/PlausicheckProducerLib.php +++ b/application/libraries/issues/PlausicheckProducerLib.php @@ -76,16 +76,19 @@ class PlausicheckProducerLib // Load Fehler Entries of Core $configArray = $this->_ci->config->item(self::CONFIG_FEHLER_INDEX); - foreach ($configArray as $coreEntry) + if (isset($configArray) && is_array($configArray)) { - if (!isset($coreEntry[self::FEHLER_KURZBZ_NAME]) - || !isset($coreEntry[self::PRODUCER_LIB_NAME]) - || !in_array($coreEntry[self::FEHLER_KURZBZ_NAME], $this->_fehlerKurzbz) - ) { - continue; - } + foreach ($configArray as $coreEntry) + { + if (!isset($coreEntry[self::FEHLER_KURZBZ_NAME]) + || !isset($coreEntry[self::PRODUCER_LIB_NAME]) + || !in_array($coreEntry[self::FEHLER_KURZBZ_NAME], $this->_fehlerKurzbz) + ) { + continue; + } - $this->_fehlerLibMappings[$coreEntry[self::FEHLER_KURZBZ_NAME]][self::PRODUCER_LIB_NAME] = $coreEntry[self::PRODUCER_LIB_NAME]; + $this->_fehlerLibMappings[$coreEntry[self::FEHLER_KURZBZ_NAME]][self::PRODUCER_LIB_NAME] = $coreEntry[self::PRODUCER_LIB_NAME]; + } } // load fehler entries of extensions diff --git a/application/models/system/Fehler_model.php b/application/models/system/Fehler_model.php index 28618f6d9..2b0dc8802 100644 --- a/application/models/system/Fehler_model.php +++ b/application/models/system/Fehler_model.php @@ -9,6 +9,7 @@ class Fehler_model extends DB_Model { parent::__construct(); $this->dbTable = 'system.tbl_fehler'; - $this->pk = 'fehlercode'; + $this->pk = array('fehlercode'); + $this->hasSequence = false; } }