Happy 2009! Updating copyright years.
[bugdar.git] / includes / functions_product.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2004-2009 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 * Constructs a massive <select> form element to create non-valid XHTML
24 * for product selection. We have to use nested <optgroup>s (invalid)
25 * because certain browsers (Safari) do not support <option disabled>,
26 * which is how I want to implement components.
27 *
28 * @param string Permission action to verify against for a product
29 * @param string Selection
30 * @param bool Include obsolete versions?
31 *
32 * @return string A large blob of <select> HTML
33 */
34 function construct_product_select($action = 'canviewbugs', $select = null, $obsolete = true)
35 {
36 $output = '';
37
38 // index all of the components by parent and ID
39 $components = array();
40 if (is_array(bugdar::$datastore['component']))
41 {
42 foreach (bugdar::$datastore['component'] as $id => $prod)
43 {
44 $components["$prod[parentid]"]["$id"] = $prod;
45 }
46 }
47
48 // these are products
49 foreach (bugdar::$datastore['product'] as $productid => $product)
50 {
51 if (!can_perform($action, $product['productid']) || !can_perform('canviewbugs', $product['productid']))
52 {
53 continue;
54 }
55
56 if ($versions = construct_version_select($productid, $select, $obsolete))
57 {
58 $output .= construct_option_group($product['title'], $versions);
59 }
60
61 // these are components
62 if ($components["$productid"])
63 {
64 foreach ($components["$productid"] as $componentid => $component)
65 {
66 $output .= construct_option_group($product['title'] . '/' . $component['title'], construct_version_select($componentid, $select, $obsolete));
67 }
68 }
69 }
70
71 return $output;
72 }
73
74 /**
75 * Constructs a string of HTML <option>s for a given product ID. This
76 * will always include global versions and inherited versions (if the
77 * passed ID is that of a component).
78 *
79 * @param integer Product ID
80 * @param string Selection
81 * @param bool Include obsolete versions?
82 *
83 * @return string Constructed <option> HTML
84 */
85 function construct_version_select($productid, $select, $obsolete)
86 {
87 $product = bugdar::$datastore['product']["$productid"];
88 $component = null;
89
90 $build = '';
91
92 // this is a component
93 if ($product == null)
94 {
95 $component = bugdar::$datastore['component']["$productid"];
96 $product = bugdar::$datastore['product']["$component[parentid]"];
97 }
98
99 foreach (bugdar::$datastore['version'] as $versionid => $version)
100 {
101 if ((!$version['productid'] || $version['productid'] == $component['productid'] || $version['productid'] == $product['productid']) && (!$version['obsolete'] || ($version['obsolete'] && $obsolete)))
102 {
103 $tpl = new BSTemplate('selectoption');
104 $tpl->vars = array(
105 'value' => intval($product['productid']) . ',' . intval($component['productid']) . ',' . intval($versionid),
106 'label' => $version['version']
107 );
108 $tpl->vars['selected'] = ($tpl->vars['value'] == $select);
109 $build .= $tpl->evaluate()->getTemplate();
110 }
111 }
112
113 return $build;
114 }
115
116 /**
117 * Constructs an <optgroup> block from a label and a string of
118 * HTML <option> elements.
119 *
120 * @param string Label for this <optgroup>
121 * @param string HTML bits
122 *
123 * @return string Composed HTML
124 */
125 function construct_option_group($glabel, $optbits)
126 {
127 $tpl = new BSTemplate('selectoptgroup');
128 $tpl->vars = array(
129 'glabel' => $glabel,
130 'optbits' => $optbits
131 );
132 return $tpl->evaluate()->getTemplate();
133 }
134
135 ?>