Skip to content

Commit

Permalink
Refactor configuration loading and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
zoglo committed Oct 23, 2022
1 parent 71e5b5b commit a41d3de
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions contao/drivers/DC_Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,9 @@ public function __construct(string $strTable)
}

// Prefill on empty (only database)
if ($this->useDatabase)
if ($this->useDatabase && !!$GLOBALS['TL_DCA'][$this->strTable]['config']['fillOnEmpty'] && empty($this->getValuesFromDatabase()))
{
$objDatabase = $this->Database->prepare("SELECT $this->column FROM $this->table WHERE id=?")
->execute($this->intId);

if (!!$GLOBALS['TL_DCA'][$this->strTable]['config']['fillOnEmpty'] && !$objDatabase->{$this->column})
{
$this->prefillConfig();
}
$this->prefillConfig();
}

// Call onload_callback (e.g. to check permissions)
Expand Down Expand Up @@ -142,10 +136,7 @@ private function prefillConfig(): void
$arrDefaults[ $key ] = $field['default'] ?? '';
}

$deserialize = serialize($arrDefaults);

$this->Database->prepare("UPDATE " . $this->table . " SET " . $this->column . "=? WHERE id=?")
->execute($deserialize, $this->intId);
$this->updateConfig($arrDefaults);
}

/**
Expand Down Expand Up @@ -702,10 +693,7 @@ protected function save($varValue): void

$arrValues[$this->strField] = $varValue;

$deserialize = serialize($arrValues);

$this->Database->prepare("UPDATE " . $this->table . " SET " . $this->column . "=? WHERE id=?")
->execute($deserialize, $this->intId);
$this->updateConfig($arrValues);
}
else
{
Expand Down Expand Up @@ -742,6 +730,15 @@ protected function save($varValue): void
}
}

/**
* Updates the configuration in the database
*/
private function updateConfig(array $arrConfig): void
{
$this->Database->prepare("UPDATE " . $this->table . " SET " . $this->column . "=? WHERE id=?")
->execute(serialize($arrConfig), $this->intId);
}

/**
* Return the values of fields from database
*/
Expand Down

0 comments on commit a41d3de

Please sign in to comment.