feat(variants): copy per-client assets (fonts/css/images) automatically in build.php

The definitions dir (private variants repo) now has a standard layout per
client: <client>.json + <client>/files/... + <client>/css/variant.css.
build.php copies <client>/ into the generated theme folder, so a
client-specific font or css is part of the variant by default.
README updated accordingly.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Tammo ter Hark 2026-11-16 21:09:02 +01:00
commit dd8714b6a0
2 changed files with 49 additions and 4 deletions

View file

@ -48,9 +48,13 @@ LimeSurvey only knows its core font packages (`application/config/fonts.php`),
so a theme cannot register a `font-*` package itself. Instead a variant ships
its own font files and css:
1. Put the font files in the variant's `files/fonts/` (e.g. `my-custom-font.woff2`).
2. Create `css/variant.css` in the variant with the `@font-face` and the body
class rule (the selected font is applied as `font-<value>` on `<body>`):
1. Put the font files in the client folder of the PRIVATE variants repo:
`<client>_vanilla/files/fonts/` (e.g. `my-custom-font.woff2`). This folder
is standard per client and is copied into the generated theme automatically
by `build.php` (together with e.g. `files/*.png` logos and `css/variant.css`).
2. Create `css/variant.css` in that same client folder with the `@font-face`
and the body class rule (the selected font is applied as `font-<value>` on
`<body>`):
```css
@font-face {
font-family: 'My Custom Font';
@ -73,7 +77,9 @@ its own font files and css:
"Custom (this theme)" optgroup of the font dropdown (`data-font-package`
stays `websafe`, so no nonexistent package is requested — the real font is
loaded by the variant's own css).
4. Run `php variants/build.php <client>` and upload/reset as usual.
4. Run `php variants/build.php <client> <definitions-dir>` (or set
RESPONDAGE_VARIANTS_DIR) and upload/reset as usual. The `<client>/` assets
folder from the definitions dir is copied into `../<client>/` automatically.
## Client variants (house styles) without code duplication

View file

@ -28,6 +28,12 @@
* point this script at that folder via the optional second argument or the
* RESPONDAGE_VARIANTS_DIR environment variable.
*
* Standard layout in that definitions dir per client:
* <variantName>.json house-style option values + fontOptions + files_add
* <variantName>/files/... client assets (logos, fonts in files/fonts/)
* <variantName>/css/... client css (e.g. css/variant.css with @font-face)
* The <variantName>/ folder is copied into the generated theme automatically.
*
* Example:
* php variants/build.php <client> /path/to/private-variants
*
@ -103,6 +109,28 @@ function setMetadata(DOMXPath $xpath, DOMDocument $doc, string $tag, string $val
$node->textContent = $value;
}
/** Recursively copy a directory (used for the variant's own assets). */
function copyDirRecursive(string $src, string $dst): void
{
if (!is_dir($dst)) {
mkdir($dst, 0775, true);
}
$dir = opendir($src);
while (($entry = readdir($dir)) !== false) {
if ($entry === '.' || $entry === '..') {
continue;
}
$s = $src . DIRECTORY_SEPARATOR . $entry;
$d = $dst . DIRECTORY_SEPARATOR . $entry;
if (is_dir($s)) {
copyDirRecursive($s, $d);
} else {
copy($s, $d);
}
}
closedir($dir);
}
setMetadata($xpath, $doc, 'name', $definition['name'] ?? $variantName);
setMetadata($xpath, $doc, 'title', $definition['title'] ?? ucfirst($variantName));
if (isset($definition['description'])) {
@ -221,6 +249,17 @@ if (!is_dir($variantDir . DIRECTORY_SEPARATOR . 'files')) {
$out = $variantDir . DIRECTORY_SEPARATOR . 'config.xml';
$doc->save($out);
// --- copy the variant's own assets (fonts, css, images) from the definitions dir ---
// Standard layout in the private variants repo: <definitionsDir>/<variantName>/
// with e.g. files/fonts/*.woff2, files/*.png, css/variant.css. Everything in that
// folder is copied into the generated theme, so a client-specific font or css is
// part of the variant by default (no manual copying needed).
$variantAssets = rtrim($definitionsDir, '/\\') . DIRECTORY_SEPARATOR . $variantName;
if (is_dir($variantAssets)) {
copyDirRecursive($variantAssets, $variantDir);
echo "Copied variant assets: $variantAssets -> $variantDir\n";
}
echo "Variant '$variantName' built: $out\n";
echo 'Options overridden: ' . (empty($definition['options']) ? '(none)' : implode(', ', array_keys($definition['options']))) . "\n";
echo "Remember: upload the folder to upload/themes/survey/ and reset the theme in LimeSurvey.\n";