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