r1330: - Rewrote the product selection HTML constructors
[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 *
32 * @return string A large blob of <select> HTML
33 */
34 function ConstructProductSelect($action = 'canviewbugs', $select = null)
35 {
36 global $bugsys;
37
38 $output = '';
39
40 // index all of the products by parent and ID
41 $products = array();
42 foreach ($bugsys->datastore['product'] AS $id => $prod)
43 {
44 $products["$prod[componentmother]"]["$id"] = $prod;
45 }
46
47 // these are products
48 foreach ($products['0'] AS $productid => $product)
49 {
50 if (!can_perform($action, $product['productid']) OR !can_perform('canviewbugs', $product['productid']))
51 {
52 continue;
53 }
54
55 $output .= ConstructOptionGroup($product['title'], ConstructVersionSelect($productid, $select));
56
57 // these are components
58 foreach ($products["$productid"] AS $componentid => $component)
59 {
60 $output .= ConstructOptionGroup($product['title'] . '/' . $component['title'], ConstructVersionSelect($componentid, $select));
61 }
62 }
63
64 return $output;
65 }
66
67 // ###################################################################
68 /**
69 * Constructs a string of HTML <option>s for a given product ID. This
70 * will always include global versions and inherited versions (if the
71 * passed ID is that of a component).
72 *
73 * @param integer Product ID
74 * @param string Selection
75 *
76 * @return string Constructed <option> HTML
77 */
78 function ConstructVersionSelect($productid, $select)
79 {
80 global $bugsys;
81
82 $product = $bugsys->datastore['product']["$productid"];
83 $component = null;
84
85 $build = '';
86
87 // this is a component
88 if ($product['componentmother'])
89 {
90 $component = $bugsys->datastore['product']["$productid"];
91 $product = $bugsys->datastore['product']["$component[componentmother]"];
92 }
93
94 foreach ($bugsys->datastore['version'] AS $versionid => $version)
95 {
96 if (!$version['productid'] OR $version['productid'] == $component['productid'] OR $version['productid'] == $product['productid'])
97 {
98 $value = intval($product['productid']) . ',' . intval($component['productid']) . ',' . intval($versionid);
99 $label = $version['version'];
100 $selected = ($value == $select);
101 eval('$build .= "' . $bugsys->template->fetch('selectoption') . '";');
102 }
103 }
104
105 return $build;
106 }
107
108 // ###################################################################
109 /**
110 * Constructs an <optgroup> block from a label and a string of
111 * HTML <option> elements.
112 *
113 * @param string Label for this <optgroup>
114 * @param string HTML bits
115 *
116 * @return string Composed HTML
117 */
118 function ConstructOptionGroup($glabel, $optbits)
119 {
120 global $bugsys;
121 eval('$HTML = "' . $bugsys->template->fetch('selectoptgroup') . '";');
122 return $HTML;
123 }
124
125 // ###################### Start parse_pcv_select #####################
126 function parse_pcv_select($input, $validate = false)
127 {
128 global $bugsys;
129
130 $input = trim($input);
131
132 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
133 {
134 return false;
135 }
136
137 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
138 if (sizeof($trio) != 3)
139 {
140 return false;
141 }
142
143 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
144
145 if (!$validate)
146 {
147 return $return;
148 }
149 else
150 {
151 // -------------------------------------------------------------------
152 // pcv validation
153 $product = $bugsys->datastore['product'][ $pcv['product'] ];
154 if (!$product)
155 {
156 return false;
157 }
158 $version = $bugsys->datastore['version'][ $pcv['version'] ];
159 if (!$version)
160 {
161 return false;
162 }
163 // no component
164 if ($pcv['component'] == 0)
165 {
166 // not global version and version.productid != product.productid
167 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
168 {
169 return false;
170 }
171 }
172 // using a component
173 else
174 {
175 $component = $bugsys->datastore['product'][ $pcv['component'] ];
176 // component has the right mother
177 if ($component['componentmother'] == $product['productid'])
178 {
179 // version.productid != {component.productid | product.productid}
180 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
181 {
182 return false;
183 }
184 }
185 else
186 {
187 return false;
188 }
189 }
190
191 if (!can_perform('canviewbugs', $product['productid']))
192 {
193 return false;
194 }
195
196 return $pcv;
197 }
198 }
199
200
201 /*=====================================================================*\
202 || ###################################################################
203 || # $HeadURL$
204 || # $Id$
205 || ###################################################################
206 \*=====================================================================*/
207 ?>