r887: Removing all the annoying calls to intval() in place of ISSO's cleaning framework
[bugdar.git] / admin / field.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 require_once('./global.php');
23 require_once('./includes/functions_datastore.php');
24
25 if (!can_perform('canadminfields'))
26 {
27 admin_login();
28 }
29
30 $TYPES = array(
31 'input_text' => $lang->string('Single-Line Text Box'),
32 'input_checkbox' => $lang->string('Checkbox Flag'),
33 'select_single' => $lang->string('Drop-Down Menu'),
34 );
35
36 // ###################################################################
37
38 if (empty($_REQUEST['do']))
39 {
40 $_REQUEST['do'] = 'modify';
41 }
42
43 // ###################################################################
44
45 if ($_REQUEST['do'] == 'kill')
46 {
47 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->input_clean('fieldid', TYPE_UINT));
48 if (!$field)
49 {
50 $admin->error($lang->getlex('error_invalid_id'));
51 }
52
53 $db->query("DELETE FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = $field[fieldid]");
54 $db->query("DELETE FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = $field[fieldid]");
55 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill DROP field$field[fieldid]");
56 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill");
57
58 build_user_help();
59
60 $admin->redirect('field.php?do=modify');
61 }
62
63 // ###################################################################
64
65 if ($_REQUEST['do'] == 'delete')
66 {
67 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->input_clean('fieldid', TYPE_UINT));
68 if (!$field)
69 {
70 $admin->error($lang->getlex('error_invalid_id'));
71 }
72
73 $admin->page_confirm($lang->string('Are you sure you want to delete this bug field? Doing so will remove everything for this field and it cannot be undone!'), "field.php?do=kill&amp;fieldid=$field[fieldid]");
74 }
75
76 // ###################################################################
77
78 if ($_REQUEST['do'] == 'update')
79 {
80 $edit = false;
81 $add = true;
82
83 $type = $bugsys->in['type'];
84
85 if ($bugsys->in['fieldid'])
86 {
87 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->input_clean('fieldid', TYPE_UINT));
88 if (!$field)
89 {
90 $admin->error($lang->getlex('error_invalid_id'));
91 }
92
93 $edit = true;
94 $add = false;
95 $type = $field['type'];
96 }
97
98 if (empty($bugsys->in['name']))
99 {
100 $admin->error($lang->string('You must specify a display name.'));
101 }
102 if (!isset($TYPES["$type"]) AND $add)
103 {
104 $admin->error($lang->string('Invalid field type specified.'));
105 }
106
107 $bugsys->input_clean_array(array(
108 'maxlength' => TYPE_UINT,
109 'usedefault' => TYPE_UINT,
110 'required' => TYPE_UINT,
111 'cansearch' => TYPE_UINT,
112 'fieldid' => TYPE_UINT
113 ));
114
115 switch ($type)
116 {
117 case 'input_text':
118 $extrafields = 'defaultvalue, regexmatch, maxlength';
119 $extradata = "'" . $bugsys->in['defaultvalue'] . "', '" . $bugsys->in['regexmatch'] . "', " . $bugsys->in['maxlength'];
120 $extraupdate = "defaultvalue = '" . $bugsys->in['defaultvalue'] . "', regexmatch = '" . $bugsys->in['regexmatch'] . "', maxlength = " . $bugsys->in['maxlength'];
121 break;
122
123 case 'input_checkbox':
124 $extrafields = 'defaultvalue';
125 $extradata = $bugsys->input_clean('defaultvalue', TYPE_UINT);
126 $extraupdate = "defaultvalue = " . $bugsys->in['defaultvalue'];
127 break;
128
129 case 'select_single':
130 $extrafields = 'selects, usedefault';
131
132 // can't use explode() here because explode() returns !empty() when splitting an empty string
133 // so we have to use preg_split with the PREG_SPLIT_NO_EMPTY flag to prevent this
134 $selects = preg_split("#\n#", trim($bugsys->in['selects']), 0, PREG_SPLIT_NO_EMPTY);
135 array_walk($selects, 'trim');
136 if (count($selects) < 1)
137 {
138 $admin->error($lang->string('You need to specify some select values.'));
139 }
140
141 $extradata = "'" . $bugsys->escape(serialize($selects)) . "', " . $bugsys->in['usedefault'];
142 $extraupdate = "selects = '" . $bugsys->escape(serialize($selects)) . "', usedefault = " . $bugsys->in['usedefault'];
143 break;
144 }
145
146 if ($add)
147 {
148 $db->query("
149 INSERT INTO " . TABLE_PREFIX . "bugfield
150 (name, description, type, required, cansearch, $extrafields)
151 VALUES
152 ('" . $bugsys->in['name'] . "',
153 '" . $bugsys->in['description'] . "', '$type', " . $bugsys->input_clean('required', TYPE_UINT) . ",
154 " . $bugsys->input_clean('cansearch', TYPE_UINT) . ", $extradata
155 )"
156 );
157
158 $fieldid = $db->insert_id();
159
160 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill ADD field$fieldid MEDIUMTEXT NULL");
161 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill");
162 }
163 else
164 {
165 $db->query("
166 UPDATE " . TABLE_PREFIX . "bugfield
167 SET name = '" . $bugsys->in['name'] . "',
168 description = '" . $bugsys->in['description'] . "',
169 required = " . $bugsys->in['required']) . ",
170 cansearch = " . $bugsys->in['cansearch']) . ",
171 $extraupdate
172 WHERE fieldid = " . $bugsys->in['fieldid']
173 );
174
175 $fieldid = $bugsys->in['fieldid'];
176 }
177
178 $bugsys->input_clean('custom', TYPE_UINT);
179 foreach ($bugsys->in['custom'] AS $usergroupid => $mask)
180 {
181 $values[] = $bugsys->clean($usergroupid, TYPE_UINT) . ", $fieldid, " . $mask;
182 }
183
184 $db->query("
185 REPLACE INTO " . TABLE_PREFIX . "bugfieldpermission
186 (usergroupid, fieldid, mask)
187 VALUES
188 (" . implode("),\n\t\t\t(", $values) . ")"
189 );
190
191 build_user_help();
192
193 $admin->redirect('field.php?do=modify', ($add ? $lang->string('The custom field has been added') : $lang->string('The custom field has been updated')));
194 }
195
196 // ###################################################################
197
198 if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
199 {
200 $bugsys->input_clean('fieldid', TYPE_UINT);
201
202 $add = (($_REQUEST['do'] == 'add') ? true : false);
203 $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false);
204 $edit = (($add) ? false : true);
205
206 $admin->page_start(($add ? $lang->string('Add New Custom Field') : $lang->string('Edit Custom Field')));
207 $admin->form_start('field.php', ($typeselect ? 'add' : 'update'));
208 if ($add AND !$typeselect)
209 {
210 $admin->form_hidden_field('type', $bugsys->in['type']);
211 }
212 if ($typeselect)
213 {
214 $admin->form_hidden_field('step', 1);
215 $admin->table_start(true, '40%');
216 $admin->table_head($lang->string('Select Type'), 2, 'custom_bug_fields');
217 }
218 else
219 {
220 $admin->table_start();
221 $admin->table_head(($add ? $lang->string('Add New Bug Field') . ' - ' . $TYPES[ $bugsys->in['type'] ] : $lang->string('Edit Field')), 2, 'custom_bug_fields_options');
222 }
223
224 if ($edit)
225 {
226 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->in['fieldid']);
227 if (!$field)
228 {
229 $admin->error($lang->getlex('error_invalid_id'));
230 }
231
232 $admin->form_hidden_field('fieldid', $field['fieldid']);
233 }
234
235 if (!$typeselect)
236 {
237 $type = (($add) ? $bugsys->in['type'] : $field['type']);
238 }
239
240 // show type selector
241 if (empty($bugsys->in['step']) AND $add)
242 {
243 foreach ($TYPES AS $name => $description)
244 {
245 $admin->list_item($description, $name);
246 }
247 $admin->row_list($lang->string('Field Type'), 'type', false);
248
249 $admin->row_submit();
250 $admin->table_end();
251 }
252 // have type, do that funkay thing!
253 else
254 {
255 if (!isset($TYPES["$type"]))
256 {
257 $admin->error($lang->getlex('error_invalid_id'));
258 }
259
260 // global fields
261 $admin->row_span($lang->string('Global Fields'), 'thead', 'center');
262 $admin->row_text($lang->string('Field Type'), $TYPES["$type"]);
263 $admin->row_input($lang->string('Display Name'), 'name', $field['name']);
264 $admin->row_textarea($lang->string('Description'), 'description', $field['description']);
265 $admin->row_yesno($lang->string('Required'), 'required', $field['required']);
266 $admin->row_yesno($lang->string('Can Be Searched'), 'cansearch', ((isset($field['cansearch'])) ? $field['cansearch'] : true));
267
268 // type-specific fields
269 $admin->row_span($lang->string('Type-Specific Fields'), 'thead', 'center');
270
271 switch ($type)
272 {
273 case 'input_text':
274 $admin->row_input($lang->string('Default Value'), 'defaultvalue', $field['defaultvalue']);
275 $admin->row_input($lang->string('Regular Expression Match'), 'regexmatch', $field['regexmatch']);
276 $admin->row_input($lang->string('Maximum Length'), 'maxlength', $field['maxlength'], 2, 10);
277 break;
278
279 case 'input_checkbox':
280 $admin->row_yesno($lang->string('Checked By Default'), 'defaultvalue', $field['defaultvalue']);
281 break;
282
283 case 'select_single':
284 $admin->row_textarea($lang->string('Selection Values'), 'selects', stripslashes(implode("\n", unserialize($field['selects']))));
285 $admin->row_yesno($lang->string('Make the First Option Default'), 'usedefault', $field['usedefault']);
286 break;
287 }
288
289 $admin->table_end();
290
291 // custom field permissions
292 $admin->table_start();
293 $admin->table_head($lang->string('Custom Field Permissions'));
294
295 if ($edit)
296 {
297 $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = $field[fieldid]");
298 while ($perm = $db->fetch_array($perms))
299 {
300 $permissions["$perm[usergroupid]"] = $perm['mask'];
301 }
302 }
303
304 $usergroups = $db->query("SELECT * FROM " . TABLE_PREFIX . "usergroup ORDER BY usergroupid");
305 while ($usergroup = $db->fetch_array($usergroups))
306 {
307 unset($listitem);
308 $admin->list_item($lang->string('No Permission'), 0, $permissions["$usergroup[usergroupid]"] == 0);
309 $admin->list_item($lang->string('Can View Field'), 1, $permissions["$usergroup[usergroupid]"] == 1);
310 $admin->list_item($lang->string('Can View, Edit Field'), 2, $permissions["$usergroup[usergroupid]"] == 2);
311 $admin->row_list($usergroup['title'], "custom[$usergroup[usergroupid]]");
312 }
313
314 $admin->table_end();
315
316 // end table
317 $admin->table_start();
318 $admin->row_submit((($edit) ? '<a href="field.php?do=delete&amp;fieldid=' . $field['fieldid'] . '">[' . $lang->string('Delete Field') . ']</a>' : ''));
319 $admin->table_end();
320 $admin->form_end();
321 }
322
323 $admin->page_end();
324 }
325
326 // ###################################################################
327
328 if ($_REQUEST['do'] == 'modify')
329 {
330 $admin->page_start($lang->string('Additional Bug Fields'));
331
332 $admin->table_start();
333 $admin->table_head($lang->string('Additional Bug Fields'), 3, 'custom_bug_fields');
334 $admin->table_column_head(array($lang->string('Display Name/Description'), $lang->string('Field ID'), $lang->string('Actions')));
335
336 $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield ORDER BY fieldid ASC");
337 while ($field = $db->fetch_array($fields))
338 {
339 $admin->row_multi_item(
340 array(
341 "$field[name]<div class=\"smallfont\"><em>$field[description]</em></div>" => 'l',
342 "$field[fieldid]" => 'c',
343 "<a href=\"field.php?do=edit&amp;fieldid=$field[fieldid]\">[" . $lang->string('Edit') . "]</a> <a href=\"field.php?do=delete&amp;fieldid=$field[fieldid]\">[" . $lang->string('Delete') . "]</a>" => 'c'
344 )
345 );
346 }
347
348 $admin->row_span('<a href="field.php?do=add">[' . $lang->string('Add New Bug Field') . ']</a>', 'tfoot', 'center', 3);
349 $admin->table_end();
350
351 $admin->page_end();
352 }
353
354 /*=====================================================================*\
355 || ###################################################################
356 || # $HeadURL$
357 || # $Id$
358 || ###################################################################
359 \*=====================================================================*/
360 ?>