r519: Make the PCV selector XHTML-compliant
[bugdar.git] / includes / functions_product.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 // #################### Start construct_pcv_select ###################
14 // constructs a product/component/version select with one go :-)
15 // NB: need to make sure we have the option to turn off just p/c selection without v
16 function construct_pcv_select($select = '', $prefix = '--')
17 {
18 global $bugsys;
19 static $HTML;
20
21 if ($HTML)
22 {
23 return $HTML;
24 }
25
26
27 foreach ($bugsys->datastore['product'] AS $product)
28 {
29 if ($product['componentmother'])
30 {
31 $components["$product[componentmother]"]["$product[productid]"] = $product;
32 }
33 else
34 {
35 $products["$product[productid]"] = $product;
36 }
37 }
38
39 foreach ($bugsys->datastore['version'] AS $version)
40 {
41 $versions["$version[productid]"]["$version[versionid]"] = $version;
42 }
43
44 foreach ($products AS $product)
45 {
46 // prefix
47 $valuepfx = "p$product[productid]";
48
49 // construct global options
50 $OPTIONS .= construct_pcv_select_global_version($product['productid'], 0, $versions, 0, $prefix, $select);
51
52 // any immediate versions
53 if (is_array($versions["$product[productid]"]))
54 {
55 foreach ($versions["$product[productid]"] AS $version)
56 {
57 $OPTIONS .= construct_option($version['version'], "{$valuepfx}c0v$version[versionid]", $select, 0, $prefix);
58 }
59 }
60
61 // add it as an optgroup
62 $HTML .= construct_optgroup($product['title'], 0, $OPTIONS, $prefix);
63 $OPTIONS = '';
64
65 // components (can't be a nested <optgroup> - it's not valid)
66 if (is_array($components["$product[productid]"]))
67 {
68 foreach ($components["$product[productid]"] AS $component)
69 {
70 // prefix
71 $valuepfx .= "c$component[productid]";
72
73 // construct global options
74 $OPTIONS .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, 1, $prefix, $select);
75
76 // versions
77 if (is_array($versions["$component[productid]"]))
78 {
79 foreach ($versions["$component[productid]"] AS $version)
80 {
81 $OPTIONS .= construct_option($version['version'], "{$valuepfx}v$version[versionid]", $select, 1, $prefix);
82 }
83 }
84
85 // add optgroup
86 $HTML .= construct_optgroup($component['title'], 1, $OPTIONS, $prefix);
87 $OPTIONS = '';
88 }
89 }
90 }
91
92 return $HTML;
93 }
94
95 // ############ Start construct_pcv_select_global_version ############
96 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $depth, $prefix = '', $select = '')
97 {
98 global $bugsys;
99 if (is_array($versions['0']))
100 {
101 foreach ($versions['0'] AS $version)
102 {
103 $global_versions_html .= construct_option($version['version'], "p{$product}c{$component}v$version[versionid]", $select, $depth, $prefix);
104 }
105 }
106 return $global_versions_html;
107 }
108
109 // ###################### Start construct_option #####################
110 function construct_option($title, $value, $selectmatch, $depth, $prefix)
111 {
112 global $bugsys;
113
114 $selected = ($selectmatch == $value);
115
116 $label = fetch_depth_mark($depth, $prefix) . ' ' . $title;
117
118 eval('$OPTIONS = "' . $bugsys->template->fetch('selectoption') . '";');
119 return $OPTIONS;
120 }
121
122 // ##################### Start construct_optgroup ####################
123 function construct_optgroup($label, $depth, $optbits, $prefix)
124 {
125 global $bugsys;
126
127 $glabel = fetch_depth_mark($depth, $prefix) . ' ' . $label;
128
129 eval('$HTML = "' . $bugsys->template->fetch('selectoptgroup') . '";');
130 return $HTML;
131 }
132
133 // ###################### Start fetch_depth_mark #####################
134 function fetch_depth_mark($depth, $mark)
135 {
136 $string = '';
137
138 for ($i = 0; $i <= $depth; $i++)
139 {
140 $string .= $mark;
141 }
142
143 return $string;
144 }
145
146 // ###################### Start parse_pcv_select #####################
147 function parse_pcv_select($input, $validate = false)
148 {
149 global $bugsys;
150
151 $input = trim($input);
152
153 /*
154 yummy regex tests....
155 var_dump(preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input));
156 var_dump(preg_match('#^p(\d.+?)c(\d.+?)v(\d.+?)$#', $input));
157 var_dump(preg_match('#^p([0-9]+?)c([0-9]+?)v([0-9]+?)$#', $input));
158 var_dump(preg_match('#^p([0-9].+?)c([0-9].+?)v([0-9].+?)$#', $input));
159 */
160
161 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
162 {
163 return false;
164 }
165
166 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
167 if (count($trio) != 3)
168 {
169 return false;
170 }
171
172 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
173
174 if (!$validate)
175 {
176 return $return;
177 }
178 else
179 {
180 // -------------------------------------------------------------------
181 // pcv validation
182 $product = $bugsys->datastore['product'][ $pcv['product'] ];
183 if (!$product)
184 {
185 return false;
186 }
187 $version = $bugsys->datastore['version'][ $pcv['version'] ];
188 if (!$version)
189 {
190 return false;
191 }
192 // no component
193 if ($pcv['component'] == 0)
194 {
195 // not global version and version.productid != product.productid
196 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
197 {
198 return false;
199 }
200 }
201 // using a component
202 else
203 {
204 $component = $bugsys->datastore['product'][ $pcv['component'] ];
205 // component has the right mother
206 if ($component['componentmother'] == $product['productid'])
207 {
208 // version.productid != {component.productid | product.productid}
209 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
210 {
211 return false;
212 }
213 }
214 else
215 {
216 return false;
217 }
218 }
219
220 return $pcv;
221 }
222 }
223
224
225 /*=====================================================================*\
226 || ###################################################################
227 || # $HeadURL$
228 || # $Id$
229 || ###################################################################
230 \*=====================================================================*/
231 ?>