Every variants/<client>.json now carries a "_formats" map: behind each
option key, the form the value should take ("on|off", "hex color, e.g.
#123456", "plain px number, e.g. 15", "off | ./files/logo.png", font
slug, chooser mode + <key>custom, ...), derived from the mother config.xml
by variants/option_formats.php. variants/document_options.php refreshes the
map in every json (--check for staleness); build.php adds a hint when it
auto-adds a NEW option key to a json. _formats is documentation only and
never read by the build or the drift check.
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
499 lines
26 KiB
PHP
499 lines
26 KiB
PHP
<?php
|
|
/**
|
|
* Build a thin client variant of this theme (vanilla_respondage).
|
|
*
|
|
* A variant is a separate LimeSurvey theme that extends this mother theme
|
|
* (<extends> in its config.xml). LimeSurvey then inherits ALL files
|
|
* (views, css, js, images, options page) from the mother theme, so any
|
|
* change made here in a file automatically applies to every variant.
|
|
*
|
|
* Only config.xml is generated per variant: it is a copy of this mother
|
|
* config.xml in which ONLY the house-style option values (colors, fonts,
|
|
* font sizes/colors/weights, logo files) listed in variants/<name>.json
|
|
* are replaced, plus the metadata (name/title/description/extends).
|
|
* Additionally an EMPTY views/subviews/footer/footer.twig override is
|
|
* written (unless the variant ships its own): the mother's footer shows a
|
|
* Respondage credit to respondents, variants must NOT show it. The
|
|
* Respondage footer on the admin options page (options/options.twig) is
|
|
* separate and is NOT overridden.
|
|
* The <engine> section is emptied except <optionspage>, so engine settings
|
|
* (css framework, packages, view/files directories, template editor) are
|
|
* inherited from the mother. The <files> section is kept as-is: LimeSurvey
|
|
* resolves each listed file in the variant first and then in the mother,
|
|
* so the mother's css/js keep loading until the variant ships its own file
|
|
* with the same name (which then replaces the mother's file). This mirrors
|
|
* what LimeSurvey itself does when you "extend" a theme in the theme editor.
|
|
*
|
|
* Usage:
|
|
* php variants/build.php <variantName> [definitions-dir]
|
|
*
|
|
* The definitions (variants/<variantName>.json) contain confidential client
|
|
* house-style values and are NOT kept in this (public) repository - see
|
|
* .gitignore. Store them in the private variants repository or locally, and
|
|
* 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.
|
|
*
|
|
* PREVIEW RULE (standing rule, row 235): every variant must have a preview that
|
|
* DIFFERS from the mother's (files/preview.png). files/preview.svg is the
|
|
* editable source of that png: it is NOT gitignored, it is added to the variant
|
|
* only ONCE at first creation (seeded from the mother if the definitions dir has
|
|
* none) and is NEVER overwritten on later builds — the user may edit it. After
|
|
* editing preview.svg, export files/preview.png from it (Inkscape: PNG export).
|
|
* The build warns when the variant's preview.png is identical to the mother's.
|
|
*
|
|
* Example:
|
|
* php variants/build.php <client> /path/to/private-variants
|
|
*
|
|
* Optional keys in the variant json:
|
|
* "files_add": {"css": ["css/variant.css"], "js": []} extra files shipped by the
|
|
* variant, appended to the <files> list of the generated config.xml
|
|
* "fontOptions": [{"value": "my_custom_font", "label": "My Custom Font"}] custom fonts,
|
|
* appended to the "Custom (this theme)" optgroup of the font dropdown
|
|
*
|
|
* The json may list ALL option keys of the mother config.xml (row 245): an EMPTY
|
|
* value ("" or null) means "inherit the mother theme's default" and is skipped —
|
|
* only non-empty values are written into the variant config. So a variant json
|
|
* can show every key the user CAN set, while leaving unknown ones to inherit.
|
|
* Row 246: when the mother config.xml gains NEW option keys, they are added to
|
|
* the json automatically as "" and a NOTE is printed listing them, so the user
|
|
* is informed about every new settable key (existing values are never touched).
|
|
* Row 264: the "_formats" map in the json documents, per option key, in which
|
|
* FORM the value should be filled in (e.g. "on|off", "hex color, e.g. #123456",
|
|
* "plain px number, e.g. 15"). It is generated from the mother config.xml by
|
|
* variants/document_options.php; new auto-added keys get their hint here too.
|
|
* "_formats" is documentation only and is never read back by the build.
|
|
*
|
|
* Output:
|
|
* ../<variantName>/config.xml (next to this theme, in upload/themes/survey)
|
|
*
|
|
* After running this script, upload the variant folder to
|
|
* upload/themes/survey/ and (re)install or reset the theme in LimeSurvey
|
|
* (Theme options -> reset), so the new config.xml is imported into the DB.
|
|
*
|
|
* If an option key in the json no longer exists in the mother config.xml,
|
|
* the script fails loudly, so drift between mother and variant is caught
|
|
* at build time instead of silently rendering wrong values.
|
|
*/
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
|
fwrite(STDERR, "This script must be run from the command line.\n");
|
|
exit(1);
|
|
}
|
|
|
|
$variantName = isset($argv[1]) ? trim($argv[1]) : '';
|
|
if ($variantName === '' || !preg_match('/^[a-z0-9_]+$/i', $variantName)) {
|
|
fwrite(STDERR, "Usage: php variants/build.php <variantName> [definitions-dir]\n");
|
|
fwrite(STDERR, " <variantName> must match an existing <definitions-dir>/<variantName>.json file.\n");
|
|
fwrite(STDERR, " Default definitions-dir: the variants/ folder next to this script, or RESPONDAGE_VARIANTS_DIR.\n");
|
|
exit(1);
|
|
}
|
|
|
|
$motherDir = dirname(__DIR__); // .../themes/survey/vanilla_respondage
|
|
$themesDir = dirname($motherDir); // .../themes/survey
|
|
$motherConfig = $motherDir . DIRECTORY_SEPARATOR . 'config.xml';
|
|
$definitionsDir = $argv[2] ?? (getenv('RESPONDAGE_VARIANTS_DIR') ?: __DIR__);
|
|
$definitionFile = rtrim($definitionsDir, '/\\') . DIRECTORY_SEPARATOR . $variantName . '.json';
|
|
|
|
foreach ([$motherConfig, $definitionFile] as $f) {
|
|
if (!is_file($f)) {
|
|
fwrite(STDERR, "Required file not found: $f\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$definition = json_decode(file_get_contents($definitionFile), true);
|
|
if (!is_array($definition)) {
|
|
fwrite(STDERR, "Invalid JSON in $definitionFile\n");
|
|
exit(1);
|
|
}
|
|
|
|
$doc = new DOMDocument();
|
|
$doc->preserveWhiteSpace = false;
|
|
$doc->formatOutput = true;
|
|
if (!$doc->load($motherConfig)) {
|
|
fwrite(STDERR, "Could not parse mother config: $motherConfig\n");
|
|
exit(1);
|
|
}
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
// --- row 246: keep the json in sync with the mother config — NEW option keys ---
|
|
// Standing rule: when the mother config.xml gains option keys that a variant json
|
|
// does not list yet, they are ADDED to the json as "" (inherit the mother default)
|
|
// and the user is INFORMED (notice below), so nothing settable goes unnoticed.
|
|
// Existing keys and values are never touched; legacy keys unknown to the mother
|
|
// are kept as-is at the end of the options object.
|
|
$motherOptionKeys = [];
|
|
foreach ($xpath->query('//options/*') as $opt) {
|
|
if ($opt instanceof DOMElement) {
|
|
$motherOptionKeys[] = $opt->nodeName;
|
|
}
|
|
}
|
|
if (!isset($definition['options']) || !is_array($definition['options'])) {
|
|
$definition['options'] = [];
|
|
}
|
|
$newKeys = [];
|
|
foreach ($motherOptionKeys as $k) {
|
|
if (!array_key_exists($k, $definition['options'])) {
|
|
$definition['options'][$k] = '';
|
|
$newKeys[] = $k;
|
|
}
|
|
}
|
|
if (!empty($newKeys)) {
|
|
require_once __DIR__ . '/option_formats.php';
|
|
$ordered = [];
|
|
foreach ($motherOptionKeys as $k) {
|
|
$ordered[$k] = $definition['options'][$k];
|
|
}
|
|
foreach ($definition['options'] as $k => $v) {
|
|
if (!in_array($k, $motherOptionKeys, true)) {
|
|
$ordered[$k] = $v; // legacy key not present in the mother config
|
|
}
|
|
}
|
|
$definition['options'] = $ordered;
|
|
// row 264: document the expected value format per new key in "_formats"
|
|
// (documentation only — never read back by the build)
|
|
if (!isset($definition['_formats']) || !is_array($definition['_formats'])) {
|
|
$definition['_formats'] = [];
|
|
}
|
|
foreach ($xpath->query('//options/*') as $opt) {
|
|
if ($opt instanceof DOMElement && in_array($opt->nodeName, $newKeys, true)) {
|
|
$definition['_formats'][$opt->nodeName] = option_format_hint($opt);
|
|
}
|
|
}
|
|
file_put_contents($definitionFile, json_encode($definition, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n");
|
|
echo "NOTE: " . count($newKeys) . " NEW option key(s) in the mother config.xml were added to $definitionFile as \"\" (inherit default) with a format hint in \"_formats\" — review them: " . implode(', ', $newKeys) . "\n";
|
|
}
|
|
|
|
$motherName = (string) $xpath->query('//metadata/name')->item(0)->textContent;
|
|
|
|
// --- metadata: identify the variant, point extends to the mother theme ---
|
|
function setMetadata(DOMXPath $xpath, DOMDocument $doc, string $tag, string $value): void
|
|
{
|
|
$node = $xpath->query("//metadata/$tag")->item(0);
|
|
if ($node === null) {
|
|
$node = $doc->createElement($tag);
|
|
$xpath->query('//metadata')->item(0)->appendChild($node);
|
|
}
|
|
$node->textContent = $value;
|
|
}
|
|
|
|
/**
|
|
* Recursively copy a directory (used for the variant's own assets).
|
|
*
|
|
* $neverOverwrite lists destination-relative paths (with '/' separators) that
|
|
* are copied ONLY when they do not exist yet in $dst — used for
|
|
* files/preview.svg: added to a variant once, at first creation, and never
|
|
* overwritten afterwards, because the user may have edited it (row 235).
|
|
*/
|
|
function copyDirRecursive(string $src, string $dst, array $neverOverwrite = [], string $prefix = ''): 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, $neverOverwrite, $prefix . $entry . '/');
|
|
} else {
|
|
$rel = $prefix . $entry; // relative to the top-level $src, '/' separators
|
|
if (in_array($rel, $neverOverwrite, true) && file_exists($d)) {
|
|
continue; // first-creation-only file already present: never overwrite
|
|
}
|
|
copy($s, $d);
|
|
}
|
|
}
|
|
closedir($dir);
|
|
}
|
|
|
|
setMetadata($xpath, $doc, 'name', $definition['name'] ?? $variantName);
|
|
setMetadata($xpath, $doc, 'title', $definition['title'] ?? ucfirst($variantName));
|
|
if (isset($definition['description'])) {
|
|
setMetadata($xpath, $doc, 'description', $definition['description']);
|
|
}
|
|
setMetadata($xpath, $doc, 'extends', $motherName);
|
|
setMetadata($xpath, $doc, 'lastUpdate', date('Y-m-d H:i:s'));
|
|
|
|
// --- empty the <engine> section except <optionspage>: inherited from the mother ---
|
|
// (same as TemplateManifest::deleteEngineInDom() does when extending a theme in LimeSurvey)
|
|
$engineNode = $xpath->query('//engine')->item(0);
|
|
if ($engineNode !== null) {
|
|
foreach (iterator_to_array($engineNode->childNodes) as $child) {
|
|
if ($child instanceof DOMElement && $child->nodeName !== 'optionspage') {
|
|
$engineNode->removeChild($child);
|
|
} elseif ($child instanceof DOMComment) {
|
|
$engineNode->removeChild($child);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- extra files shipped by the variant itself ---
|
|
// In the variant json: "files_add": {"css": ["css/variant.css"], "js": []}
|
|
// They are appended to the <files> list. The mother's files keep loading until the
|
|
// variant ships a file with the same name; from that moment it replaces the mother's
|
|
// file (LimeSurvey "add" also means "replace" for identical paths).
|
|
if (!empty($definition['files_add'])) {
|
|
$filesNode = $xpath->query('//files')->item(0);
|
|
if ($filesNode === null) {
|
|
fwrite(STDERR, "No <files> section found in mother config\n");
|
|
exit(1);
|
|
}
|
|
foreach (['css', 'js', 'print_css'] as $type) {
|
|
$values = $definition['files_add'][$type] ?? [];
|
|
if (empty($values)) {
|
|
continue;
|
|
}
|
|
$typeNode = $xpath->query('//files/' . $type)->item(0);
|
|
if ($typeNode === null) {
|
|
$typeNode = $doc->createElement($type);
|
|
$filesNode->appendChild($typeNode);
|
|
}
|
|
foreach ((array) $values as $file) {
|
|
$add = $doc->createElement('add');
|
|
$add->appendChild($doc->createTextNode((string) $file));
|
|
$typeNode->appendChild($add);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- custom fonts of the variant, added to the font dropdown ---
|
|
// In the variant json: "fontOptions": [{"value": "my_custom_font", "label": "My Custom Font"}]
|
|
// The font files themselves live in the variant's files/fonts/ and the @font-face +
|
|
// GENERIC .font-<value> rule (not body-scoped, so the per-element font overrides
|
|
// work too) in a css file shipped by the variant (see files_add, e.g.
|
|
// css/variant.css with src: url('../files/fonts/my-custom-font.woff2')).
|
|
// data-font-package stays "websafe": LimeSurvey only knows the core font packages
|
|
// (application/config/fonts.php), and "websafe" is the neutral one, so no missing
|
|
// package is requested. The real font is loaded by the variant's own css.
|
|
if (!empty($definition['fontOptions'])) {
|
|
$optgroup = $xpath->query('//options/font/dropdownoptions/optgroup[@label="Custom (this theme)"]')->item(0);
|
|
if ($optgroup === null) {
|
|
fwrite(STDERR, "No <optgroup label=\"Custom (this theme)\"> found in the font dropdown of the mother config.xml\n");
|
|
exit(1);
|
|
}
|
|
foreach ((array) $definition['fontOptions'] as $font) {
|
|
if (empty($font['value']) || empty($font['label'])) {
|
|
fwrite(STDERR, "Each entry in 'fontOptions' needs 'value' and 'label'\n");
|
|
exit(1);
|
|
}
|
|
$value = preg_replace('/[^a-z0-9_]/i', '_', (string) $font['value']);
|
|
$option = $doc->createElement('option');
|
|
$option->setAttribute('class', 'font-' . $value);
|
|
$option->setAttribute('value', $value);
|
|
$option->setAttribute('data-font-package', 'websafe');
|
|
$option->appendChild($doc->createTextNode((string) $font['label']));
|
|
$optgroup->appendChild($option);
|
|
}
|
|
}
|
|
|
|
// --- options: replace only the house-style values listed in the json ---
|
|
if (!empty($definition['options']) && !is_array($definition['options'])) {
|
|
fwrite(STDERR, "'options' in $definitionFile must be an object of optionName => value\n");
|
|
exit(1);
|
|
}
|
|
// A variant that ships its own font(s) via fontOptions gets the first custom font as
|
|
// the DEFAULT value of the "font" option (so it is pre-selected in the options page
|
|
// dropdown), unless the json sets options.font explicitly.
|
|
if (!empty($definition['fontOptions'])
|
|
&& (empty($definition['options']) || !isset($definition['options']['font']) || $definition['options']['font'] === '')) {
|
|
$definition['options']['font'] = preg_replace('/[^a-z0-9_]/i', '_', (string) $definition['fontOptions'][0]['value']);
|
|
}
|
|
// --- Prompt M migration: map the OLD logo json keys to the NEW per-logo keys ---
|
|
// Old model: <pos>logo ("on"/"off") + <pos>logofile (path) + <pos>logohideonsmall
|
|
// ("on" = hide below the md breakpoint) + one shared logotopmargin ("15px").
|
|
// New model: <pos>logofile ("off" = no logo, path = show), <pos>logotopmargin (plain
|
|
// number 0-100), <pos>showon{computer,tablet,mobile} ("on"/"off").
|
|
// The old keys are removed from the json options afterwards, so the unknown-key
|
|
// check below does not fail on them.
|
|
$logoPositions = ['brand', 'center', 'right'];
|
|
if (!isset($definition['options']) || !is_array($definition['options'])) {
|
|
$definition['options'] = [];
|
|
}
|
|
if (isset($definition['options']['logotopmargin']) && $definition['options']['logotopmargin'] !== '') {
|
|
$margin = preg_replace('/[^0-9]/', '', (string) $definition['options']['logotopmargin']);
|
|
if ($margin === '') {
|
|
$margin = '15';
|
|
}
|
|
foreach ($logoPositions as $p) {
|
|
$definition['options'][$p . 'logotopmargin'] = $margin;
|
|
}
|
|
unset($definition['options']['logotopmargin']);
|
|
}
|
|
foreach ($logoPositions as $pos) {
|
|
$oldOn = $pos . 'logo';
|
|
$oldHide = $pos . 'logohideonsmall';
|
|
if (isset($definition['options'][$oldOn]) && $definition['options'][$oldOn] === 'off') {
|
|
// old "off" wins over any file: in the new model the logo is hidden via logofile = "off"
|
|
$definition['options'][$pos . 'logofile'] = 'off';
|
|
}
|
|
unset($definition['options'][$oldOn]);
|
|
if (isset($definition['options'][$oldHide])) {
|
|
// old hideonsmall "on" = hide on mobile only -> showonmobile "off";
|
|
// old "off" = never hide -> show on all devices
|
|
$showEverywhere = ($definition['options'][$oldHide] !== 'on');
|
|
$definition['options'][$pos . 'showoncomputer'] = 'on';
|
|
$definition['options'][$pos . 'showontablet'] = 'on';
|
|
$definition['options'][$pos . 'showonmobile'] = $showEverywhere ? 'on' : 'off';
|
|
unset($definition['options'][$oldHide]);
|
|
}
|
|
}
|
|
|
|
// --- Row 115/135/149/157 migration: the color-chooser options (navbarfontcolor,
|
|
// headerbackgroundcolor, progressbarcolor, progressbarbackgroundcolor,
|
|
// progressbarfontcolor, bodybackgroundcolor, grouptitletextcolor,
|
|
// groupdescriptiontextcolor; row 180 plan P2 adds questiontitletextcolor,
|
|
// questiontitlebackgroundcolor, questionbackgroundcolor; row 184 plan P3 adds
|
|
// answerstextcolor; row 186 plan P4 adds questionhelpbackgroundcolor; row 191 plan
|
|
// P6 adds the six per-button choosers; row 195 adds checkboxcolor; row 197 adds
|
|
// questionhelpfontcolor + questionbordercolor; row 221 adds sliderhandlecolor) became mode
|
|
// dropdowns
|
|
// (default|defaultbackgroundcolor|color1|color2|color3|other) with a <key>custom
|
|
// colorpicker child. A legacy HEX value in the json means "custom color": map it
|
|
// to mode "other" and move the hex into the <key>custom child, so the variant
|
|
// default stays valid for the dropdown. A legacy mode "primary"/"secondary"
|
|
// (old 4-way textcolor dropdown) maps to "color1"/"color2".
|
|
$colorChooserKeys = ['navbarfontcolor', 'headerbackgroundcolor', 'progressbarcolor', 'progressbarbackgroundcolor', 'progressbarfontcolor', 'bodybackgroundcolor', 'grouptitletextcolor', 'groupdescriptiontextcolor', 'questiontitletextcolor', 'questiontitlebackgroundcolor', 'questionbackgroundcolor', 'answerstextcolor', 'questionhelpbackgroundcolor', 'questionhelpfontcolor', 'questionbordercolor', 'checkboxcolor', 'sliderhandlecolor', 'previousbuttonbackgroundcolor', 'previousbuttontextcolor', 'nextbuttonbackgroundcolor', 'nextbuttontextcolor', 'submitbuttonbackgroundcolor', 'submitbuttontextcolor'];
|
|
$colorChooserLegacyModes = ['primary' => 'color1', 'secondary' => 'color2'];
|
|
foreach ($colorChooserKeys as $ck) {
|
|
if (isset($definition['options'][$ck])
|
|
&& is_string($definition['options'][$ck])
|
|
&& preg_match('/^#[0-9a-fA-F]{3,8}$/', $definition['options'][$ck])) {
|
|
$definition['options'][$ck . 'custom'] = $definition['options'][$ck];
|
|
$definition['options'][$ck] = 'other';
|
|
} elseif (isset($definition['options'][$ck])
|
|
&& is_string($definition['options'][$ck])
|
|
&& isset($colorChooserLegacyModes[$definition['options'][$ck]])) {
|
|
$definition['options'][$ck] = $colorChooserLegacyModes[$definition['options'][$ck]];
|
|
}
|
|
}
|
|
|
|
// --- Row 157 migration: the old <el>fontweight json keys (400|700) became
|
|
// <el>fontoptions (normal|bold|italic|bolditalic); map the old values and remove
|
|
// the old keys so the unknown-key check below does not fail on them.
|
|
// Row 180 plan P2: questiontitle joined (questiontitlefontweight ->
|
|
// questiontitlefontoptions).
|
|
$fontWeightToOptions = ['400' => 'normal', '700' => 'bold'];
|
|
foreach (['grouptitle', 'groupdescription', 'questiontitle'] as $el) {
|
|
$oldKey = $el . 'fontweight';
|
|
if (isset($definition['options'][$oldKey])) {
|
|
$w = (string) $definition['options'][$oldKey];
|
|
$definition['options'][$el . 'fontoptions'] = $fontWeightToOptions[$w] ?? 'normal';
|
|
unset($definition['options'][$oldKey]);
|
|
}
|
|
}
|
|
|
|
$optionsNode = $xpath->query('//options')->item(0);
|
|
if ($optionsNode === null) {
|
|
fwrite(STDERR, "No <options> section found in mother config\n");
|
|
exit(1);
|
|
}
|
|
foreach (($definition['options'] ?? []) as $key => $value) {
|
|
if ($value === '' || $value === null) {
|
|
continue; // row 245: empty value = inherit the mother theme's default
|
|
}
|
|
$matches = $xpath->query('//options/' . $key);
|
|
if ($matches->length === 0) {
|
|
fwrite(STDERR, "Option '$key' from $definitionFile does not exist in the mother config.xml.\n");
|
|
fwrite(STDERR, "Remove it from the json or rename it there; the mother theme changed.\n");
|
|
exit(1);
|
|
}
|
|
$option = $matches->item(0);
|
|
// Replace only the text value, keep child elements (e.g. <dropdownoptions> of the font option)
|
|
foreach (iterator_to_array($option->childNodes) as $child) {
|
|
if ($child instanceof DOMText) {
|
|
$option->removeChild($child);
|
|
}
|
|
}
|
|
$option->insertBefore($doc->createTextNode((string) $value), $option->firstChild);
|
|
}
|
|
|
|
// --- write the variant config ---
|
|
$variantDir = $themesDir . DIRECTORY_SEPARATOR . $variantName;
|
|
if (!is_dir($variantDir) && !mkdir($variantDir, 0775, true)) {
|
|
fwrite(STDERR, "Could not create directory: $variantDir\n");
|
|
exit(1);
|
|
}
|
|
if (!is_dir($variantDir . DIRECTORY_SEPARATOR . 'files')) {
|
|
mkdir($variantDir . DIRECTORY_SEPARATOR . 'files', 0775, true);
|
|
}
|
|
$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;
|
|
// Row 235: files/preview.svg is added to a variant only at FIRST creation and is
|
|
// never overwritten afterwards (the user may have edited it in the variant), so
|
|
// it is passed in $neverOverwrite. Everything else is overwritten as before.
|
|
$neverOverwrite = ['files/preview.svg'];
|
|
if (is_dir($variantAssets)) {
|
|
copyDirRecursive($variantAssets, $variantDir, $neverOverwrite);
|
|
echo "Copied variant assets: $variantAssets -> $variantDir\n";
|
|
}
|
|
|
|
// --- row 235: seed files/preview.svg at first creation only ---
|
|
// The variant's preview must DIFFER from the mother's (standing rule), so the
|
|
// developer defines a variant-specific preview.svg (editable source of
|
|
// files/preview.png) in <definitionsDir>/<variantName>/files/. If neither the
|
|
// definitions dir nor the variant itself has one yet (first creation), seed it
|
|
// from the mother's files/preview.svg as a starting point to edit.
|
|
$variantPreviewSvg = $variantDir . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'preview.svg';
|
|
if (!is_file($variantPreviewSvg)) {
|
|
$motherPreviewSvg = $motherDir . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'preview.svg';
|
|
if (is_file($motherPreviewSvg)) {
|
|
if (!is_dir(dirname($variantPreviewSvg))) {
|
|
mkdir(dirname($variantPreviewSvg), 0775, true);
|
|
}
|
|
copy($motherPreviewSvg, $variantPreviewSvg);
|
|
echo "Seeded files/preview.svg from the mother (first creation) — EDIT IT so the variant preview differs from vanilla_respondage, then export files/preview.png\n";
|
|
} else {
|
|
fwrite(STDERR, "NOTE: no files/preview.svg found for this variant (neither in $variantAssets nor in the mother). Create one and export files/preview.png — the variant preview must differ from the mother's (row 235).\n");
|
|
}
|
|
}
|
|
|
|
// --- row 235 standing rule: a variant preview must differ from the mother's ---
|
|
$variantPreviewPng = $variantDir . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'preview.png';
|
|
$motherPreviewPng = $motherDir . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'preview.png';
|
|
if (is_file($variantPreviewPng) && is_file($motherPreviewPng)
|
|
&& md5_file($variantPreviewPng) === md5_file($motherPreviewPng)) {
|
|
fwrite(STDERR, "WARNING: $variantName files/preview.png is IDENTICAL to the mother's — standing rule (row 235): each variant must have its own preview. Define files/preview.svg in the definitions dir, edit it, export files/preview.png.\n");
|
|
}
|
|
|
|
// --- footer override: variants show NO Respondage theme footer to respondents ---
|
|
// The mother's views/subviews/footer/footer.twig renders a "proudly developed by
|
|
// Respondage" credit; in a variant that must not appear (the Respondage footer on
|
|
// the admin OPTIONS page lives in options/options.twig and is NOT overridden).
|
|
// LimeSurvey resolves a view in the variant folder first, so an (empty) override
|
|
// file here wins over the inherited mother view. A client-supplied
|
|
// <variantName>/views/subviews/footer/footer.twig (copied above) takes precedence.
|
|
$variantFooterDir = $variantDir . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'subviews' . DIRECTORY_SEPARATOR . 'footer';
|
|
$variantFooterFile = $variantFooterDir . DIRECTORY_SEPARATOR . 'footer.twig';
|
|
if (!is_file($variantFooterFile)) {
|
|
if (!is_dir($variantFooterDir)) {
|
|
mkdir($variantFooterDir, 0775, true);
|
|
}
|
|
file_put_contents($variantFooterFile, "{#\n Variant override (generated by build.php): render NOTHING.\n The mother theme vanilla_respondage shows a Respondage credit in its\n views/subviews/footer/footer.twig; variants must not show it to respondents.\n The Respondage footer on the admin options page (options/options.twig) is\n separate and stays.\n#}\n");
|
|
echo "Wrote empty footer override: $variantFooterFile\n";
|
|
}
|
|
|
|
echo "Variant '$variantName' built: $out\n";
|
|
echo 'Options overridden: ' . (empty($definition['options']) ? '(none)' : implode(', ', array_keys($definition['options']))) . "\n";
|
|
echo "Note: existing files in $variantDir were OVERWRITTEN from the definitions dir,\n";
|
|
echo " EXCEPT files/preview.svg: added only at first creation, never overwritten (row 235).\n";
|
|
echo "REBUILD RULE: re-run this build after EVERY change to $variantName.json or to the\n";
|
|
echo " <definitionsDir>/$variantName/ assets — a variant folder that is not rebuilt after\n";
|
|
echo " a definitions change is stale (fonts/logo/css then don't match the definitions).\n";
|
|
echo "Remember: upload the folder to upload/themes/survey/ and reset the theme in LimeSurvey.\n";
|