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