-- Migration of the 'enabled' column of Facturx extrafields towards conditions
-- that dol_eval() still accepts. Replays harmlessly at every module init()
-- (activation or upgrade) via _load_tables('/facturx/sql/'), before the
-- addExtraField() calls of the descriptor, which only ever INSERT a missing
-- row and never rewrite an existing one.
--
-- From Dolibarr 23 on, dol_eval() only accepts a short white list of $
-- patterns. Every condition below that still names $conf outside of
-- '$conf->module->enabled' was refused with "Bad string syntax to evaluate
-- (found use of $ not matching pattern...)" and the field stayed hidden.

-- Chorus fields of the demat4dolibarr era. Restricted to their own names: the
-- condition used to be replaced on every row carrying '$conf->facturx->enabled',
-- which also caught facturx_shipto_id and facturx_synthese_group and tied them
-- to the Chorus toggle they have nothing to do with.
UPDATE llx_extrafields SET enabled='!getDolGlobalString("FACTURX_DISABLE_CHORUS_EXTRAFIELDS")' WHERE enabled = '$conf->facturx->enabled' AND name IN ('d4d_separator', 'd4d_service_code', 'd4d_contract_number', 'd4d_promise_code', 'd4d_chorus_id', 'fx_chorus_target');
UPDATE llx_extrafields SET enabled='!getDolGlobalString("FACTURX_DISABLE_CHORUS_EXTRAFIELDS")' WHERE enabled = '!isset($conf->global->FACTURX_DISABLE_CHORUS_EXTRAFIELDS)';
UPDATE llx_extrafields SET enabled='!getDolGlobalString("FACTURX_DISABLE_CHORUS_EXTRAFIELDS")' WHERE enabled = 'empty($conf->global->FACTURX_DISABLE_CHORUS_EXTRAFIELDS)';

-- Fields shared with the peppol module: visible as soon as either module is on.
UPDATE llx_extrafields SET enabled='isModEnabled("facturx") || isModEnabled("peppol")' WHERE enabled = '$conf->facturx->enabled || (isset($conf->peppol) && $conf->peppol->enabled)';

-- Fields of the module alone. The second statement repairs the installations
-- where the unrestricted Chorus migration above already overwrote them.
UPDATE llx_extrafields SET enabled='isModEnabled("facturx")' WHERE enabled = '$conf->facturx->enabled' AND name IN ('facturx_shipto_id', 'facturx_synthese_group');
UPDATE llx_extrafields SET enabled='isModEnabled("facturx")' WHERE enabled = '!getDolGlobalString("FACTURX_DISABLE_CHORUS_EXTRAFIELDS")' AND name IN ('facturx_shipto_id', 'facturx_synthese_group');
