r1392: Lots of work going into making the custom fields part of the bug table and...
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 // ###################################################################
23 /**
24 * Constructs HTML code <select>s from an array. You use they keys when
25 * you need to access a multi-dimensional array of data.
26 *
27 * @access public
28 *
29 * @param string HTML name of the select
30 * @param array Array of <option>s
31 * @param integer ID of the selected item, 0 for none
32 * @param string Name of the index where values are stored in the $array
33 * @param string Name of the iddex where the labels are stored in $array
34 * @param bool Value of the blank option, FALSE turns it off
35 * @param bool Construct a multiple-selection <select> menu and append "[]" to the end of the name
36 *
37 * @return string Constructed HTML output
38 */
39 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false, $multiple = false)
40 {
41 global $bugsys;
42
43 if ($multiple)
44 {
45 $selected = explode(',', $selected);
46 }
47
48 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
49 if ($includenil !== false)
50 {
51 $opts[] = '<option value="' . $includenil . '"' . ((!$selected OR (is_array($selected) AND in_array($includenil, $selected))) ? ' selected="selected"' : '') . '> ---------</option>';
52 }
53 foreach ($array AS $value => $label)
54 {
55 $newval = ($valuekey ? $label["$valuekey"] : $value);
56 $newlab = ($labelkey ? $label["$labelkey"] : $label);
57 $opts[] = '<option value="' . $newval . '"' . (($selected == $newval OR (is_array($selected) AND in_array($newval, $selected))) ? ' selected="selected"' : '') . '>' . $newlab . '</option>';
58 }
59 return '<select class="input" name="' . $name . ($multiple ? '[]' : '') . '"' . ($multiple ? ' multiple="multiple" size="' . (sizeof($array) < 8 ? sizeof($array) + 1 : 8) . '"' : '') . '>' . implode("\n\t", $opts) . "\r</select>";
60 }
61
62 // ################### Start construct_user_display ##################
63 // $userinfo needs userid, email, displayname, and showemail
64 function construct_user_display($userinfo, $html = true)
65 {
66 global $bugsys;
67
68 if (!$userinfo['userid'])
69 {
70 $userinfo['displayname'] = _('Guest');
71 $userinfo['showemail'] = false;
72 }
73
74 if ($html)
75 {
76 eval('$username = "' . $bugsys->template->fetch('username_display') . '";');
77 }
78 else
79 {
80 if ($userinfo['showemail'])
81 {
82 $username = sprintf(_('%1$s &lt;%2$s&gt;'), $userinfo['displayname'], $userinfo['email']);
83 }
84 else
85 {
86 $username = $userinfo['displayname'];
87 }
88 }
89
90 return $username;
91 }
92
93 // ######################## Start can_perform ########################
94 // short-hand for bitwise &
95 function can_perform($bitmask, $productid = 0, $userinfo = null)
96 {
97 global $bugsys;
98
99 if ($userinfo == null)
100 {
101 $userinfo =& $bugsys->userinfo;
102 }
103
104 if (!isset($bugsys->permissions["$bitmask"]))
105 {
106 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
107 }
108
109 if (!$userinfo['permissions'])
110 {
111 $userinfo['permissions'] = (int)$bugsys->datastore['usergroup']["$userinfo[usergroupid]"]['permissions'];
112 }
113
114 if ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
115 {
116 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
117
118 if (!in_array($bitmask, $inspecific))
119 {
120 $bugsys->debug("verdict* on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]));
121 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]);
122 }
123 }
124
125 $bugsys->debug("verdict on can_perform($bitmask, $productid, $userinfo[userid]) = " . ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]));
126 return ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]);
127 }
128
129 // ###################################################################
130 /**
131 * Runs through a given datastore item and creates a series of <select>
132 * options.
133 *
134 * @access public
135 *
136 * @param string Datastore name
137 * @param string Array index for the label
138 * @param string Array index for the value
139 * @param mixed The selected value(s)
140 * @param bool Include a blank option? TRUE will set a null value, FALSE turns it off, anything else is used as the value for the blank option
141 * @param bool Generate it using admin printers?
142 *
143 * @return string Unelss in admin mode, returns the constructed options
144 */
145 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
146 {
147 global $bugsys;
148
149 if ($adminmode)
150 {
151 global $admin;
152 }
153
154 $select = '';
155
156 if ($includeblank === true OR $includeblank !== false)
157 {
158 $newval = ($inclueblank === true ? '' : $includeblank);
159 if ($adminmode)
160 {
161 $admin->list_item('', '', ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false));
162 }
163 else
164 {
165 $label = '';
166 $value = $newval;
167 $selected = ((!$selectedvalue OR (is_array($selectedvalue) AND in_array($newval, $selectedvalue))) ? true : false);
168 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
169 }
170 }
171
172 foreach ($bugsys->datastore["$datastore"] AS $item)
173 {
174 $label = $item["$labelname"];
175 $value = $item["$valuename"];
176 $selected = (($value == $selectedvalue OR (is_array($selectedvalue) AND in_array($value, $selectedvalue))) ? true : false);
177
178 if ($adminmode)
179 {
180 $admin->list_item($label, $value, $selected);
181 }
182 else
183 {
184 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
185 }
186 }
187
188 if (!$adminmode)
189 {
190 return $select;
191 }
192 }
193
194 // ################## Start construct_custom_fields ##################
195 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false, $searchMode = false)
196 {
197 global $bugsys;
198 static $fields;
199
200 if (!is_array($fields))
201 {
202 $fields = array();
203 $fields_fetch = $bugsys->db->query("
204 SELECT bugfield.*, permission.mask
205 FROM " . TABLE_PREFIX . "bugfield AS bugfield
206 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
207 ON (bugfield.fieldid = permission.fieldid)
208 WHERE (permission.mask = 2 OR permission.mask = 1)
209 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
210 );
211 while ($field = $bugsys->db->fetch_array($fields_fetch))
212 {
213 $fields["$field[fieldid]"] = $field;
214 }
215 }
216
217 $fieldbits = array();
218
219 foreach ($fields AS $field)
220 {
221 if ($nodefault)
222 {
223 $field['defaultvalue'] = '';
224 }
225
226 if (!is_null($bug["custom$field[fieldid]"]))
227 {
228 $bugsys->debug("not null: $field[fieldid]");
229 $value = $bug["custom$field[fieldid]"];
230 }
231 else
232 {
233 $value = $field['defaultvalue'];
234 }
235
236 if ($ignore21mask AND $field['mask'] != 0)
237 {
238 $field['mask'] = 2;
239 }
240
241 if ($field['mask'] == 2)
242 {
243 switch ($field['type'])
244 {
245 case 'input_text':
246 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
247 break;
248
249 case 'input_checkbox':
250 $selected = ($value ? ' checked="checked"' : '');
251 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
252 break;
253
254 case 'select_single':
255 $selects = unserialize($field['selects']);
256 $value = trim($value);
257
258 $options = '';
259
260 $id = -1;
261 $select = '';
262 if (!$field['usedefault'] AND !trim($value))
263 {
264 $selected = ' selected="selected"';
265 }
266 else
267 {
268 $selected = '';
269 }
270 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
271
272 foreach ($selects AS $id => $select)
273 {
274 $selected = '';
275 $select = stripslashes(trim($select));
276 if ($select == $value)
277 {
278 $selected = ' selected="selected"';
279 }
280 else if ($field['usedefault'] AND $id == 0)
281 {
282 $selected = ' selected="selected"';
283 }
284 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
285 }
286 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
287 break;
288 }
289 }
290 else
291 {
292 $bugsys->debug('mask 1 processing');
293 if (is_null($bug["custom$field[fieldid]"]))
294 {
295 $bugsys->debug("is null: $field[fieldid]");
296 if ($field['type'] == 'select_single')
297 {
298 if ($field['usedefault'])
299 {
300 $temp = unserialize($field['selects']);
301 $value = trim($temp[0]);
302 }
303 else
304 {
305 $value = $bug["custom$field[fieldid]"];
306 }
307 }
308 else
309 {
310 $value = $field['defaultvalue'];
311 }
312 }
313 else
314 {
315 $value = $bug["custom$field[fieldid]"];
316 }
317
318 if ($field['type'] == 'input_checkbox')
319 {
320 $value = ($value ? 'True' : 'False');
321 }
322 $field['value'] = $value;
323 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
324 }
325 $fieldbits[] = $tempfield;
326 }
327
328 return $fieldbits;
329 }
330
331 // ###################################################################
332 /**
333 * This takes the bug API object and input data and then sanitizes, verifies,
334 * and processes the data for custom fields. If there are any errors,
335 * they are passed to the message reporter.
336 *
337 * @param object A BugAPI object
338 * @param object MessageReporter object
339 * @param bool If there are errors, add them to an errorbox format? If not, then display-on-encounter
340 * @param array If you don't want to get the data from $bugsys->in[], then an optional input source
341 *
342 * @return mixed NULL if an ID is passed, string if bugid is NULL
343 */
344 function process_custom_fields(&$bugapi, &$msg, $errorbox = false, $inputdata = array())
345 {
346 global $bugsys;
347
348 if (!$inputdata)
349 {
350 $inputdata =& $bugsys->in;
351 }
352
353 $fields = $bugsys->db->query("
354 SELECT bugfield.*
355 FROM " . TABLE_PREFIX . "bugfield AS bugfield
356 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
357 ON (bugfield.fieldid = permission.fieldid)
358 WHERE permission.mask = 2
359 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
360 );
361 while ($field = $bugsys->db->fetch_array($fields))
362 {
363 $fieldname = "custom$field[fieldid]";
364 if ($field['type'] == 'input_checkbox')
365 {
366 $bugapi->set($fieldname, intval(isset($inputdata["$fieldname"])));
367 continue;
368 }
369
370 if ($field['required'] AND empty($inputdata["$fieldname"]))
371 {
372 $errorlist[] = sprintf(_('The "%1$s" field is a required field.'), $field['name']);
373 continue;
374 }
375
376 if (!empty($field['regexmatch']))
377 {
378 if (!preg_match('#' . str_replace('#', '\#', $field['regexmatch']) . '#si', $inputdata["$fieldname"]))
379 {
380 $errorlist[] = sprintf(_('%1$s does not match the specified format'), $field['name']);
381 continue;
382 }
383 }
384
385 if (isset($inputdata["$fieldname"]))
386 {
387 if ($field['type'] == 'input_text')
388 {
389 $bugapi->set($fieldname, $inputdata["$fieldname"]);
390 }
391 else
392 {
393 if ($inputdata["$fieldname"] == -1)
394 {
395 $bugapi->set($fieldname, '');
396 continue;
397 }
398
399 $temp = unserialize($field['selects']);
400 $bugapi->set($fieldname, trim($temp[ intval($inputdata["$fieldname"]) ]));;
401 }
402 }
403 }
404
405 if ($errorlist)
406 {
407 if ($errorbox)
408 {
409 foreach ($errorlist AS $err)
410 {
411 $msg->add_error($err);
412 }
413 }
414 else
415 {
416 $msg->error($errorlist[0]);
417 }
418 }
419 }
420
421 // ####################### Start fetch_on_bits #######################
422 function fetch_on_bits($mask, $userinfo = null)
423 {
424 global $bugsys;
425
426 if ($userinfo == null)
427 {
428 $userinfo =& $bugsys->userinfo;
429 }
430
431 $onbits = array();
432
433 $usergroupid = $userinfo['usergroupid'];
434
435 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $bugsys->permissions["$mask"] AND is_array($bugsys->datastore['product']))
436 {
437 foreach ($bugsys->datastore['product'] AS $id => $product)
438 {
439 $onbits["$id"] = $id;
440 }
441 }
442
443 if (is_array($bugsys->datastore['permission']["$usergroupid"]))
444 {
445 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
446 {
447 if ($bit & $bugsys->permissions["$mask"])
448 {
449 $onbits["$productid"] = $productid;
450 }
451 else
452 {
453 if ($onbits["$productid"])
454 {
455 unset($onbits["$productid"]);
456 }
457 }
458 }
459 }
460
461 if (sizeof($onbits) < 1)
462 {
463 $onbits = array(0);
464 }
465
466 return implode(',', $onbits);
467 }
468
469 // #################### Start isso_pre_parse_hook ####################
470 // the pre-parse hook for ISSO's template engine
471 function isso_pre_parse_hook($template)
472 {
473 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
474 return $template;
475 }
476
477 // ###################### Start fetch_help_link ######################
478 // returns a prepared link to insert into templates that opens up a
479 // help popup in the user-end
480 function fetch_help_link($topic)
481 {
482 global $bugsys;
483
484 if (isset($bugsys->datastore['help']["$topic"]))
485 {
486 eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
487 return $temp;
488 }
489 else
490 {
491 if ($bugsys->debug)
492 {
493 return "[[INVALID TOPIC: $topic]]";
494 }
495 // do we want this?
496 else if (null == 1)
497 {
498 return eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
499 }
500 }
501 }
502
503 // ###################################################################
504 /**
505 * Returns a user array of information that is specific to all visiting
506 * users (guests). This can then be passed to any function that requires
507 * user information.
508 *
509 * @access public
510 *
511 * @return array User information array
512 */
513 function fetch_guest_user()
514 {
515 global $bugsys;
516
517 return array(
518 'usergroupid' => 1,
519 'userid' => 0,
520 'email' => '',
521 'displayname' => '',
522 'showcolors' => 1,
523 'permissions' => $bugsys->datastore['usergroup'][1]['permissions'],
524 'displaytitle' => $bugsys->datastore['usergroup'][1]['displaytitle'],
525 'timezone' => $bugsys->options['defaulttimezone']
526 );
527 }
528
529 // ###################################################################
530 /**
531 * Does an exhaustive permissions check on the bug. It checks for hidden
532 * bug status and ability to view hidden bugs. This normally was done
533 * at the top of each page, but it got so big, it was moved to a function.
534 *
535 * @access public
536 *
537 * @param array Bug array
538 * @param array Alternate user array
539 *
540 * @return bool Does the user have permission
541 */
542 function check_bug_permissions($bug, $userinfo = null)
543 {
544 global $bugsys;
545 if ($userinfo == null)
546 {
547 $userinfo = $bugsys->userinfo;
548 }
549
550 $bugsys->debug("checking permissions for $userinfo[userid] on bug $bug[bugid]");
551
552 $bugsys->debug('*** START VERBOSE CHECK ***');
553
554 $bugsys->debug('* !can_perform(canviewbugs, $bug[product], $userinfo) = ' . (int)(!can_perform('canviewbugs', $bug['product'], $userinfo)));
555 $bugsys->debug('* $bug[hidden] = ' . (int)$bug['hidden']);
556 $bugsys->debug('* $userinfo[userid] (' . $userinfo['userid'] . ') == $bug[userid] (' . $bug['userid'] . ') = ' . (int)($userinfo['userid'] == $bug['userid']));
557 $bugsys->debug('* can_perform(canviewownhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewownhidden', $bug['product'], $userinfo)));
558 $bugsys->debug('* can_perform(canviewhidden, $bug[product], $userinfo) = ' . (int)(!!can_perform('canviewhidden', $bug['product'], $userinfo)));
559 $bugsys->debug('* !$bug[hidden] = ' . (int)(!$bug['hidden']));
560
561 $bugsys->debug('*** END PERMISSIONS CHECK ***');
562
563 if
564 (
565 !can_perform('canviewbugs', $bug['product'], $userinfo)
566 OR
567 !(
568 (
569 $bug['hidden']
570 AND
571 (
572 ($userinfo['userid'] == $bug['userid'] AND can_perform('canviewownhidden', $bug['product'], $userinfo))
573 OR
574 can_perform('canviewhidden', $bug['product'], $userinfo)
575 )
576 )
577 OR
578 !$bug['hidden']
579 )
580 )
581 {
582 $bugsys->debug('*** DONE WITH REAL CALLS ***');
583 return false;
584 }
585
586 $bugsys->debug('*** DONE WITH REAL CALLS ***');
587
588 return true;
589 }
590
591 // ###################################################################
592 /**
593 * Takes an array of bug information and returns another array with
594 * information that is suitable for display as all the IDs have been
595 * replaced by their string equivalents
596 *
597 * @param array Unprocessed bug data
598 * @param string Color to display if the user has opted to not show status colours
599 *
600 * @param array Bug array with data fit for display
601 */
602 function ProcessBugDataForDisplay($bug, $color = '')
603 {
604 global $bugsys;
605
606 $bug['hiddendisplay'] = (can_perform('canviewhidden', $bug['product']) OR (can_perform('canviewownhidden') AND $bug['userid'] == $bugsys->userinfo['userid']));
607
608 $bug['bgcolor'] = ($bugsys->userinfo['showcolors'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $color);
609 $bug['product'] = $bugsys->datastore['product']["$bug[product]"]['title'];
610 $bug['version'] = $bugsys->datastore['version']["$bug[version]"]['version'];
611 $bug['component'] = $bugsys->datastore['component']["$bug[component]"]['title'];
612 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
613 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
614 $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority'];
615 $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity'];
616
617 $bug['lastposttime'] = ($bug['hiddendisplay'] ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
618 $bug['lastpost'] = ($bug['hiddendisplay'] ? $bug['hiddenlastpostbyname'] : $bug['lastpostbyname']);
619
620 $bug['lastposttime'] = $bugsys->datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
621
622 return $bug;
623 }
624
625 // ###################################################################
626 /**
627 * Loads the pagination module and sets all of the appropriate options
628 * for it
629 *
630 * @access public
631 */
632 function LoadPaginationFramework()
633 {
634 global $bugsys;
635
636 $bugsys->load('pagination', 'pagination', true);
637 $bugsys->pagination->setDefaultPerPage($bugsys->options['defaultpp']);
638 $bugsys->pagination->setMaxPerPage($bugsys->options['maxpp']);
639 $bugsys->pagination->setPageLinks($bugsys->options['pagelinks']);
640 $bugsys->pagination->setPageVar('p');
641 $bugsys->pagination->setPerPageVar('pp');
642 $bugsys->pagination->setBitProcessor('PageNavigatorBitCallback');
643 $bugsys->pagination->setNavigatorProcessor('PageNavigatorCallback');
644 $bugsys->pagination->processIncomingData();
645 }
646
647 // ###################################################################
648 /**
649 * Callback function for the Pagination->BitProcessor()
650 *
651 * @param string Base link
652 * @param bool Do not show this as a link
653 * @param integer Page number
654 * @param object Page navigator framework
655 *
656 * @return string Processed HTML
657 */
658 function PageNavigatorBitCallback($baselink, $nolink, $number, $paginator)
659 {
660 global $bugsys;
661 eval('$return = "' . $bugsys->template->fetch('pagenav_bit') . '";');
662 return $return;
663 }
664
665 // ###################################################################
666 /**
667 * Callback function for the Pagination->NavigatorProcessor()
668 *
669 * @param string Base URL
670 * @param integer Next page number
671 * @param integer Previous page number
672 * @param array Show information
673 * @param string Individual page bits
674 * @param object Page navigator framework
675 *
676 * @return string Processed HTML
677 */
678 function PageNavigatorCallback($baselink, $nextpage, $prevpage, $show, $pagebits, $paginator)
679 {
680 global $bugsys;
681 eval('$return = "' . $bugsys->template->fetch('pagenav') . '";');
682 return $return;
683 }
684
685 /*=====================================================================*\
686 || ###################################################################
687 || # $HeadURL$
688 || # $Id$
689 || ###################################################################
690 \*=====================================================================*/
691 ?>