* Sets the variables of a style.
*
* @param string[] $variables
+ * @param string[] $variablesDarkMode
*/
- public function setVariables(array $variables = [])
+ public function setVariables(array $variables, array $variablesDarkMode): void
{
- // delete old variables
- $sql = "DELETE FROM wcf" . WCF_N . "_style_variable_value
- WHERE styleID = ?";
- $statement = WCF::getDB()->prepareStatement($sql);
- $statement->execute([$this->styleID]);
-
- // insert new variables
- if (!empty($variables)) {
- $sql = "SELECT *
- FROM wcf" . WCF_N . "_style_variable";
- $statement = WCF::getDB()->prepareStatement($sql);
- $statement->execute();
- $styleVariables = [];
- while ($row = $statement->fetchArray()) {
- $variableName = $row['variableName'];
-
- if (isset($variables[$variableName])) {
- // compare value, save only if differs from default
- if ($variables[$variableName] != $row['defaultValue']) {
- $styleVariables[$row['variableID']] = $variables[$variableName];
- }
- }
- }
-
- if (!empty($styleVariables)) {
- $sql = "INSERT INTO wcf" . WCF_N . "_style_variable_value
- (styleID, variableID, variableValue)
- VALUES (?, ?, ?)";
- $statement = WCF::getDB()->prepareStatement($sql);
-
- WCF::getDB()->beginTransaction();
- foreach ($styleVariables as $variableID => $variableValue) {
- $statement->execute([
- $this->styleID,
- $variableID,
- $variableValue,
- ]);
- }
- WCF::getDB()->commitTransaction();
- }
+ $sql = "SELECT variableID, variableName
+ FROM wcf1_style_variable";
+ $statement = WCF::getDB()->prepare($sql);
+ $statement->execute();
+ $styleVariables = $statement->fetchMap('variableID', 'variableName');
+
+ $variables = \array_filter($variables, static function (string $key) use ($styleVariables) {
+ return \in_array($key, $styleVariables, true);
+ }, \ARRAY_FILTER_USE_KEY);
+
+ $variablesDarkMode = \array_filter($variablesDarkMode, static function (string $key) use ($styleVariables) {
+ return \in_array($key, $styleVariables, true);
+ }, \ARRAY_FILTER_USE_KEY);
+
+ $sql = "INSERT INTO wcf1_style_variable_value
+ (styleID, variableID, variableValue, variableValueDarkMode)
+ VALUES (?, ?, ?, ?)
+ ON DUPLICATE KEY UPDATE variableValue = VALUES(variableValue),
+ variableValueDarkMode = VALUES(variableValueDarkMode)";
+ $statement = WCF::getDB()->prepare($sql);
+ foreach ($styleVariables as $variableID => $variableName) {
+ $statement->execute([
+ $this->styleID,
+ $variableID,
+ $variables[$variableName] ?? null,
+ $variablesDarkMode[$variableName] ?? null,
+ ]);
}
- $this->writeStyleFile();
+ StyleHandler::getInstance()->resetStylesheet($this->getDecoratedObject());
}
/**