r1329: Our new product selection system is interesting, but looks kinda unhelpful...
[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 Selection
30 *
31 * @return string A large blob of <select> HTML
32 */
33 function ConstructProductSelect($select = null)
34 {
35 global $bugsys;
36
37 $output = '';
38 $build = '';
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 $build .= ConstructVersionSelect($productid, $select);
51
52 // these are components
53 foreach ($products["$productid"] AS $componentid => $component)
54 {
55 $build .= ConstructOptionGroup($component['title'], ConstructVersionSelect($componentid, $select));
56 }
57
58 $output .= ConstructOptionGroup($product['title'], $build);
59 $build = '';
60 }
61
62 return $output;
63 }
64
65 // ###################################################################
66 /**
67 * Constructs a string of HTML <option>s for a given product ID. This
68 * will always include global versions and inherited versions (if the
69 * passed ID is that of a component).
70 *
71 * @param integer Product ID
72 * @param string Selection
73 *
74 * @return string Constructed <option> HTML
75 */
76 function ConstructVersionSelect($productid, $select)
77 {
78 global $bugsys;
79
80 $product = $bugsys->datastore['product']["$productid"];
81 $component = null;
82
83 $build = '';
84
85 // this is a component
86 if ($product['componentmother'])
87 {
88 $component = $bugsys->datastore['product']["$productid"];
89 $product = $bugsys->datastore['product']["$component[componentmother]"];
90 }
91
92 foreach ($bugsys->datastore['version'] AS $versionid => $version)
93 {
94 if (!$version['productid'] OR $version['productid'] == $component['productid'] OR $version['productid'] == $product['productid'])
95 {
96 $value = intval($product['productid']) . ',' . intval($component['productid']) . ',' . intval($versionid);
97 $label = $version['version'];
98 $selected = ($value == $select);
99 eval('$build .= "' . $bugsys->template->fetch('selectoption') . '";');
100 }
101 }
102
103 return $build;
104 }
105
106 // ###################################################################
107 /**
108 * Constructs an <optgroup> block from a label and a string of
109 * HTML <option> elements.
110 *
111 * @param string Label for this <optgroup>
112 * @param string HTML bits
113 *
114 * @return string Composed HTML
115 */
116 function ConstructOptionGroup($glabel, $optbits)
117 {
118 global $bugsys;
119 eval('$HTML = "' . $bugsys->template->fetch('selectoptgroup') . '";');
120 return $HTML;
121 }
122
123 // #################### Start construct_pcv_select ###################
124 // constructs a product/component/version select with one go :-)
125 // NB: need to make sure we have the option to turn off just p/c selection without v
126 function construct_pcv_select($action = 'canviewbugs', $select = '', $prefix = '--')
127 {
128 global $bugsys;
129 static $HTML;
130
131 if ($HTML)
132 {
133 return $HTML;
134 }
135
136 foreach ($bugsys->datastore['product'] AS $product)
137 {
138 if ($product['componentmother'])
139 {
140 $components["$product[componentmother]"]["$product[productid]"] = $product;
141 }
142 else
143 {
144 $products["$product[productid]"] = $product;
145 }
146 }
147
148 foreach ($bugsys->datastore['version'] AS $version)
149 {
150 $versions["$version[productid]"]["$version[versionid]"] = $version;
151 }
152
153 foreach ($products AS $product)
154 {
155 if (!can_perform($action, $product['productid']) OR !can_perform('canviewbugs', $product['productid']))
156 {
157 continue;
158 }
159
160 // set options
161 $OPTIONS = '';
162
163 // prefix
164 $valuepfx = "p$product[productid]";
165
166 // construct global options
167 $OPTIONS .= construct_pcv_select_global_version($product['productid'], 0, $versions, 0, $prefix, $select);
168
169 // any immediate versions
170 if (is_array($versions["$product[productid]"]))
171 {
172 foreach ($versions["$product[productid]"] AS $version)
173 {
174 $OPTIONS .= construct_option($version['version'], "{$valuepfx}c0v$version[versionid]", $select, 0, $prefix);
175 }
176 }
177 else
178 {
179 $OPTIONS = '<option disabled="disabled"></option>';
180 }
181
182 // components (can't be a nested <optgroup> - it's not valid)
183 if (is_array($components["$product[productid]"]))
184 {
185 foreach ($components["$product[productid]"] AS $component)
186 {
187 // set options2
188 $OPTIONS2 = '';
189
190 // prefix
191 $valuepfx = "p$product[productid]c$component[productid]";
192
193 // construct global options
194 $OPTIONS2 .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, 1, $prefix, $select);
195
196 // product versions
197 if (is_array($versions["$component[componentmother]"]))
198 {
199 foreach ($versions["$component[componentmother]"] AS $version)
200 {
201 $OPTIONS2 .= construct_option($version['version'], "{$valuepfx}v$version[versionid]", $select, 1, $prefix);
202 }
203 }
204
205 // versions
206 if (is_array($versions["$component[productid]"]))
207 {
208 foreach ($versions["$component[productid]"] AS $version)
209 {
210 $OPTIONS2 .= construct_option($version['version'], "{$valuepfx}v$version[versionid]", $select, 1, $prefix);
211 }
212 }
213
214 // add optgroup
215 $OPTIONS .= construct_optgroup($component['title'], 1, $OPTIONS2, $prefix);
216 }
217 }
218
219 // add it as an optgroup
220 $HTML .= construct_optgroup($product['title'], 0, $OPTIONS, $prefix);
221 }
222
223 return $HTML;
224 }
225
226 // ############ Start construct_pcv_select_global_version ############
227 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $depth, $prefix = '', $select = '')
228 {
229 global $bugsys;
230 if (is_array($versions['0']))
231 {
232 foreach ($versions['0'] AS $version)
233 {
234 $global_versions_html .= construct_option($version['version'], "p{$product}c{$component}v$version[versionid]", $select, $depth, $prefix);
235 }
236 }
237 return $global_versions_html;
238 }
239
240 // ###################### Start construct_option #####################
241 function construct_option($title, $value, $selectmatch, $depth, $prefix)
242 {
243 global $bugsys;
244
245 $selected = ($selectmatch == $value);
246
247 $label = fetch_depth_mark($depth, $prefix) . ' ' . $title;
248
249 eval('$OPTIONS = "' . $bugsys->template->fetch('selectoption') . '";');
250 return $OPTIONS;
251 }
252
253 // ##################### Start construct_optgroup ####################
254 function construct_optgroup($label, $depth, $optbits, $prefix)
255 {
256 global $bugsys;
257
258 $glabel = fetch_depth_mark($depth, $prefix) . ' ' . $label;
259
260 eval('$HTML = "' . $bugsys->template->fetch('selectoptgroup') . '";');
261 return $HTML;
262 }
263
264 // ###################### Start fetch_depth_mark #####################
265 function fetch_depth_mark($depth, $mark)
266 {
267 $string = '';
268
269 for ($i = 0; $i <= $depth; $i++)
270 {
271 $string .= $mark;
272 }
273
274 return $string;
275 }
276
277 // ###################### Start parse_pcv_select #####################
278 function parse_pcv_select($input, $validate = false)
279 {
280 global $bugsys;
281
282 $input = trim($input);
283
284 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
285 {
286 return false;
287 }
288
289 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
290 if (sizeof($trio) != 3)
291 {
292 return false;
293 }
294
295 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
296
297 if (!$validate)
298 {
299 return $return;
300 }
301 else
302 {
303 // -------------------------------------------------------------------
304 // pcv validation
305 $product = $bugsys->datastore['product'][ $pcv['product'] ];
306 if (!$product)
307 {
308 return false;
309 }
310 $version = $bugsys->datastore['version'][ $pcv['version'] ];
311 if (!$version)
312 {
313 return false;
314 }
315 // no component
316 if ($pcv['component'] == 0)
317 {
318 // not global version and version.productid != product.productid
319 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
320 {
321 return false;
322 }
323 }
324 // using a component
325 else
326 {
327 $component = $bugsys->datastore['product'][ $pcv['component'] ];
328 // component has the right mother
329 if ($component['componentmother'] == $product['productid'])
330 {
331 // version.productid != {component.productid | product.productid}
332 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
333 {
334 return false;
335 }
336 }
337 else
338 {
339 return false;
340 }
341 }
342
343 if (!can_perform('canviewbugs', $product['productid']))
344 {
345 return false;
346 }
347
348 return $pcv;
349 }
350 }
351
352
353 /*=====================================================================*\
354 || ###################################################################
355 || # $HeadURL$
356 || # $Id$
357 || ###################################################################
358 \*=====================================================================*/
359 ?>