r654: No longer need fetch_user_display_name() [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 construct_option_select ##################
14 // creates a <select> menu from an array
15 // key vars are used when you need to get data out of the $label array
16 function construct_option_select($name, $array, $selected = 0, $valuekey = '', $labelkey = '', $includenil = false)
17 {
18 global $bugsys;
19
20 // if we're not working on a boolean false, we use it for the value (allows -1 and 0)
21 if ($includenil !== false)
22 {
23 $opts[] = '<option value="' . $includenil . '"' . ((!$selected) ? ' selected="selected"' : '') . '>' . $bugsys->lang->string('Not Selected') . '</option>';
24 }
25 foreach ($array AS $value => $label)
26 {
27 $opts[] = '<option value="' . (($valuekey) ? $label["$valuekey"] : $value) . '"' . (($selected == (($valuekey) ? $label["$valuekey"] : $value)) ? ' selected="selected"' : '') . '>' . (($labelkey) ? $label["$labelkey"] : $label) . '</option>';
28 }
29 return '<select class="input" name="' . $name . '">' . implode("\n\t", $opts) . "\r</select>";
30 }
31
32 // ################### Start construct_user_display ##################
33 // $userinfo needs userid, email, displayname, and showemail
34 function construct_user_display($userinfo, $html = true)
35 {
36 global $bugsys;
37 fetch_user_display_name($userinfo);
38
39 if ($html)
40 {
41 eval('$username = "' . $bugsys->template->fetch('username_display') . '";');
42 }
43 else
44 {
45 if ($userinfo['showemail'])
46 {
47 $username = sprintf($bugsys->lang->string('%1$s &lt;%2$s&gt;'), $userinfo['displayname'], $userinfo['email']);
48 }
49 else
50 {
51 $username = $userinfo['displayname'];
52 }
53 }
54
55 return $username;
56 }
57
58 // ######################## Start can_perform ########################
59 // short-hand for bitwise &
60 function can_perform($bitmask, $productid = 0)
61 {
62 global $bugsys, $_PERMISSION;
63
64 $userinfo = $bugsys->userinfo;
65
66 if (!isset($_PERMISSION["$bitmask"]))
67 {
68 trigger_error('Invalid bitmask "' . $bitmask . '" specified for can_perform() [includes/functions.php]', E_USER_WARNING);
69 }
70
71 if ($productid AND isset($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"]))
72 {
73 $inspecific = array('cansearch', 'canbeassignedto', 'canadminpanel', 'canadminbugs', 'canadminfields', 'canadminversions', 'canadminusers', 'canadmingroups', 'canadmintools');
74
75 if (!in_array($bitmask, $inspecific))
76 {
77 return ($bugsys->datastore['permission']["$userinfo[usergroupid]"]["$productid"] & $_PERMISSION["$bitmask"]);
78 }
79 }
80
81 return ($userinfo['permissions'] & $_PERMISSION["$bitmask"]);
82 }
83
84 // ################# Start construct_datastore_select ################
85 // loops through the specified datastore to create <select>s
86 function construct_datastore_select($datastore, $labelname, $valuename, $selectedvalue = 0, $includeblank = false, $adminmode = false)
87 {
88 global $bugsys;
89
90 if ($adminmode)
91 {
92 global $admin;
93 }
94
95 $select = '';
96
97 if ($includeblank)
98 {
99 if ($adminmode)
100 {
101 $admin->list_item('', '', ((!$selectedvalue) ? true : false));
102 }
103 else
104 {
105 $label = '';
106 $value = '';
107 $selected = ((!$selectedvalue) ? true : false);
108 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
109 }
110 }
111
112 foreach ($bugsys->datastore["$datastore"] AS $item)
113 {
114 $label = $item["$labelname"];
115 $value = $item["$valuename"];
116 $selected = (($value == $selectedvalue) ? true : false);
117
118 if ($adminmode)
119 {
120 $admin->list_item($label, $value, $selected);
121 }
122 else
123 {
124 eval('$select .= "' . $bugsys->template->fetch('selectoption') . '";');
125 }
126 }
127
128 if (!$adminmode)
129 {
130 return $select;
131 }
132 }
133
134 // ################## Start construct_custom_fields ##################
135 function construct_custom_fields($bug = array(), $ignore21mask = false, $nodefault = false)
136 {
137 global $bugsys;
138 static $fields;
139
140 if (!is_array($fields))
141 {
142 $fields = array();
143 $fields_fetch = $bugsys->db->query("
144 SELECT bugfield.*, permission.mask
145 FROM " . TABLE_PREFIX . "bugfield AS bugfield
146 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
147 ON (bugfield.fieldid = permission.fieldid)
148 WHERE (permission.mask = 2 OR permission.mask = 1)
149 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
150 );
151 while ($field = $bugsys->db->fetch_array($fields_fetch))
152 {
153 $fields["$field[fieldid]"] = $field;
154 }
155 }
156
157 $fieldvalues = $bugsys->db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = " . intval($bug['bugid']));
158
159 $fieldbits = '';
160
161 foreach ($fields AS $field)
162 {
163 if ($nodefault)
164 {
165 $field['defaultvalue'] = '';
166 }
167
168 if (!is_null($bug["field$field[fieldid]"]))
169 {
170 $bugsys->debug("not null: $field[fieldid]");
171 $value = $bug["field$field[fieldid]"];
172 }
173 else
174 {
175 $value = $field['defaultvalue'];
176 }
177
178 if ($ignore21mask)
179 {
180 $field['mask'] = 2;
181 }
182
183 if ($field['mask'] == 2)
184 {
185 switch ($field['type'])
186 {
187 case 'input_text':
188 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
189 break;
190
191 case 'input_checkbox':
192 $selected = (($value) ? ' checked="checked"' : '');
193 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_checkbox') . '";');
194 break;
195
196 case 'select_single':
197 $selects = unserialize($field['selects']);
198 $value = trim($value);
199
200 $options = '';
201
202 $id = -1;
203 $select = '';
204 if (!$field['usedefault'] AND !trim($value))
205 {
206 $selected = ' selected="selected"';
207 }
208 else
209 {
210 $selected = '';
211 }
212 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
213
214 foreach ($selects AS $id => $select)
215 {
216 $selected = '';
217 $select = stripslashes(trim($select));
218 if ($select == $value)
219 {
220 $selected = ' selected="selected"';
221 }
222 else if ($field['usedefault'] AND $id == 0)
223 {
224 $selected = ' selected="selected"';
225 }
226 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
227 }
228 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
229 break;
230 }
231 }
232 else
233 {
234 $bugsys->debug('mask 1 processing');
235 if (is_null($fieldvalues["field$field[fieldid]"]))
236 {
237 $bugsys->debug("is null: $field[fieldid]");
238 if ($field['type'] == 'select_single')
239 {
240 if ($field['usedefault'])
241 {
242 $temp = unserialize($field['selects']);
243 $value = trim($temp[0]);
244 }
245 else
246 {
247 $value = $fieldvalues["field$field[fieldid]"];
248 }
249 }
250 else
251 {
252 $value = $field['defaultvalue'];
253 }
254 }
255 else
256 {
257 $value = $fieldvalues["field$field[fieldid]"];
258 }
259
260 if ($field['type'] == 'input_checkbox')
261 {
262 $value = (($value) ? 'True' : 'False');
263 }
264 $field['value'] = $value;
265 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_static_text') . '";');
266 }
267 $fieldbits[] = $tempfield;
268 }
269
270 return $fieldbits;
271 }
272
273 // ################### Start process_custom_fields ###################
274 function process_custom_fields($bugid, $inputdata = array())
275 {
276 global $bugsys;
277
278 if (!$inputdata)
279 {
280 $inputdata =& $bugsys->in;
281 }
282
283 $fields = $bugsys->db->query("
284 SELECT bugfield.*
285 FROM " . TABLE_PREFIX . "bugfield AS bugfield
286 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
287 ON (bugfield.fieldid = permission.fieldid)
288 WHERE permission.mask = 2
289 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
290 );
291 while ($field = $bugsys->db->fetch_array($fields))
292 {
293 if ($field['type'] == 'input_checkbox')
294 {
295 $fieldbuild[] = 'field' . $field['fieldid'];
296 if (isset($inputdata["field$field[fieldid]"]))
297 {
298 $fieldvalue[] = 1;
299 }
300 else
301 {
302 $fieldvalue[] = 0;
303 }
304 continue;
305 }
306
307 if ($field['required'] AND empty($inputdata["field$field[fieldid]"]))
308 {
309 $errorlist[] = sprintf($bugsys->lang->string('The "%1$s" field is a required field.'), $field['name']);
310 continue;
311 }
312
313 if (isset($inputdata["field$field[fieldid]"]))
314 {
315 $fieldbuild[] = 'field' . $field['fieldid'];
316
317 if ($field['type'] == 'input_text')
318 {
319 $fieldvalue[] = "'" . $inputdata["field$field[fieldid]"] . "'";
320 }
321 else
322 {
323 if ($inputdata["field$field[fieldid]"] == -1)
324 {
325 $fieldvalue[] = "''";
326 continue;
327 }
328
329 $temp = unserialize($field['selects']);
330 $fieldvalue[] = "'" . trim($temp[ intval($inputdata["field$field[fieldid]"]) ]) . "'";
331 }
332 }
333 }
334
335 if ($errorlist)
336 {
337 return $errorlist;
338 }
339
340 if (count($fieldbuild) < 1)
341 {
342 return;
343 }
344
345 $bugsys->db->query("REPLACE INTO " . TABLE_PREFIX . "bugvaluefill (bugid, " . implode(', ', $fieldbuild) . ") VALUES ($bugid, " . implode(', ', $fieldvalue) . ")");
346 }
347
348 // ####################### Start fetch_on_bits #######################
349 function fetch_on_bits($mask)
350 {
351 global $bugsys, $_PERMISSION;
352
353 $onbits = array();
354
355 $usergroupid = $bugsys->userinfo['usergroupid'];
356
357 if ($bugsys->datastore['usergroup']["$usergroupid"]['permissions'] & $_PERMISSION["$mask"])
358 {
359 foreach ($bugsys->datastore['product'] AS $id => $product)
360 {
361 if (!$product['componentmother'])
362 {
363 $onbits["$id"] = $id;
364 }
365 }
366 }
367
368 foreach ($bugsys->datastore['permission']["$usergroupid"] AS $productid => $bit)
369 {
370 if ($bit & $_PERMISSION["$mask"])
371 {
372 $onbits["$productid"] = $productid;
373 }
374 else
375 {
376 if ($onbits["$productid"])
377 {
378 unset($onbits["$productid"]);
379 }
380 }
381 }
382
383 if (count($onbits) < 1)
384 {
385 $onbits = array(0);
386 }
387
388 return implode(',', $onbits);
389 }
390
391 // #################### Start isso_pre_parse_hook ####################
392 // the pre-parse hook for ISSO's template engine
393 function isso_pre_parse_hook($template)
394 {
395 $template = preg_replace('#\$help\[(.*)\]#', '" . fetch_help_link("\1") . "', $template);
396 return $template;
397 }
398
399 // ###################### Start fetch_help_link ######################
400 // returns a prepared link to insert into templates that opens up a
401 // help popup in the user-end
402 function fetch_help_link($topic)
403 {
404 global $bugsys;
405
406 if (isset($bugsys->datastore['help']["$topic"]))
407 {
408 eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
409 return $temp;
410 }
411 else
412 {
413 if ($bugsys->debug)
414 {
415 return "[[INVALID TOPIC: $topic]]";
416 }
417 // do we want this?
418 else if (null == 1)
419 {
420 return eval('$temp = "' . $bugsys->template->fetch('help_link') . '";');
421 }
422 }
423 }
424
425 /*=====================================================================*\
426 || ###################################################################
427 || # $HeadURL$
428 || # $Id$
429 || ###################################################################
430 \*=====================================================================*/
431 ?>