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