vanilla_respondage/variants/option_formats.php

97 lines
3.8 KiB
PHP
Raw Normal View History

<?php
/**
* Shared helper: human-readable "what format should this option value have"
* hint per option key, derived from the mother config.xml.
*
* Used by:
* - variants/document_options.php — writes/updates the "_formats" map in
* every variants/<client>.json (the per-item format documentation);
* - variants/build.php — when new option keys are auto-added to a
* variant json (row 246), their format hint is added to "_formats" too.
*
* The hint is documentation only; build.php never reads "_formats".
*/
if (!function_exists('option_format_hint')) {
/**
* @param DOMElement $opt an <options>/<key> element of the mother config.xml
* @return string format hint, e.g. 'on|off', 'hex color, e.g. #123456'
*/
function option_format_hint(DOMElement $opt): string
{
$key = $opt->nodeName;
$type = $opt->getAttribute('type');
$options = $opt->getAttribute('options');
$parent = $opt->getAttribute('parent');
// color-chooser pair: <key> = mode dropdown, <key>custom = hex picker
if ($type === 'dropdown' && $options === 'default|defaultbackgroundcolor|color1|color2|color3|other') {
return "chooser mode: one of $options — when 'other', put the hex in {$key}custom";
}
if ($type === 'colorpicker') {
// a colorpicker always stores a hex value (the on|off options attr is
// only UI chrome); the <key>custom pickers feed the chooser dropdowns
$hint = "hex color, e.g. #123456";
if (substr($key, -6) === 'custom') {
$base = substr($key, 0, -6);
$hint .= " (used when $base = 'other')";
} elseif ($parent !== '') {
$hint .= " (used when $parent = 'other')";
}
return $hint;
}
if ($type === 'buttons') {
return $options !== '' ? "one of: $options" : "on|off";
}
if ($type === 'checkbox') {
return "on|off (checkbox)";
}
if ($type === 'fontselect') {
return "font value slug or 'base' (= same as base font), e.g. \"inter\"";
}
if ($type === 'dropdown') {
if ($key === 'font') {
return "font value slug, e.g. \"inter\" (dropdown optgroups; a variant's default = its first fontOptions value)";
}
return $options !== '' ? "one of: $options" : "dropdown value";
}
if ($type === 'imagefile') {
return "off | theme-relative image path, e.g. ./files/logo.png (gif|ico|jpg|jpeg|png only — SVG is rejected)";
}
if ($type === 'text') {
if (preg_match('/radius/i', $key)) {
return "CSS length in px or rem, e.g. 5px (border-radius; NOT seconds)";
}
if (preg_match('/margin|height/i', $key)) {
return "plain px number, e.g. 15 (margins 0-100)";
}
if (preg_match('/distance/i', $key)) {
return "length, e.g. 20px or 1.5rem";
}
if ($key === 'questionborderstyle') {
return "CSS border-style keyword, e.g. solid|dashed|dotted|none";
}
return "free text";
}
if ($type === 'duration') {
return "duration in seconds, e.g. 300";
}
return "value of type '$type'";
}
/**
* Build the full key => hint map from a loaded mother config.xml DOMDocument.
*
* @return array<string,string> in mother config order
*/
function option_format_map(DOMDocument $doc): array
{
$map = [];
foreach ((new DOMXPath($doc))->query('//options/*') as $opt) {
if ($opt instanceof DOMElement) {
$map[$opt->nodeName] = option_format_hint($opt);
}
}
return $map;
}
}