From 9d80ced6c8d556a66f41b1065588b2c2a08c6723 Mon Sep 17 00:00:00 2001 From: enured <enis.nuredini@uzh.ch> Date: Wed, 15 Feb 2023 18:54:48 +0100 Subject: [PATCH] B15570: Changed DB handling in class FormAction. Fixed multi-db problem with this. Included change for check of existing form editor report. refs #15570 --- .../Classes/Core/Database/DatabaseUpdate.php | 2 +- extension/Classes/Core/Form/FormAction.php | 37 ++++++++++++------- extension/Classes/Core/QuickFormQuery.php | 2 +- extension/Classes/Core/Save.php | 4 +- .../Tests/Unit/Core/Form/FormActionTest.php | 22 +++++------ 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/extension/Classes/Core/Database/DatabaseUpdate.php b/extension/Classes/Core/Database/DatabaseUpdate.php index 3d99cb972..bc759eb67 100644 --- a/extension/Classes/Core/Database/DatabaseUpdate.php +++ b/extension/Classes/Core/Database/DatabaseUpdate.php @@ -260,7 +260,7 @@ class DatabaseUpdate { */ private function enforceExistenceOfFormEditorReport() { $dbT3 = $this->store->getVar(SYSTEM_DB_NAME_T3, STORE_SYSTEM); - $sql = "SELECT `uid` FROM " . $dbT3 . ".`tt_content` WHERE `CType`='qfq_qfq' AND `deleted`=0 AND `bodytext` LIKE '%file=_formEditor%'"; + $sql = "SELECT `uid` FROM " . $dbT3 . ".`tt_content` WHERE `CType`='qfq_qfq' AND `deleted`=0 AND (`bodytext` LIKE '%file=_formEditor%' OR `bodytext` LIKE '%file={{file:SU:::_formEditor}}%')"; $res = $this->db->sql($sql); if (empty($res)) { $message = '<h2>FormEditor Report not found</h2>' diff --git a/extension/Classes/Core/Form/FormAction.php b/extension/Classes/Core/Form/FormAction.php index f84c96f66..b697d440a 100644 --- a/extension/Classes/Core/Form/FormAction.php +++ b/extension/Classes/Core/Form/FormAction.php @@ -38,32 +38,41 @@ class FormAction { /** * @var Database */ - private $db = null; + private $dbArray = null; + + /** + * @var int|string|null + */ + private $dbIndexData; + private $dbIndexQfq; /** * @var Store */ private $store = null; + /** * @param array $formSpec - * @param Database $db + * @param array $db * @param bool|false $phpUnit * @throws \CodeException * @throws \UserFormException * @throws \UserReportException */ - public function __construct(array $formSpec, Database $db, $phpUnit = false) { + public function __construct(array $formSpec, array $dbArray, $phpUnit = false) { #TODO: rewrite $phpUnit to: "if (!defined('PHPUNIT_QFQ')) {...}" $this->formSpec = $formSpec; $this->primaryTableName = Support::setIfNotSet($formSpec, F_TABLE_NAME); - $this->db = $db; - $this->store = Store::getInstance('', $phpUnit); - $this->evaluate = new Evaluate($this->store, $this->db); + $this->dbArray = $dbArray; + $this->dbIndexData = $this->store::getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM); + $this->dbIndexQfq = $this->store::getVar(SYSTEM_DB_INDEX_QFQ, STORE_SYSTEM); + + $this->evaluate = new Evaluate($this->store, $this->dbArray[$this->dbIndexData]); } @@ -140,7 +149,7 @@ class FormAction { // Process templateGroup action elements if (isset($fe[FE_ID_CONTAINER]) && $fe[FE_ID_CONTAINER] > 0) { // Get native 'templateGroup'-FE - to retrieve MAX_LENGTH - $templateGroup = $this->db->sql(SQL_FORM_ELEMENT_TEMPLATE_GROUP_FE_ID, ROW_EXPECT_1, [$fe[FE_ID_CONTAINER]], + $templateGroup = $this->dbArray[$this->dbIndexQfq]->sql(SQL_FORM_ELEMENT_TEMPLATE_GROUP_FE_ID, ROW_EXPECT_1, [$fe[FE_ID_CONTAINER]], "Action FormElements should not be assigned to a container (exception: templateGroup). FormElement.id=" . $fe[FE_ID] . ", feIdContainer=" . $fe[FE_ID_CONTAINER] . ' is not a templateGroup'); if (isset($templateGroup[FE_TYPE]) && $templateGroup[FE_TYPE] == FE_TYPE_TEMPLATE_GROUP) { @@ -175,7 +184,7 @@ class FormAction { break; default: // Always work on recent data: previous actions might have modified the data. - $this->store->fillStoreWithRecord($this->primaryTableName, $recordId, $this->db, $this->formSpec[F_PRIMARY_KEY] ?? ''); + $this->store->fillStoreWithRecord($this->primaryTableName, $recordId, $this->dbArray[$this->dbIndexData], $this->formSpec[F_PRIMARY_KEY] ?? ''); } if (!$this->checkRequiredList($fe)) { @@ -383,7 +392,7 @@ class FormAction { // Check if there is a column with the same name as the 'action'-FormElement. if ($flagFeAction && false !== $this->store->getVar($fe[FE_NAME], STORE_RECORD)) { // After an insert or update, propagate the (new) slave id to the master record. - $this->db->sql("UPDATE `" . $this->primaryTableName . "` SET `" . $fe[FE_NAME] . "` = $slaveId WHERE `id` = ? LIMIT 1", ROW_REGULAR, [$recordId]); + $this->dbArray[$this->dbIndexData]->sql("UPDATE `" . $this->primaryTableName . "` SET `" . $fe[FE_NAME] . "` = $slaveId WHERE `id` = ? LIMIT 1", ROW_REGULAR, [$recordId]); } } @@ -514,7 +523,7 @@ class FormAction { // will be used in sub paste's // $clipboard["_src_id"] = $newColumns[COLUMN_ID]; - $rowSrc = $this->db->sql("SELECT * FROM `$recordSourceTable` WHERE `id`=?", ROW_EXPECT_1, [$newColumns[COLUMN_ID]]); + $rowSrc = $this->dbArray[$this->dbIndexData]->sql("SELECT * FROM `$recordSourceTable` WHERE `id`=?", ROW_EXPECT_1, [$newColumns[COLUMN_ID]]); $this->checkNCopyFiles($rowSrc, $newColumns); @@ -565,11 +574,11 @@ class FormAction { foreach ($translateMap as $oldId => $newId) { - $row = $this->db->sql("SELECT `$translateIdColumn` FROM `$tableName` WHERE `id`=$newId", ROW_EXPECT_1); + $row = $this->dbArray[$this->dbIndexData]->sql("SELECT `$translateIdColumn` FROM `$tableName` WHERE `id`=$newId", ROW_EXPECT_1); if (!empty($row[$translateIdColumn])) { $newNewId = $translateMap[$row[$translateIdColumn]]; - $this->db->sql("UPDATE `$tableName` SET `$translateIdColumn`=$newNewId WHERE `id`=$newId LIMIT 1"); + $this->dbArray[$this->dbIndexData]->sql("UPDATE `$tableName` SET `$translateIdColumn`=$newNewId WHERE `id`=$newId LIMIT 1"); } } @@ -619,7 +628,7 @@ class FormAction { $values = array(); $placeholder = array(); - $columns = $this->db->sql("SHOW FIELDS FROM " . $destTable); + $columns = $this->dbArray[$this->dbIndexData]->sql("SHOW FIELDS FROM " . $destTable); // Process all columns of destTable foreach ($columns as $col) { @@ -658,7 +667,7 @@ class FormAction { $sql = "INSERT INTO `$destTable` ($keyString) VALUES ($valueString)"; - return $this->db->sql($sql, ROW_REGULAR, $values); + return $this->dbArray[$this->dbIndexData]->sql($sql, ROW_REGULAR, $values); } # copyRecord() } diff --git a/extension/Classes/Core/QuickFormQuery.php b/extension/Classes/Core/QuickFormQuery.php index 6d3741430..b2454ef97 100644 --- a/extension/Classes/Core/QuickFormQuery.php +++ b/extension/Classes/Core/QuickFormQuery.php @@ -617,7 +617,7 @@ class QuickFormQuery { throw new \CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN); } - $formAction = new FormAction($this->formSpec, $this->dbArray[$this->dbIndexData], $this->phpUnit); + $formAction = new FormAction($this->formSpec, $this->dbArray, $this->phpUnit); switch ($formModeNew) { case FORM_LOAD: $formAction->elements($recordId, $this->feSpecAction, FE_TYPE_BEFORE_LOAD); diff --git a/extension/Classes/Core/Save.php b/extension/Classes/Core/Save.php index 8721cba3b..60f4f47f0 100644 --- a/extension/Classes/Core/Save.php +++ b/extension/Classes/Core/Save.php @@ -88,7 +88,7 @@ class Save { } $this->evaluate = new Evaluate($this->store, $this->dbArray[$this->dbIndexData]); - $this->formAction = new FormAction($formSpec, $this->dbArray[$this->dbIndexData]); + $this->formAction = new FormAction($formSpec, $this->dbArray); $this->qfqLogFilename = Path::absoluteQfqLogFile(); } @@ -106,7 +106,7 @@ class Save { * @throws \UserReportException */ public function process($recordId) { - $formAction = new FormAction($this->formSpec, $this->dbArray[$this->dbIndexData]); + $formAction = new FormAction($this->formSpec, $this->dbArray); // Author: Enis Nuredini // If developer is in formElement editor check for valid key and database field type diff --git a/extension/Tests/Unit/Core/Form/FormActionTest.php b/extension/Tests/Unit/Core/Form/FormActionTest.php index e11b994d8..931d8d0be 100644 --- a/extension/Tests/Unit/Core/Form/FormActionTest.php +++ b/extension/Tests/Unit/Core/Form/FormActionTest.php @@ -33,7 +33,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoad() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); // Nothing to do: should not throw an exception $formAction->elements(0, array(), ''); @@ -83,7 +83,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException1() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD; @@ -108,7 +108,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException0() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD; @@ -133,7 +133,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException2() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_BEFORE_LOAD; @@ -158,7 +158,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException3() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_UPDATE; @@ -182,7 +182,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException4() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_UPDATE; @@ -221,7 +221,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException5() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $this->store->setVar('city', 'New York', STORE_FORM, true); $feSpecAction[FE_NAME] = ''; @@ -246,7 +246,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoad2() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_LOAD; @@ -274,7 +274,7 @@ class FormActionTest extends AbstractDatabaseTest { */ public function testBeforeLoadException6() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_LOAD; @@ -301,7 +301,7 @@ class FormActionTest extends AbstractDatabaseTest { public function testInsert() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $feSpecAction[FE_NAME] = ''; $feSpecAction[FE_TYPE] = FE_TYPE_AFTER_SAVE; @@ -361,7 +361,7 @@ class FormActionTest extends AbstractDatabaseTest { public function testUpdate() { $formSpec[F_TABLE_NAME] = 'Person'; - $formAction = new FormAction($formSpec, $this->dbArray[DB_INDEX_DEFAULT], true); + $formAction = new FormAction($formSpec, $this->dbArray, true); $masterId = 2; -- GitLab