NEW DEV Can set color of the on/off button.

This commit is contained in:
Laurent Destailleur
2025-10-22 11:05:59 +02:00
parent 73c559b9d5
commit be351614e2
4 changed files with 38 additions and 21 deletions

View File

@@ -911,10 +911,11 @@ class FormSetupItem
} elseif ($this->type == 'yesno') { } elseif ($this->type == 'yesno') {
if (!empty($conf->use_javascript_ajax)) { if (!empty($conf->use_javascript_ajax)) {
$input = $this->fieldParams['input'] ?? array(); $input = $this->fieldParams['input'] ?? array();
$revertonoff = !empty($this->fieldParams['revertonoff']) ? 1 : 0; $revertonoff = empty($this->fieldParams['revertonoff']) ? 0 : 1;
$forcereload = !empty($this->fieldParams['forcereload']) ? 1 : 0; $forcereload = empty($this->fieldParams['forcereload']) ? 0 : 1;
$suffixarray = array('ifoff' => empty($this->fieldParams['alertifoff']) ? '' : '_red', 'ifon' => empty($this->fieldParams['alertifon']) ? '' : '_red');
$out .= ajax_constantonoff($this->confKey, $input, $this->entity, $revertonoff, 0, $forcereload); $out .= ajax_constantonoff($this->confKey, $input, $this->entity, $revertonoff, 0, $forcereload, 2, 0, 0, $suffixarray);
} else { } else {
$out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1); $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
} }

View File

@@ -662,21 +662,21 @@ function ajax_event($htmlname, $events)
/** /**
* On/off button for constant * On/off button for constant
* *
* @param string $code Name of constant * @param string $code Name of constant
* @param array<string,string[]> $input It's array of complementary actions to do if success ("disabled"|"enabled'|'set'|'del') => CSS element to switch, 'alert' => message to show, ... Example: array('disabled'=>array(0=>'cssid')) * @param array<string,string[]> $input It's array of complementary actions to do if success ("disabled"|"enabled'|'set'|'del') => CSS element to switch, 'alert' => message to show, ... Example: array('disabled'=>array(0=>'cssid'))
* @param ?int $entity Entity. Current entity is used if null. * @param ?int $entity Entity. Current entity is used if null.
* @param int<0,1> $revertonoff 1 = Revert on/off * @param int<0,1> $revertonoff 1 = Revert on/off
* @param int<0,1> $strict 0 = Default, 1=Only the complementary actions "disabled" and "enabled" (found into $input) are processed. Use only "disabled" with delConstant and "enabled" with setConstant. * @param int<0,1> $strict 0 = Default, 1=Only the complementary actions "disabled" and "enabled" (found into $input) are processed. Use only "disabled" with delConstant and "enabled" with setConstant.
* @param int $forcereload Force to reload page if we click/change value (this is supported only when there is no 'alert' option in input) * @param int $forcereload Force to reload page if we click/change value (this is supported only when there is no 'alert' option in input)
* @param int<0,2> $marginleftonlyshort 1 = Add a short left margin on picto, 2 = Add a larger left margin on picto, 0 = No left margin. * @param int<0,2> $marginleftonlyshort 1 = Add a short left margin on picto, 2 = Add a larger left margin on picto, 0 = No left margin.
* @param int<0,1> $forcenoajax 1 = Force to use a ahref link instead of ajax code. * @param int<0,1> $forcenoajax 1 = Force to use a ahref link instead of ajax code.
* @param int<0,1> $setzeroinsteadofdel 1 = Set constant to '0' instead of deleting it when $input is empty. * @param int<0,1> $setzeroinsteadofdel 1 = Set constant to '0' instead of deleting it when $input is empty.
* @param string $suffix Suffix to use on the name of the switch picto when option is on. Example: '', '_red' * @param string|array $suffix Suffix to use on the name of the switch picto when option is on. Example: array('ifoff' => '_red', 'ifon' => '_green')
* @param string $mode Add parameter &mode= to the href link (Used for href link) * @param string $mode Add parameter &mode= to the href link (Used for href link)
* @param string $morecss More CSS * @param string $morecss More CSS. Example: 'inline-block reposition'
* @param User|int $userconst If set, use the ajax On/Off for user or user ID $userconst * @param User|int $userconst If set, use the ajax On/Off for user or user ID $userconst
* @param string $showwarning String to show a warning when enabled the option * @param string $showwarning String to show a warning when enabled the option
* @return string * @return string The HTML component of button
* @see ajax_object_onoff() to update the status of an object * @see ajax_object_onoff() to update the status of an object
*/ */
function ajax_constantonoff($code, $input = array(), $entity = null, $revertonoff = 0, $strict = 0, $forcereload = 0, $marginleftonlyshort = 2, $forcenoajax = 0, $setzeroinsteadofdel = 0, $suffix = '', $mode = '', $morecss = 'inline-block', $userconst = 0, $showwarning = '') function ajax_constantonoff($code, $input = array(), $entity = null, $revertonoff = 0, $strict = 0, $forcereload = 0, $marginleftonlyshort = 2, $forcenoajax = 0, $setzeroinsteadofdel = 0, $suffix = '', $mode = '', $morecss = 'inline-block', $userconst = 0, $showwarning = '')
@@ -756,9 +756,18 @@ function ajax_constantonoff($code, $input = array(), $entity = null, $revertonof
} else { } else {
$value = getDolGlobalString($code); $value = getDolGlobalString($code);
} }
if (is_array($suffix)) {
$suffixon = $suffix['ifon'];
$suffixoff = $suffix['ifoff'];
} else { // old mode deprecated
$suffixon = (string) $suffix;
$suffixoff = '';
}
$out .= '<div id="confirm_'.$code.'" title="" style="display: none;"></div>'; $out .= '<div id="confirm_'.$code.'" title="" style="display: none;"></div>';
$out .= '<span id="set_'.$code.'" class="valignmiddle inline-block linkobject '.($value ? 'hideobject' : '').($morecss ? ' '.$morecss : '').'">'.($revertonoff ? img_picto($langs->trans("Enabled"), 'switch_on', '', 0, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Disabled"), 'switch_off', '', 0, 0, 0, '', '', $marginleftonlyshort)).'</span>'; $out .= '<span id="set_'.$code.'" class="valignmiddle inline-block linkobject '.($value ? 'hideobject' : '').($morecss ? ' '.$morecss : '').'">'.($revertonoff ? img_picto($langs->trans("Enabled"), 'switch_on'.$suffixoff, '', 0, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Disabled"), 'switch_off'.$suffixoff, '', 0, 0, 0, '', '', $marginleftonlyshort)).'</span>';
$out .= '<span id="del_'.$code.'" class="valignmiddle inline-block linkobject '.($value ? '' : 'hideobject').($morecss ? ' '.$morecss : '').'">'.($revertonoff ? img_picto($langs->trans("Disabled"), 'switch_off'.$suffix, '', 0, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Enabled"), 'switch_on'.$suffix, '', 0, 0, 0, '', '', $marginleftonlyshort)).'</span>'; $out .= '<span id="del_'.$code.'" class="valignmiddle inline-block linkobject '.($value ? '' : 'hideobject').($morecss ? ' '.$morecss : '').'">'.($revertonoff ? img_picto($langs->trans("Disabled"), 'switch_off'.$suffixon, '', 0, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Enabled"), 'switch_on'.$suffixon, '', 0, 0, 0, '', '', $marginleftonlyshort)).'</span>';
$out .= "\n"; $out .= "\n";
} }

View File

@@ -5563,6 +5563,8 @@ function img_picto($titlealt, $picto, $moreatt = '', $pictoisfullpath = 0, $srco
'switch_on_grey', 'switch_on_grey',
'switch_on_red', 'switch_on_red',
'switch_off', 'switch_off',
'switch_off_grey',
'switch_off_red',
'uparrow', 'uparrow',
'1uparrow', '1uparrow',
'1downarrow', '1downarrow',
@@ -5620,6 +5622,8 @@ function img_picto($titlealt, $picto, $moreatt = '', $pictoisfullpath = 0, $srco
'switch_on' => 'font-status4', 'switch_on' => 'font-status4',
'switch_on_warning' => 'font-status4 warning', 'switch_on_warning' => 'font-status4 warning',
'switch_on_red' => 'font-status8', 'switch_on_red' => 'font-status8',
'switch_off_warning' => 'font-status4 warning',
'switch_off_red' => 'font-status8',
'holiday' => 'infobox-holiday', 'holiday' => 'infobox-holiday',
'info' => 'opacityhigh', 'info' => 'opacityhigh',
'info_black' => 'font-status1', 'info_black' => 'font-status1',
@@ -5896,6 +5900,9 @@ function getImgPictoConv($mode = 'fa')
'movement' => 'people-carry', 'movement' => 'people-carry',
'sign-out' => 'sign-out-alt', 'sign-out' => 'sign-out-alt',
'switch_off' => 'toggle-off', 'switch_off' => 'toggle-off',
'switch_off_grey' => 'toggle-off',
'switch_off_warning' => 'toggle-off',
'switch_off_red' => 'toggle-off',
'switch_on' => 'toggle-on', 'switch_on' => 'toggle-on',
'switch_on_grey' => 'toggle-on', 'switch_on_grey' => 'toggle-on',
'switch_on_warning' => 'toggle-on', 'switch_on_warning' => 'toggle-on',

View File

@@ -131,7 +131,7 @@ $item = $formSetup->newItem('MYMODULE_MYPARAM4');
$item->setAsThirdpartyType(); $item->setAsThirdpartyType();
// Setup conf for a selection of a boolean // Setup conf for a selection of a boolean
$formSetup->newItem('MYMODULE_MYPARAM5')->setAsYesNo(); $formSetup->newItem('MYMODULE_MYPARAM5')->setAsYesNo(); // ->fieldParams['alertifoff'] = 1 or ->fieldParams['alertifon'] = 1;
// Setup conf for a selection of an Email template of type thirdparty // Setup conf for a selection of an Email template of type thirdparty
$formSetup->newItem('MYMODULE_MYPARAM6')->setAsEmailTemplate('thirdparty'); $formSetup->newItem('MYMODULE_MYPARAM6')->setAsEmailTemplate('thirdparty');