]> src.bluestatic.org Git - bugdar.git/blob - admin/field.php
r836: Implementing user API in userctrl.php
[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 = " . intval($bugsys->in['fieldid']));
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 = " . intval($bugsys->in['fieldid']));
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 = " . intval($bugsys->in['fieldid']));
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 switch ($type)
108 {
109 case 'input_text':
110 $extrafields = 'defaultvalue, regexmatch, maxlength';
111 $extradata = "'" . $bugsys->in['defaultvalue'] . "', '" . $bugsys->in['regexmatch'] . "', " . intval($bugsys->in['maxlength']);
112 $extraupdate = "defaultvalue = '" . $bugsys->in['defaultvalue'] . "', regexmatch = '" . $bugsys->in['regexmatch'] . "', maxlength = " . intval($bugsys->in['maxlength']);
113 break;
114
115 case 'input_checkbox':
116 $extrafields = 'defaultvalue';
117 $extradata = intval($bugsys->in['defaultvalue']);
118 $extraupdate = "defaultvalue = " . intval($bugsys->in['defaultvalue']);
119 break;
120
121 case 'select_single':
122 $extrafields = 'selects, usedefault';
123
124 // can't use explode() here because explode() returns !empty() when splitting an empty string
125 // so we have to use preg_split with the PREG_SPLIT_NO_EMPTY flag to prevent this
126 $selects = preg_split("#\n#", trim($bugsys->in['selects']), 0, PREG_SPLIT_NO_EMPTY);
127 array_walk($selects, 'trim');
128 if (count($selects) < 1)
129 {
130 $admin->error($lang->string('You need to specify some select values.'));
131 }
132
133 $extradata = "'" . $bugsys->escape(serialize($selects)) . "', " . intval($bugsys->in['usedefault']);
134 $extraupdate = "selects = '" . $bugsys->escape(serialize($selects)) . "', usedefault = " . intval($bugsys->in['usedefault']);
135 break;
136 }
137
138 if ($add)
139 {
140 $db->query("
141 INSERT INTO " . TABLE_PREFIX . "bugfield
142 (name, description, type, required, cansearch, $extrafields)
143 VALUES
144 ('" . $bugsys->in['name'] . "',
145 '" . $bugsys->in['description'] . "', '$type', " . intval($bugsys->in['required']) . ",
146 " . intval($bugsys->in['cansearch']) . ", $extradata
147 )"
148 );
149
150 $fieldid = $db->insert_id();
151
152 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill ADD field$fieldid MEDIUMTEXT NULL");
153 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill");
154 }
155 else
156 {
157 $db->query("
158 UPDATE " . TABLE_PREFIX . "bugfield
159 SET name = '" . $bugsys->in['name'] . "',
160 description = '" . $bugsys->in['description'] . "',
161 required = " . intval($bugsys->in['required']) . ",
162 cansearch = " . intval($bugsys->in['cansearch']) . ",
163 $extraupdate
164 WHERE fieldid = " . intval($bugsys->in['fieldid'])
165 );
166
167 $fieldid = intval($bugsys->in['fieldid']);
168 }
169
170 foreach ($bugsys->in['custom'] AS $usergroupid => $mask)
171 {
172 $values[] = intval($usergroupid) . ", $fieldid, " . intval($mask);
173 }
174
175 $db->query("
176 REPLACE INTO " . TABLE_PREFIX . "bugfieldpermission
177 (usergroupid, fieldid, mask)
178 VALUES
179 (" . implode("),\n\t\t\t(", $values) . ")"
180 );
181
182 build_user_help();
183
184 $admin->redirect('field.php?do=modify', ($add ? $lang->string('The custom field has been added') : $lang->string('The custom field has been updated')));
185 }
186
187 // ###################################################################
188
189 if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
190 {
191 $add = (($_REQUEST['do'] == 'add') ? true : false);
192 $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false);
193 $edit = (($add) ? false : true);
194
195 $admin->page_start(($add ? $lang->string('Add New Custom Field') : $lang->string('Edit Custom Field')));
196 $admin->form_start('field.php', ($typeselect ? 'add' : 'update'));
197 if ($add AND !$typeselect)
198 {
199 $admin->form_hidden_field('type', $bugsys->in['type']);
200 }
201 if ($typeselect)
202 {
203 $admin->form_hidden_field('step', 1);
204 $admin->table_start(true, '40%');
205 $admin->table_head($lang->string('Select Type'), 2, 'custom_bug_fields');
206 }
207 else
208 {
209 $admin->table_start();
210 $admin->table_head(($add ? $lang->string('Add New Bug Field') . ' - ' . $TYPES[ $bugsys->in['type'] ] : $lang->string('Edit Field')), 2, 'custom_bug_fields_options');
211 }
212
213 if ($edit)
214 {
215 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . intval($bugsys->in['fieldid']));
216 if (!$field)
217 {
218 $admin->error($lang->getlex('error_invalid_id'));
219 }
220
221 $admin->form_hidden_field('fieldid', $field['fieldid']);
222 }
223
224 if (!$typeselect)
225 {
226 $type = (($add) ? $bugsys->in['type'] : $field['type']);
227 }
228
229 // show type selector
230 if (empty($bugsys->in['step']) AND $add)
231 {
232 foreach ($TYPES AS $name => $description)
233 {
234 $admin->list_item($description, $name);
235 }
236 $admin->row_list($lang->string('Field Type'), 'type', false);
237
238 $admin->row_submit();
239 $admin->table_end();
240 }
241 // have type, do that funkay thing!
242 else
243 {
244 if (!isset($TYPES["$type"]))
245 {
246 $admin->error($lang->getlex('error_invalid_id'));
247 }
248
249 // global fields
250 $admin->row_span($lang->string('Global Fields'), 'thead', 'center');
251 $admin->row_text($lang->string('Field Type'), $TYPES["$type"]);
252 $admin->row_input($lang->string('Display Name'), 'name', $field['name']);
253 $admin->row_textarea($lang->string('Description'), 'description', $field['description']);
254 $admin->row_yesno($lang->string('Required'), 'required', $field['required']);
255 $admin->row_yesno($lang->string('Can Be Searched'), 'cansearch', ((isset($field['cansearch'])) ? $field['cansearch'] : true));
256
257 // type-specific fields
258 $admin->row_span($lang->string('Type-Specific Fields'), 'thead', 'center');
259
260 switch ($type)
261 {
262 case 'input_text':
263 $admin->row_input($lang->string('Default Value'), 'defaultvalue', $field['defaultvalue']);
264 $admin->row_input($lang->string('Regular Expression Match'), 'regexmatch', $field['regexmatch']);
265 $admin->row_input($lang->string('Maximum Length'), 'maxlength', $field['maxlength'], 2, 10);
266 break;
267
268 case 'input_checkbox':
269 $admin->row_yesno($lang->string('Checked By Default'), 'defaultvalue', $field['defaultvalue']);
270 break;
271
272 case 'select_single':
273 $admin->row_textarea($lang->string('Selection Values'), 'selects', stripslashes(implode("\n", unserialize($field['selects']))));
274 $admin->row_yesno($lang->string('Make the First Option Default'), 'usedefault', $field['usedefault']);
275 break;
276 }
277
278 $admin->table_end();
279
280 // custom field permissions
281 $admin->table_start();
282 $admin->table_head($lang->string('Custom Field Permissions'));
283
284 if ($edit)
285 {
286 $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = $field[fieldid]");
287 while ($perm = $db->fetch_array($perms))
288 {
289 $permissions["$perm[usergroupid]"] = $perm['mask'];
290 }
291 }
292
293 $usergroups = $db->query("SELECT * FROM " . TABLE_PREFIX . "usergroup ORDER BY usergroupid");
294 while ($usergroup = $db->fetch_array($usergroups))
295 {
296 unset($listitem);
297 $admin->list_item($lang->string('No Permission'), 0, $permissions["$usergroup[usergroupid]"] == 0);
298 $admin->list_item($lang->string('Can View Field'), 1, $permissions["$usergroup[usergroupid]"] == 1);
299 $admin->list_item($lang->string('Can View, Edit Field'), 2, $permissions["$usergroup[usergroupid]"] == 2);
300 $admin->row_list($usergroup['title'], "custom[$usergroup[usergroupid]]");
301 }
302
303 $admin->table_end();
304
305 // end table
306 $admin->table_start();
307 $admin->row_submit((($edit) ? '<a href="field.php?do=delete&amp;fieldid=' . $field['fieldid'] . '">[' . $lang->string('Delete Field') . ']</a>' : ''));
308 $admin->table_end();
309 $admin->form_end();
310 }
311
312 $admin->page_end();
313 }
314
315 // ###################################################################
316
317 if ($_REQUEST['do'] == 'modify')
318 {
319 $admin->page_start($lang->string('Additional Bug Fields'));
320
321 $admin->table_start();
322 $admin->table_head($lang->string('Additional Bug Fields'), 3, 'custom_bug_fields');
323 $admin->table_column_head(array($lang->string('Display Name/Description'), $lang->string('Field ID'), $lang->string('Actions')));
324
325 $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield ORDER BY fieldid ASC");
326 while ($field = $db->fetch_array($fields))
327 {
328 $admin->row_multi_item(
329 array(
330 "$field[name]<div class=\"smallfont\"><em>$field[description]</em></div>" => 'l',
331 "$field[fieldid]" => 'c',
332 "<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'
333 )
334 );
335 }
336
337 $admin->row_span('<a href="field.php?do=add">[' . $lang->string('Add New Bug Field') . ']</a>', 'tfoot', 'center', 3);
338 $admin->table_end();
339
340 $admin->page_end();
341 }
342
343 /*=====================================================================*\
344 || ###################################################################
345 || # $HeadURL$
346 || # $Id$
347 || ###################################################################
348 \*=====================================================================*/
349 ?>