-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
100 lines (80 loc) · 3.17 KB
/
Copy pathinstall.php
File metadata and controls
100 lines (80 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Install-Script fuer das synch Addon.
*/
$addon = rex_addon::get('synch');
$cleanKey = static function (string $name): string {
$key = strtolower($name);
$key = preg_replace('/[^a-zA-Z0-9_]/', '_', $key) ?? $key;
$key = preg_replace('/_+/', '_', $key) ?? $key;
return strtolower(trim($key, '_'));
};
/**
* @param string $tableName
*/
$ensureKeyColumn = static function (string $tableName): void {
if ($tableName === '') {
throw new \RuntimeException('Leerer Tabellenname ist nicht erlaubt.');
}
$table = rex_sql_table::get($tableName);
$table->ensureColumn(new rex_sql_column('key', 'varchar(191)', true));
$table->ensureIndex(new rex_sql_index('key', ['key'], rex_sql_index::UNIQUE));
$table->alter();
};
/**
* @param string $tableName
* @param string $fallbackPrefix
*/
$fillMissingKeys = static function (string $tableName, string $fallbackPrefix) use ($cleanKey): void {
$sql = rex_sql::factory();
$sql->setQuery('SELECT id, name, `key` FROM ' . $tableName . ' ORDER BY id');
while ($sql->hasNext()) {
$id = (int) $sql->getValue('id');
$name = (string) ($sql->getValue('name') ?: ($fallbackPrefix . '_' . $id));
$existingKey = (string) $sql->getValue('key');
if ($existingKey !== '') {
$sql->next();
continue;
}
$baseKey = $cleanKey($name);
if ($baseKey === '') {
$baseKey = $fallbackPrefix . '_' . $id;
}
$uniqueKey = $baseKey;
$counter = 1;
while (true) {
$checkSql = rex_sql::factory();
$checkSql->setQuery('SELECT id FROM ' . $tableName . ' WHERE `key` = ? AND id <> ? LIMIT 1', [$uniqueKey, $id]);
if ($checkSql->getRows() === 0) {
break;
}
$uniqueKey = $baseKey . '_' . $counter;
++$counter;
}
$updateSql = rex_sql::factory();
$updateSql->setTable($tableName);
$updateSql->setWhere(['id' => $id]);
$updateSql->setValue('key', $uniqueKey);
$updateSql->update();
$sql->next();
}
};
try {
$ensureKeyColumn(rex::getTable('module'));
$fillMissingKeys(rex::getTable('module'), 'module');
$ensureKeyColumn(rex::getTable('template'));
$fillMissingKeys(rex::getTable('template'), 'template');
$ensureKeyColumn(rex::getTable('action'));
$fillMissingKeys(rex::getTable('action'), 'action');
// Stabile und sichere Defaults.
$addon->setConfig('sync_frontend', false);
$addon->setConfig('sync_backend', false);
$addon->setConfig('sync_direction', 'files_to_db');
$addon->setConfig('conflict_strategy', 'newer_wins');
$addon->setConfig('update_existing_on_key_conflict', true);
$addon->setConfig('allow_empty_filesystem_cleanup', false);
echo rex_view::success('Synch Addon erfolgreich installiert. Key-Spalten sind vorhanden und fehlende Keys wurden erzeugt.');
echo rex_view::info('<strong>Hinweis:</strong> Auto-Sync ist standardmaessig deaktiviert und kann in den Einstellungen aktiviert werden.');
} catch (Throwable $throwable) {
echo rex_view::error('Fehler bei der Installation: ' . $throwable->getMessage());
}