r762: Fixing bug://report/10:
[bugdar.git] / includes / functions_product.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Bugdar [#]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($action = 'canviewbugs', $select = '', $prefix = '--')
17 {
18 global $bugsys;
19 static $HTML;
20
21 if ($HTML)
22 {
23 return $HTML;
24 }
25
26 foreach ($bugsys->datastore['product'] AS $product)
27 {
28 if ($product['componentmother'])
29 {
30 $components["$product[componentmother]"]["$product[productid]"] = $product;
31 }
32 else
33 {
34 $products["$product[productid]"] = $product;
35 }
36 }
37
38 foreach ($bugsys->datastore['version'] AS $version)
39 {
40 $versions["$version[productid]"]["$version[versionid]"] = $version;
41 }
42
43 foreach ($products AS $product)
44 {
45 if (!can_perform($action, $product['productid']) OR !can_perform('canviewbugs', $product['productid']))
46 {
47 continue;
48 }
49
50 // set options
51 $OPTIONS = '';
52
53 // prefix
54 $valuepfx = "p$product[productid]";
55
56 // construct global options
57 $OPTIONS .= construct_pcv_select_global_version($product['productid'], 0, $versions, 0, $prefix, $select);
58
59 // any immediate versions
60 if (is_array($versions["$product[productid]"]))
61 {
62 foreach ($versions["$product[productid]"] AS $version)
63 {
64 $OPTIONS .= construct_option($version['version'], "{$valuepfx}c0v$version[versionid]", $select, 0, $prefix);
65 }
66 }
67 else
68 {
69 $OPTIONS = '<option disabled="disabled"></option>';
70 }
71
72 // components (can't be a nested <optgroup> - it's not valid)
73 if (is_array($components["$product[productid]"]))
74 {
75 foreach ($components["$product[productid]"] AS $component)
76 {
77 // set options2
78 $OPTIONS2 = '';
79
80 // prefix
81 $valuepfx = "p$product[productid]c$component[productid]";
82
83 // construct global options
84 $OPTIONS2 .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, 1, $prefix, $select);
85
86 // product versions
87 if (is_array($versions["$component[componentmother]"]))
88 {
89 foreach ($versions["$component[componentmother]"] AS $version)
90 {
91 $OPTIONS2 .= construct_option($version['version'], "{$valuepfx}v$version[versionid]", $select, 1, $prefix);
92 }
93 }
94
95 // versions
96 if (is_array($versions["$component[productid]"]))
97 {
98 foreach ($versions["$component[productid]"] AS $version)
99 {
100 $OPTIONS2 .= construct_option($version['version'], "{$valuepfx}v$version[versionid]", $select, 1, $prefix);
101 }
102 }
103
104 // add optgroup
105 $OPTIONS .= construct_optgroup($component['title'], 1, $OPTIONS2, $prefix);
106 }
107 }
108
109 // add it as an optgroup
110 $HTML .= construct_optgroup($product['title'], 0, $OPTIONS, $prefix);
111 }
112
113 return $HTML;
114 }
115
116 // ############ Start construct_pcv_select_global_version ############
117 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $depth, $prefix = '', $select = '')
118 {
119 global $bugsys;
120 if (is_array($versions['0']))
121 {
122 foreach ($versions['0'] AS $version)
123 {
124 $global_versions_html .= construct_option($version['version'], "p{$product}c{$component}v$version[versionid]", $select, $depth, $prefix);
125 }
126 }
127 return $global_versions_html;
128 }
129
130 // ###################### Start construct_option #####################
131 function construct_option($title, $value, $selectmatch, $depth, $prefix)
132 {
133 global $bugsys;
134
135 $selected = ($selectmatch == $value);
136
137 $label = fetch_depth_mark($depth, $prefix) . ' ' . $title;
138
139 eval('$OPTIONS = "' . $bugsys->template->fetch('selectoption') . '";');
140 return $OPTIONS;
141 }
142
143 // ##################### Start construct_optgroup ####################
144 function construct_optgroup($label, $depth, $optbits, $prefix)
145 {
146 global $bugsys;
147
148 $glabel = fetch_depth_mark($depth, $prefix) . ' ' . $label;
149
150 eval('$HTML = "' . $bugsys->template->fetch('selectoptgroup') . '";');
151 return $HTML;
152 }
153
154 // ###################### Start fetch_depth_mark #####################
155 function fetch_depth_mark($depth, $mark)
156 {
157 $string = '';
158
159 for ($i = 0; $i <= $depth; $i++)
160 {
161 $string .= $mark;
162 }
163
164 return $string;
165 }
166
167 // ###################### Start parse_pcv_select #####################
168 function parse_pcv_select($input, $validate = false)
169 {
170 global $bugsys;
171
172 $input = trim($input);
173
174 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
175 {
176 return false;
177 }
178
179 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
180 if (count($trio) != 3)
181 {
182 return false;
183 }
184
185 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
186
187 if (!$validate)
188 {
189 return $return;
190 }
191 else
192 {
193 // -------------------------------------------------------------------
194 // pcv validation
195 $product = $bugsys->datastore['product'][ $pcv['product'] ];
196 if (!$product)
197 {
198 return false;
199 }
200 $version = $bugsys->datastore['version'][ $pcv['version'] ];
201 if (!$version)
202 {
203 return false;
204 }
205 // no component
206 if ($pcv['component'] == 0)
207 {
208 // not global version and version.productid != product.productid
209 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
210 {
211 return false;
212 }
213 }
214 // using a component
215 else
216 {
217 $component = $bugsys->datastore['product'][ $pcv['component'] ];
218 // component has the right mother
219 if ($component['componentmother'] == $product['productid'])
220 {
221 // version.productid != {component.productid | product.productid}
222 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
223 {
224 return false;
225 }
226 }
227 else
228 {
229 return false;
230 }
231 }
232
233 if (!can_perform('canviewbugs', $product['productid']))
234 {
235 return false;
236 }
237
238 return $pcv;
239 }
240 }
241
242
243 /*=====================================================================*\
244 || ###################################################################
245 || # $HeadURL$
246 || # $Id$
247 || ###################################################################
248 \*=====================================================================*/
249 ?>