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