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