Fix a bug in ConstructVersionSelect() that would cause all the versions to be selected
[bugdar.git] / includes / functions_product.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 // ###################################################################
23 /**
24 * Constructs a massive <select> form element to create non-valid XHTML
25 * for product selection. We have to use nested <optgroup>s (invalid)
26 * because certain browsers (Safari) do not support <option disabled>,
27 * which is how I want to implement components.
28 *
29 * @param string Permission action to verify against for a product
30 * @param string Selection
31 * @param bool Include obsolete versions?
32 *
33 * @return string A large blob of <select> HTML
34 */
35 function ConstructProductSelect($action = 'canviewbugs', $select = null, $obsolete = true)
36 {
37 global $bugsys;
38
39 $output = '';
40
41 // index all of the components by parent and ID
42 $components = array();
43 if (is_array(bugdar::$datastore['component']))
44 {
45 foreach (bugdar::$datastore['component'] AS $id => $prod)
46 {
47 $components["$prod[parentid]"]["$id"] = $prod;
48 }
49 }
50
51 // these are products
52 foreach (bugdar::$datastore['product'] AS $productid => $product)
53 {
54 if (!can_perform($action, $product['productid']) OR !can_perform('canviewbugs', $product['productid']))
55 {
56 continue;
57 }
58
59 if ($versions = ConstructVersionSelect($productid, $select, $obsolete))
60 {
61 $output .= ConstructOptionGroup($product['title'], $versions);
62 }
63
64 // these are components
65 if ($components["$productid"])
66 {
67 foreach ($components["$productid"] AS $componentid => $component)
68 {
69 $output .= ConstructOptionGroup($product['title'] . '/' . $component['title'], ConstructVersionSelect($componentid, $select, $obsolete));
70 }
71 }
72 }
73
74 return $output;
75 }
76
77 // ###################################################################
78 /**
79 * Constructs a string of HTML <option>s for a given product ID. This
80 * will always include global versions and inherited versions (if the
81 * passed ID is that of a component).
82 *
83 * @param integer Product ID
84 * @param string Selection
85 * @param bool Include obsolete versions?
86 *
87 * @return string Constructed <option> HTML
88 */
89 function ConstructVersionSelect($productid, $select, $obsolete)
90 {
91 global $bugsys;
92
93 $product = bugdar::$datastore['product']["$productid"];
94 $component = null;
95
96 $build = '';
97
98 // this is a component
99 if ($product == null)
100 {
101 $component = bugdar::$datastore['component']["$productid"];
102 $product = bugdar::$datastore['product']["$component[parentid]"];
103 }
104
105 foreach (bugdar::$datastore['version'] AS $versionid => $version)
106 {
107 if ((!$version['productid'] OR $version['productid'] == $component['productid'] OR $version['productid'] == $product['productid']) AND (!$version['obsolete'] OR ($version['obsolete'] AND $obsolete)))
108 {
109 $tpl = new BSTemplate('selectoption');
110 $tpl->vars = array(
111 'value' => intval($product['productid']) . ',' . intval($component['productid']) . ',' . intval($versionid),
112 'label' => $version['version']
113 );
114 $tpl->vars['selected'] = ($tpl->vars['value'] == $select);
115 $build .= $tpl->evaluate()->getTemplate();
116 }
117 }
118
119 return $build;
120 }
121
122 // ###################################################################
123 /**
124 * Constructs an <optgroup> block from a label and a string of
125 * HTML <option> elements.
126 *
127 * @param string Label for this <optgroup>
128 * @param string HTML bits
129 *
130 * @return string Composed HTML
131 */
132 function ConstructOptionGroup($glabel, $optbits)
133 {
134 $tpl = new BSTemplate('selectoptgroup');
135 $tpl->vars = array(
136 'glabel' => $glabel,
137 'optbits' => $optbits
138 );
139 return $tpl->evaluate()->getTemplate();
140 }
141
142 /*=====================================================================*\
143 || ###################################################################
144 || # $HeadURL$
145 || # $Id$
146 || ###################################################################
147 \*=====================================================================*/
148 ?>