EOD;
// Need to match exactly the old UI to replace it
$old_ui_regex = '/
.*?FILTRE DOMINANCE :.*?Cas \"CASE VIDE\".*?<\/div>.*?<\/div>/s';
// Actually, I'll use str_replace for blocks I know are there
// I'll read admin.php around the UI block to be 100% sure of the string.
$js_additions = <<<'EOD'
function toggleMultiSelect(id) {
const menu = document.getElementById(id + '-menu');
if(!menu) return;
menu.classList.toggle('show');
const closer = function(e) {
if (!e.target.closest('.multi-select-dropdown')) {
menu.classList.remove('show');
document.removeEventListener('click', closer);
}
};
setTimeout(() => document.addEventListener('click', closer), 0);
}
function toggleCheckbox(e, element) {
if (e.target.tagName === 'INPUT') return;
const cb = element.querySelector('input[type="checkbox"]');
if(cb) {
cb.checked = !cb.checked;
cb.dispatchEvent(new Event('change'));
}
}
function handleAnyOption(id, anyCb) {
if (anyCb.checked) {
document.querySelectorAll('.' + id + '-checkbox:not(.any-option)').forEach(cb => cb.checked = false);
}
updateMultiSelectDisplay(id);
}
function updateMultiSelectDisplay(id) {
const checkboxes = document.querySelectorAll('.' + id + '-checkbox');
const btn = document.getElementById(id + '-btn');
if(!btn) return;
const anyOption = document.querySelector('.' + id + '-checkbox.any-option');
const selected = [];
let hasSelection = false;
checkboxes.forEach(cb => {
if (cb.checked && !cb.classList.contains('any-option')) {
selected.push(cb.dataset.label);
hasSelection = true;
}
});
if (hasSelection && anyOption) anyOption.checked = false;
if (selected.length === 0) {
btn.innerHTML = 'N\'importe laquelle ';
} else {
if (selected.length > 2) {
btn.innerHTML = selected.length + ' sélectionné(s) ';
} else {
btn.innerHTML = selected.join(', ') + ' ';
}
}
}
function selectAllMultiSelect(id, selectAll) {
document.querySelectorAll('.' + id + '-checkbox').forEach(cb => {
if (cb.classList.contains('any-option')) {
cb.checked = !selectAll;
} else {
cb.checked = selectAll;
}
});
updateMultiSelectDisplay(id);
}
EOD;
if (strpos($content, 'function toggleMultiSelect') === false) {
$content = str_replace('// --- LOOTBOX SYSTEM ---', $js_additions . "\n\n" . ' // --- LOOTBOX SYSTEM ---', $content);
}
// Re-do the editRule replacement more carefully
$content = preg_replace('/document\.getElementById\(\'rule_orb_op\'\)\.value = data\.orbital_count_op \|\| \"\";.*document\.getElementById\(\'rule_empty\'\)\.checked = data\.is_empty_case == 1;/s',
'document.getElementById(\'rule_orb_op\').value = data.orbital_count_op || "";
document.getElementById(\'rule_orb_val\').value = data.orbital_count_val !== null ? data.orbital_count_val : "";
document.getElementById(\'rule_terr_op\').value = data.terrestrial_count_op || "";
document.getElementById(\'rule_terr_val\').value = data.terrestrial_count_val !== null ? data.terrestrial_count_val : "";
// Populate Multi-selects
const orbVals = (data.orbital_dominance || "").split(",");
document.querySelectorAll(\'.orb-factions-checkbox\').forEach(cb => {
cb.checked = orbVals.includes(cb.value);
});
const anyOrb = document.querySelector(\'.orb-factions-checkbox.any-option\');
if (anyOrb && (orbVals.length === 0 || orbVals[0] === "")) anyOrb.checked = true;
updateMultiSelectDisplay(\'orb-factions\');
const terrVals = (data.terrestrial_dominance || "").split(",");
document.querySelectorAll(\'.terr-factions-checkbox\').forEach(cb => {
cb.checked = terrVals.includes(cb.value);
});
const anyTerr = document.querySelector(\'.terr-factions-checkbox.any-option\');
if (anyTerr && (terrVals.length === 0 || terrVals[0] === "")) anyTerr.checked = true;
updateMultiSelectDisplay(\'terr-factions\');
document.getElementById(\'rule_empty\').checked = data.is_empty_case == 1;', $content);
$content = str_replace('document.getElementById(\'ruleForm\').reset(); document.getElementById(\'rule_id\').value = 0;',
'document.getElementById(\'ruleForm\').reset();
document.getElementById(\'rule_id\').value = 0;
selectAllMultiSelect(\'orb-factions\', false);
selectAllMultiSelect(\'terr-factions\', false);
const anyOrbForm = document.querySelector(\'.orb-factions-checkbox.any-option\');
if(anyOrbForm) anyOrbForm.checked = true;
const anyTerrForm = document.querySelector(\'.terr-factions-checkbox.any-option\');
if(anyTerrForm) anyTerrForm.checked = true;
updateMultiSelectDisplay(\'orb-factions\');
updateMultiSelectDisplay(\'terr-factions\');', $content);
// Final UI swap (Manual block replace to be safe)
// I'll search for specific markers in UI
$start_marker = '
FILTRE DOMINANCE :
';
$end_marker = '
';
// Find the parent containers
// I'll just use the preg_replace on handler and table as they worked.
// For UI I will use a very specific string replace of the whole block.
file_put_contents($admin_path, $content);
echo "Admin patched successfully.";