r904: Adding and implementing the FieldAPI
[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
78 switch ($type)
79 {
80 case 'input_text':
81 $field->set('defaultvalue', $bugsys->in['defaultvalue']);
82 $field->set('regexmatch', $bugsys->in['regexmatch']);
83 $field->set('maxlength', $bugsys->in['maxlength']);
84 break;
85
86 case 'input_checkbox':
87 $field->set('defaultvalue', $bugsys->in['defaultvalue']);
88 break;
89
90 case 'select_single':
91 $field->set('selects', $bugsys->in['selects']);
92 $field->set('usedefault', $bugsys->in['usedefault']);
93 break;
94 }
95
96 $field->set('name', $bugsys->in['name']);
97 $field->set('description', $bugsys->in['description']);
98 $field->set('required', $bugsys->in['required']);
99 $field->set('cansearch', $bugsys->in['cansearch']);
100
101 if ($add)
102 {
103 $field->set('type', $type);
104 $field->insert();
105 $fieldid = $field->insertid;
106
107 $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill ADD field$fieldid MEDIUMTEXT NULL");
108 $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill");
109 }
110 else
111 {
112 $field->update();
113 $fieldid = $field->values['fieldid'];
114 }
115
116 $bugsys->input_clean('custom', TYPE_UINT);
117 foreach ($bugsys->in['custom'] AS $usergroupid => $mask)
118 {
119 $values[] = $bugsys->clean($usergroupid, TYPE_UINT) . ", $fieldid, " . $mask;
120 }
121
122 $db->query("
123 REPLACE INTO " . TABLE_PREFIX . "bugfieldpermission
124 (usergroupid, fieldid, mask)
125 VALUES
126 (" . implode("),\n\t\t\t(", $values) . ")"
127 );
128
129 $admin->redirect('field.php?do=modify', ($add ? $lang->string('The custom field has been added') : $lang->string('The custom field has been updated')));
130 }
131
132 // ###################################################################
133
134 if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
135 {
136 $field = new FieldAPI($bugsys);
137
138 $add = (($_REQUEST['do'] == 'add') ? true : false);
139 $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false);
140 $edit = (($add) ? false : true);
141
142 $admin->page_start(($add ? $lang->string('Add New Custom Field') : $lang->string('Edit Custom Field')));
143 $admin->form_start('field.php', ($typeselect ? 'add' : 'update'));
144 if ($add AND !$typeselect)
145 {
146 $admin->form_hidden_field('type', $bugsys->in['type']);
147 }
148 if ($typeselect)
149 {
150 $admin->form_hidden_field('step', 1);
151 $admin->table_start(true, '40%');
152 $admin->table_head($lang->string('Select Type'), 2, 'custom_bug_fields');
153 }
154 else
155 {
156 $admin->table_start();
157 $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');
158 }
159
160 if ($edit)
161 {
162 $field->set('fieldid', $bugsys->in['fieldid']);
163 $field->set_condition();
164 $field->fetch();
165
166 $admin->form_hidden_field('fieldid', $field->objdata['fieldid']);
167 }
168
169 if (!$typeselect)
170 {
171 $type = (($add) ? $bugsys->in['type'] : $field->objdata['type']);
172 }
173
174 // show type selector
175 if (empty($bugsys->in['step']) AND $add)
176 {
177 foreach (FieldAPI::field_types() AS $name => $description)
178 {
179 $admin->list_item($description, $name);
180 }
181 $admin->row_list($lang->string('Field Type'), 'type', false);
182
183 $admin->row_submit();
184 $admin->table_end();
185 }
186 // have type, do that funkay thing!
187 else
188 {
189 $field->set('type', $type);
190
191 // global fields
192 $admin->row_span($lang->string('Global Fields'), 'thead', 'center');
193 $admin->row_text($lang->string('Field Type'), FieldAPI::field_types($type));
194 $admin->row_input($lang->string('Display Name'), 'name', $field->objdata['name']);
195 $admin->row_textarea($lang->string('Description'), 'description', $field->objdata['description']);
196 $admin->row_yesno($lang->string('Required'), 'required', $field->objdata['required']);
197 $admin->row_yesno($lang->string('Can Be Searched'), 'cansearch', ((isset($field->objdata['cansearch'])) ? $field->objdata['cansearch'] : true));
198
199 // type-specific fields
200 $admin->row_span($lang->string('Type-Specific Fields'), 'thead', 'center');
201
202 switch ($type)
203 {
204 case 'input_text':
205 $admin->row_input($lang->string('Default Value'), 'defaultvalue', $field->objdata['defaultvalue']);
206 $admin->row_input($lang->string('Regular Expression Match'), 'regexmatch', $field->objdata['regexmatch']);
207 $admin->row_input($lang->string('Maximum Length'), 'maxlength', $field->objdata['maxlength'], 2, 10);
208 break;
209
210 case 'input_checkbox':
211 $admin->row_yesno($lang->string('Checked By Default'), 'defaultvalue', $field->objdata['defaultvalue']);
212 break;
213
214 case 'select_single':
215 $admin->row_textarea($lang->string('Selection Values'), 'selects', stripslashes(implode("\n", unserialize($field->objdata['selects']))));
216 $admin->row_yesno($lang->string('Make the First Option Default'), 'usedefault', $field->objdata['usedefault']);
217 break;
218 }
219
220 $admin->table_end();
221
222 // custom field permissions
223 $admin->table_start();
224 $admin->table_head($lang->string('Custom Field Permissions'));
225
226 if ($edit)
227 {
228 $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $field->objdata['fieldid']);
229 while ($perm = $db->fetch_array($perms))
230 {
231 $permissions["$perm[usergroupid]"] = $perm['mask'];
232 }
233 }
234
235 $usergroups = $db->query("SELECT * FROM " . TABLE_PREFIX . "usergroup ORDER BY usergroupid");
236 while ($usergroup = $db->fetch_array($usergroups))
237 {
238 unset($listitem);
239 $admin->list_item($lang->string('No Permission'), 0, $permissions["$usergroup[usergroupid]"] == 0);
240 $admin->list_item($lang->string('Can View Field'), 1, $permissions["$usergroup[usergroupid]"] == 1);
241 $admin->list_item($lang->string('Can View, Edit Field'), 2, $permissions["$usergroup[usergroupid]"] == 2);
242 $admin->row_list($usergroup['title'], "custom[$usergroup[usergroupid]]");
243 }
244
245 $admin->table_end();
246
247 // end table
248 $admin->table_start();
249 $admin->row_submit((($edit) ? '<a href="field.php?do=delete&amp;fieldid=' . $field->objdata['fieldid'] . '">[' . $lang->string('Delete Field') . ']</a>' : ''));
250 $admin->table_end();
251 $admin->form_end();
252 }
253
254 $admin->page_end();
255 }
256
257 // ###################################################################
258
259 if ($_REQUEST['do'] == 'modify')
260 {
261 $admin->page_start($lang->string('Additional Bug Fields'));
262
263 $admin->table_start();
264 $admin->table_head($lang->string('Additional Bug Fields'), 3, 'custom_bug_fields');
265 $admin->table_column_head(array($lang->string('Display Name/Description'), $lang->string('Field ID'), $lang->string('Actions')));
266
267 $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield ORDER BY fieldid ASC");
268 while ($field = $db->fetch_array($fields))
269 {
270 $admin->row_multi_item(
271 array(
272 "$field[name]<div class=\"smallfont\"><em>$field[description]</em></div>" => 'l',
273 "$field[fieldid]" => 'c',
274 "<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'
275 )
276 );
277 }
278
279 $admin->row_span('<a href="field.php?do=add">[' . $lang->string('Add New Bug Field') . ']</a>', 'tfoot', 'center', 3);
280 $admin->table_end();
281
282 $admin->page_end();
283 }
284
285 /*=====================================================================*\
286 || ###################################################################
287 || # $HeadURL$
288 || # $Id$
289 || ###################################################################
290 \*=====================================================================*/
291 ?>