Changeset 24499
- Timestamp:
- 11/28/09 15:12:02 (3 years ago)
- Files:
-
- branches/1.2/lib/helper/JavascriptBaseHelper.php (modified) (1 diff)
- branches/1.3/lib/helper/JavascriptBaseHelper.php (modified) (1 diff)
- branches/1.4/lib/helper/JavascriptBaseHelper.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.2/lib/helper/JavascriptBaseHelper.php
r20870 r24499 24 24 /* 25 25 * Provides a set basic of helpers for calling JavaScript functions. 26 *27 *28 * For documentation on +javascript_include_tag+ see ActionView::Helpers::AssetTagHelper.29 26 */ 30 27 31 /** 32 * Returns a link that'll trigger a javascript function using the 33 * onclick handler and return false after the fact. 34 * 35 * Examples: 36 * <?php echo link_to_function('Greeting', "alert('Hello world!')") ?> 37 * <?php echo link_to_function(image_tag('delete'), "do_delete()", array('confirm' => 'Really?')) ?> 38 */ 39 function link_to_function($name, $function, $html_options = array()) 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']) ) 40 42 { 41 $html_options = _parse_attributes($html_options); 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;'; 42 48 43 $html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#'; 44 if ( isset($html_options['confirm']) ) 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 (!is_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)) 45 157 { 46 $confirm = escape_javascript($html_options['confirm']); 47 unset($html_options['confirm']); 48 $function = "if(window.confirm('$confirm')){ $function;}"; 158 $value = options_for_javascript($value); 49 159 } 50 $html_options['onclick'] = $function.'; return false;'; 160 $opts[] = $key.":".boolean_for_javascript($value); 161 } 162 sort($opts); 51 163 52 return content_tag('a', $name, $html_options); 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'); 53 179 } 54 55 /** 56 * Returns a button that'll trigger a javascript function using the 57 * onclick handler and return false after the fact. 58 * 59 * Examples: 60 * <?php echo button_to_function('Greeting', "alert('Hello world!')") ?> 61 */ 62 function button_to_function($name, $function, $html_options = array()) 63 { 64 $html_options = _parse_attributes($html_options); 65 66 $html_options['onclick'] = $function.'; return false;'; 67 $html_options['type'] = 'button'; 68 $html_options['value'] = $name; 69 70 return tag('input', $html_options); 71 } 72 73 /** 74 * Returns a JavaScript tag with the '$content' inside. If no content is passed, it works as the slot() method and will output everythin between 75 * javascript_tag() and end_javascript_tag(), 76 * Example: 77 * <?php echo javascript_tag("alert('All is good')") ?> 78 * => <script type="text/javascript">alert('All is good')</script> 79 * <?php javascript_tag() ?>alert('All is good')<?php end_javascript_tag() ?> 80 */ 81 function javascript_tag($content = null) 82 { 83 if (!is_null($content)) 84 { 85 return content_tag('script', javascript_cdata_section($content), array('type' => 'text/javascript')); 86 } 87 else 88 { 89 ob_start(); 90 } 91 } 92 93 function end_javascript_tag() 94 { 95 echo javascript_tag(ob_get_clean()); 96 } 97 98 function javascript_cdata_section($content) 99 { 100 return "\n//".cdata_section("\n$content\n//")."\n"; 101 } 102 103 /** 104 * Mark the start of a block that should only be shown in the browser if JavaScript 105 * is switched on. 106 */ 107 function if_javascript() 108 { 109 if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest()) 110 { 111 ob_start(); 112 } 113 } 114 115 /** 116 * Mark the end of a block that should only be shown in the browser if JavaScript 117 * is switched on. 118 */ 119 function end_if_javascript() 120 { 121 if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest()) 122 { 123 $content = ob_get_clean(); 124 echo javascript_tag("document.write('" . esc_js_no_entities($content) . "');"); 125 } 126 } 127 128 /** 129 * converts the given PHP array or string to the corresponding javascript array or string. 130 * javascript strings need to be single quoted. 131 * 132 * @param option (typically from option array) 133 * @return string javascript string or array equivalent 134 */ 135 function array_or_string_for_javascript($option) 136 { 137 if (is_array($option)) 138 { 139 return "['".join('\',\'', $option)."']"; 140 } 141 else if (is_string($option) && $option[0] != "'") 142 { 143 return "'$option'"; 144 } 145 return $option; 146 } 147 148 /** 149 * converts the the PHP options array into a javscript array 150 * 151 * @param array 152 * @return string javascript arry equivalent 153 */ 154 function options_for_javascript($options) 155 { 156 $opts = array(); 157 foreach ($options as $key => $value) 158 { 159 if (is_array($value)) 160 { 161 $value = options_for_javascript($value); 162 } 163 $opts[] = $key.":".boolean_for_javascript($value); 164 } 165 sort($opts); 166 167 return '{'.join(', ', $opts).'}'; 168 } 169 170 /** 171 * converts the given PHP boolean to the corresponding javascript boolean. 172 * booleans need to be true or false (php would print 1 or nothing). 173 * 174 * @param bool (typically from option array) 175 * @return string javascript boolean equivalent 176 */ 177 function boolean_for_javascript($bool) 178 { 179 if (is_bool($bool)) 180 { 181 return ($bool===true ? 'true' : 'false'); 182 } 183 return $bool; 184 } 180 return $bool; 181 } branches/1.3/lib/helper/JavascriptBaseHelper.php
r23810 r24499 24 24 /* 25 25 * Provides a set basic of helpers for calling JavaScript functions. 26 *27 *28 * For documentation on +javascript_include_tag+ see ActionView::Helpers::AssetTagHelper.29 26 */ 30 27 31 /** 32 * Returns a link that'll trigger a javascript function using the 33 * onclick handler and return false after the fact. 34 * 35 * Examples: 36 * <?php echo link_to_function('Greeting', "alert('Hello world!')") ?> 37 * <?php echo link_to_function(image_tag('delete'), "do_delete()", array('confirm' => 'Really?')) ?> 38 */ 39 function link_to_function($name, $function, $html_options = array()) 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']) ) 40 42 { 41 $html_options = _parse_attributes($html_options); 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;'; 42 48 43 $html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#'; 44 if ( isset($html_options['confirm']) ) 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)) 45 157 { 46 $confirm = escape_javascript($html_options['confirm']); 47 unset($html_options['confirm']); 48 $function = "if(window.confirm('$confirm')){ $function;}"; 158 $value = options_for_javascript($value); 49 159 } 50 $html_options['onclick'] = $function.'; return false;'; 160 $opts[] = $key.":".boolean_for_javascript($value); 161 } 162 sort($opts); 51 163 52 return content_tag('a', $name, $html_options); 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'); 53 179 } 54 55 /** 56 * Returns a button that'll trigger a javascript function using the 57 * onclick handler and return false after the fact. 58 * 59 * Examples: 60 * <?php echo button_to_function('Greeting', "alert('Hello world!')") ?> 61 */ 62 function button_to_function($name, $function, $html_options = array()) 63 { 64 $html_options = _parse_attributes($html_options); 65 66 $html_options['onclick'] = $function.'; return false;'; 67 $html_options['type'] = 'button'; 68 $html_options['value'] = $name; 69 70 return tag('input', $html_options); 71 } 72 73 /** 74 * Returns a JavaScript tag with the '$content' inside. If no content is passed, it works as the slot() method and will output everythin between 75 * javascript_tag() and end_javascript_tag(), 76 * Example: 77 * <?php echo javascript_tag("alert('All is good')") ?> 78 * => <script type="text/javascript">alert('All is good')</script> 79 * <?php javascript_tag() ?>alert('All is good')<?php end_javascript_tag() ?> 80 */ 81 function javascript_tag($content = null) 82 { 83 if (null !== $content) 84 { 85 return content_tag('script', javascript_cdata_section($content), array('type' => 'text/javascript')); 86 } 87 else 88 { 89 ob_start(); 90 } 91 } 92 93 function end_javascript_tag() 94 { 95 echo javascript_tag(ob_get_clean()); 96 } 97 98 function javascript_cdata_section($content) 99 { 100 return "\n//".cdata_section("\n$content\n//")."\n"; 101 } 102 103 /** 104 * Mark the start of a block that should only be shown in the browser if JavaScript 105 * is switched on. 106 */ 107 function if_javascript() 108 { 109 if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest()) 110 { 111 ob_start(); 112 } 113 } 114 115 /** 116 * Mark the end of a block that should only be shown in the browser if JavaScript 117 * is switched on. 118 */ 119 function end_if_javascript() 120 { 121 if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest()) 122 { 123 $content = ob_get_clean(); 124 echo javascript_tag("document.write('" . esc_js_no_entities($content) . "');"); 125 } 126 } 127 128 /** 129 * converts the given PHP array or string to the corresponding javascript array or string. 130 * javascript strings need to be single quoted. 131 * 132 * @param option (typically from option array) 133 * @return string javascript string or array equivalent 134 */ 135 function array_or_string_for_javascript($option) 136 { 137 if (is_array($option)) 138 { 139 return "['".join('\',\'', $option)."']"; 140 } 141 else if (is_string($option) && $option[0] != "'") 142 { 143 return "'$option'"; 144 } 145 return $option; 146 } 147 148 /** 149 * converts the the PHP options array into a javscript array 150 * 151 * @param array 152 * @return string javascript arry equivalent 153 */ 154 function options_for_javascript($options) 155 { 156 $opts = array(); 157 foreach ($options as $key => $value) 158 { 159 if (is_array($value)) 160 { 161 $value = options_for_javascript($value); 162 } 163 $opts[] = $key.":".boolean_for_javascript($value); 164 } 165 sort($opts); 166 167 return '{'.join(', ', $opts).'}'; 168 } 169 170 /** 171 * converts the given PHP boolean to the corresponding javascript boolean. 172 * booleans need to be true or false (php would print 1 or nothing). 173 * 174 * @param bool (typically from option array) 175 * @return string javascript boolean equivalent 176 */ 177 function boolean_for_javascript($bool) 178 { 179 if (is_bool($bool)) 180 { 181 return ($bool===true ? 'true' : 'false'); 182 } 183 return $bool; 184 } 180 return $bool; 181 } branches/1.4/lib/helper/JavascriptBaseHelper.php
r23810 r24499 24 24 /* 25 25 * Provides a set basic of helpers for calling JavaScript functions. 26 *27 *28 * For documentation on +javascript_include_tag+ see ActionView::Helpers::AssetTagHelper.29 26 */ 30 27 31 /** 32 * Returns a link that'll trigger a javascript function using the 33 * onclick handler and return false after the fact. 34 * 35 * Examples: 36 * <?php echo link_to_function('Greeting', "alert('Hello world!')") ?> 37 * <?php echo link_to_function(image_tag('delete'), "do_delete()", array('confirm' => 'Really?')) ?> 38 */ 39 function link_to_function($name, $function, $html_options = array()) 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']) ) 40 42 { 41 $html_options = _parse_attributes($html_options); 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;'; 42 48 43 $html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#'; 44 if ( isset($html_options['confirm']) ) 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)) 45 157 { 46 $confirm = escape_javascript($html_options['confirm']); 47 unset($html_options['confirm']); 48 $function = "if(window.confirm('$confirm')){ $function;}"; 158 $value = options_for_javascript($value); 49 159 } 50 $html_options['onclick'] = $function.'; return false;'; 160 $opts[] = $key.":".boolean_for_javascript($value); 161 } 162 sort($opts); 51 163 52 return content_tag('a', $name, $html_options); 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'); 53 179 } 54 55 /** 56 * Returns a button that'll trigger a javascript function using the 57 * onclick handler and return false after the fact. 58 * 59 * Examples: 60 * <?php echo button_to_function('Greeting', "alert('Hello world!')") ?> 61 */ 62 function button_to_function($name, $function, $html_options = array()) 63 { 64 $html_options = _parse_attributes($html_options); 65 66 $html_options['onclick'] = $function.'; return false;'; 67 $html_options['type'] = 'button'; 68 $html_options['value'] = $name; 69 70 return tag('input', $html_options); 71 } 72 73 /** 74 * Returns a JavaScript tag with the '$content' inside. If no content is passed, it works as the slot() method and will output everythin between 75 * javascript_tag() and end_javascript_tag(), 76 * Example: 77 * <?php echo javascript_tag("alert('All is good')") ?> 78 * => <script type="text/javascript">alert('All is good')</script> 79 * <?php javascript_tag() ?>alert('All is good')<?php end_javascript_tag() ?> 80 */ 81 function javascript_tag($content = null) 82 { 83 if (null !== $content) 84 { 85 return content_tag('script', javascript_cdata_section($content), array('type' => 'text/javascript')); 86 } 87 else 88 { 89 ob_start(); 90 } 91 } 92 93 function end_javascript_tag() 94 { 95 echo javascript_tag(ob_get_clean()); 96 } 97 98 function javascript_cdata_section($content) 99 { 100 return "\n//".cdata_section("\n$content\n//")."\n"; 101 } 102 103 /** 104 * Mark the start of a block that should only be shown in the browser if JavaScript 105 * is switched on. 106 */ 107 function if_javascript() 108 { 109 if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest()) 110 { 111 ob_start(); 112 } 113 } 114 115 /** 116 * Mark the end of a block that should only be shown in the browser if JavaScript 117 * is switched on. 118 */ 119 function end_if_javascript() 120 { 121 if (!sfContext::getInstance()->getRequest()->isXmlHttpRequest()) 122 { 123 $content = ob_get_clean(); 124 echo javascript_tag("document.write('" . esc_js_no_entities($content) . "');"); 125 } 126 } 127 128 /** 129 * converts the given PHP array or string to the corresponding javascript array or string. 130 * javascript strings need to be single quoted. 131 * 132 * @param option (typically from option array) 133 * @return string javascript string or array equivalent 134 */ 135 function array_or_string_for_javascript($option) 136 { 137 if (is_array($option)) 138 { 139 return "['".join('\',\'', $option)."']"; 140 } 141 else if (is_string($option) && $option[0] != "'") 142 { 143 return "'$option'"; 144 } 145 return $option; 146 } 147 148 /** 149 * converts the the PHP options array into a javscript array 150 * 151 * @param array 152 * @return string javascript arry equivalent 153 */ 154 function options_for_javascript($options) 155 { 156 $opts = array(); 157 foreach ($options as $key => $value) 158 { 159 if (is_array($value)) 160 { 161 $value = options_for_javascript($value); 162 } 163 $opts[] = $key.":".boolean_for_javascript($value); 164 } 165 sort($opts); 166 167 return '{'.join(', ', $opts).'}'; 168 } 169 170 /** 171 * converts the given PHP boolean to the corresponding javascript boolean. 172 * booleans need to be true or false (php would print 1 or nothing). 173 * 174 * @param bool (typically from option array) 175 * @return string javascript boolean equivalent 176 */ 177 function boolean_for_javascript($bool) 178 { 179 if (is_bool($bool)) 180 { 181 return ($bool===true ? 'true' : 'false'); 182 } 183 return $bool; 184 } 180 return $bool; 181 }