Forum

Translate name usin...
 
Notifications
Retirer tout

Translate name using API

3 Posts
2 Utilisateurs
0 Reactions
4,951 Vu
 BEAU
(@mamath)
Posts: 24
Active Member
Début du sujet
 
[#2077]

Hi,

I propose evolution of the function jsonDumObj from api/index.php.
the following version has two enhancement :
- translation of name (if object has a _isNameTranslatable property)
- return "refName" value if object has refId and refType property.

		
function jsonDumpObj($obj, $included=false) {
	$res="";
	foreach($obj as $fld=>$val) {
		if (is_object($val)) {
		  if ($res!="") { $res.=", ";}
			$res.=jsonDumpObj($val, true);
		} else if (substr($fld,0,1)=='_'
		  or $obj->isAttributeSetToField($fld, 'hidden')
		  or $included and ($fld=='id' or $fld=='refType' or $fld=='refId' or $fld=='refName' 
		                 or $fld=='handled' or $fld=='done' or $fld=='idle' or $fld=='cancelled') ) {
			// Nothing
		} else {
		  if ($fld=='name' && property_exists($obj, '_isNameTranslatable') && $obj->_isNameTranslatable) { $val=i18n($val); }
		  if ($res!="") { $res.=", ";}
		  $res.='"' . htmlEncode($fld) . '":"' . htmlEncodeJson($val) . '"';
		  if (substr($fld,0,2)=='id' and strlen($fld)>2) {
		  	$idclass=substr($fld,2);
		  	if (strtoupper(substr($idclass,0,1))==substr($idclass,0,1) and property_exists($idclass, 'name')) {
		  		$res.=", ";
		  		$val2=SqlList::getNameFromId($idclass, $val);
		  		if (property_exists($idclass, '_isNameTranslatable') && $idclass->_isNameTranslatable) { $val2=i18n($val2); }
		  		$res.='"name' . $idclass . '":"' . htmlEncodeJson($val2) . '"';
		  	}
		  } 
		}  
	}
	if (property_exists($obj, 'refId') && property_exists($obj, 'refType') && !property_exists($obj, 'refName') &&
		 $obj->refId!="" and $obj->refType!="") {
		$idclass=$obj->refType;
		if (strtoupper(substr($idclass,0,1))==substr($idclass,0,1) and property_exists($idclass, 'name')) {
			$res.=", ";
			$res.='"refName":"' . htmlEncodeJson(SqlList::getNameFromId($idclass, $obj->refId)) . '"';
		}
	}
	return $res;
}

Best regards


 
Posté : 9 Nov PM 12:1111
 BEAU
(@mamath)
Posts: 24
Active Member
Début du sujet
 

Oups, i make a bug .... the right code following :

function jsonDumpObj($obj, $included=false) {
	$res="";
	foreach($obj as $fld=>$val) {
		if (is_object($val)) {
		  if ($res!="") { $res.=", ";}
			$res.=jsonDumpObj($val, true);
		} else if (substr($fld,0,1)=='_'
		  or $obj->isAttributeSetToField($fld, 'hidden')
		  or $included and ($fld=='id' or $fld=='refType' or $fld=='refId' or $fld=='refName' 
		                 or $fld=='handled' or $fld=='done' or $fld=='idle' or $fld=='cancelled') ) {
			// Nothing
		} else {
		  if ($fld=='name' && property_exists($obj, '_isNameTranslatable') && $obj->_isNameTranslatable) { $val=i18n($val); }
		  if ($res!="") { $res.=", ";}
		  $res.='"' . htmlEncode($fld) . '":"' . htmlEncodeJson($val) . '"';
		  if (substr($fld,0,2)=='id' and strlen($fld)>2) {
		  	$idclass=substr($fld,2);
		  	if (strtoupper(substr($idclass,0,1))==substr($idclass,0,1) and property_exists($idclass, 'name')) {
		  		$res.=", ";
		  		$val2=SqlList::getNameFromId($idclass, $val);
		  		$res.='"name' . $idclass . '":"' . htmlEncodeJson($val2) . '"';
		  	}
		  } 
		}  
	}
	if (property_exists($obj, 'refId') && property_exists($obj, 'refType') && !property_exists($obj, 'refName') &&
		 $obj->refId!="" and $obj->refType!="") {
		$idclass=$obj->refType;
		if (strtoupper(substr($idclass,0,1))==substr($idclass,0,1) and property_exists($idclass, 'name')) {
			$res.=", ";
			$res.='"refName":"' . htmlEncodeJson(SqlList::getNameFromId($idclass, $obj->refId)) . '"';
		}
	}
	return $res;
}

Regards


 
Posté : 12 Nov PM 12:1111
(@babynus)
Posts: 14952
Membre Admin
 

Hi,
Thanks for your proposition.
Will be tested and included in V4.6


 
Posté : 12 Nov PM 21:1111
Share:
Retour en haut