From 3e454de77e48f3e088793435740b5d5c44874837 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Tue, 18 Apr 2023 09:23:54 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B01150=20AtributeCustomFields=20:=20GetFor?= =?UTF-8?q?JSON=20is=20now=20delegated=20to=20the=20handler=20Method=20was?= =?UTF-8?q?=20always=20returning=20null=20before?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/attributedef.class.inc.php | 13 +++++++-- core/customfieldshandler.class.inc.php | 20 +++++++++++-- core/ormcustomfieldsvalue.class.inc.php | 38 +++++++++++++++++-------- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 72d07718c..75a1ea6e0 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -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; } /** diff --git a/core/customfieldshandler.class.inc.php b/core/customfieldshandler.class.inc.php index 4f18774f8..ba0e4b328 100644 --- a/core/customfieldshandler.class.inc.php +++ b/core/customfieldshandler.class.inc.php @@ -16,9 +16,6 @@ // You should have received a copy of the GNU Affero General Public License // along with iTop. If not, see -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); diff --git a/core/ormcustomfieldsvalue.class.inc.php b/core/ormcustomfieldsvalue.class.inc.php index c669fde60..c926c247d 100644 --- a/core/ormcustomfieldsvalue.class.inc.php +++ b/core/ormcustomfieldsvalue.class.inc.php @@ -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); } /**