N°1150 AtributeCustomFields : GetForJSON is now delegated to the handler

Method was always returning null before
This commit is contained in:
Pierre Goiffon
2023-04-18 09:23:54 +02:00
parent adcd0eb9b0
commit 3e454de77e
3 changed files with 53 additions and 18 deletions

View File

@@ -980,7 +980,7 @@ abstract class AttributeDefinition
*
* @param mixed $value field value
*
* @return string JSON encoded string of the value
* @return string|array PHP struct that can be properly encoded
*
*/
public function GetForJSON($value)
@@ -13345,13 +13345,20 @@ class AttributeCustomFields extends AttributeDefinition
*
* @param \ormCustomFieldsValue $value
*
* @return array
* @return string|array
*
* @since 3.1.0 N°1150 now returns the value (was always returning null before)
*/
public function GetForJSON($value)
{
return null;
try {
$sRet = $value->GetForJSON();
}
catch (Exception $e) {
$sRet = 'Custom field error: '.$e->getMessage();
}
return $sRet;
}
/**

View File

@@ -16,9 +16,6 @@
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
use Combodo\iTop\Form\Form;
use Combodo\iTop\Form\FormManager;
/**
* Base class to implement a handler for AttributeCustomFields
*
@@ -101,12 +98,29 @@ abstract class CustomFieldsHandler
* @param string $sSeparator
* @param string $sTextQualifier
* @param bool|true $bLocalize
*
* @return mixed
*/
abstract public function GetAsCSV($aValues, $sSeparator = ',', $sTextQualifier = '"', $bLocalize = true);
/**
* @param $aValues
*
* @return array|null
*
* @since 3.1.0 N°1150 Method creation
*/
public function GetAsJSON($aValues)
{
// Other GetAsCSV/GetAsHTML/GetAsXML methods are abstract, but were here from the start
// To ensure backward compatibility with older extensions, we are defining a default impl for this method
// Older extensions might have children classes without this new method
return null;
}
/**
* @param DBObject $oHostObject
*
* @return array Associative array id => value
*/
abstract public function ReadValues(DBObject $oHostObject);

View File

@@ -59,36 +59,50 @@ class ormCustomFieldsValue
public function GetAsHTML($bLocalize = true)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
$oHandler = $oAttDef->GetHandler($this->GetValues());
return $oHandler->GetAsHTML($this->aCurrentValues, $bLocalize);
return $this->GetHandler()->GetAsHTML($this->aCurrentValues, $bLocalize);
}
public function GetAsXML($bLocalize = true)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
$oHandler = $oAttDef->GetHandler($this->GetValues());
return $oHandler->GetAsXML($this->aCurrentValues, $bLocalize);
return $this->GetHandler()->GetAsXML($this->aCurrentValues, $bLocalize);
}
public function GetAsCSV($sSeparator = ',', $sTextQualifier = '"', $bLocalize = true)
{
return $this->GetHandler()->GetAsCSV($this->aCurrentValues, $sSeparator, $sTextQualifier, $bLocalize);
}
/**
* @return string|array
* @throws \Exception
* @since 3.1.0 N°1150 Method creation
*/
public function GetForJSON()
{
return $this->GetHandler()->GetAsJSON($this->aCurrentValues);
}
/**
* @return \CustomFieldsHandler
* @throws \Exception
* @since 3.1.0 N°1150 Method creation
*/
final protected function GetHandler()
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
$oHandler = $oAttDef->GetHandler($this->GetValues());
return $oHandler->GetAsCSV($this->aCurrentValues, $sSeparator, $sTextQualifier, $bLocalize);
return $oAttDef->GetHandler($this->GetValues());
}
/**
* Get various representations of the value, for insertion into a template (e.g. in Notifications)
* @param $value mixed The current value of the field
*
* @param $sVerb string The verb specifying the representation of the value
* @param $bLocalize bool Whether or not to localize the value
*/
public function GetForTemplate($sVerb, $bLocalize = true)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
$oHandler = $oAttDef->GetHandler($this->GetValues());
return $oHandler->GetForTemplate($this->aCurrentValues, $sVerb, $bLocalize);
return $this->GetHandler()->GetForTemplate($this->aCurrentValues, $sVerb, $bLocalize);
}
/**