r202: Use MySQL NULL to handle defaults
[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 'textarea' => 'Multi-Line Text Area',
23 'input_checkbox' => 'Checkbox Flag',
24 'select_single' => 'Drop-Down Menu',
25 );
26
27 // ###################################################################
28
29 if (empty($_REQUEST['do']))
30 {
31 $_REQUEST['do'] = 'modify';
32 }
33
34 // ###################################################################
35
36 if ($_REQUEST['do'] == 'kill')
37 {
38 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . intval($bugsys->in['fieldid']));
39 if (!$field)
40 {
41 $admin->error(phrase('error_invalid_id'));
42 }
43
44 $db->query("DELETE FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = $field[fieldid]");
45 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill DROP $field[shortname]");
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['shortname']) AND $add)
87 {
88 $admin->error('You must specify a short name/call name.');
89 }
90 if ((preg_match('#[^a-z0-9_]#', $bugsys->in['shortname']) OR !preg_match('#^[a-z]#', $bugsys->in['shortname']) OR preg_match('#[^a-z0-9]$#', $bugsys->in['shortname'])) AND $add)
91 {
92 $admin->error('The short name can only contain lowercase letters, numbers, and underscores; it must also begin with a letter and cannot end in anything but a letter or a number.');
93 }
94
95 if (empty($bugsys->in['name']))
96 {
97 $admin->error('You must specify a display name.');
98 }
99 if (!isset($TYPES["$type"]) AND $add)
100 {
101 $admin->error('Invalid field type specified.');
102 }
103
104 switch ($type)
105 {
106 case 'input_text':
107 case 'textarea':
108 $extrafields = 'defaultvalue, regexmatch, maxlength';
109 $extradata = "'" . $bugsys->in['defaultvalue'] . "', '" . $bugsys->in['regexmatch'] . "', " . intval($bugsys->in['maxlength']);
110 $extraupdate = "defaultvalue = '" . $bugsys->in['defaultvalue'] . "', regexmatch = '" . $bugsys->in['regexmatch'] . "', maxlength = " . intval($bugsys->in['maxlength']);
111 break;
112
113 case 'input_checkbox':
114 $extrafields = 'defaultvalue';
115 $extradata = intval($bugsys->in['defaultvalue']);
116 $extraupdate = "defaultvalue = " . intval($bugsys->in['defaultvalue']);
117 break;
118
119 case 'select_single':
120 $extrafields = 'selects, usedefault';
121
122 // can't use explode() here because explode() returns !empty() when splitting an empty string
123 // so we have to use preg_split with the PREG_SPLIT_NO_EMPTY flag to prevent this
124 $selects = preg_split("#\n#", trim($bugsys->in['selects']), 0, PREG_SPLIT_NO_EMPTY);
125 array_walk($selects, 'trim');
126 if (count($selects) < 1)
127 {
128 $admin->error('You need to specify some select values.');
129 }
130
131 $extradata = "'" . $bugsys->escape(serialize($selects)) . "', " . intval($bugsys->in['usedefault']);
132 $extraupdate = "selects = '" . $bugsys->escape(serialize($selects)) . "', usedefault = " . intval($bugsys->in['usedefault']);
133 break;
134 }
135
136 if ($add)
137 {
138 if ($db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE shortname = '" . $bugsys->in['shortname'] . "'") OR $bugsys->in['shortname'] == 'bugid')
139 {
140 $admin->error('That short name is already in use, please change it so it is unique.');
141 }
142
143 $db->query("
144 INSERT INTO bugfield
145 (shortname, name, description, type, required, private, $extrafields)
146 VALUES
147 ('" . $bugsys->in['shortname'] . "', '" . $bugsys->in['name'] . "',
148 '" . $bugsys->in['description'] . "', '$type', " . intval($bugsys->in['required']) . ",
149 " . intval($bugsys->in['private']) . ", $extradata
150 )"
151 );
152
153 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill ADD " . $bugsys->in['shortname'] . " MEDIUMTEXT NULL");
154 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill");
155 }
156 else
157 {
158 $db->query("
159 UPDATE " . TABLE_PREFIX . "bugfield
160 SET name = '" . $bugsys->in['name'] . "',
161 description = '" . $bugsys->in['description'] . "',
162 required = " . intval($bugsys->in['required']) . ",
163 private = " . intval($bugsys->in['private']) . ",
164 $extraupdate
165 WHERE fieldid = " . intval($bugsys->in['fieldid'])
166 );
167 }
168
169 $admin->redirect('fields.php?do=modify', (($add) ? 'The custom bug field has been added.' : 'The bug field has been updated.'));
170 }
171
172 // ###################################################################
173
174 if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
175 {
176 $add = (($_REQUEST['do'] == 'add') ? true : false);
177 $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false);
178 $edit = (($add) ? false : true);
179
180 $admin->page_start(phrase((($add) ? 'add_new_field' : 'edit_field')));
181 $admin->form_start('fields.php', (($typeselect) ? 'add' : 'update'));
182 if ($add AND !$typeselect)
183 {
184 $admin->form_hidden_field('type', $bugsys->in['type']);
185 }
186 if ($typeselect)
187 {
188 $admin->form_hidden_field('step', 1);
189 $admin->table_start(true, '40%');
190 $admin->table_head('Select Type');
191 }
192 else
193 {
194 $admin->table_start();
195 $admin->table_head((($add) ? phrase('add_new_field') . ' - ' . $TYPES[ $bugsys->in['type'] ] : phrase('edit_field')));
196 }
197
198 if ($edit)
199 {
200 $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . intval($bugsys->in['fieldid']));
201 if (!$field)
202 {
203 $admin->error(phrase('error_invalid_id'));
204 }
205
206 $admin->form_hidden_field('fieldid', $field['fieldid']);
207 }
208
209 if (!$typeselect)
210 {
211 $type = (($add) ? $bugsys->in['type'] : $field['type']);
212 }
213
214 // show type selector
215 if (empty($bugsys->in['step']) AND $add)
216 {
217 foreach ($TYPES AS $name => $description)
218 {
219 $admin->list_item($description, $name);
220 }
221 $admin->row_list('Select field type', 'type', false);
222
223 $admin->row_submit();
224 $admin->table_end();
225 }
226 // have type, do that funkay thing!
227 else
228 {
229 if (!isset($TYPES["$type"]))
230 {
231 $admin->error(phrase('error_invalid_id'));
232 }
233
234 // global fields
235 $admin->row_span('Global Fields', 'thead', 'center');
236 $admin->row_text('Field Type', $TYPES["$type"]);
237 $admin->row_input('Short Name/Call Name', 'shortname', $field['shortname']);
238 $admin->row_input('Display Name', 'name', $field['name']);
239 $admin->row_textarea('Description', 'description', $field['description']);
240 $admin->row_yesno('Required', 'required', $field['required']);
241 $admin->row_yesno('Private Field', 'private', $field['private']);
242
243 // type-specific fields
244 $admin->row_span('Type-Specific Fields', 'thead', 'center');
245
246 switch ($type)
247 {
248 case 'input_text':
249 $admin->row_input('Default Value', 'defaultvalue', $field['defaultvalue']);
250 $admin->row_input('Regular Expression Match', 'regexmatch', $field['regexmatch']);
251 $admin->row_input('Maximum Length', 'maxlength', $field['maxlength'], 2, 10);
252 break;
253
254 case 'textarea':
255 $admin->row_textarea('Default Value', 'defaultvalue', $field['defaultvalue']);
256 $admin->row_input('Regular Expression Match', 'regexmatch', $field['regexmatch']);
257 $admin->row_input('Maximum Length', 'maxlength', $field['maxlength'], 2, 10);
258 break;
259
260 case 'input_checkbox':
261 $admin->row_yesno('Checked By Default', 'defaultvalue', $field['defaultvalue']);
262 break;
263
264 case 'select_single':
265 $admin->row_textarea('Selection Values', 'selects', implode("\n", unserialize($field['selects'])));
266 $admin->row_yesno('Make the First Option Default', 'usedefault', $field['usedefault']);
267 break;
268 }
269
270 // end table
271 $admin->row_submit((($edit) ? '<a href="fields.php?do=delete&amp;fieldid=' . $field['fieldid'] . '">[Delete Field]</a>' : ''));
272 $admin->table_end();
273 $admin->form_end();
274 }
275
276 $admin->page_end();
277 }
278
279 // ###################################################################
280
281 if ($_REQUEST['do'] == 'modify')
282 {
283 $admin->page_start(phrase('additional_bug_fields'));
284
285 $admin->table_start();
286 $admin->table_head(phrase('additional_bug_fields'), 3);
287 $admin->table_column_head(array('Display Name/Description', 'Short Name/Field ID', 'Actions'));
288
289 $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield ORDER BY fieldid ASC");
290 while ($field = $db->fetch_array($fields))
291 {
292 $admin->row_multi_item(
293 array(
294 "$field[name]<div class=\"smallfont\"><em>$field[description]</em></div>" => 'l',
295 "$field[shortname] (fieldid: $field[fieldid])" => 'c',
296 "<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'
297 )
298 );
299 }
300
301 $admin->row_span('<input type="button" name="addug" value=" ' . phrase('add_new_field') . ' " onclick="window.location = \'fields.php?do=add\';" />', 'tfoot', 'center', 3);
302 $admin->table_end();
303
304 $admin->page_end();
305 }
306
307 /*=====================================================================*\
308 || ###################################################################
309 || # $HeadURL$
310 || # $Id$
311 || ###################################################################
312 \*=====================================================================*/
313 ?>