r256: Logs now work with the new custom fields system. Took a while, but it works...
[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 . '"' . ((!$selected) ? ' selected="selected"' : '') . '>Not Selected</option>';
71 }
72 foreach ($array AS $value => $label)
73 {
74 $opts[] = '<option value="' . (($valuekey) ? $label["$valuekey"] : $value) . '"' . (($selected == (($valuekey) ? $label["$valuekey"] : $value)) ? ' selected="selected"' : '') . '>' . (($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]" . (($userinfo['showemail']) ? " &lt;$userinfo[email]&gt;" : '') . (($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
106 if (!isset($_PERMISSION["$bitmask"]))
107 {
108 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
109 }
110
111 if (!$userinfo)
112 {
113 global $bugsys;
114 return ($bugsys->userinfo['permissions'] & $_PERMISSION["$bitmask"]);
115 }
116 return ($userinfo['permissions'] & $_PERMISSION["bitmask"]);
117 }
118
119 // #################### Start construct_pcv_select ###################
120 // constructs a product/component/version select with one go :-)
121 // NB: need to make sure we have the option to turn off just p/c selection without v
122 function construct_pcv_select($select = '', $prefix = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
123 {
124 global $bugsys;
125 static $HTML;
126
127 if ($HTML)
128 {
129 return $HTML;
130 }
131
132
133 $selected = ' checked="checked"';
134
135 $products_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "product ORDER BY displayorder ASC");
136 while ($product = $bugsys->db->fetch_array($products_fetch))
137 {
138 if ($product['componentmother'])
139 {
140 $components["$product[componentmother]"]["$product[productid]"] = $product;
141 }
142 else
143 {
144 $products["$product[productid]"] = $product;
145 }
146 }
147
148 $versions_fetch = $bugsys->db->query("SELECT * FROM " . TABLE_PREFIX . "version ORDER BY displayorder");
149 while ($version = $bugsys->db->fetch_array($versions_fetch))
150 {
151 $versions["$version[productid]"]["$version[versionid]"] = $version;
152 }
153
154 foreach ($products AS $product)
155 {
156 $row['prefix'] = '';
157 $valuepfx = "p$product[productid]";
158 $row['value'] = "{$valuepfx}c0v0";
159 $row['title'] = "<strong style=\"text-decoration: underline\">$product[title]</strong>";
160 $row['description'] = $product['description'];
161 $show['input'] = false;
162 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
163 $HTML .= construct_pcv_select_global_version($product['productid'], 0, $versions, $prefix, $select);
164 if (is_array($versions["$product[productid]"]))
165 {
166 foreach ($versions["$product[productid]"] AS $version)
167 {
168 $row['prefix'] = $prefix . $prefix;
169 $row['value'] = "{$valuepfx}c0v$version[versionid]";
170 $row['title'] = $version['version'];
171 $row['selected'] = (($select == $row['value']) ? $selected : '');
172 $row['description'] = '';
173 $show['input'] = true;
174 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
175 }
176 }
177
178 if (is_array($components["$product[productid]"]))
179 {
180 foreach ($components["$product[productid]"] AS $component)
181 {
182 $row['prefix'] = $prefix;
183 $valuepfx .= "c$component[productid]";
184 $row['value'] = "{$valuepfx}v0";
185 $row['selected'] = (($select == $row['value']) ? $selected : '');
186 $row['title'] = "<span style=\"text-decoration: underline\">$component[title]</span>";
187 $row['description'] = $component['description'];
188 $show['input'] = false;
189 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
190 $HTML .= construct_pcv_select_global_version($component['componentmother'], $component['productid'], $versions, $prefix, $select);
191 if (is_array($versions["$component[productid]"]))
192 {
193 foreach ($versions["$component[productid]"] AS $version)
194 {
195 $show['input'] = true;
196 $row['prefix'] = $prefix . $prefix;
197 $row['value'] = "{$valuepfx}v$version[versionid]";
198 $row['selected'] = (($select == $row['value']) ? $selected : '');
199 $row['title'] = $version['version'];
200 $row['description'] = '';
201 eval('$HTML .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
202 }
203 }
204 }
205 }
206 }
207
208 return $HTML;
209 }
210
211 // ############ Start construct_pcv_select_global_version ############
212 function construct_pcv_select_global_version($product = 0, $component = 0, $versions = array(), $prefix = '', $select = '')
213 {
214 global $bugsys;
215 if (is_array($versions['0']))
216 {
217 foreach ($versions['0'] AS $version)
218 {
219 $row['prefix'] = $prefix . $prefix;
220 $row['typeselect'] = $type;
221 $row['value'] = "p{$product}c{$component}v$version[versionid]";
222 $row['selected'] = (($select == $row['value']) ? ' checked="checked"' : '');
223 $row['title'] = $version['version'];
224 $row['description'] = '';
225 $show['input'] = true;
226 eval('$global_versions_html .= "' . $bugsys->template->fetch('pcv_select_row') . '";');
227 }
228 }
229 return $global_versions_html;
230 }
231
232 // ###################### Start parse_pcv_select #####################
233 function parse_pcv_select($input, $validate = false)
234 {
235 global $bugsys;
236
237 $input = trim($input);
238
239 /*
240 yummy regex tests....
241 var_dump(preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input));
242 var_dump(preg_match('#^p(\d.+?)c(\d.+?)v(\d.+?)$#', $input));
243 var_dump(preg_match('#^p([0-9]+?)c([0-9]+?)v([0-9]+?)$#', $input));
244 var_dump(preg_match('#^p([0-9].+?)c([0-9].+?)v([0-9].+?)$#', $input));
245 */
246
247 if (preg_match('#^p(\d+?)c(\d+?)v(\d+?)$#', $input) == 0)
248 {
249 return false;
250 }
251
252 $trio = preg_split('#(p|c|v)#i', $input, -1, PREG_SPLIT_NO_EMPTY);
253 if (count($trio) != 3)
254 {
255 return false;
256 }
257
258 $pcv = array('product' => intval($trio[0]), 'component' => intval($trio[1]), 'version' => intval($trio[2]));
259
260 if (!$validate)
261 {
262 return $return;
263 }
264 else
265 {
266 // -------------------------------------------------------------------
267 // pcv validation
268 $product = $bugsys->datastore['product'][ $pcv['product'] ];
269 if (!$product)
270 {
271 return false;
272 }
273 $version = $bugsys->datastore['version'][ $pcv['version'] ];
274 if (!$version)
275 {
276 return false;
277 }
278 // no component
279 if ($pcv['component'] == 0)
280 {
281 // not global version and version.productid != product.productid
282 if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
283 {
284 return false;
285 }
286 }
287 // using a component
288 else
289 {
290 $component = $bugsys->datastore['product'][ $pcv['component'] ];
291 // component has the right mother
292 if ($component['componentmother'] == $product['productid'])
293 {
294 // version.productid != {component.productid | product.productid}
295 if (($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid']) AND $version['productid'] != 0)
296 {
297 return false;
298 }
299 }
300 else
301 {
302 return false;
303 }
304 }
305
306 return $pcv;
307 }
308 }
309
310 // ################# Start construct_datastore_select ################
311 // loops through the specified datastore to create <select>s
312 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false)
313 {
314 global $bugsys;
315
316 $select = '';
317
318 if ($includeblank)
319 {
320 $label = '';
321 $value = '';
322 $selected = ((!$selectedvalue) ? true : false);
323 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
324 }
325
326 foreach ($bugsys->datastore["$datastore"] AS $item)
327 {
328 $label = $item["$labelname"];
329 $value = $item["$valuename"];
330 $selected = (($value == $selectedvalue) ? true : false);
331 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
332 }
333 return $select;
334 }
335
336 // ################## Start construct_custom_fields ##################
337 function construct_custom_fields($bug = array())
338 {
339 global $bugsys;
340 static $fields;
341
342 if (!is_array($fields))
343 {
344 $fields = array();
345 $fields_fetch = $bugsys->db->query("
346 SELECT bugfield.*
347 FROM " . TABLE_PREFIX . "bugfield AS bugfield
348 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
349 ON (bugfield.fieldid = permission.fieldid)
350 WHERE permission.mask = 2
351 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
352 );
353 while ($field = $bugsys->db->fetch_array($fields_fetch))
354 {
355 $fields["$field[fieldid]"] = $field;
356 }
357 }
358
359 $fieldbits = '';
360
361 foreach ($fields AS $field)
362 {
363 if (!is_null($bug["field$field[fieldid]"]))
364 {
365 $value = $bug["field$field[fieldid]"];
366 }
367 else
368 {
369 $value = $field['defaultvalue'];
370 }
371
372 switch ($field['type'])
373 {
374 case 'input_text':
375 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
376 break;
377
378 case 'input_checkbox':
379 $selected = (($value) ? ' checked="checked"' : '');
380 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
381 break;
382
383 case 'select_single':
384 $selects = unserialize($field['selects']);
385 $value = trim($value);
386
387 $options = '';
388
389 $id = -1;
390 $select = '';
391 if (!$field['usedefault'] AND !trim($value))
392 {
393 $selected = ' selected="selected"';
394 }
395 else
396 {
397 $selected = '';
398 }
399 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
400
401 foreach ($selects AS $id => $select)
402 {
403 $selected = '';
404 $select = stripslashes(trim($select));
405 if ($select == $value)
406 {
407 $selected = ' selected="selected"';
408 }
409 else if ($field['usedefault'] AND $id == 0)
410 {
411 $selected = ' selected="selected"';
412 }
413 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
414 }
415 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
416 break;
417 }
418 $fieldbits .= $tempfield;
419 }
420
421 return $fieldbits;
422 }
423
424 // ################### Start process_custom_fields ###################
425 function process_custom_fields($bugid, $inputdata = array())
426 {
427 global $bugsys;
428
429 if (!$inputdata)
430 {
431 $inputdata =& $bugsys->in;
432 }
433
434 $fields = $bugsys->db->query("
435 SELECT bugfield.*
436 FROM " . TABLE_PREFIX . "bugfield AS bugfield
437 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
438 ON (bugfield.fieldid = permission.fieldid)
439 WHERE permission.mask = 2
440 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
441 );
442 while ($field = $bugsys->db->fetch_array($fields))
443 {
444 if ($field['type'] == 'input_checkbox')
445 {
446 $fieldbuild[] = 'field' . $field['fieldid'];
447 if (isset($inputdata["field$field[fieldid]"]))
448 {
449 $fieldvalue[] = 1;
450 }
451 else
452 {
453 $fieldvalue[] = 0;
454 }
455 continue;
456 }
457
458 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
459 {
460 $errorlist[] = phrase('field_x_is_required', $field['name']);
461 continue;
462 }
463
464 if (isset($inputdata["field$field[fieldid]"]))
465 {
466 $fieldbuild[] = 'field' . $field['fieldid'];
467
468 if ($field['type'] == 'input_text')
469 {
470 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
471 }
472 else
473 {
474 if ($inputdata["field$field[fieldid]"] == -1)
475 {
476 $fieldvalue[] = "''";
477 continue;
478 }
479
480 $temp = unserialize($field['selects']);
481 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
482 }
483 }
484 }
485
486 if ($errorlist)
487 {
488 return $errorlist;
489 }
490
491 if (count($fieldbuild) < 1)
492 {
493 return;
494 }
495
496 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
497 }
498
499 /*=====================================================================*\
500 || ###################################################################
501 || # $HeadURL$
502 || # $Id$
503 || ###################################################################
504 \*=====================================================================*/
505 ?>