Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) 2004 David Heinemeier Hansson
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
/**
13
 * JavascriptBaseHelper.
14
 *
15
 * @package    symfony
16
 * @subpackage helper
17
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18
 * @author     John Christopher <john.christopher@symfony-project.com>
19
 * @author     David Heinemeier Hansson
20
 * @author     Fabian Lange <fabian.lange@symfony-project.com>
21
 * @version    SVN: $Id: JavascriptBaseHelper.php 24499 2009-11-28 14:12:02Z FabianLange $
22
 */
23
 
24
/*
25
 * Provides a set basic of helpers for calling JavaScript functions.
26
 */
27
 
28
/**
29
 * Returns a link that will trigger a javascript function using the
30
 * onclick handler and return false after the fact.
31
 *
32
 * Examples:
33
 *   <?php echo link_to_function('Greeting', "alert('Hello world!')") ?>
34
 *   <?php echo link_to_function(image_tag('delete'), "do_delete()", array('confirm' => 'Really?')) ?>
35
 */
36
function link_to_function($name, $function, $html_options = array())
37
{
38
  $html_options = _parse_attributes($html_options);
39
 
40
  $html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#';
41
  if ( isset($html_options['confirm']) )
42
  {
43
    $confirm = escape_javascript($html_options['confirm']);
44
    unset($html_options['confirm']);
45
    $function = "if(window.confirm('$confirm')){ $function;}";
46
  }
47
  $html_options['onclick'] = $function.'; return false;';
48
 
49
  return content_tag('a', $name, $html_options);
50
}
51
 
52
/**
53
 * Returns a button that will trigger a javascript function using the
54
 * onclick handler and return false after the fact.
55
 *
56
 * Examples:
57
 *   <?php echo button_to_function('Greeting', "alert('Hello world!')") ?>
58
 */
59
function button_to_function($name, $function, $html_options = array())
60
{
61
  $html_options = _parse_attributes($html_options);
62
 
63
  $html_options['onclick'] = $function.'; return false;';
64
  $html_options['type']    = 'button';
65
  $html_options['value']   = $name;
66
 
67
  return tag('input', $html_options);
68
}
69
 
70
/**
71
 * Returns a JavaScript tag with the '$content' inside. If no content is passed, it works as the slot() method and will output everythin between
72
 * javascript_tag() and end_javascript_tag(),
73
 * Example:
74
 *   <?php echo javascript_tag("alert('All is good')") ?>
75
 *   => <script type="text/javascript">alert('All is good')</script>
76
 *   <?php javascript_tag() ?>alert('All is good')<?php end_javascript_tag() ?>
77
 */
78
function javascript_tag($content = null)
79
{
80
  if (null !== $content)
81
  {
82
    return content_tag('script', javascript_cdata_section($content), array('type' => 'text/javascript'));
83
  }
84
  else
85
  {
86
    ob_start();
87
  }
88
}
89
 
90
function end_javascript_tag()
91
{
92
  echo javascript_tag(ob_get_clean());
93
}
94
 
95
function javascript_cdata_section($content)
96
{
97
  return "\n//".cdata_section("\n$content\n//")."\n";
98
}
99
 
100
/**
101
 * Mark the start of a block that should only be shown in the browser if JavaScript
102
 * is switched on.
103
 */
104
function if_javascript()
105
{
106
  if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest())
107
  {
108
    ob_start();
109
  }
110
}
111
 
112
/**
113
 * Mark the end of a block that should only be shown in the browser if JavaScript
114
 * is switched on.
115
 */
116
function end_if_javascript()
117
{
118
  if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest())
119
  {
120
    $content = ob_get_clean();
121
    echo javascript_tag("document.write('" . esc_js_no_entities($content) . "');");
122
  }
123
}
124
 
125
/**
126
 * converts the given PHP array or string to the corresponding javascript array or string.
127
 * javascript strings need to be single quoted.
128
 *
129
 * @param option (typically from option array)
130
 * @return string javascript string or array equivalent
131
 */
132
function array_or_string_for_javascript($option)
133
{
134
  if (is_array($option))
135
  {
136
    return "['".join('\',\'', $option)."']";
137
  }
138
  else if (is_string($option) && $option[0] != "'")
139
  {
140
    return "'$option'";
141
  }
142
  return $option;
143
}
144
 
145
/**
146
* converts the the PHP options array into a javscript array
147
 *
148
 * @param array
149
 * @return string javascript arry equivalent
150
*/
151
function options_for_javascript($options)
152
{
153
  $opts = array();
154
  foreach ($options as $key => $value)
155
  {
156
    if (is_array($value))
157
    {
158
     $value = options_for_javascript($value);
159
    }
160
    $opts[] = $key.":".boolean_for_javascript($value);
161
  }
162
  sort($opts);
163
 
164
  return '{'.join(', ', $opts).'}';
165
}
166
 
167
/**
168
 * converts the given PHP boolean to the corresponding javascript boolean.
169
 * booleans need to be true or false (php would print 1 or nothing).
170
 *
171
 * @param bool (typically from option array)
172
 * @return string javascript boolean equivalent
173
 */
174
function boolean_for_javascript($bool)
175
{
176
  if (is_bool($bool))
177
  {
178
    return ($bool===true ? 'true' : 'false');
179
  }
180
  return $bool;
181
}