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