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