r1367: Set the default value for obsoletes to be no
[bugdar.git] / includes / functions_product.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 foreach ($bugsys->datastore['component'] AS $id => $prod)
44 {
45 $components["$prod[parentid]"]["$id"] = $prod;
46 }
47
48 // these are products
49 foreach ($bugsys->datastore['product'] AS $productid => $product)
50 {
51 if (!can_perform($action, $product['productid']) OR !can_perform('canviewbugs', $product['productid']))
52 {
53 continue;
54 }
55
56 if ($versions = ConstructVersionSelect($productid, $select, $obsolete))
57 {
58 $output .= ConstructOptionGroup($product['title'], $versions);
59 }
60
61 // these are components
62 if ($components["$productid"])
63 {
64 foreach ($components["$productid"] AS $componentid => $component)
65 {
66 $output .= ConstructOptionGroup($product['title'] . '/' . $component['title'], ConstructVersionSelect($componentid, $select, $obsolete));
67 }
68 }
69 }
70
71 return $output;
72 }
73
74 // ###################################################################
75 /**
76 * Constructs a string of HTML <option>s for a given product ID. This
77 * will always include global versions and inherited versions (if the
78 * passed ID is that of a component).
79 *
80 * @param integer Product ID
81 * @param string Selection
82 * @param bool Include obsolete versions?
83 *
84 * @return string Constructed <option> HTML
85 */
86 function ConstructVersionSelect($productid, $select, $obsolete)
87 {
88 global $bugsys;
89
90 $product = $bugsys->datastore['product']["$productid"];
91 $component = null;
92
93 $build = '';
94
95 // this is a component
96 if ($product == null)
97 {
98 $component = $bugsys->datastore['component']["$productid"];
99 $product = $bugsys->datastore['product']["$component[parentid]"];
100 }
101
102 foreach ($bugsys->datastore['version'] AS $versionid => $version)
103 {
104 if ((!$version['productid'] OR $version['productid'] == $component['productid'] OR $version['productid'] == $product['productid']) AND (!$version['obsolete'] OR ($version['obsolete'] AND $obsolete)))
105 {
106 $value = intval($product['productid']) . ',' . intval($component['productid']) . ',' . intval($versionid);
107 $label = $version['version'];
108 $selected = ($value == $select);
109 eval('$build .= "' . $bugsys->template->fetch('selectoption') . '";');
110 }
111 }
112
113 return $build;
114 }
115
116 // ###################################################################
117 /**
118 * Constructs an <optgroup> block from a label and a string of
119 * HTML <option> elements.
120 *
121 * @param string Label for this <optgroup>
122 * @param string HTML bits
123 *
124 * @return string Composed HTML
125 */
126 function ConstructOptionGroup($glabel, $optbits)
127 {
128 global $bugsys;
129 eval('$HTML = "' . $bugsys->template->fetch('selectoptgroup') . '";');
130 return $HTML;
131 }
132
133 /*=====================================================================*\
134 || ###################################################################
135 || # $HeadURL$
136 || # $Id$
137 || ###################################################################
138 \*=====================================================================*/
139 ?>