SHtml.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * Description of SHtml
  8. *
  9. * @author lordovol
  10. */
  11. class SHtml extends CHtml {
  12. /**
  13. * Generates a button.
  14. * @param string the button label
  15. * @param array additional HTML attributes. Besides normal HTML attributes, a few special
  16. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  17. * @return string the generated button tag
  18. * @see clientChange
  19. */
  20. public static function button($label='button', $htmlOptions=array()) {
  21. if (!isset($htmlOptions['name']))
  22. $htmlOptions['name'] = self::ID_PREFIX . self::$count++;
  23. if (!isset($htmlOptions['type']))
  24. $htmlOptions['type'] = 'button';
  25. if (!isset($htmlOptions['value']))
  26. $htmlOptions['value'] = $label;
  27. $htmlOptions['live']=false;
  28. self::clientChange('click', $htmlOptions);
  29. return self::tag('input', $htmlOptions);
  30. }
  31. /**
  32. * Generates a push button that can submit the current form in POST method.
  33. * @param string the button label
  34. * @param mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
  35. * @param array AJAX options (see {@link ajax})
  36. * @param array additional HTML attributes. Besides normal HTML attributes, a few special
  37. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  38. * @return string the generated button
  39. */
  40. public static function ajaxSubmitButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())
  41. {
  42. $ajaxOptions['type']='POST';
  43. $htmlOptions['live']=false;
  44. return self::ajaxButton($label,$url,$ajaxOptions,$htmlOptions);
  45. }
  46. /**
  47. * Generates a link that can initiate AJAX requests.
  48. * @param string the link body (it will NOT be HTML-encoded.)
  49. * @param mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
  50. * @param array AJAX options (see {@link ajax})
  51. * @param array additional HTML attributes. Besides normal HTML attributes, a few special
  52. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  53. * @return string the generated link
  54. * @see normalizeUrl
  55. * @see ajax
  56. */
  57. public static function ajaxLink($text, $url, $ajaxOptions=array(), $htmlOptions=array()) {
  58. if (isset($url['id'])) {
  59. $url['id'] = urlencode($url['id']);
  60. }
  61. if (!isset($htmlOptions['href']))
  62. $htmlOptions['href'] = '#';
  63. $ajaxOptions['url'] = $url;
  64. $htmlOptions['ajax'] = $ajaxOptions;
  65. $htmlOptions['live']=false;
  66. self::clientChange('click', $htmlOptions);
  67. return self::tag('a', $htmlOptions, $text);
  68. }
  69. /**
  70. * Generates a push button that can initiate AJAX requests.
  71. * @param string the button label
  72. * @param mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
  73. * @param array AJAX options (see {@link ajax})
  74. * @param array additional HTML attributes. Besides normal HTML attributes, a few special
  75. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  76. * @return string the generated button
  77. */
  78. public static function ajaxButton($label, $url, $ajaxOptions=array(), $htmlOptions=array()) {
  79. $ajaxOptions['url'] = $url;
  80. $htmlOptions['ajax'] = $ajaxOptions;
  81. $htmlOptions['live']=false;
  82. return self::button($label, $htmlOptions);
  83. }
  84. /**
  85. * Generates the JavaScript with the specified client changes.
  86. * @param string event name (without 'on')
  87. * @param array HTML attributes which may contain the following special attributes
  88. * specifying the client change behaviors:
  89. * <ul>
  90. * <li>submit: string, specifies the URL that the button should submit to. If empty, the current requested URL will be used.</li>
  91. * <li>params: array, name-value pairs that should be submitted together with the form. This is only used when 'submit' option is specified.</li>
  92. * <li>csrf: boolean, whether a CSRF token should be submitted when {@link CHttpRequest::enableCsrfValidation} is true. Defaults to false.
  93. * This option has been available since version 1.0.7. You may want to set this to be true if there is no enclosing
  94. * form around this element. This option is meaningful only when 'submit' option is set.</li>
  95. * <li>return: boolean, the return value of the javascript. Defaults to false, meaning that the execution of
  96. * javascript would not cause the default behavior of the event. This option has been available since version 1.0.2.</li>
  97. * <li>confirm: string, specifies the message that should show in a pop-up confirmation dialog.</li>
  98. * <li>ajax: array, specifies the AJAX options (see {@link ajax}).</li>
  99. * </ul>
  100. * @param boolean whether the event should be "live" (a jquery event concept). Defaults to true.
  101. * This parameter has been available since version 1.1.1.
  102. */
  103. protected static function clientChange($event,&$htmlOptions){
  104. $htmlOptions['live']=false;
  105. parent::clientChange($event, $htmlOptions);
  106. }
  107. }
  108. ?>