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