PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Savant/Savant2/Savant2_Plugin_checkbox.php

https://github.com/radicaldesigns/amp
PHP | 103 lines | 38 code | 10 blank | 55 comment | 3 complexity | beac287eef84a792812b0ea25fbdcb62 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 single checkbox <input> element.
  9. *
  10. * $Id: Savant2_Plugin_checkbox.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_checkbox extends Savant2_Plugin {
  30. /**
  31. *
  32. * Outputs a single checkbox <input> element.
  33. *
  34. * @access public
  35. *
  36. * @param string $name The HTML "name=" value for the checkbox.
  37. *
  38. * @param string $value The value of the checkbox when checked.
  39. *
  40. * @param array $checked If $value is in this array of values,
  41. * mark the checkbox as checked.
  42. *
  43. * @param array $default The value to return if the checkbox is not
  44. * checked.
  45. *
  46. * @param string|array $attr Any extra HTML attributes to place
  47. * within the checkbox element.
  48. *
  49. * @return string
  50. *
  51. */
  52. function plugin(
  53. $name,
  54. $value = '1',
  55. $checked = null,
  56. $default = null,
  57. $attr = null)
  58. {
  59. $html = '';
  60. // define the hidden default value (if any) when not checked
  61. if (! is_null($default)) {
  62. $html .= '<input type="hidden"';
  63. $html .= ' name="' . htmlspecialchars($name) . '"';
  64. $html .= ' value="' .htmlspecialchars($default) . '" />';
  65. $html .= "\n";
  66. }
  67. // start the checkbox tag with name and value
  68. $html .= '<input type="checkbox"';
  69. $html .= ' name="' . htmlspecialchars($name) . '"';
  70. $html .= ' value="' . htmlspecialchars($value) . '"';
  71. // is the checkbox checked?
  72. settype($checked, 'array');
  73. if (in_array($value, $checked)) {
  74. $html .= ' checked="checked"';
  75. }
  76. // add extra attributes
  77. if (is_array($attr)) {
  78. // add from array
  79. foreach ($attr as $key => $val) {
  80. $key = htmlspecialchars($key);
  81. $val = htmlspecialchars($val);
  82. $html .= " $key=\"$val\"";
  83. }
  84. } elseif (! is_null($attr)) {
  85. // add from scalar
  86. $html .= " $attr";
  87. }
  88. // close the checkbox tag and return
  89. $html .= ' />';
  90. return $html;
  91. }
  92. }
  93. ?>