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