feat(variants): export a variant json from saved UI theme options
export_options.php reads lime_template_configuration.options for the mother theme (global row, or --sid <surveyId> with fallback to global on 'inherit'), fills in only the keys that differ from the mother config.xml defaults (rest stay "" = inherit), normalizes image::theme::files/x.png to ./files/x.png, includes the _formats map, and writes <definitions-dir>/<variantName>.json (refuses overwrite without --force). DB credentials are read read-only from application/config/config.php. Workflow documented in the README: pre-style a customer variant in the LimeSurvey UI, export the json, fine-tune, build. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
parent
f3d3912c2f
commit
c996b27cd1
2 changed files with 223 additions and 0 deletions
15
README.md
15
README.md
|
|
@ -403,6 +403,21 @@ the variant's `preview.png` is identical to the mother's.
|
|||
2. In the private variants repo, create `variants/<client>.json` with the
|
||||
variant name/title/description and the option values, plus a `<client>/`
|
||||
asset folder (logos, `files/fonts/`, `css/variant.css`,
|
||||
**Fast start — pre-style in the UI, then export:** set the house-style
|
||||
values once in the LimeSurvey UI (global Theme options of
|
||||
`vanilla_respondage`, or the theme options of one survey), then generate
|
||||
the json from those stored settings:
|
||||
```
|
||||
php variants/export_options.php <client> --global # or: --sid <surveyId>
|
||||
php variants/export_options.php <client> --sid <surveyId> --title "Client LimeSurvey theme" --definitions-dir <dir> [--force]
|
||||
```
|
||||
`export_options.php` reads `lime_template_configuration.options` (read-only;
|
||||
DB credentials come from `application/config/config.php`), fills in only the
|
||||
keys whose stored value differs from the mother `config.xml` default (the
|
||||
rest stay `""` = inherit), normalizes `image::theme::files/x.png` to
|
||||
`./files/x.png`, and includes the `_formats` map. A survey row that stores
|
||||
`inherit` falls back to the global row. `files_add`/`fontOptions` and the
|
||||
`<client>/` asset folder still need manual setup afterwards.
|
||||
`files/preview.svg`/`preview.png`). The json may list **ALL option keys**
|
||||
of the mother `config.xml` (109 at the time of writing) so it is visible what
|
||||
CAN be set per variant: an **empty value (`""`) means "inherit the default
|
||||
|
|
|
|||
208
variants/export_options.php
Normal file
208
variants/export_options.php
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
<?php
|
||||
/**
|
||||
* Export a variant json from the CURRENTLY SAVED theme options of the mother
|
||||
* theme (vanilla_respondage) in a LimeSurvey installation — the fast way to
|
||||
* pre-style a customer variant: set the colors/fonts/logos once in the
|
||||
* LimeSurvey UI (global Theme options, or the theme options of one survey),
|
||||
* then run this script to generate variants/<client>.json from those stored
|
||||
* values, and fine-tune the json afterwards.
|
||||
*
|
||||
* Usage:
|
||||
* php variants/export_options.php <variantName> [options]
|
||||
*
|
||||
* Options:
|
||||
* --global read the GLOBAL row (default)
|
||||
* --sid <id> read the row of survey <id> (falls back to the global
|
||||
* row when the survey row stores 'inherit')
|
||||
* --title "..." title for the json (default: "<VariantName> LimeSurvey theme")
|
||||
* --definitions-dir where to write <variantName>.json (default: variants/)
|
||||
* --force overwrite an existing <variantName>.json
|
||||
*
|
||||
* How it works:
|
||||
* 1. Reads the DB credentials from <limesurvey-root>/application/config/config.php
|
||||
* (the theme dir is upload/themes/survey/<theme>, so the root is 4 levels up).
|
||||
* 2. Reads lime_template_configuration.options for template_name =
|
||||
* <mother name> (global row, or the --sid row) — read-only SELECT.
|
||||
* 3. Compares each stored value with the mother config.xml default: keys whose
|
||||
* stored value DIFFERS from the default are written into the json;
|
||||
* keys equal to the default are written as "" (inherit), so the generated
|
||||
* variant stays thin. The "_formats" map (row 264) is included.
|
||||
* 4. "files_add" / "fontOptions" cannot be derived from stored options — they
|
||||
* are left empty in the generated json for manual completion (client fonts
|
||||
* in <client>/files/fonts/ + css/variant.css, see README).
|
||||
*
|
||||
* The generated json still needs the client assets folder (<definitions-dir>/
|
||||
* <variantName>/ with logos/fonts/css) before `build.php` can build the variant.
|
||||
*/
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
fwrite(STDERR, "This script must be run from the command line.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
require __DIR__ . '/option_formats.php';
|
||||
|
||||
$variantName = '';
|
||||
$sid = null;
|
||||
$useGlobal = true;
|
||||
$title = null;
|
||||
$defsDir = null;
|
||||
$force = false;
|
||||
$args = array_slice($argv, 1);
|
||||
for ($i = 0; $i < count($args); $i++) {
|
||||
switch ($args[$i]) {
|
||||
case '--global': $useGlobal = true; $sid = null; break;
|
||||
case '--sid': $sid = (int) ($args[++$i] ?? 0); $useGlobal = false; break;
|
||||
case '--title': $title = (string) ($args[++$i] ?? ''); break;
|
||||
case '--definitions-dir': $defsDir = (string) ($args[++$i] ?? ''); break;
|
||||
case '--force': $force = true; break;
|
||||
default:
|
||||
if ($variantName === '' && $args[$i][0] !== '-') {
|
||||
$variantName = $args[$i];
|
||||
} else {
|
||||
fwrite(STDERR, "Unknown argument: {$args[$i]}\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($variantName === '' || !preg_match('/^[a-z0-9_]+$/i', $variantName)) {
|
||||
fwrite(STDERR, "Usage: php variants/export_options.php <variantName> [--global|--sid <id>] [--title \"...\"] [--definitions-dir <dir>] [--force]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$themeDir = dirname(__DIR__); // upload/themes/survey/<theme>
|
||||
$lsRoot = dirname(dirname(dirname(dirname($themeDir)))); // limesurvey root
|
||||
$motherConfig = $themeDir . DIRECTORY_SEPARATOR . 'config.xml';
|
||||
$dbConfigFile = $lsRoot . '/application/config/config.php';
|
||||
$defsDir = $defsDir ?? (getenv('RESPONDAGE_VARIANTS_DIR') ?: __DIR__);
|
||||
$outFile = rtrim($defsDir, '/\\') . DIRECTORY_SEPARATOR . $variantName . '.json';
|
||||
|
||||
foreach ([$motherConfig, $dbConfigFile] as $f) {
|
||||
if (!is_file($f)) {
|
||||
fwrite(STDERR, "Required file not found: $f\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (is_file($outFile) && !$force) {
|
||||
fwrite(STDERR, "$outFile already exists — use --force to overwrite.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// --- 1. DB credentials from the LimeSurvey config (textual parse, no Yii boot) ---
|
||||
$dbConfig = file_get_contents($dbConfigFile);
|
||||
if (!preg_match("/'connectionString'\s*=>\s*'([^']+)'/", $dbConfig, $m)
|
||||
|| !preg_match("/'username'\s*=>\s*'([^']*)'/", $dbConfig, $u)
|
||||
|| !preg_match("/'password'\s*=>\s*'([^']*)'/", $dbConfig, $p)) {
|
||||
fwrite(STDERR, "Could not read DB credentials from $dbConfigFile\n");
|
||||
exit(1);
|
||||
}
|
||||
$conn = $m[1]; // e.g. mysql:host=localhost;port=3306;dbname=xxx
|
||||
if (!preg_match('/^mysql:host=([^;]+)(?:;port=(\d+))?;dbname=([^;]+)/', $conn, $cm)) {
|
||||
fwrite(STDERR, "Unsupported connectionString: $conn\n");
|
||||
exit(1);
|
||||
}
|
||||
$pdo = new PDO(
|
||||
"mysql:host={$cm[1]};port=" . ($cm[2] ?: '3306') . ";dbname={$cm[3]};charset=utf8mb4",
|
||||
$u[1],
|
||||
$p[1],
|
||||
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
|
||||
);
|
||||
|
||||
// --- 2. mother theme name + stored options row ---
|
||||
$doc = new DOMDocument();
|
||||
if (!$doc->load($motherConfig)) {
|
||||
fwrite(STDERR, "Could not parse $motherConfig\n");
|
||||
exit(1);
|
||||
}
|
||||
$xpath = new DOMXPath($doc);
|
||||
$motherName = (string) $xpath->query('//metadata/name')->item(0)->textContent;
|
||||
|
||||
$table = $pdo->quote($motherName) === '' ? 'lime_template_configuration' : 'lime_template_configuration';
|
||||
$sql = "SELECT id, options FROM $table WHERE template_name = :tpl AND sid IS NULL AND gsid IS NULL AND uid IS NULL";
|
||||
if (!$useGlobal) {
|
||||
$sql = "SELECT id, options FROM $table WHERE template_name = :tpl AND sid = :sid";
|
||||
}
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->bindValue(':tpl', $motherName);
|
||||
if (!$useGlobal) {
|
||||
$stmt->bindValue(':sid', $sid, PDO::PARAM_INT);
|
||||
}
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$row) {
|
||||
fwrite(STDERR, "No template_configuration row found for theme '$motherName' " . ($useGlobal ? '(global)' : "(sid=$sid)") . "\n");
|
||||
exit(1);
|
||||
}
|
||||
$storedOptions = json_decode((string) $row['options'], true);
|
||||
if (!is_array($storedOptions)) {
|
||||
// e.g. survey row storing the literal 'inherit' -> fall back to the global row
|
||||
if (!$useGlobal) {
|
||||
fwrite(STDERR, "Survey row sid=$sid stores '" . substr((string) $row['options'], 0, 20) . "' — falling back to the global row.\n");
|
||||
$stmt = $pdo->prepare("SELECT id, options FROM $table WHERE template_name = :tpl AND sid IS NULL AND gsid IS NULL AND uid IS NULL");
|
||||
$stmt->bindValue(':tpl', $motherName);
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$storedOptions = json_decode((string) $row['options'], true);
|
||||
}
|
||||
if (!is_array($storedOptions)) {
|
||||
fwrite(STDERR, "Stored options are not a JSON object (row id {$row['id']})\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// --- 3. build the variant json: differing values filled in, rest "" ---
|
||||
$options = [];
|
||||
$diffCount = 0;
|
||||
foreach ($xpath->query('//options/*') as $opt) {
|
||||
if (!($opt instanceof DOMElement)) {
|
||||
continue;
|
||||
}
|
||||
$key = $opt->nodeName;
|
||||
// default = the option's own text node only (NOT textContent: that would
|
||||
// include the labels of child <dropdownoptions>)
|
||||
$default = '';
|
||||
foreach ($opt->childNodes as $child) {
|
||||
if ($child instanceof DOMText) {
|
||||
$default .= $child->textContent;
|
||||
}
|
||||
}
|
||||
$default = trim($default);
|
||||
$stored = array_key_exists($key, $storedOptions) && is_scalar($storedOptions[$key])
|
||||
? (string) $storedOptions[$key]
|
||||
: null;
|
||||
if ($stored !== null && $stored !== '') {
|
||||
// normalize the stored image pseudo-URL to the json convention ./files/...
|
||||
if (strpos($stored, 'image::theme::files/') === 0) {
|
||||
$stored = './files/' . substr($stored, strlen('image::theme::files/'));
|
||||
}
|
||||
if ($stored !== $default) {
|
||||
$options[$key] = $stored;
|
||||
$diffCount++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$options[$key] = '';
|
||||
}
|
||||
|
||||
$definition = [
|
||||
'name' => $variantName,
|
||||
'title' => $title ?? ucfirst($variantName) . ' LimeSurvey theme',
|
||||
'description' => '<strong>' . ucfirst($variantName) . ' theme, based on Vanilla Respondage</strong><br>Thin variant of vanilla_respondage: only the house-style option values (colors, fonts, font sizes/weights/colors, logo files) differ. All views, CSS and JS are inherited from the mother theme. Generated by variants/export_options.php from the saved ' . ($useGlobal ? 'global' : "survey sid=$sid") . ' options of ' . $motherName . ' — fine-tune here.',
|
||||
'_note' => 'All option keys of the mother config.xml are listed. An empty value ("") means: inherit the default from ' . $motherName . ' (build.php skips empty values). Keys marked HOUSE-STYLE in config.xml are the per-client house-style values; the rest are structural and normally left empty. "_formats" documents the expected value format per key. Generated from stored DB options; files_add/fontOptions and the <client>/ asset folder still need manual setup.',
|
||||
'options' => $options,
|
||||
'files_add' => ['css' => [], 'js' => []],
|
||||
'fontOptions' => [],
|
||||
'_formats' => option_format_map($doc),
|
||||
];
|
||||
|
||||
if (!is_dir($defsDir)) {
|
||||
mkdir($defsDir, 0775, true);
|
||||
}
|
||||
file_put_contents($outFile, json_encode($definition, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n");
|
||||
|
||||
echo "Wrote $outFile\n";
|
||||
echo " source: lime_template_configuration row id {$row['id']} of '$motherName' (" . ($useGlobal ? 'global' : "sid=$sid") . ")\n";
|
||||
echo " $diffCount option key(s) differ from the mother defaults and were filled in; the rest are \"\" (inherit)\n";
|
||||
echo " filled keys: " . implode(', ', array_keys(array_filter($options, fn($v) => $v !== ''))) . "\n";
|
||||
echo "Next: create <definitions-dir>/$variantName/ with client assets (logos, files/fonts/, css/variant.css),\n";
|
||||
echo " fill files_add/fontOptions if needed, then: php variants/build.php $variantName <definitions-dir>\n";
|
||||
Loading…
Reference in a new issue