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