~budgester/irm/trunk

« back to all changes in this revision

Viewing changes to lib/Savant2/Savant2_Plugin_options.php

  • Committer: budgester at budgester
  • Date: 2008-03-05 23:14:13 UTC
  • Revision ID: budgester@budgester.com-20080305231413-k5vqfuckfo09ju42
Initial import of IRM codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
* Base plugin class.
 
5
*/
 
6
require_once 'Savant2/Plugin.php';
 
7
 
 
8
/**
 
9
 
10
* Outputs a series of HTML <option>s.
 
11
 
12
* $Id: Savant2_Plugin_options.php,v 1.2 2005/08/09 22:19:39 pmjones Exp $
 
13
 
14
* @author Paul M. Jones <pmjones@ciaweb.net>
 
15
 
16
* @package Savant2
 
17
 
18
* @license LGPL http://www.gnu.org/copyleft/lesser.html
 
19
 
20
* This program is free software; you can redistribute it and/or modify
 
21
* it under the terms of the GNU Lesser General Public License as
 
22
* published by the Free Software Foundation; either version 2.1 of the
 
23
* License, or (at your option) any later version.
 
24
 
25
* This program is distributed in the hope that it will be useful, but
 
26
* WITHOUT ANY WARRANTY; without even the implied warranty of
 
27
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
28
* Lesser General Public License for more details.
 
29
 
30
*/
 
31
 
 
32
class Savant2_Plugin_options extends Savant2_Plugin {
 
33
        
 
34
        
 
35
        /**
 
36
        *
 
37
        * Outputs a series of HTML <option>s.
 
38
        * 
 
39
        * Outputs a series of HTML <option>s based on an associative array
 
40
        * where the key is the option value and the value is the option
 
41
        * label. You can pass a "selected" value as well to tell the
 
42
        * function which option value(s) should be marked as selected.
 
43
        * 
 
44
        * @access public
 
45
        * 
 
46
        * @param array $options An associative array of key-value pairs; the
 
47
        * key is the option value, the value is the option label.
 
48
        * 
 
49
        * @param string|array $selected A string or array that matches one
 
50
        * or more option values, to tell the function what options should be
 
51
        * marked as selected.  Defaults to an empty array.
 
52
        * 
 
53
        * @param string|array $attr Extra attributes to apply to the option
 
54
        * tag.  If a string, they are added as-is; if an array, the key is
 
55
        * the attribute name and the value is the attribute value.
 
56
        * 
 
57
        * @return string A set of HTML <option> tags.
 
58
        * 
 
59
        */
 
60
        
 
61
        function plugin($options, $selected = array(), $attr = null,
 
62
                $labelIsValue = false)
 
63
        {
 
64
                $html = '';
 
65
                
 
66
                // force $selected to be an array.  this allows multi-selects to
 
67
                // have multiple selected options.
 
68
                settype($selected, 'array');
 
69
                settype($options, 'array');
 
70
                
 
71
                // loop through the options array
 
72
                foreach ($options as $value => $label) {
 
73
                        
 
74
                        // is the label being used as the value?
 
75
                        if ($labelIsValue) {
 
76
                                $value = $label;
 
77
                        }
 
78
                        
 
79
                        // set the value and label in the tag
 
80
                        $html .= '<option value="' . htmlspecialchars($value) . '"';
 
81
                        $html .= ' label="' . htmlspecialchars($label) . '"';
 
82
                        
 
83
                        // is the option one of the selected values?
 
84
                        if (in_array($value, $selected)) {
 
85
                                $html .= ' selected="selected"';
 
86
                        }
 
87
                        
 
88
                        // are we adding extra attributes?
 
89
                        if (is_array($attr)) {
 
90
                                // yes, from an array
 
91
                                foreach ($attr as $key => $val) {
 
92
                                        $val = htmlspecialchars($val);
 
93
                                        $html .= " $key=\"$val\"";
 
94
                                }
 
95
                        } elseif (! is_null($attr)) {
 
96
                                // yes, from a string
 
97
                                $html .= ' ' . $attr;
 
98
                        }
 
99
                        
 
100
                        // add the label and close the tag
 
101
                        $html .= '>' . htmlspecialchars($label) . "</option>\n";
 
102
                }
 
103
                
 
104
                return $html;
 
105
        }
 
106
}
 
107
?>
 
 
b'\\ No newline at end of file'