r905: Fixed a bug with updating the selects items
[bugdar.git] / admin / field.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 require_once('./global.php');
23 require_once('./includes/api_field.php');
24
25 if (!can_perform('canadminfields'))
26 {
27 admin_login();
28 }
29
30 // ###################################################################
31
32 if (empty($_REQUEST['do']))
33 {
34 $_REQUEST['do'] = 'modify';
35 }
36
37 // ###################################################################
38
39 if ($_REQUEST['do'] == 'kill')
40 {
41 $field = new FieldAPI($bugsys);
42 $field->set('fieldid', $bugsys->in['fieldid']);
43 $field->set_condition();
44 $field->delete();
45
46 $admin->redirect('field.php?do=modify');
47 }
48
49 // ###################################################################
50
51 if ($_REQUEST['do'] == 'delete')
52 {
53 $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=" . $bugsys->in['fieldid']);
54 }
55
56 // ###################################################################
57
58 if ($_REQUEST['do'] == 'update')
59 {
60 $edit = false;
61 $add = true;
62
63 $type = $bugsys->in['type'];
64
65 $field = new FieldAPI($bugsys);
66
67 if ($bugsys->in['fieldid'])
68 {
69 $field->set('fieldid', $bugsys->in['fieldid']);
70 $field->set_condition();
71 $field->fetch();
72
73 $edit = true;
74 $add = false;
75 $type = $field->objdata['type'];
76 }
77 else
78 {
79 $field->set('type', $type);
80 }
81
82 switch ($type)
83 {
84 case 'input_text':
85 $field->set('defaultvalue', $bugsys->in['defaultvalue']);
86 $field->set('regexmatch', $bugsys->in['regexmatch']);
87 $field->set('maxlength', $bugsys->in['maxlength']);
88 break;
89
90 case 'input_checkbox':
91 $field->set('defaultvalue', $bugsys->in['defaultvalue']);
92 break;
93
94 case 'select_single':
95 $field->set('selects', $bugsys->in['selects']);
96 $field->set('usedefault', $bugsys->in['usedefault']);
97 break;
98 }
99
100 $field->set('name', $bugsys->in['name']);
101 $field->set('description', $bugsys->in['description']);
102 $field->set('required', $bugsys->in['required']);
103 $field->set('cansearch', $bugsys->in['cansearch']);
104
105 if ($add)
106 {
107 $field->insert();
108 $fieldid = $field->insertid;
109
110 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill ADD field$fieldid MEDIUMTEXT NULL");
111 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill");
112 }
113 else
114 {
115 $field->update();
116 $fieldid = $field->values['fieldid'];
117 }
118
119 $bugsys->input_clean('custom', TYPE_UINT);
120 foreach ($bugsys->in['custom'] AS $usergroupid => $mask)
121 {
122 $values[] = $bugsys->clean($usergroupid, TYPE_UINT) . ", $fieldid, " . $mask;
123 }
124
125 $db->query("
126 REPLACE INTO " . TABLE_PREFIX . "bugfieldpermission
127 (usergroupid, fieldid, mask)
128 VALUES
129 (" . implode("),\n\t\t\t(", $values) . ")"
130 );
131
132 $admin->redirect('field.php?do=modify', ($add ? $lang->string('The custom field has been added') : $lang->string('The custom field has been updated')));
133 }
134
135 // ###################################################################
136
137 if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
138 {
139 $field = new FieldAPI($bugsys);
140
141 $add = (($_REQUEST['do'] == 'add') ? true : false);
142 $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false);
143 $edit = (($add) ? false : true);
144
145 $admin->page_start(($add ? $lang->string('Add New Custom Field') : $lang->string('Edit Custom Field')));
146 $admin->form_start('field.php', ($typeselect ? 'add' : 'update'));
147 if ($add AND !$typeselect)
148 {
149 $admin->form_hidden_field('type', $bugsys->in['type']);
150 }
151 if ($typeselect)
152 {
153 $admin->form_hidden_field('step', 1);
154 $admin->table_start(true, '40%');
155 $admin->table_head($lang->string('Select Type'), 2, 'custom_bug_fields');
156 }
157 else
158 {
159 $admin->table_start();
160 $admin->table_head(($add ? $lang->string('Add New Bug Field') . ' - ' . FieldAPI::field_types($bugsys->in['type']) : $lang->string('Edit Field')), 2, 'custom_bug_fields_options');
161 }
162
163 if ($edit)
164 {
165 $field->set('fieldid', $bugsys->in['fieldid']);
166 $field->set_condition();
167 $field->fetch();
168
169 $admin->form_hidden_field('fieldid', $field->objdata['fieldid']);
170 }
171
172 if (!$typeselect)
173 {
174 $type = (($add) ? $bugsys->in['type'] : $field->objdata['type']);
175 }
176
177 // show type selector
178 if (empty($bugsys->in['step']) AND $add)
179 {
180 foreach (FieldAPI::field_types() AS $name => $description)
181 {
182 $admin->list_item($description, $name);
183 }
184 $admin->row_list($lang->string('Field Type'), 'type', false);
185
186 $admin->row_submit();
187 $admin->table_end();
188 }
189 // have type, do that funkay thing!
190 else
191 {
192 $field->set('type', $type);
193
194 // global fields
195 $admin->row_span($lang->string('Global Fields'), 'thead', 'center');
196 $admin->row_text($lang->string('Field Type'), FieldAPI::field_types($type));
197 $admin->row_input($lang->string('Display Name'), 'name', $field->objdata['name']);
198 $admin->row_textarea($lang->string('Description'), 'description', $field->objdata['description']);
199 $admin->row_yesno($lang->string('Required'), 'required', $field->objdata['required']);
200 $admin->row_yesno($lang->string('Can Be Searched'), 'cansearch', ((isset($field->objdata['cansearch'])) ? $field->objdata['cansearch'] : true));
201
202 // type-specific fields
203 $admin->row_span($lang->string('Type-Specific Fields'), 'thead', 'center');
204
205 switch ($type)
206 {
207 case 'input_text':
208 $admin->row_input($lang->string('Default Value'), 'defaultvalue', $field->objdata['defaultvalue']);
209 $admin->row_input($lang->string('Regular Expression Match'), 'regexmatch', $field->objdata['regexmatch']);
210 $admin->row_input($lang->string('Maximum Length'), 'maxlength', $field->objdata['maxlength'], 2, 10);
211 break;
212
213 case 'input_checkbox':
214 $admin->row_yesno($lang->string('Checked By Default'), 'defaultvalue', $field->objdata['defaultvalue']);
215 break;
216
217 case 'select_single':
218 $admin->row_textarea($lang->string('Selection Values'), 'selects', stripslashes(implode("\n", unserialize($field->objdata['selects']))));
219 $admin->row_yesno($lang->string('Make the First Option Default'), 'usedefault', $field->objdata['usedefault']);
220 break;
221 }
222
223 $admin->table_end();
224
225 // custom field permissions
226 $admin->table_start();
227 $admin->table_head($lang->string('Custom Field Permissions'));
228
229 if ($edit)
230 {
231 $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $field->objdata['fieldid']);
232 while ($perm = $db->fetch_array($perms))
233 {
234 $permissions["$perm[usergroupid]"] = $perm['mask'];
235 }
236 }
237
238 $usergroups = $db->query("SELECT * FROM " . TABLE_PREFIX . "usergroup ORDER BY usergroupid");
239 while ($usergroup = $db->fetch_array($usergroups))
240 {
241 unset($listitem);
242 $admin->list_item($lang->string('No Permission'), 0, $permissions["$usergroup[usergroupid]"] == 0);
243 $admin->list_item($lang->string('Can View Field'), 1, $permissions["$usergroup[usergroupid]"] == 1);
244 $admin->list_item($lang->string('Can View, Edit Field'), 2, $permissions["$usergroup[usergroupid]"] == 2);
245 $admin->row_list($usergroup['title'], "custom[$usergroup[usergroupid]]");
246 }
247
248 $admin->table_end();
249
250 // end table
251 $admin->table_start();
252 $admin->row_submit((($edit) ? '<a href="field.php?do=delete&amp;fieldid=' . $field->objdata['fieldid'] . '">[' . $lang->string('Delete Field') . ']</a>' : ''));
253 $admin->table_end();
254 $admin->form_end();
255 }
256
257 $admin->page_end();
258 }
259
260 // ###################################################################
261
262 if ($_REQUEST['do'] == 'modify')
263 {
264 $admin->page_start($lang->string('Additional Bug Fields'));
265
266 $admin->table_start();
267 $admin->table_head($lang->string('Additional Bug Fields'), 3, 'custom_bug_fields');
268 $admin->table_column_head(array($lang->string('Display Name/Description'), $lang->string('Field ID'), $lang->string('Actions')));
269
270 $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield ORDER BY fieldid ASC");
271 while ($field = $db->fetch_array($fields))
272 {
273 $admin->row_multi_item(
274 array(
275 "$field[name]<div class=\"smallfont\"><em>$field[description]</em></div>" => 'l',
276 "$field[fieldid]" => 'c',
277 "<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'
278 )
279 );
280 }
281
282 $admin->row_span('<a href="field.php?do=add">[' . $lang->string('Add New Bug Field') . ']</a>', 'tfoot', 'center', 3);
283 $admin->table_end();
284
285 $admin->page_end();
286 }
287
288 /*=====================================================================*\
289 || ###################################################################
290 || # $HeadURL$
291 || # $Id$
292 || ###################################################################
293 \*=====================================================================*/
294 ?>