amp /include/Savant/Savant2/Savant2_Plugin_options.php

Language PHP Lines 107
MD5 Hash ef72b8e328d0283fb4c9e4f637d22e42 Estimated Cost $680 (why?)
Repository https://github.com/radicaldesigns/amp.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
<?php

/**
* Base plugin class.
*/
require_once 'Savant2/Plugin.php';

/**
* 
* Outputs a series of HTML <option>s.
* 
* $Id: Savant2_Plugin_options.php,v 1.1 2004/10/04 01:52:23 pmjones Exp $
* 
* @author Paul M. Jones <pmjones@ciaweb.net>
* 
* @package Savant2
* 
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
*/

class Savant2_Plugin_options extends Savant2_Plugin {
	
	
	/**
	*
	* Outputs a series of HTML <option>s.
	* 
	* Outputs a series of HTML <option>s based on an associative array
	* where the key is the option value and the value is the option
	* label. You can pass a "selected" value as well to tell the
	* function which option value(s) should be marked as selected.
	* 
	* @access public
	* 
	* @param array $options An associative array of key-value pairs; the
	* key is the option value, the value is the option label.
	* 
	* @param string|array $selected A string or array that matches one
	* or more option values, to tell the function what options should be
	* marked as selected.  Defaults to an empty array.
	* 
	* @param string|array $attr Extra attributes to apply to the option
	* tag.  If a string, they are added as-is; if an array, the key is
	* the attribute name and the value is the attribute value.
	* 
	* @return string A set of HTML <option> tags.
	* 
	*/
	
	function plugin($options, $selected = array(), $attr = null,
		$labelIsValue = false)
	{
		$html = '';
		
		// force $selected to be an array.  this allows multi-selects to
		// have multiple selected options.
		settype($selected, 'array');
		settype($options, 'array');
		
		// loop through the options array
		foreach ($options as $value => $label) {
			
			// is the label being used as the value?
			if ($labelIsValue) {
				$value = $label;
			}
			
			// set the value and label in the tag
			$html .= '<option value="' . htmlspecialchars($value) . '"';
			$html .= ' label="' . htmlspecialchars($label) . '"';
			
			// is the option one of the selected values?
			if (in_array($value, $selected)) {
				$html .= ' selected="selected"';
			}
			
			// are we adding extra attributes?
			if (is_array($attr)) {
				// yes, from an array
				foreach ($attr as $key => $val) {
					$val = htmlspecialchars($val);
					$html .= " $key=\"$val\"";
				}
			} elseif (! is_null($attr)) {
				// yes, from a string
				$html .= ' ' . $attr;
			}
			
			// add the label and close the tag
			$html .= '>' . htmlspecialchars($label) . "</option>\n";
		}
		
		return $html;
	}
}
?>
Back to Top