r67: - Upping template system to use ISSO
[bugdar.git] / includes / functions.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 phrase ##########################
14 function phrase()
15 {
16 global $bugsys;
17
18 $args = func_get_args();
19 $numargs = sizeof($args);
20
21 if ($numargs < 1)
22 {
23 return false;
24 }
25
26 if ($phrasetext = $bugsys->language["$args[0]"])
27 {
28 if ($numargs < 2)
29 {
30 $phrase = $phrasetext;
31 }
32 else
33 {
34 $args[0] = $phrasetext;
35 if (($phrase = @call_user_func_array('sprintf', $args)) === false)
36 {
37 for ($i = 1; $i < $numargs; $i++)
38 {
39 $phrase = str_replace("%{$i}\$s", $args["$i"], $args[0]);
40 }
41 }
42 }
43 return preg_replace('#%([0-9].*?)\$s#', '<strong>[ARG \\1: UNDEFINED]</strong>', $phrase);
44 }
45 else
46 {
47 return "<strong>[UNDEFINED PHRASE: $args[0]]</strong>";
48 }
49 }
50
51 // ################## Start fetch_user_display_name ##################
52 // preps a dispaly name if one isn't set
53 // should be able to be removed by the final version as registration should set this
54 function fetch_user_display_name(&$userinfo)
55 {
56 if (!$userinfo['displayname'])
57 {
58 $userinfo['displayname'] = ucwords(trim(str_replace(array('@', '.com', '.net', '.edu', '.org', '.info', '.biz'), ' ', $userinfo['email'])));
59 }
60 }
61
62 // ################## Start construct_option_select ##################
63 // creates a <select> menu from an array
64 // key vars are used when you need to get data out of the $label array
65 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
66 {
67 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
68 if ($includenil !== false)
69 {
70 $opts[] = '<option value="' . $includenil . '"' . iff(!$selected, ' selected="selected"') . '>Not Selected</option>';
71 }
72 foreach ($array AS $value => $label)
73 {
74 $opts[] = '<option value="' . iff($valuekey, $label["$valuekey"], $value) . '"' . iff($selected == iff($valuekey, $label["$valuekey"], $value), ' selected="selected"') . '>' . iff($labelkey, $label["$labelkey"], $label) . '</option>';
75 }
76 return '<select name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
77 }
78
79 // ########################## Start datelike #########################
80 function datelike($format, $timestamp)
81 {
82 global $bugsys;
83
84 if (!$format OR $format == 'standard')
85 {
86 $format = $bugsys->options['dateformat'];
87 }
88
89 return date($format, ($timestamp + (60 * $bugsys->userinfo['timezone'])));
90 }
91
92 // ################### Start construct_user_display ##################
93 // $userinfo needs userid, email, displayname, and showemail
94 function construct_user_display($userinfo, $userid = true)
95 {
96 fetch_user_display_name($userinfo);
97 return "$userinfo[displayname]" . iff($userinfo['showemail'], " &lt;$userinfo[email]&gt;") . iff($userid, " (userid: $userinfo[userid])");
98 }
99
100 // ######################## Start can_perform ########################
101 // short-hand for bitwise &
102 function can_perform($bitmask, $userinfo = null)
103 {
104 global $_PERMISSION;
105 if (!$userinfo)
106 {
107 global $bugsys;
108 return ($bugsys->userinfo['permissions'] & $_PERMISSION["$bitmask"]);
109 }
110 return ($userinfo['permissions'] & $_PERMISSION["bitmask"]);
111 }
112
113 // #################### Start construct_pcv_select ###################
114 // constructs a product/component/version select with one go :-)
115 // NB: need to make sure we have the option to turn off just p/c selection without v
116 function construct_pcv_select($select = '', $prefix = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
117 {
118 global $bugsys;
119 static $HTML;
120
121 if ($HTML)
122 {
123 return $HTML;
124 }
125
126
127 $selected = ' checked="checked"';
128
129 $products_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
130 while ($product = $bugsys->db->fetch_array($products_fetch))
131 {
132 if ($product['componentmother'])
133 {
134 $components["$product[componentmother]"]["$product[productid]"] = $product;
135 }
136 else
137 {
138 $products["$product[productid]"] = $product;
139 }
140 }
141
142 $versions_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder");
143 while ($version = $bugsys->db->fetch_array($versions_fetch))
144 {
145 $versions["$version[productid]"]["$version[versionid]"] = $version;
146 }
147
148 foreach ($products AS $product)
149 {
150 $row['prefix'] = '';
151 $valuepfx = "p$product[productid]";
152 $row['value'] = "{$valuepfx}c0v0";
153 $row['title'] = "<strong>$product[title]</strong>";
154 $row['description'] = '';
155 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
156 $HTML .= construct_pcv_select_global_version($product['productid'], 0, $versions, $prefix, $select);
157 if (is_array($versions["$product[productid]"]))
158 {
159 foreach ($versions["$product[productid]"] AS $version)
160 {
161 $row['prefix'] = $prefix . $prefix;
162 $row['value'] = "{$valuepfx}c0v$version[versionid]";
163 $row['title'] = $version['version'];
164 $row['selected'] = iff($select == $row['value'], $selected, '');
165 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
166 }
167 }
168
169 if (is_array($components["$product[productid]"]))
170 {
171 foreach ($components["$product[productid]"] AS $component)
172 {
173 $row['prefix'] = $prefix;
174 $valuepfx .= "c$component[productid]";
175 $row['value'] = "{$valuepfx}v0";
176 $row['selected'] = iff($select == $row['value'], $selected, '');
177 $row['title'] = $component['title'];
178 $row['description'] = '';
179 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
180 $HTML .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, $prefix, $select);
181 if (is_array($versions["$component[productid]"]))
182 {
183 foreach ($versions["$component[productid]"] AS $version)
184 {
185 $row['prefix'] = $prefix . $prefix;
186 $row['value'] = "{$valuepfx}v$version[versionid]";
187 $row['selected'] = iff($select == $row['value'], $selected, '');
188 $row['title'] = $version['version'];
189 $row['description'] = '';
190 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
191 }
192 }
193 }
194 }
195 }
196
197 return $HTML;
198 }
199
200 // ############ Start construct_pcv_select_global_version ############
201 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $prefix = '', $select = '')
202 {
203 global $bugsys;
204 if (is_array($versions['0']))
205 {
206 foreach ($versions['0'] AS $version)
207 {
208 $row['prefix'] = $prefix . $prefix;
209 $row['typeselect'] = $type;
210 $row['value'] = "p{$product}c{$component}v$version[versionid]";
211 $row['selected'] = iff($select == $row['value'], ' checked="checked"', '');
212 $row['title'] = $version['version'];
213 $row['description'] = '';
214 eval('$global_versions_html .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
215 }
216 }
217 return $global_versions_html;
218 }
219
220 // ###################### Start parse_pcv_select #####################
221 function parse_pcv_select($input, $validate = false)
222 {
223 global $bugsys;
224
225 $input = trim($input);
226
227 /*
228 yummy regex tests....
229 var_dump(preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input));
230 var_dump(preg_match('#^p(\d.+?)c(\d.+?)v(\d.+?)$#', $input));
231 var_dump(preg_match('#^p([0-9]+?)c([0-9]+?)v([0-9]+?)$#', $input));
232 var_dump(preg_match('#^p([0-9].+?)c([0-9].+?)v([0-9].+?)$#', $input));
233 */
234
235 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
236 {
237 return false;
238 }
239
240 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
241 if (count($trio) != 3)
242 {
243 return false;
244 }
245
246 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
247
248 if (!$validate)
249 {
250 return $return;
251 }
252 else
253 {
254 // -------------------------------------------------------------------
255 // pcv validation
256 $product = $bugsys->datastore['product'][ $pcv['product'] ];
257 if (!$product)
258 {
259 return false;
260 }
261 $version = $bugsys->datastore['version'][ $pcv['version'] ];
262 if (!$version)
263 {
264 return false;
265 }
266 // no component
267 if ($pcv['component'] == 0)
268 {
269 // not global version and version.productid != product.productid
270 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
271 {
272 return false;
273 }
274 }
275 // using a component
276 else
277 {
278 $component = $bugsys->datastore['product'][ $pcv['component'] ];
279 // component has the right mother
280 if ($component['componentmother'] == $product['productid'])
281 {
282 // version.productid != {component.productid | product.productid}
283 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
284 {
285 return false;
286 }
287 }
288 else
289 {
290 return false;
291 }
292 }
293
294 return $pcv;
295 }
296 }
297
298 /*=====================================================================*\
299 || ###################################################################
300 || # $HeadURL$
301 || # $Id$
302 || ###################################################################
303 \*=====================================================================*/
304 ?>