JavascriptBaseHelper.php 5.3 KB

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