/**
* converts the the PHP options array into a javscript array
 *
 * @param array
 * @return string javascript arry equivalent
*/
function options_for_javascript($options)
{
    $opts = array();
    foreach ($options as $key => $value) {
        if (is_array($value)) {
            $value = options_for_javascript($value);
        }
        $opts[] = $key . ":" . boolean_for_javascript($value);
    }
    sort($opts);
    return '{' . join(', ', $opts) . '}';
}
function _options_for_ajax($options)
{
    $js_options = _build_callbacks($options);
    $js_options['asynchronous'] = isset($options['type']) && $options['type'] == 'synchronous' ? false : true;
    if (isset($options['method'])) {
        $js_options['method'] = array_or_string_for_javascript($options['method']);
    }
    if (isset($options['position'])) {
        $js_options['insertion'] = "Insertion." . sfInflector::camelize($options['position']);
    }
    $js_options['evalScripts'] = !isset($options['script']) || $options['script'] == '0' || $options['script'] == false ? false : true;
    if (isset($options['form'])) {
        $js_options['parameters'] = 'Form.serialize(this)';
    } else {
        if (isset($options['submit'])) {
            $js_options['parameters'] = "Form.serialize(document.getElementById('{$options['submit']}'))";
        } else {
            if (isset($options['with'])) {
                $js_options['parameters'] = $options['with'];
            }
        }
    }
    return options_for_javascript($js_options);
}
 * file that was distributed with this source code.
 */
require_once __DIR__ . '/../../bootstrap/unit.php';
require_once __DIR__ . '/../../../lib/helper/TagHelper.php';
require_once __DIR__ . '/../../../lib/helper/JavascriptBaseHelper.php';
$t = new lime_test(9, new lime_output_color());
// boolean_for_javascript()
$t->diag('boolean_for_javascript()');
$t->is(boolean_for_javascript(true), 'true', 'boolean_for_javascript() makes a javascript representation of the boolean if the param is boolean');
$t->is(boolean_for_javascript(false), 'false', 'boolean_for_javascript() makes a javascript representation of the boolean if the param is boolean');
$t->is(boolean_for_javascript(1 == 0), 'false', 'boolean_for_javascript() makes a javascript representation of the boolean if the param is boolean');
$t->is(boolean_for_javascript('dummy'), 'dummy', 'boolean_for_javascript() makes a javascript representation of the boolean if the param is boolean');
//options_for_javascript()
$t->diag('options_for_javascript()');
$t->is(options_for_javascript(array("'a'" => "'b'", "'c'" => false)), "{'a':'b', 'c':false}", 'options_for_javascript() makes a javascript representation of the passed array');
$t->is(options_for_javascript(array("'a'" => array("'b'" => "'c'"))), "{'a':{'b':'c'}}", 'options_for_javascript() works with nested arrays');
//javascript_tag()
$t->diag('javascript_tag()');
$expect = <<<EOT
<script type="text/javascript">
//<![CDATA[
alert("foo");
//]]>
</script>
EOT;
$t->is(javascript_tag('alert("foo");'), $expect, 'javascript_tag() takes the content as string parameter');
//link_to_function()
$t->diag('link_to_function()');
$t->is(link_to_function('foo', 'alert(\'bar\')'), '<a href="#" onclick="alert(\'bar\'); return false;">foo</a>', 'link_to_function generates a link with onClick handler for function');
//#4152
$t->is(link_to_function('foo', 'alert(\'bar\')', array('confirm' => 'sure?')), '<a href="#" onclick="if(window.confirm(\'sure?\')){ alert(\'bar\');}; return false;">foo</a>', 'link_to_function works fine with confirm dialog');
Esempio n. 4
0
/**
 * Enable to use Modalbox script : http://www.wildbit.com/labs/modalbox/
 *
 * *
 * @author Gerald Estadieu <*****@*****.**>
 * @since  15 Apr 2007
 *
 */
function m_link_to($name, $url, $html_options = array(), $modal_options = array())
{
    use_helper('Javascript');
    if (!isset($html_options['title'])) {
        $html_options['title'] = $name;
    }
    $modal_options = array_merge($modal_options, array('title' => 'this.title'));
    $params_to_escape = sfConfig::get('app_params_to_escape_list');
    // escape strings for js
    foreach ($modal_options as $option => $value) {
        if (in_array($option, $params_to_escape)) {
            $modal_options[$option] = "'" . $value . "'";
        }
    }
    $js_options = options_for_javascript($modal_options);
    $html_options['onclick'] = "Modalbox.show(this.href, " . $js_options . "); return false;";
    return link_to($name, $url, $html_options);
}