From 28275e4afb318502f5e5ea1f5350fb864ce7e326 Mon Sep 17 00:00:00 2001 From: Jan Kristinus Date: Sat, 23 Sep 2023 12:26:43 +0200 Subject: [PATCH] CS --- install.php | 5 +- plugins/manager/lib/yform/manager.php | 114 ++++---- plugins/manager/lib/yform/manager/dataset.php | 5 +- plugins/manager/pages/data_import.php | 255 +++++++++--------- 4 files changed, 188 insertions(+), 191 deletions(-) diff --git a/install.php b/install.php index e6253de6..1e918392 100644 --- a/install.php +++ b/install.php @@ -8,8 +8,9 @@ try { // transaction test. $testSQL = rex_sql::factory(); - $testSQL->beginTransaction(); - $testSQL->commit(); + $testSQL->transactional(static function (rex_sql $testSQL) { + $testSQL->setQuery('SELECT 1'); + }); } catch (Exception $e) { throw new rex_sql_exception('db does not support transactions', $e); } diff --git a/plugins/manager/lib/yform/manager.php b/plugins/manager/lib/yform/manager.php index 8742c580..75305a07 100644 --- a/plugins/manager/lib/yform/manager.php +++ b/plugins/manager/lib/yform/manager.php @@ -259,9 +259,9 @@ public function getDataPage() case 'clone': case 'collection_edit': if ( - ('add' == $func && $this->hasDataPageFunction('add')) || - (('edit' == $func || 'clone' == $func) && $data_id) || - ('collection_edit' == $func && $this->table->isMassEditAllowed()) + ('add' == $func && $this->hasDataPageFunction('add')) + || (('edit' == $func || 'clone' == $func) && $data_id) + || ('collection_edit' == $func && $this->table->isMassEditAllowed()) ) { if ('collection_edit' === $func) { $query = $this->table->query()->alias('t0'); @@ -364,59 +364,59 @@ public function getDataPage() } } - $sql_db = rex_sql::factory(); - $sql_db->beginTransaction(); - - $transactionErrorMessage = null; - try { - $afterFieldsExecuted = static function (rex_yform $yform) { - /** @var rex_yform_value_abstract $valueObject */ - foreach ($yform->objparams['values'] as $valueObject) { - if ('submit' == $valueObject->getName()) { - if (2 == $valueObject->getValue()) { // apply - $yform->setObjectparams('form_showformafterupdate', 1); - // $yform->executeFields(); + $sql_db = rex_sql::factory(); + $form = ''; + $sql_db->transactional(static function () use (&$form, &$yform, $data, $func) { + + $afterFieldsExecuted = static function (rex_yform $yform) { + /** @var rex_yform_value_abstract $valueObject */ + foreach ($yform->objparams['values'] as $valueObject) { + if ('submit' == $valueObject->getName()) { + if (2 == $valueObject->getValue()) { // apply + $yform->setObjectparams('form_showformafterupdate', 1); + // $yform->executeFields(); + } } } - } - }; - - if ('clone' == $func) { - $afterFieldsExecuted = static function (rex_yform $yform) use ($afterFieldsExecuted) { - $yform->objparams['form_hiddenfields']['func'] = 'add'; - unset($yform->objparams['form_hiddenfields']['data_id']); - - // In den Feldern Anpassungen vornehmen - foreach ($yform->objparams['values'] as $k => $v) { - // Submit-Buttons von "Edit" auf "Add" zurückstellen - if ($v instanceof rex_yform_value_submit) { - $yform->objparams['form_output'][$k] = str_replace( - [rex_i18n::msg('yform_save') . 'objparams['form_output'][$k], - ); - continue; + }; + + if ('clone' == $func) { + $afterFieldsExecuted = static function (rex_yform $yform) use ($afterFieldsExecuted) { + $yform->objparams['form_hiddenfields']['func'] = 'add'; + unset($yform->objparams['form_hiddenfields']['data_id']); + + // In den Feldern Anpassungen vornehmen + foreach ($yform->objparams['values'] as $k => $v) { + // Submit-Buttons von "Edit" auf "Add" zurückstellen + if ($v instanceof rex_yform_value_submit) { + $yform->objparams['form_output'][$k] = str_replace( + [rex_i18n::msg('yform_save') . 'objparams['form_output'][$k], + ); + continue; + } + + // im Feldtyp be_manager_relation / Typ 5 (inline) ebenfalls die Datensatz-ID der + // verbundenen Sätze entfernen. Nur "inline" ist problematisch + if ($v instanceof rex_yform_value_be_manager_relation && 5 == $v->getElement('type')) { + $fieldName = preg_quote($v->getFieldName()); + $pattern = '//'; + $yform->objparams['form_output'][$k] = preg_replace($pattern, '', $yform->objparams['form_output'][$k]); + } } - // im Feldtyp be_manager_relation / Typ 5 (inline) ebenfalls die Datensatz-ID der - // verbundenen Sätze entfernen. Nur "inline" ist problematisch - if ($v instanceof rex_yform_value_be_manager_relation && 5 == $v->getElement('type')) { - $fieldName = preg_quote($v->getFieldName()); - $pattern = '//'; - $yform->objparams['form_output'][$k] = preg_replace($pattern, '', $yform->objparams['form_output'][$k]); + if (is_callable($afterFieldsExecuted)) { + $afterFieldsExecuted($yform); } - } + }; + } - if (is_callable($afterFieldsExecuted)) { - $afterFieldsExecuted($yform); - } - }; - } + $form = $data->executeForm($yform, $afterFieldsExecuted); - $form = $data->executeForm($yform, $afterFieldsExecuted); + }); - $sql_db->commit(); if ($yform->objparams['actions_executed']) { switch ($func) { case 'add': @@ -502,22 +502,14 @@ public function getDataPage() ]; } } - } catch (\Throwable $e) { - if ($sql_db->inTransaction()) { - $sql_db->rollBack(); - } - $transactionErrorMessage = $e->getMessage(); - if ($transactionErrorMessage) { - if (rex::getUser()->isAdmin()) { - // dump($e); - } - $mainMessages[] = [ - 'type' => 'error', - 'message' => rex_i18n::msg('yform_editdata_collection_error_abort', $transactionErrorMessage), - ]; - } + } catch (\Throwable $e) { + $mainMessages[] = [ + 'type' => 'error', + 'message' => rex_i18n::msg('yform_editdata_collection_error_abort', $e->getMessage()), + ]; } + } break; } diff --git a/plugins/manager/lib/yform/manager/dataset.php b/plugins/manager/lib/yform/manager/dataset.php index 38a714f0..24f1d059 100644 --- a/plugins/manager/lib/yform/manager/dataset.php +++ b/plugins/manager/lib/yform/manager/dataset.php @@ -744,7 +744,10 @@ private static function tableToModel(string $table): string return self::getModelClass($table) ?: __CLASS__; } - private static function modelToTable(): string + /** + * @internal + */ + final protected static function modelToTable(): string { $class = static::class; diff --git a/plugins/manager/pages/data_import.php b/plugins/manager/pages/data_import.php index fcd98171..bcc2ae59 100644 --- a/plugins/manager/pages/data_import.php +++ b/plugins/manager/pages/data_import.php @@ -75,166 +75,167 @@ if ($import_start) { $sql_db = rex_sql::factory(); - $sql_db->beginTransaction(); - $error_message = null; try { - $fp = fopen($filename, 'r'); - $firstbytes = fread($fp, 3); - $bom = pack('CCC', 0xEF, 0xBB, 0xBF); - if ($bom != $firstbytes) { - rewind($fp); - } - - $idColumn = null; - while (false !== ($line_array = fgetcsv($fp, 30384, $div))) { - if (0 == count($fieldarray)) { - $fieldarray = $line_array; - $fieldarray = array_map('rex_string::normalize', $fieldarray); - - if (in_array('', $fieldarray)) { - echo rex_view::error(rex_i18n::msg('yform_manager_import_error_missingfielddefinition')); - $show_importform = true; - $func = 'import'; - break; - } + $sql_db->transactional(function () use ($filename, $show_importform, $div, $fieldarray, $missing_columns, $debug, &$counter, &$dcounter, &$ecounter, &$rcounter, &$icounter, &$errorcounter, $fields) { - if (count($fieldarray) != count(array_unique($fieldarray))) { - echo rex_view::error(rex_i18n::msg('yform_manager_import_error_duplicatefielddefinition')); - $show_importform = true; - $func = 'import'; - break; - } + $fp = fopen($filename, 'r'); + $firstbytes = fread($fp, 3); + $bom = pack('CCC', 0xEF, 0xBB, 0xBF); + if ($bom != $firstbytes) { + rewind($fp); + } - $mc = []; - foreach ($fieldarray as $k => $v) { - $v = rex_string::normalize($v); - $fieldarray[$k] = $v; - if (!array_key_exists($fieldarray[$k], $fields) && 'id' != $fieldarray[$k]) { - $mc[$fieldarray[$k]] = $fieldarray[$k]; - } - if ('id' === $fieldarray[$k]) { - $idColumn = $k; - } - } + $idColumn = null; + while (false !== ($line_array = fgetcsv($fp, 30384, $div))) { + if (0 == count($fieldarray)) { + $fieldarray = $line_array; + $fieldarray = array_map('rex_string::normalize', $fieldarray); - if (count($mc) > 0) { - if (3 == $missing_columns) { - echo rex_view::error(rex_i18n::msg('yform_manager_import_error_missingfields', implode(', ', $mc))); + if (in_array('', $fieldarray)) { + echo rex_view::error(rex_i18n::msg('yform_manager_import_error_missingfielddefinition')); $show_importform = true; $func = 'import'; break; } - if (2 == $missing_columns) { - $i = rex_sql::factory(); - - foreach ($mc as $mcc) { - rex_sql::factory() - ->setTable(rex_yform_manager_field::table()) - ->setValue('table_name', $this->table->getTablename()) - ->setValue('prio', 999) - ->setValue('type_id', 'value') - ->setValue('type_name', 'text') - ->setValue('name', $mcc) - ->setValue('label', 'TEXT `' . $mcc . '`') - ->setValue('list_hidden', 0) - ->setValue('db_type', 'text') - ->insert(); - - echo rex_view::info(rex_i18n::msg('yform_manager_import_field_added', $mcc)); - } - rex_yform_manager_table_api::generateTablesAndFields(); + if (count($fieldarray) != count(array_unique($fieldarray))) { + echo rex_view::error(rex_i18n::msg('yform_manager_import_error_duplicatefielddefinition')); + $show_importform = true; + $func = 'import'; + break; + } - $fields = []; - foreach (rex_yform_manager_table::get($this->table->getTableName())->getFields() as $field) { - $fields[strtolower($field->getName())] = $field; + $mc = []; + foreach ($fieldarray as $k => $v) { + $v = rex_string::normalize($v); + $fieldarray[$k] = $v; + if (!array_key_exists($fieldarray[$k], $fields) && 'id' != $fieldarray[$k]) { + $mc[$fieldarray[$k]] = $fieldarray[$k]; } - } else { - if (count($fieldarray) == count($mc)) { - echo rex_view::error(rex_i18n::msg('yform_manager_import_error_min_missingfields', implode(', ', $mc))); + if ('id' === $fieldarray[$k]) { + $idColumn = $k; + } + } + + if (count($mc) > 0) { + if (3 == $missing_columns) { + echo rex_view::error(rex_i18n::msg('yform_manager_import_error_missingfields', implode(', ', $mc))); $show_importform = true; + $func = 'import'; break; } + if (2 == $missing_columns) { + $i = rex_sql::factory(); + + foreach ($mc as $mcc) { + rex_sql::factory() + ->setTable(rex_yform_manager_field::table()) + ->setValue('table_name', $this->table->getTablename()) + ->setValue('prio', 999) + ->setValue('type_id', 'value') + ->setValue('type_name', 'text') + ->setValue('name', $mcc) + ->setValue('label', 'TEXT `' . $mcc . '`') + ->setValue('list_hidden', 0) + ->setValue('db_type', 'text') + ->insert(); + + echo rex_view::info(rex_i18n::msg('yform_manager_import_field_added', $mcc)); + } + + rex_yform_manager_table_api::generateTablesAndFields(); - foreach ($fieldarray as $k => $name) { - if (isset($mc[$name])) { - unset($fieldarray[$k]); + $fields = []; + foreach (rex_yform_manager_table::get($this->table->getTableName())->getFields() as $field) { + $fields[strtolower($field->getName())] = $field; + } + } else { + if (count($fieldarray) == count($mc)) { + echo rex_view::error(rex_i18n::msg('yform_manager_import_error_min_missingfields', implode(', ', $mc))); + $show_importform = true; + break; + } + + foreach ($fieldarray as $k => $name) { + if (isset($mc[$name])) { + unset($fieldarray[$k]); + } } } } - } - } else { - if (!$line_array) { - break; - } - - if (null !== $idColumn && isset($line_array[$idColumn]) && $line_array[$idColumn] > 0) { - $id = $line_array[$idColumn]; - $dataset = $this->table->getRawDataset((int) $id); } else { - $id = null; - $dataset = $this->table->createDataset(); - } - - $exists = $dataset->exists(); + if (!$line_array) { + break; + } - foreach ($line_array as $k => $v) { - if (empty($fieldarray[$k]) || 'id' === $fieldarray[$k]) { - continue; + if (null !== $idColumn && isset($line_array[$idColumn]) && $line_array[$idColumn] > 0) { + $id = $line_array[$idColumn]; + $dataset = $this->table->getRawDataset((int) $id); + } else { + $id = null; + $dataset = $this->table->createDataset(); } - $dataset->setValue($fieldarray[$k], $v); - } + $exists = $dataset->exists(); - ++$counter; + foreach ($line_array as $k => $v) { + if (empty($fieldarray[$k]) || 'id' === $fieldarray[$k]) { + continue; + } - $dataset->save(); + $dataset->setValue($fieldarray[$k], $v); + } - if ($messages = $dataset->getMessages()) { - $messages = array_unique($messages); - foreach ($messages as $key => $msg) { - if ('' == $msg) { - $msg = rex_i18n::msg('yform_values_message_is_missing', '', $key); - } else { - $msg = rex_i18n::translate($msg); + ++$counter; + + $dataset->save(); + + if ($messages = $dataset->getMessages()) { + $messages = array_unique($messages); + foreach ($messages as $key => $msg) { + if ('' == $msg) { + $msg = rex_i18n::msg('yform_values_message_is_missing', '', $key); + } else { + $msg = rex_i18n::translate($msg); + } + $messages[$key] = $msg; } - $messages[$key] = $msg; + ++$dcounter; + $dataId = 'ID: ' . $id; + echo rex_view::error(rex_i18n::msg('yform_manager_import_error_dataimport', $dataId, '
* ' . implode('
* ', $messages))); + } elseif ($exists) { + ++$rcounter; + } else { + ++$icounter; } - ++$dcounter; - $dataId = 'ID: ' . $id; - echo rex_view::error(rex_i18n::msg('yform_manager_import_error_dataimport', $dataId, '
* ' . implode('
* ', $messages))); - } elseif ($exists) { - ++$rcounter; - } else { - ++$icounter; } + + $show_list = true; } - $show_list = true; - } - - rex_extension::registerPoint(new rex_extension_point( - 'YFORM_DATASET_IMPORTED', - '', - [ - 'divider' => $div, - 'table' => $this->table, - 'filename' => $filename, - 'missing_columns' => $missing_columns, - 'debug' => $debug, - 'data_imported' => $counter, // importierte - 'data_not_imported' => $dcounter, // nicht imporierte - 'data_empty_rows' => $ecounter, // leere reihen - 'data_replaced' => $rcounter, // replace counter - 'data_inserted' => $icounter, // insert counter - 'data_errors' => $errorcounter, - ], - )); - $sql_db->commit(); + rex_extension::registerPoint(new rex_extension_point( + 'YFORM_DATASET_IMPORTED', + '', + [ + 'divider' => $div, + 'table' => $this->table, + 'filename' => $filename, + 'missing_columns' => $missing_columns, + 'debug' => $debug, + 'data_imported' => $counter, // importierte + 'data_not_imported' => $dcounter, // nicht imporierte + 'data_empty_rows' => $ecounter, // leere reihen + 'data_replaced' => $rcounter, // replace counter + 'data_inserted' => $icounter, // insert counter + 'data_errors' => $errorcounter, + ], + )); + + }); + } catch (\Throwable $e) { - $sql_db->rollBack(); $error_message = $e->getMessage(); }