Moved the phpVarToJavascript function into our FormatHelper. It isn't really the ideal place for it, but I don't want to create a new helper (at least, not yet)

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@121 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-14 20:36:38 +00:00
parent 404323c6c1
commit cdec4c3301
2 changed files with 88 additions and 86 deletions

View File

@@ -23,75 +23,6 @@ $javascript->link('jqGrid/jquery.jqGrid.js', false);
$javascript->link('jqGrid/js/jqModal', false);
$javascript->link('jqGrid/js/jqDnR', false);
// Helper function to convert PHP vars to javascript
function phpVarToJavascript($var, $name = '', $depth='', $special = false) {
// Establish a prefix to use before printing $var
$prefix = $depth;
// If given a name, set it up JS style
if ($name)
$prefix .= $name . ": ";
if (!isset($var))
return $prefix . 'null';
if (is_bool($var))
return $prefix . ($var ? "true" : "false");
if (is_numeric($var))
return $prefix . $var;
if (is_string($var)) {
// Check to see if this is a special
if ($special)
return $prefix . $var;
// OK, must just be a string after all
return $prefix . "'$var'";
}
// The only thing left that we know how to dump
// is an array. Die if we have anything else.
if (!is_array($var)) {
die;
return null;
}
// Keep a lookout for special cases, flagged by '--special'
// eg: array('name' => array('--special' => array('a' => 'one', 'b' => 'two')))
// GIVES: name: [ 'one', 'two' ]
// NOT: name: { --special: { a: 'one', b: 'two' } }
// eg: array('name' => array('--special' => 'varname'))
// GIVES: name: varname
// NOT: name: { --special: 'varname' }
if (isset($var['--special']) && count($var) == 1)
return phpVarToJavascript($var['--special'], $name, $depth, true);
// PHP array indices can be a mix of integer and string based.
// Just guess here, unless flagged as a special case.
if (isset($var[0]) || $special)
return ($prefix . "[\n"
. implode(",\n",
array_map('phpVarToJavascript',
array_values($var),
array(),
array_fill(0, count($var), $depth.' ')
))
. "\n$depth]");
return ($prefix . "{\n"
. implode(",\n",
array_map('phpVarToJavascript',
array_values($var), array_keys($var),
array_fill(0, count($var), $depth.' ')
))
. "\n$depth}");
}
// Define the URL to fetch data from.
// To prevent having to keep the controller and the view
@@ -181,23 +112,22 @@ jQuery(document).ready(function(){
jQuery('#<?php echo $gridId; ?>').jqGrid(
<?php
echo phpVarToJavascript(array('mtype' => 'GET',
'datatype' => 'xml',
'url' => $url,
'postData' => $postData,
'colNames' => array_keys($jqGridColumns),
'colModel' => array('--special' => $jqGridColumns),
/* 'sortname' => 'id', */
/* 'sortorder' => 'ASC', */
'height' => $height,
'rowNum' => $limit,
'rowList' => $limitOptions,
'caption' => $caption,
'imgpath' => $imgpath,
'viewrecords' => true,
'pager' => array('--special' => "jQuery('#{$gridId}-pager')"),
//'toolbar' => array(true,"bottom"),
)); ?>
echo FormatHelper::phpVarToJavascript
(array('mtype' => 'GET',
'datatype' => 'xml',
'url' => $url,
'postData' => $postData,
'colNames' => array_keys($jqGridColumns),
'colModel' => array('--special' => $jqGridColumns),
'height' => $height,
'rowNum' => $limit,
'rowList' => $limitOptions,
'caption' => $caption,
'imgpath' => $imgpath,
'viewrecords' => true,
'pager' => array('--special' => "jQuery('#{$gridId}-pager')"),
//'toolbar' => array(true,"bottom"),
)); ?>
);
/* jQuery('#t_<?php echo $gridId; ?>').height(25).hide() */

View File

@@ -185,4 +185,76 @@ class FormatHelper extends AppHelper {
/* } */
/* echo('</TABLE>' . "\n"); */
// Helper function to convert PHP vars to javascript
function phpVarToJavascript($var, $name = '', $depth='', $special = false) {
// Establish a prefix to use before printing $var
$prefix = $depth;
// If given a name, set it up JS style
if ($name)
$prefix .= $name . ": ";
if (!isset($var))
return $prefix . 'null';
if (is_bool($var))
return $prefix . ($var ? "true" : "false");
if (is_numeric($var))
return $prefix . $var;
if (is_string($var)) {
// Check to see if this is a special
if ($special)
return $prefix . $var;
// OK, must just be a string after all
return $prefix . "'$var'";
}
// The only thing left that we know how to dump
// is an array. Die if we have anything else.
if (!is_array($var)) {
die;
return null;
}
// Keep a lookout for special cases, flagged by '--special'
// eg: array('name' => array('--special' => array('a' => 'one', 'b' => 'two')))
// GIVES: name: [ 'one', 'two' ]
// NOT: name: { --special: { a: 'one', b: 'two' } }
// eg: array('name' => array('--special' => 'varname'))
// GIVES: name: varname
// NOT: name: { --special: 'varname' }
if (isset($var['--special']) && count($var) == 1)
return self::phpVarToJavascript($var['--special'], $name, $depth, true);
// PHP array indices can be a mix of integer and string based.
// Just guess here, unless flagged as a special case.
if (isset($var[0]) || $special)
return ($prefix . "[\n"
. implode(",\n",
array_map(array('FormatHelper', 'phpVarToJavascript'),
array_values($var),
array(),
array_fill(0, count($var), $depth.' ')
))
. "\n$depth]");
return ($prefix . "{\n"
. implode(",\n",
array_map(array('FormatHelper', 'phpVarToJavascript'),
array_values($var), array_keys($var),
array_fill(0, count($var), $depth.' ')
))
. "\n$depth}");
}
}