PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Savant/Savant2/Savant2_Plugin_radios.php

https://github.com/radicaldesigns/amp
PHP | 130 lines | 49 code | 16 blank | 65 comment | 5 complexity | acd52d690cd8ea05137efc2f3913643b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * Base plugin class.
  4. */
  5. require_once 'Savant2/Plugin.php';
  6. /**
  7. *
  8. * Outputs a set of radio <input>s with the same name.
  9. *
  10. * $Id: Savant2_Plugin_radios.php,v 1.1 2004/10/04 01:52:23 pmjones Exp $
  11. *
  12. * @author Paul M. Jones <pmjones@ciaweb.net>
  13. *
  14. * @package Savant2
  15. *
  16. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU Lesser General Public License as
  20. * published by the Free Software Foundation; either version 2.1 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful, but
  24. * WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. */
  29. class Savant2_Plugin_radios extends Savant2_Plugin {
  30. /**
  31. *
  32. * Outputs a set of radio <input>s with the same name.
  33. *
  34. * @access public
  35. *
  36. * @param string $name The HTML "name=" value of all the radio <input>s.
  37. *
  38. * @param array $radios An array of key-value pairs where the key is the
  39. * radio button value and the value is the radio button label.
  40. *
  41. * @param string $checked A comparison string; if any of the $option
  42. * element values and $checked are the same, that radio button will
  43. * be marked as "checked" (otherwise not).
  44. *
  45. * @param array $default The value to return if no radio buttons are
  46. * checked.
  47. *
  48. * @param string|array $attr Any extra HTML attributes to place
  49. * within the checkbox element.
  50. *
  51. * @param string $sep The HTML text to place between every radio
  52. * button in the set.
  53. *
  54. * @return string
  55. *
  56. */
  57. function plugin(
  58. $name,
  59. $radios,
  60. $checked = null,
  61. $default = null,
  62. $sep = "<br />\n",
  63. $attr = null,
  64. $labelIsValue = false
  65. )
  66. {
  67. settype($radios, 'array');
  68. $html = '';
  69. // define the hidden default value (if any) when no buttons are checked
  70. if (! is_null($default)) {
  71. $html .= '<input type="hidden"';
  72. $html .= ' name="' . htmlspecialchars($name) . '"';
  73. $html .= ' value="' . htmlspecialchars($default) . '" />';
  74. $html .= "\n";
  75. }
  76. // the array of individual radio buttons
  77. $radio = array();
  78. // build the full set of radio buttons
  79. foreach ($radios as $value => $label) {
  80. // reset to blank HTML for this radio button
  81. $tmp = '';
  82. // is the label being used as the value?
  83. if ($labelIsValue) {
  84. $value = $label;
  85. }
  86. // start the radio button tag
  87. $tmp .= '<input type="radio"';
  88. $tmp .= ' name="' . htmlspecialchars($name) . '"';
  89. $tmp .= ' value="' . htmlspecialchars($value) . '"';
  90. // is the radio button selected?
  91. if ($value == $checked) {
  92. $tmp .= ' checked="checked"';
  93. }
  94. // add extra attributes
  95. if (is_array($attr)) {
  96. // add from array
  97. foreach ($attr as $key => $val) {
  98. $key = htmlspecialchars($key);
  99. $val = htmlspecialchars($val);
  100. $tmp .= " $key=\"$val\"";
  101. }
  102. } elseif (! is_null($attr)) {
  103. // add from scalar
  104. $tmp .= " $attr";
  105. }
  106. // add the label and save the button in the array
  107. $tmp .= ' />' . htmlspecialchars($label);
  108. $radio[] = $tmp;
  109. }
  110. // return the radio buttons with separators
  111. return $html . implode($sep, $radio);
  112. }
  113. }
  114. ?>