Remove includes/class_api_error.php and all of the places we require() it
[bugdar.git] / admin / field.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 Blue Static
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 2 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 APIError(array(new API_Error_Handler($admin), 'admin_error'));
26
27 NavLinks::fieldsPages();
28 $navigator->set_focus('tab', 'fields', null);
29
30 if (!can_perform('canadminfields'))
31 {
32 admin_login();
33 }
34
35 // ###################################################################
36
37 if (empty($_REQUEST['do']))
38 {
39 $_REQUEST['do'] = 'modify';
40 }
41
42 // ###################################################################
43
44 if ($_REQUEST['do'] == 'kill')
45 {
46 $field = new FieldAPI();
47 $field->set('fieldid', $input->in['fieldid']);
48 $field->set_condition();
49 $field->delete();
50
51 $admin->redirect('field.php?do=modify');
52 }
53
54 // ###################################################################
55
56 if ($_REQUEST['do'] == 'delete')
57 {
58 $admin->page_confirm(T('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', 'kill', array('fieldid' => $input->in['fieldid']));
59 }
60
61 // ###################################################################
62
63 if ($_REQUEST['do'] == 'update')
64 {
65 $edit = false;
66 $add = true;
67
68 $type = $input->in['type'];
69
70 $field = new FieldAPI();
71
72 if ($input->in['fieldid'])
73 {
74 $field->set('fieldid', $input->in['fieldid']);
75 $field->set_condition();
76 $field->fetch();
77
78 $edit = true;
79 $add = false;
80 $type = $field->record['type'];
81 }
82 else
83 {
84 $field->set('type', $type);
85 }
86
87 switch ($type)
88 {
89 case 'input_text':
90 $field->set('defaultvalue', $input->in['defaultvalue']);
91 $field->set('regexmatch', $input->in['regexmatch']);
92 $field->set('maxlength', $input->in['maxlength']);
93 break;
94
95 case 'input_checkbox':
96 $field->set('defaultvalue', $input->in['defaultvalue']);
97 break;
98
99 case 'select_single':
100 $field->set('selects', $input->in['selects']);
101 $field->set('usedefault', $input->in['usedefault']);
102 break;
103 }
104
105 $field->set('name', $input->in['name']);
106 $field->set('description', $input->in['description']);
107 $field->set('required', $input->in['required']);
108 $field->set('cansearch', $input->in['cansearch']);
109
110 if ($add)
111 {
112 $field->insert();
113 $fieldid = $field->insertid;
114
115 $db->query("ALTER TABLE " . TABLE_PREFIX . "bug ADD custom$fieldid MEDIUMTEXT NULL");
116 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bug");
117 }
118 else
119 {
120 $field->update();
121 $fieldid = $field->values['fieldid'];
122 }
123
124 $input->inputClean('custom', TYPE_UINT);
125 foreach ($input->in['custom'] AS $usergroupid => $mask)
126 {
127 $values[] = $bugsys->clean($usergroupid, TYPE_UINT) . ", $fieldid, " . $mask;
128 }
129
130 $db->query("
131 REPLACE INTO " . TABLE_PREFIX . "bugfieldpermission
132 (usergroupid, fieldid, mask)
133 VALUES
134 (" . implode("),\n\t\t\t(", $values) . ")"
135 );
136
137 $admin->redirect('field.php?do=modify', ($add ? T('The custom field has been added') : T('The custom field has been updated')));
138 }
139
140 // ###################################################################
141
142 if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
143 {
144 $field = new FieldAPI();
145
146 $add = (($_REQUEST['do'] == 'add') ? true : false);
147 $typeselect = (($add AND empty($input->in['step'])) ? true : false);
148 $edit = (($add) ? false : true);
149
150 if ($add)
151 {
152 NavLinks::fieldsAdd();
153 $navigator->set_focus('link', 'fields-add', 'fields');
154 }
155 else
156 {
157 NavLinks::fieldsEdit($input->in['fieldid']);
158 $navigator->set_focus('link', 'fields-pages-fields', 'fields-pages');
159 }
160
161 $admin->page_start(($add ? T('Add New Custom Field') : T('Edit Custom Field')));
162 $admin->form_start('field.php', ($typeselect ? 'add' : 'update'));
163 if ($add AND !$typeselect)
164 {
165 $admin->form_hidden_field('type', $input->in['type']);
166 }
167 if ($typeselect)
168 {
169 $admin->form_hidden_field('step', 1);
170 $admin->table_start(true, '40%');
171 $admin->table_head(T('Select Type'));
172 }
173 else
174 {
175 $admin->table_start();
176 $admin->table_head(($add ? T('Add New Bug Field') . ' - ' . FieldAPI::field_types($input->in['type']) : T('Edit Field')));
177 }
178
179 if ($edit)
180 {
181 $field->set('fieldid', $input->in['fieldid']);
182 $field->set_condition();
183 $field->fetch();
184
185 $admin->form_hidden_field('fieldid', $field->record['fieldid']);
186 }
187
188 if (!$typeselect)
189 {
190 $type = (($add) ? $input->in['type'] : $field->record['type']);
191 }
192
193 // show type selector
194 if (empty($input->in['step']) AND $add)
195 {
196 foreach (FieldAPI::field_types() AS $name => $description)
197 {
198 $admin->list_item($description, $name);
199 }
200 $admin->row_list(T('Field Type'), 'type', false);
201
202 $admin->row_submit();
203 $admin->table_end();
204 }
205 // have type, do that funkay thing!
206 else
207 {
208 $field->set('type', $type);
209
210 // global fields
211 $admin->row_span(T('Global Fields'), 'thead', 'center');
212 $admin->row_text(T('Field Type'), FieldAPI::field_types($type));
213 $admin->row_input(T('Display Name'), 'name', $field->record['name']);
214 $admin->row_textarea(T('Description'), 'description', $field->record['description']);
215 $admin->row_yesno(T('Required'), 'required', $field->record['required']);
216 $admin->row_yesno(T('Can Be Searched'), 'cansearch', ((isset($field->record['cansearch'])) ? $field->record['cansearch'] : true));
217
218 // type-specific fields
219 $admin->row_span(T('Type-Specific Fields'), 'thead', 'center');
220
221 switch ($type)
222 {
223 case 'input_text':
224 $admin->row_input(T('Default Value'), 'defaultvalue', $field->record['defaultvalue']);
225 $admin->row_input(T('Regular Expression Match'), 'regexmatch', $field->record['regexmatch']);
226 $admin->row_input(T('Maximum Length'), 'maxlength', $field->record['maxlength'], 2, 10);
227 break;
228
229 case 'input_checkbox':
230 $admin->row_yesno(T('Checked By Default'), 'defaultvalue', $field->record['defaultvalue']);
231 break;
232
233 case 'select_single':
234 $admin->row_textarea(T('Selection Values'), 'selects', stripslashes(implode("\n", (array)unserialize($field->record['selects']))));
235 $admin->row_yesno(T('Make the First Option Default'), 'usedefault', $field->record['usedefault']);
236 break;
237 }
238
239 $admin->table_end();
240
241 // custom field permissions
242 $admin->table_start();
243 $admin->table_head(T('Custom Field Permissions'));
244
245 if ($edit)
246 {
247 $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $field->record['fieldid']);
248 foreach ($perms as $perm)
249 {
250 $permissions["$perm[usergroupid]"] = $perm['mask'];
251 }
252 }
253
254 $usergroups = $db->query("SELECT * FROM " . TABLE_PREFIX . "usergroup ORDER BY usergroupid");
255 foreach ($usergroups as $usergroup)
256 {
257 unset($listitem);
258 $admin->list_item(T('No Permission'), 0, $permissions["$usergroup[usergroupid]"] == 0);
259 $admin->list_item(T('Can View Field'), 1, $permissions["$usergroup[usergroupid]"] == 1);
260 $admin->list_item(T('Can View, Edit Field'), 2, $permissions["$usergroup[usergroupid]"] == 2);
261 $admin->row_list($usergroup['title'], "custom[$usergroup[usergroupid]]");
262 }
263
264 $admin->table_end();
265
266 // end table
267 $admin->table_start();
268 $admin->row_submit();
269 $admin->table_end();
270 $admin->form_end();
271 }
272
273 $admin->page_end();
274 }
275
276 // ###################################################################
277
278 if ($_REQUEST['do'] == 'modify')
279 {
280 NavLinks::fieldsAdd();
281 $navigator->set_focus('link', 'fields-pages-fields', 'fields-pages');
282
283 $admin->page_start(T('Additional Bug Fields'));
284
285 $admin->table_start();
286 $admin->table_head(T('Additional Bug Fields'), 3, 'custom_bug_fields');
287 $admin->table_column_head(array(T('Display Name/Description'), T('Field ID'), T('Actions')));
288
289 $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield ORDER BY fieldid ASC");
290 foreach ($fields as $field)
291 {
292 $admin->row_multi_item(
293 array(
294 "$field[name]<div class=\"smallfont\"><em>$field[description]</em></div>" => 'l',
295 "$field[fieldid]" => 'c',
296 "<a href=\"field.php?do=edit&amp;fieldid=$field[fieldid]\">[" . T('Edit') . "]</a> <a href=\"field.php?do=delete&amp;fieldid=$field[fieldid]\">[" . T('Delete') . "]</a>" => 'c'
297 )
298 );
299 }
300
301 $admin->table_end();
302
303 $admin->page_end();
304 }
305
306 /*=====================================================================*\
307 || ###################################################################
308 || # $HeadURL$
309 || # $Id$
310 || ###################################################################
311 \*=====================================================================*/
312 ?>