diff --git a/core/attributedef/AttributeBoolean.php b/core/attributedef/AttributeBoolean.php new file mode 100644 index 000000000..ae90fcdab --- /dev/null +++ b/core/attributedef/AttributeBoolean.php @@ -0,0 +1,242 @@ +GetSQLColSpec() : ''); + } + + public function MakeRealValue($proposedValue, $oHostObj) + { + if (is_null($proposedValue)) { + return null; + } + if ($proposedValue === '') { + return null; + } + if ((int)$proposedValue) { + return true; + } + + return false; + } + + public function ScalarToSQL($value) + { + if ($value) { + return 1; + } + + return 0; + } + + public function GetValueLabel($bValue) + { + if (is_null($bValue)) { + $sLabel = Dict::S('Core:' . get_class($this) . '/Value:null'); + } else { + $sValue = $bValue ? 'yes' : 'no'; + $sDefault = Dict::S('Core:' . get_class($this) . '/Value:' . $sValue); + $sLabel = $this->SearchLabel('/Attribute:' . $this->m_sCode . '/Value:' . $sValue, $sDefault, true /*user lang*/); + } + + return $sLabel; + } + + public function GetValueDescription($bValue) + { + if (is_null($bValue)) { + $sDescription = Dict::S('Core:' . get_class($this) . '/Value:null+'); + } else { + $sValue = $bValue ? 'yes' : 'no'; + $sDefault = Dict::S('Core:' . get_class($this) . '/Value:' . $sValue . '+'); + $sDescription = $this->SearchLabel('/Attribute:' . $this->m_sCode . '/Value:' . $sValue . '+', $sDefault, + true /*user lang*/); + } + + return $sDescription; + } + + public function GetAsHTML($bValue, $oHostObject = null, $bLocalize = true) + { + if (is_null($bValue)) { + $sRes = ''; + } elseif ($bLocalize) { + $sLabel = $this->GetValueLabel($bValue); + $sDescription = $this->GetValueDescription($bValue); + // later, we could imagine a detailed description in the title + $sRes = "" . parent::GetAsHtml($sLabel) . ""; + } else { + $sRes = $bValue ? 'yes' : 'no'; + } + + return $sRes; + } + + public function GetAsXML($bValue, $oHostObject = null, $bLocalize = true) + { + if (is_null($bValue)) { + $sFinalValue = ''; + } elseif ($bLocalize) { + $sFinalValue = $this->GetValueLabel($bValue); + } else { + $sFinalValue = $bValue ? 'yes' : 'no'; + } + $sRes = parent::GetAsXML($sFinalValue, $oHostObject, $bLocalize); + + return $sRes; + } + + public function GetAsCSV( + $bValue, $sSeparator = ',', $sTextQualifier = '"', $oHostObject = null, $bLocalize = true, + $bConvertToPlainText = false + ) + { + if (is_null($bValue)) { + $sFinalValue = ''; + } elseif ($bLocalize) { + $sFinalValue = $this->GetValueLabel($bValue); + } else { + $sFinalValue = $bValue ? 'yes' : 'no'; + } + $sRes = parent::GetAsCSV($sFinalValue, $sSeparator, $sTextQualifier, $oHostObject, $bLocalize); + + return $sRes; + } + + public static function GetFormFieldClass() + { + return '\\Combodo\\iTop\\Form\\Field\\SelectField'; + } + + /** + * @param \DBObject $oObject + * @param \Combodo\iTop\Form\Field\SelectField $oFormField + * + * @return \Combodo\iTop\Form\Field\SelectField + * @throws \CoreException + */ + public function MakeFormField(DBObject $oObject, $oFormField = null) + { + if ($oFormField === null) { + $sFormFieldClass = static::GetFormFieldClass(); + $oFormField = new $sFormFieldClass($this->GetCode()); + } + + $oFormField->SetChoices(array('yes' => $this->GetValueLabel(true), 'no' => $this->GetValueLabel(false))); + parent::MakeFormField($oObject, $oFormField); + + return $oFormField; + } + + public function GetEditValue($value, $oHostObj = null) + { + if (is_null($value)) { + return ''; + } else { + return $this->GetValueLabel($value); + } + } + + public function GetForJSON($value) + { + return (bool)$value; + } + + public function MakeValueFromString( + $sProposedValue, $bLocalizedValue = false, $sSepItem = null, $sSepAttribute = null, $sSepValue = null, + $sAttributeQualifier = null + ) + { + $sInput = mb_strtolower(trim($sProposedValue)); + if ($bLocalizedValue) { + switch ($sInput) { + case '1': // backward compatibility + case $this->GetValueLabel(true): + $value = true; + break; + case '0': // backward compatibility + case 'no': + case $this->GetValueLabel(false): + $value = false; + break; + default: + $value = null; + } + } else { + switch ($sInput) { + case '1': // backward compatibility + case 'yes': + $value = true; + break; + case '0': // backward compatibility + case 'no': + $value = false; + break; + default: + $value = null; + } + } + + return $value; + } + + public function RecordAttChange(DBObject $oObject, $original, $value): void + { + parent::RecordAttChange($oObject, $original ? 1 : 0, $value ? 1 : 0); + } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeScalar::class; + } + + public function GetAllowedValues($aArgs = array(), $sContains = ''): array + { + return [ + 0 => $this->GetValueLabel(false), + 1 => $this->GetValueLabel(true) + ]; + } + + public function GetDisplayStyle() + { + return $this->GetOptional('display_style', 'select'); + } +} \ No newline at end of file