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