PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Savant/Savant2/Savant2_Plugin_options.php

https://github.com/radicaldesigns/amp
PHP | 107 lines | 32 code | 14 blank | 61 comment | 3 complexity | ef72b8e328d0283fb4c9e4f637d22e42 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 series of HTML <option>s.
  9. *
  10. * $Id: Savant2_Plugin_options.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_options extends Savant2_Plugin {
  30. /**
  31. *
  32. * Outputs a series of HTML <option>s.
  33. *
  34. * Outputs a series of HTML <option>s based on an associative array
  35. * where the key is the option value and the value is the option
  36. * label. You can pass a "selected" value as well to tell the
  37. * function which option value(s) should be marked as selected.
  38. *
  39. * @access public
  40. *
  41. * @param array $options An associative array of key-value pairs; the
  42. * key is the option value, the value is the option label.
  43. *
  44. * @param string|array $selected A string or array that matches one
  45. * or more option values, to tell the function what options should be
  46. * marked as selected. Defaults to an empty array.
  47. *
  48. * @param string|array $attr Extra attributes to apply to the option
  49. * tag. If a string, they are added as-is; if an array, the key is
  50. * the attribute name and the value is the attribute value.
  51. *
  52. * @return string A set of HTML <option> tags.
  53. *
  54. */
  55. function plugin($options, $selected = array(), $attr = null,
  56. $labelIsValue = false)
  57. {
  58. $html = '';
  59. // force $selected to be an array. this allows multi-selects to
  60. // have multiple selected options.
  61. settype($selected, 'array');
  62. settype($options, 'array');
  63. // loop through the options array
  64. foreach ($options as $value => $label) {
  65. // is the label being used as the value?
  66. if ($labelIsValue) {
  67. $value = $label;
  68. }
  69. // set the value and label in the tag
  70. $html .= '<option value="' . htmlspecialchars($value) . '"';
  71. $html .= ' label="' . htmlspecialchars($label) . '"';
  72. // is the option one of the selected values?
  73. if (in_array($value, $selected)) {
  74. $html .= ' selected="selected"';
  75. }
  76. // are we adding extra attributes?
  77. if (is_array($attr)) {
  78. // yes, from an array
  79. foreach ($attr as $key => $val) {
  80. $val = htmlspecialchars($val);
  81. $html .= " $key=\"$val\"";
  82. }
  83. } elseif (! is_null($attr)) {
  84. // yes, from a string
  85. $html .= ' ' . $attr;
  86. }
  87. // add the label and close the tag
  88. $html .= '>' . htmlspecialchars($label) . "</option>\n";
  89. }
  90. return $html;
  91. }
  92. }
  93. ?>