r933: - Adding a setting to control status displays
[bugdar.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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'] = $bugsys->lang->string('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($bugsys->lang->string('%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 ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
110 {
111 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
112
113 if (!in_array($bitmask, $inspecific))
114 {
115 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $bugsys->permissions["$bitmask"]);
116 }
117 }
118
119 return ($userinfo['permissions'] & $bugsys->permissions["$bitmask"]);
120 }
121
122 // ################# Start construct_datastore_select ################
123 // loops through the specified datastore to create <select>s
124 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
125 {
126 global $bugsys;
127
128 if ($adminmode)
129 {
130 global $admin;
131 }
132
133 $select = '';
134
135 if ($includeblank)
136 {
137 if ($adminmode)
138 {
139 $admin->list_item('', '', ((!$selectedvalue) ? true : false));
140 }
141 else
142 {
143 $label = '';
144 $value = '';
145 $selected = ((!$selectedvalue) ? true : false);
146 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
147 }
148 }
149
150 foreach ($bugsys->datastore["$datastore"] AS $item)
151 {
152 $label = $item["$labelname"];
153 $value = $item["$valuename"];
154 $selected = (($value == $selectedvalue) ? true : false);
155
156 if ($adminmode)
157 {
158 $admin->list_item($label, $value, $selected);
159 }
160 else
161 {
162 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
163 }
164 }
165
166 if (!$adminmode)
167 {
168 return $select;
169 }
170 }
171
172 // ################## Start construct_custom_fields ##################
173 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false)
174 {
175 global $bugsys;
176 static $fields;
177
178 if (!is_array($fields))
179 {
180 $fields = array();
181 $fields_fetch = $bugsys->db->query("
182 SELECT bugfield.*, permission.mask
183 FROM " . TABLE_PREFIX . "bugfield AS bugfield
184 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
185 ON (bugfield.fieldid = permission.fieldid)
186 WHERE (permission.mask = 2 OR permission.mask = 1)
187 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
188 );
189 while ($field = $bugsys->db->fetch_array($fields_fetch))
190 {
191 $fields["$field[fieldid]"] = $field;
192 }
193 }
194
195 $fieldvalues = $bugsys->db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = " . $bugsys->clean($bug['bugid'], TYPE_UINT));
196
197 $fieldbits = array();
198
199 foreach ($fields AS $field)
200 {
201 if ($nodefault)
202 {
203 $field['defaultvalue'] = '';
204 }
205
206 if (!is_null($bug["field$field[fieldid]"]))
207 {
208 $bugsys->debug("not null: $field[fieldid]");
209 $value = $bug["field$field[fieldid]"];
210 }
211 else
212 {
213 $value = $field['defaultvalue'];
214 }
215
216 if ($ignore21mask AND $field['mask'] != 0)
217 {
218 $field['mask'] = 2;
219 }
220
221 if ($field['mask'] == 2)
222 {
223 switch ($field['type'])
224 {
225 case 'input_text':
226 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
227 break;
228
229 case 'input_checkbox':
230 $selected = (($value) ? ' checked="checked"' : '');
231 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
232 break;
233
234 case 'select_single':
235 $selects = unserialize($field['selects']);
236 $value = trim($value);
237
238 $options = '';
239
240 $id = -1;
241 $select = '';
242 if (!$field['usedefault'] AND !trim($value))
243 {
244 $selected = ' selected="selected"';
245 }
246 else
247 {
248 $selected = '';
249 }
250 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
251
252 foreach ($selects AS $id => $select)
253 {
254 $selected = '';
255 $select = stripslashes(trim($select));
256 if ($select == $value)
257 {
258 $selected = ' selected="selected"';
259 }
260 else if ($field['usedefault'] AND $id == 0)
261 {
262 $selected = ' selected="selected"';
263 }
264 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
265 }
266 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
267 break;
268 }
269 }
270 else
271 {
272 $bugsys->debug('mask 1 processing');
273 if (is_null($fieldvalues["field$field[fieldid]"]))
274 {
275 $bugsys->debug("is null: $field[fieldid]");
276 if ($field['type'] == 'select_single')
277 {
278 if ($field['usedefault'])
279 {
280 $temp = unserialize($field['selects']);
281 $value = trim($temp[0]);
282 }
283 else
284 {
285 $value = $fieldvalues["field$field[fieldid]"];
286 }
287 }
288 else
289 {
290 $value = $field['defaultvalue'];
291 }
292 }
293 else
294 {
295 $value = $fieldvalues["field$field[fieldid]"];
296 }
297
298 if ($field['type'] == 'input_checkbox')
299 {
300 $value = (($value) ? 'True' : 'False');
301 }
302 $field['value'] = $value;
303 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
304 }
305 $fieldbits[] = $tempfield;
306 }
307
308 return $fieldbits;
309 }
310
311 // ################### Start process_custom_fields ###################
312 function process_custom_fields($bugid, $inputdata = array())
313 {
314 global $bugsys;
315
316 if (!$inputdata)
317 {
318 $inputdata =& $bugsys->in;
319 }
320
321 $fields = $bugsys->db->query("
322 SELECT bugfield.*
323 FROM " . TABLE_PREFIX . "bugfield AS bugfield
324 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
325 ON (bugfield.fieldid = permission.fieldid)
326 WHERE permission.mask = 2
327 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
328 );
329 while ($field = $bugsys->db->fetch_array($fields))
330 {
331 if ($field['type'] == 'input_checkbox')
332 {
333 $fieldbuild[] = 'field' . $field['fieldid'];
334 if (isset($inputdata["field$field[fieldid]"]))
335 {
336 $fieldvalue[] = 1;
337 }
338 else
339 {
340 $fieldvalue[] = 0;
341 }
342 continue;
343 }
344
345 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
346 {
347 $errorlist[] = sprintf($bugsys->lang->string('The "%1$s" field is a required field.'), $field['name']);
348 continue;
349 }
350
351 if (isset($inputdata["field$field[fieldid]"]))
352 {
353 $fieldbuild[] = 'field' . $field['fieldid'];
354
355 if ($field['type'] == 'input_text')
356 {
357 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
358 }
359 else
360 {
361 if ($inputdata["field$field[fieldid]"] == -1)
362 {
363 $fieldvalue[] = "''";
364 continue;
365 }
366
367 $temp = unserialize($field['selects']);
368 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
369 }
370 }
371 }
372
373 if ($errorlist)
374 {
375 return $errorlist;
376 }
377
378 if (sizeof($fieldbuild) < 1)
379 {
380 return;
381 }
382
383 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
384 }
385
386 // ####################### Start fetch_on_bits #######################
387 function fetch_on_bits($mask, $userinfo = null)
388 {
389 global $bugsys;
390
391 if ($userinfo == null)
392 {
393 $userinfo =& $bugsys->userinfo;
394 }
395
396 $onbits = array();
397
398 $usergroupid = $userinfo['usergroupid'];
399
400 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $bugsys->permissions["$mask"])
401 {
402 foreach ($bugsys->datastore['product'] AS $id => $product)
403 {
404 if (!$product['componentmother'])
405 {
406 $onbits["$id"] = $id;
407 }
408 }
409 }
410
411 if (is_array($bugsys->datastore['permission']["$usergroupid"]))
412 {
413 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
414 {
415 if ($bit & $bugsys->permissions["$mask"])
416 {
417 $onbits["$productid"] = $productid;
418 }
419 else
420 {
421 if ($onbits["$productid"])
422 {
423 unset($onbits["$productid"]);
424 }
425 }
426 }
427 }
428
429 if (sizeof($onbits) < 1)
430 {
431 $onbits = array(0);
432 }
433
434 return implode(',', $onbits);
435 }
436
437 // #################### Start isso_pre_parse_hook ####################
438 // the pre-parse hook for ISSO's template engine
439 function isso_pre_parse_hook($template)
440 {
441 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
442 return $template;
443 }
444
445 // ###################### Start fetch_help_link ######################
446 // returns a prepared link to insert into templates that opens up a
447 // help popup in the user-end
448 function fetch_help_link($topic)
449 {
450 global $bugsys;
451
452 if (isset($bugsys->datastore['help']["$topic"]))
453 {
454 eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
455 return $temp;
456 }
457 else
458 {
459 if ($bugsys->debug)
460 {
461 return "[[INVALID TOPIC: $topic]]";
462 }
463 // do we want this?
464 else if (null == 1)
465 {
466 return eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
467 }
468 }
469 }
470
471 // ###################################################################
472 /**
473 * Returns a user array of information that is specific to all visiting
474 * users (guests). This can then be passed to any function that requires
475 * user information.
476 *
477 * @access public
478 *
479 * @return array User information array
480 */
481 function fetch_guest_user()
482 {
483 global $bugsys;
484
485 return array(
486 'usergroupid' => 1,
487 'userid' => 0,
488 'email' => '',
489 'displayname' => '',
490 'showcolours' => 1,
491 'permissions' => $bugsys->datastore['usergroup'][1]['permissions'],
492 'displaytitle' => $bugsys->datastore['usergroup'][1]['displaytitle'],
493 );
494 }
495
496 /*=====================================================================*\
497 || ###################################################################
498 || # $HeadURL$
499 || # $Id$
500 || ###################################################################
501 \*=====================================================================*/
502 ?>