0) {
$stmt = $db->prepare("UPDATE celestial_object_status_rules SET name = ?, status_id = ?, profile_id = ?, priority = ?, orbital_count_op = ?, orbital_count_val = ?, terrestrial_count_op = ?, terrestrial_count_val = ?, orbital_dominant_factions = ?, ground_dominant_factions = ?, is_empty_case = ?, dominance_diff_required = ? WHERE id = ?");
$stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominant_factions, $ground_dominant_factions, $is_empty_case, $dominance_diff_required, $id]);
} else {
$stmt = $db->prepare("INSERT INTO celestial_object_status_rules (name, status_id, profile_id, priority, orbital_count_op, orbital_count_val, terrestrial_count_op, terrestrial_count_val, orbital_dominant_factions, ground_dominant_factions, is_empty_case, dominance_diff_required) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominant_factions, $ground_dominant_factions, $is_empty_case, $dominance_diff_required]);
}
header("Location: admin.php?tab=statuses&success=1");
exit;
}
PHP;
// Update UI
$new_ui = <<<'HTML'
HTML;
// Update JS
$js_code = <<<'JS'
function toggleMS(id) {
const menu = document.getElementById(id + '_menu');
const isVisible = menu.classList.contains('show');
document.querySelectorAll('.multi-select-menu').forEach(m => m.classList.remove('show'));
if (!isVisible) menu.classList.add('show');
}
document.addEventListener('click', function(e) {
if (!e.target.closest('.multi-select-dropdown')) {
document.querySelectorAll('.multi-select-menu').forEach(m => m.classList.remove('show'));
}
});
function updateMSLabel(prefix) {
const any = document.getElementById(prefix + '_any');
const cbs = document.querySelectorAll('.' + prefix + '-cb:checked');
const btn = document.getElementById(prefix + '_btn');
if (any && any.checked) {
btn.innerHTML = 'N\'importe laquelle ';
} else if (cbs.length === 0) {
if(any) any.checked = true;
btn.innerHTML = 'N\'importe laquelle ';
} else {
const names = Array.from(cbs).map(c => c.dataset.name);
if (names.length > 2) btn.innerHTML = names.length + ' sélectionnés ';
else btn.innerHTML = names.join(', ') + ' ';
}
}
function handleMSChange(cb, prefix, isAny) {
const any = document.getElementById(prefix + '_any');
const others = document.querySelectorAll('.' + prefix + '-cb');
if (isAny && cb.checked) {
others.forEach(o => o.checked = false);
} else if (!isAny && cb.checked) {
if(any) any.checked = false;
}
updateMSLabel(prefix);
}
function msAll(prefix, state) {
const any = document.getElementById(prefix + '_any');
const others = document.querySelectorAll('.' + prefix + '-cb');
if (state) {
if(any) any.checked = false;
others.forEach(o => o.checked = true);
} else {
others.forEach(o => o.checked = false);
if(any) any.checked = true;
}
updateMSLabel(prefix);
}
function editRule(data) {
document.getElementById('rule_id').value = data.id;
document.getElementById('rule_name').value = data.name;
document.getElementById('rule_profile_id').value = data.profile_id || "";
document.getElementById('rule_status_id').value = data.status_id;
document.getElementById('rule_priority').value = data.priority || 0;
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 : "";
// Orbital Multi-select
const orbFactions = (data.orbital_dominant_factions || "").split(',').filter(x => x);
msAll('orb', false);
if (orbFactions.length === 0) {
document.getElementById('orb_any').checked = true;
} else {
document.getElementById('orb_any').checked = false;
orbFactions.forEach(val => {
const cb = document.querySelector(`.orb-cb[value="${val}"]`);
if(cb) cb.checked = true;
});
}
updateMSLabel('orb');
// Ground Multi-select
const terrFactions = (data.ground_dominant_factions || "").split(',').filter(x => x);
msAll('terr', false);
if (terrFactions.length === 0) {
document.getElementById('terr_any').checked = true;
} else {
document.getElementById('terr_any').checked = false;
terrFactions.forEach(val => {
const cb = document.querySelector(`.terr-cb[value="${val}"]`);
if(cb) cb.checked = true;
});
}
updateMSLabel('terr');
document.getElementById('rule_empty').checked = data.is_empty_case == 1;
document.getElementById('rule_diff').checked = data.dominance_diff_required == 1;
window.scrollTo(0,0);
}
function resetRuleForm() {
document.getElementById('ruleForm').reset();
document.getElementById('rule_id').value = 0;
msAll('orb', false);
msAll('terr', false);
}
JS;
// Apply changes to admin.php
$content = file_get_contents('admin.php');
// Handler
$content = preg_replace('/if \(\$_SERVER\[\'REQUEST_METHOD\'\] === \'POST\' && isset\(\$_POST\[\'action\'\]\) && \$_POST\[\'action\'\] === \'upsert_status_rule\'\) \{.*?header\(\"Location: admin\.php\?tab=statuses&success=1\"\);\n exit;/s', $handler_code, $content);
// UI
$content = preg_replace('/.*?FILTRE DOMINANCE :.*?Cas \"CASE VIDE\".*?<\/div>.*?<\/div>/s', $new_ui . "\n" . '
', $content);
// JS
$content = preg_replace('/function editRule\(data\) \{.*?function resetSettlementTypeForm\(\) \{.*?\}/s', $js_code . "\n\n function editSettlementType(data) {\n document.getElementById('set_t_id').value = data.id;\n document.getElementById('set_t_name').value = data.name;\n document.getElementById('set_t_slug').value = data.slug;\n document.getElementById('set_t_desc').value = data.description;\n window.scrollTo(0,0);\n }\n function resetSettlementTypeForm() { document.getElementById('settlementTypeForm').reset(); document.getElementById('set_t_id').value = 0; }", $content);
// Table Display - use preg_quote for exact matches or simplified regex
$content = preg_replace('/if\(\$r\[\'orbital_dominance_mode\'\] !== \'ANY\'\) \$conds\[\] = \"Orbital \" \. \$r\[\'orbital_dominance_mode\'\] \. \" \\\[\.\.\.\\\]\";/s', 'if($r[\'orbital_dominant_factions\']) $conds[] = "Orbital IN (" . $r[\'orbital_dominant_factions\'] . ")";', $content);
$content = preg_replace('/if\(\$r\[\'terrestrial_dominance_mode\'\] !== \'ANY\'\) \$conds\[\] = \"Ground \" \. \$r\[\'terrestrial_dominance_mode\'\] \. \" \\\[\.\.\.\\\]\";/s', 'if($r[\'ground_dominant_factions\']) $conds[] = "Ground IN (" . $r[\'ground_dominant_factions\'] . ")";', $content);
file_put_contents('admin.php', $content);
echo "admin.php updated.\n";