The problem with UTF keys and diffirence with them before call json_encode
and after.
Code Example:
<?= $this->dialogContainer('dialogTest', '1',
array(
'modal' => true,
'buttons' => array(
'Отмена 3' => new Zend_Json_Expr('function(){ alert("Cancel 3");
$(this).dialog("close") }'),
'Latin 2' => new Zend_Json_Expr('function(){ alert("Latin 2");
$(this).dialog("close") }'),
'Добавить 1' => new Zend_Json_Expr('function(){ alert("Add 1");
$(this).dialog("close") }')
)),
array('title' => 'ZendJsonExprFinder Test Multilangual Dialog'))
?>
Code generated by current Zend_Json::encode() implementation:
<code>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
$("#dialogTest").dialog({"modal":true,"buttons":{"\u041e\u0442\u043c\u0435\u043d\u0430
3":"____\u041e\u0442\u043c\u0435\u043d\u0430 3_0","Latin 2":function(){
alert("Latin 2"); $(this).dialog("close")
},"\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c
1":"____\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c 1_2"}});
});
//]]>
</code>
These changes in code may partialy solve this problem:
public static function encode($valueToEncode, $cycleCheck = false, $options
= array())
{
// ... here is not changed code
//only do post-proccessing to revert back the Zend_Json_Expr if any.
if(count($javascriptExpressions) > 0) {
for($i = 0; $i < count($javascriptExpressions); $i++) {
$key = $javascriptExpressions[$i]['key'];
$value = $javascriptExpressions[$i]['value'];
$magicKey = $javascriptExpressions[$i]['magicKey'];
$magicValue = $javascriptExpressions[$i]['magicValue'];
$encodedResult = str_replace(
'"'.$magicKey.'":"'.$magicValue.'"',
'"'.$key.'":'.$value,
$encodedResult
);
}
}
return $encodedResult;
}
private static function _recursiveJsonExprFinder(&$value, array
&$javascriptExpressions, &$currentKey=null)
{
if($value instanceof Zend_Json_Expr) {
$count = count($javascriptExpressions);
$magicKey = "____Zend_Json_Expression_Index_".($count);
$magicValue = "____Zend_Json_Expression_Value_".($count);
$javascriptExpressions[] = array(
"key" => $currentKey,
"value" => $value->__toString(),
"magicKey" => $magicKey,
"magicValue" => $magicValue
);
$value = $magicValue;
$currentKey = $magicKey;
} elseif(is_array($value)) {
$keys = array_keys($value);
$value2 = array();
foreach($keys AS $k1) {
$k2 = $k1;
$value2[$k2] = self::_recursiveJsonExprFinder($value[$k1],
$javascriptExpressions, $k2);
}
$value = $value2;
} elseif(is_object($value)) {
foreach($value AS $k => $v) {
$value->$k = self::_recursiveJsonExprFinder($value->$k,
$javascriptExpressions, $k);
}
}
return $value;
}
And generated code:
<code>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
$("#dialogTest").dialog({"modal":true,"buttons":{"Отмена 3":function(){
alert("Cancel 3"); $(this).dialog("close") },"Latin 2":function(){
alert("Latin 2"); $(this).dialog("close") },"Добавить 1":function(){
alert("Add 1"); $(this).dialog("close") }}});
});
//]]>
</code>
--
View this message in context:
http://www.nabble.com/Recomendation-to-solve-problem-with-ZendJsonExprFinder-in-Zend_Json%3A%3Aencode%28%29-tp21405959p21405959.html
Sent from the Zend Framework mailing list archive at Nabble.com.