r270: - Initial SVN for autoaction.php
[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 fetch_user_display_name ##################
14 // preps a dispaly name if one isn't set
15 // should be able to be removed by the final version as registration should set this
16 function fetch_user_display_name(&$userinfo)
17 {
18 if (!$userinfo['displayname'])
19 {
20 $userinfo['displayname'] = ucwords(trim(str_replace(array('@', '.com', '.net', '.edu', '.org', '.info', '.biz'), ' ', $userinfo['email'])));
21 }
22 }
23
24 // ################## Start construct_option_select ##################
25 // creates a <select> menu from an array
26 // key vars are used when you need to get data out of the $label array
27 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
28 {
29 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
30 if ($includenil !== false)
31 {
32 $opts[] = '<option value="' . $includenil . '"' . ((!$selected) ? ' selected="selected"' : '') . '>Not Selected</option>';
33 }
34 foreach ($array AS $value => $label)
35 {
36 $opts[] = '<option value="' . (($valuekey) ? $label["$valuekey"] : $value) . '"' . (($selected == (($valuekey) ? $label["$valuekey"] : $value)) ? ' selected="selected"' : '') . '>' . (($labelkey) ? $label["$labelkey"] : $label) . '</option>';
37 }
38 return '<select name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
39 }
40
41 // ########################## Start datelike #########################
42 function datelike($format, $timestamp)
43 {
44 global $bugsys;
45
46 if (!$format OR $format == 'standard')
47 {
48 $format = $bugsys->options['dateformat'];
49 }
50
51 return date($format, ($timestamp + (60 * $bugsys->userinfo['timezone'])));
52 }
53
54 // ################### Start construct_user_display ##################
55 // $userinfo needs userid, email, displayname, and showemail
56 function construct_user_display($userinfo, $userid = true)
57 {
58 fetch_user_display_name($userinfo);
59 return "$userinfo[displayname]" . (($userinfo['showemail']) ? " &lt;$userinfo[email]&gt;" : '') . (($userid) ? " (userid: $userinfo[userid])" : '');
60 }
61
62 // ######################## Start can_perform ########################
63 // short-hand for bitwise &
64 function can_perform($bitmask, $userinfo = null)
65 {
66 global $_PERMISSION;
67
68 if (!isset($_PERMISSION["$bitmask"]))
69 {
70 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
71 }
72
73 if (!$userinfo)
74 {
75 global $bugsys;
76 return ($bugsys->userinfo['permissions'] & $_PERMISSION["$bitmask"]);
77 }
78 return ($userinfo['permissions'] & $_PERMISSION["bitmask"]);
79 }
80
81 // #################### Start construct_pcv_select ###################
82 // constructs a product/component/version select with one go :-)
83 // NB: need to make sure we have the option to turn off just p/c selection without v
84 function construct_pcv_select($select = '', $prefix = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
85 {
86 global $bugsys;
87 static $HTML;
88
89 if ($HTML)
90 {
91 return $HTML;
92 }
93
94
95 $selected = ' checked="checked"';
96
97 $products_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
98 while ($product = $bugsys->db->fetch_array($products_fetch))
99 {
100 if ($product['componentmother'])
101 {
102 $components["$product[componentmother]"]["$product[productid]"] = $product;
103 }
104 else
105 {
106 $products["$product[productid]"] = $product;
107 }
108 }
109
110 $versions_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder");
111 while ($version = $bugsys->db->fetch_array($versions_fetch))
112 {
113 $versions["$version[productid]"]["$version[versionid]"] = $version;
114 }
115
116 foreach ($products AS $product)
117 {
118 $row['prefix'] = '';
119 $valuepfx = "p$product[productid]";
120 $row['value'] = "{$valuepfx}c0v0";
121 $row['title'] = "<strong style=\"text-decoration: underline\">$product[title]</strong>";
122 $row['description'] = $product['description'];
123 $show['input'] = false;
124 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
125 $HTML .= construct_pcv_select_global_version($product['productid'], 0, $versions, $prefix, $select);
126 if (is_array($versions["$product[productid]"]))
127 {
128 foreach ($versions["$product[productid]"] AS $version)
129 {
130 $row['prefix'] = $prefix . $prefix;
131 $row['value'] = "{$valuepfx}c0v$version[versionid]";
132 $row['title'] = $version['version'];
133 $row['selected'] = (($select == $row['value']) ? $selected : '');
134 $row['description'] = '';
135 $show['input'] = true;
136 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
137 }
138 }
139
140 if (is_array($components["$product[productid]"]))
141 {
142 foreach ($components["$product[productid]"] AS $component)
143 {
144 $row['prefix'] = $prefix;
145 $valuepfx .= "c$component[productid]";
146 $row['value'] = "{$valuepfx}v0";
147 $row['selected'] = (($select == $row['value']) ? $selected : '');
148 $row['title'] = "<span style=\"text-decoration: underline\">$component[title]</span>";
149 $row['description'] = $component['description'];
150 $show['input'] = false;
151 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
152 $HTML .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, $prefix, $select);
153 if (is_array($versions["$component[productid]"]))
154 {
155 foreach ($versions["$component[productid]"] AS $version)
156 {
157 $show['input'] = true;
158 $row['prefix'] = $prefix . $prefix;
159 $row['value'] = "{$valuepfx}v$version[versionid]";
160 $row['selected'] = (($select == $row['value']) ? $selected : '');
161 $row['title'] = $version['version'];
162 $row['description'] = '';
163 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
164 }
165 }
166 }
167 }
168 }
169
170 return $HTML;
171 }
172
173 // ############ Start construct_pcv_select_global_version ############
174 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $prefix = '', $select = '')
175 {
176 global $bugsys;
177 if (is_array($versions['0']))
178 {
179 foreach ($versions['0'] AS $version)
180 {
181 $row['prefix'] = $prefix . $prefix;
182 $row['typeselect'] = $type;
183 $row['value'] = "p{$product}c{$component}v$version[versionid]";
184 $row['selected'] = (($select == $row['value']) ? ' checked="checked"' : '');
185 $row['title'] = $version['version'];
186 $row['description'] = '';
187 $show['input'] = true;
188 eval('$global_versions_html .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
189 }
190 }
191 return $global_versions_html;
192 }
193
194 // ###################### Start parse_pcv_select #####################
195 function parse_pcv_select($input, $validate = false)
196 {
197 global $bugsys;
198
199 $input = trim($input);
200
201 /*
202 yummy regex tests....
203 var_dump(preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input));
204 var_dump(preg_match('#^p(\d.+?)c(\d.+?)v(\d.+?)$#', $input));
205 var_dump(preg_match('#^p([0-9]+?)c([0-9]+?)v([0-9]+?)$#', $input));
206 var_dump(preg_match('#^p([0-9].+?)c([0-9].+?)v([0-9].+?)$#', $input));
207 */
208
209 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
210 {
211 return false;
212 }
213
214 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
215 if (count($trio) != 3)
216 {
217 return false;
218 }
219
220 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
221
222 if (!$validate)
223 {
224 return $return;
225 }
226 else
227 {
228 // -------------------------------------------------------------------
229 // pcv validation
230 $product = $bugsys->datastore['product'][ $pcv['product'] ];
231 if (!$product)
232 {
233 return false;
234 }
235 $version = $bugsys->datastore['version'][ $pcv['version'] ];
236 if (!$version)
237 {
238 return false;
239 }
240 // no component
241 if ($pcv['component'] == 0)
242 {
243 // not global version and version.productid != product.productid
244 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
245 {
246 return false;
247 }
248 }
249 // using a component
250 else
251 {
252 $component = $bugsys->datastore['product'][ $pcv['component'] ];
253 // component has the right mother
254 if ($component['componentmother'] == $product['productid'])
255 {
256 // version.productid != {component.productid | product.productid}
257 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
258 {
259 return false;
260 }
261 }
262 else
263 {
264 return false;
265 }
266 }
267
268 return $pcv;
269 }
270 }
271
272 // ################# Start construct_datastore_select ################
273 // loops through the specified datastore to create <select>s
274 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
275 {
276 global $bugsys;
277
278 if ($adminmode)
279 {
280 global $admin;
281 }
282
283 $select = '';
284
285 if ($includeblank)
286 {
287 if ($adminmode)
288 {
289 $admin->list_item('', '', ((!$selectedvalue) ? true : false));
290 }
291 else
292 {
293 $label = '';
294 $value = '';
295 $selected = ((!$selectedvalue) ? true : false);
296 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
297 }
298 }
299
300 foreach ($bugsys->datastore["$datastore"] AS $item)
301 {
302 $label = $item["$labelname"];
303 $value = $item["$valuename"];
304 $selected = (($value == $selectedvalue) ? true : false);
305
306 if ($adminmode)
307 {
308 $admin->list_item($label, $value, $selected);
309 }
310 else
311 {
312 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
313 }
314 }
315
316 if (!$adminmode)
317 {
318 return $select;
319 }
320 }
321
322 // ################## Start construct_custom_fields ##################
323 function construct_custom_fields($bug = array())
324 {
325 global $bugsys;
326 static $fields;
327
328 if (!is_array($fields))
329 {
330 $fields = array();
331 $fields_fetch = $bugsys->db->query("
332 SELECT bugfield.*
333 FROM " . TABLE_PREFIX . "bugfield AS bugfield
334 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
335 ON (bugfield.fieldid = permission.fieldid)
336 WHERE permission.mask = 2
337 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
338 );
339 while ($field = $bugsys->db->fetch_array($fields_fetch))
340 {
341 $fields["$field[fieldid]"] = $field;
342 }
343 }
344
345 $fieldbits = '';
346
347 foreach ($fields AS $field)
348 {
349 if (!is_null($bug["field$field[fieldid]"]))
350 {
351 $value = $bug["field$field[fieldid]"];
352 }
353 else
354 {
355 $value = $field['defaultvalue'];
356 }
357
358 switch ($field['type'])
359 {
360 case 'input_text':
361 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
362 break;
363
364 case 'input_checkbox':
365 $selected = (($value) ? ' checked="checked"' : '');
366 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
367 break;
368
369 case 'select_single':
370 $selects = unserialize($field['selects']);
371 $value = trim($value);
372
373 $options = '';
374
375 $id = -1;
376 $select = '';
377 if (!$field['usedefault'] AND !trim($value))
378 {
379 $selected = ' selected="selected"';
380 }
381 else
382 {
383 $selected = '';
384 }
385 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
386
387 foreach ($selects AS $id => $select)
388 {
389 $selected = '';
390 $select = stripslashes(trim($select));
391 if ($select == $value)
392 {
393 $selected = ' selected="selected"';
394 }
395 else if ($field['usedefault'] AND $id == 0)
396 {
397 $selected = ' selected="selected"';
398 }
399 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
400 }
401 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
402 break;
403 }
404 $fieldbits .= $tempfield;
405 }
406
407 return $fieldbits;
408 }
409
410 // ################### Start process_custom_fields ###################
411 function process_custom_fields($bugid, $inputdata = array())
412 {
413 global $bugsys;
414
415 if (!$inputdata)
416 {
417 $inputdata =& $bugsys->in;
418 }
419
420 $fields = $bugsys->db->query("
421 SELECT bugfield.*
422 FROM " . TABLE_PREFIX . "bugfield AS bugfield
423 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
424 ON (bugfield.fieldid = permission.fieldid)
425 WHERE permission.mask = 2
426 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
427 );
428 while ($field = $bugsys->db->fetch_array($fields))
429 {
430 if ($field['type'] == 'input_checkbox')
431 {
432 $fieldbuild[] = 'field' . $field['fieldid'];
433 if (isset($inputdata["field$field[fieldid]"]))
434 {
435 $fieldvalue[] = 1;
436 }
437 else
438 {
439 $fieldvalue[] = 0;
440 }
441 continue;
442 }
443
444 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
445 {
446 $errorlist[] = lang::p('field_x_is_required', $field['name']);
447 continue;
448 }
449
450 if (isset($inputdata["field$field[fieldid]"]))
451 {
452 $fieldbuild[] = 'field' . $field['fieldid'];
453
454 if ($field['type'] == 'input_text')
455 {
456 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
457 }
458 else
459 {
460 if ($inputdata["field$field[fieldid]"] == -1)
461 {
462 $fieldvalue[] = "''";
463 continue;
464 }
465
466 $temp = unserialize($field['selects']);
467 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
468 }
469 }
470 }
471
472 if ($errorlist)
473 {
474 return $errorlist;
475 }
476
477 if (count($fieldbuild) < 1)
478 {
479 return;
480 }
481
482 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
483 }
484
485 /*=====================================================================*\
486 || ###################################################################
487 || # $HeadURL$
488 || # $Id$
489 || ###################################################################
490 \*=====================================================================*/
491 ?>