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