From e004a3aa7dba42778b083e2fd182c692fc6ec1c1 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 22 May 2005 23:12:18 +0000 Subject: [PATCH] r191: Add code works, update code is there but untested. Modify code is incomplete. Delete and kill code is not done. --- admin/fields.php | 253 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 234 insertions(+), 19 deletions(-) diff --git a/admin/fields.php b/admin/fields.php index be20ae7..1946b8a 100644 --- a/admin/fields.php +++ b/admin/fields.php @@ -17,6 +17,14 @@ if (!can_perform('canadminfields')) admin_login(); } +$TYPES = array( + 'input_text' => 'Single-Line Text Box', + 'textarea' => 'Multi-Line Text Area', + 'input_checkbox' => 'Checkbox Flag', + 'select_single' => 'Drop-Down Menu', + //'select_multi' => 'Multiple-Selection Menu' +); + // ################################################################### if (empty($_REQUEST['do'])) @@ -35,42 +43,249 @@ if ($_REQUEST['do'] == 'kill') if ($_REQUEST['do'] == 'delete') { - // display delete confirmation message -} - -// ################################################################### - -if ($_POST['do'] == 'insert') -{ - // run code to insert new item into database -} -// ################################################################### - -if ($_REQUEST['do'] == 'add') -{ - // display form to add new item } // ################################################################### -if ($_POST['do'] == 'update') +if ($_REQUEST['do'] == 'update') { - // run code to update item in database + $edit = false; + $add = true; + + $type = $bugsys->in['type']; + + if ($bugsys->in['fieldid']) + { + $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfields WHERE fieldid = " . intval($bugsys->in['fieldid'])); + if (!$field) + { + $admin->error(phrase('error_invalid_id')); + } + + $edit = true; + $add = false; + $type = $field['type']; + } + + if (empty($bugsys->in['shortname']) AND $add) + { + $admin->error('You must specify a short name/call name.'); + } + 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) + { + $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.'); + } + + if (empty($bugsys->in['name'])) + { + $admin->error('You must specify a display name.'); + } + if (!isset($TYPES["$type"]) AND $add) + { + $admin->error('Invalid field type specified.'); + } + + switch ($bugsys->in['type']) + { + case 'input_text': + case 'textarea': + $extrafields = 'defaultvalue, regexmatch, maxlength'; + $extradata = "'" . $bugsys->in['defaultvalue'] . "', '" . $bugsys->in['regexmatch'] . "', " . intval($bugsys->in['maxlength']); + $extraupdate = "defaultvalue = '" . $bugsys->in['defaultvalue'] . "', regexmatch = '" . $bugsys->in['regexmatch'] . "', maxlength = " . intval($bugsys->in['maxlength']); + break; + + case 'input_checkbox': + $extrafields = 'default'; + $extradata = intval($bugsys->in['default']); + $extraupdate = "default = " . intval($bugsys->in['default']); + break; + + case 'select_single': + $extrafields = 'selects, usedefault'; + + // can't use explode() here because explode() returns !empty() when splitting an empty string + // so we have to use preg_split with the PREG_SPLIT_NO_EMPTY flag to prevent this + $selects = preg_split("#\n#", trim($bugsys->in['selects']), 0, PREG_SPLIT_NO_EMPTY); + array_walk($selects, 'trim'); + + if (count($selects) < 1) + { + $admin->error('You need to specify some select values.'); + } + + $extradata = "'" . $bugsys->escape(serialize($selects)) . "', " . intval($bugsys->in['usedefault']); + $extraupdate = "selects = '" . $bugsys->escape(serialize($selects)) . "', usedefault = " . intval($bugsys->in['usedefault']); + break; + } + + /*var_dump($extrafields); + var_dump($extradata); + var_dump($extraupdate); + exit;*/ + + if ($add) + { + if ($db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfields WHERE shortname = '" . $bugsys->in['shortname'] . "'") OR $bugsys->in['shortname'] == 'bugid') + { + $admin->error('That short name is already in use, please change it so it is unique.'); + } + + $db->query(" + INSERT INTO bugfields + (shortname, name, description, type, required, private, $extrafields) + VALUES + ('" . $bugsys->in['shortname'] . "', '" . $bugsys->in['name'] . "', + '" . $bugsys->in['description'] . "', '$type', " . intval($bugsys->in['required']) . ", + " . intval($bugsys->in['private']) . ", $extradata + )" + ); + + $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvalues ADD " . $bugsys->in['shortname'] . " MEDIUMTEXT NOT NULL"); + $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvalues"); + } + else + { + $db->query(" + UPDATE " . TABLE_PREFIX . "bugfields + SET name = '" . $bugsys->in['name'] . "', + description = '" . $bugsys->in['description'] . "', + required = " . intval($bugsys->in['required']) . ", + private = " . intval($bugsys->in['private']) . ", + $extraupdate + WHERE fieldid = " . intval($bugsys->in['fieldid']) + ); + } + + $admin->redirect('fields.php?do=modify', (($add) ? 'The custom bug field has been added.' : 'The bug field has been updated.')); } // ################################################################### -if ($_REQUEST['do'] == 'edit') +if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') { - // display form to edit item + $add = (($_REQUEST['do'] == 'add') ? true : false); + $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false); + $edit = (($add) ? false : true); + + $admin->page_start((($add) ? phrase('add_new_field') : 'Edit Profile Field')); + $admin->form_start('fields.php', (($typeselect) ? 'add' : 'update')); + if ($add AND !$typeselect) + { + $admin->form_hidden_field('type', $bugsys->in['type']); + } + if ($typeselect) + { + $admin->form_hidden_field('step', 1); + $admin->table_start(true, '40%'); + $admin->table_head('Select Type'); + } + else + { + $admin->table_start(); + $admin->table_head((($add) ? phrase('add_new_field') . ' - ' . $TYPES[ $bugsys->in['type'] ] : phrase('edit_field'))); + } + + if ($edit) + { + $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfields WHERE fieldid = " . intval($bugsys->in['fieldid'])); + if (!$field) + { + $admin->error(phrase('error_invalid_id')); + } + + $admin->form_hidden_field('fieldid', $field['fieldid']); + } + + if (!$typeselect) + { + $type = (($add) ? $bugsys->in['type'] : $field['type']); + } + + // show type selector + if (empty($bugsys->in['step']) AND $add) + { + foreach ($TYPES AS $name => $description) + { + $admin->list_item($description, $name); + } + $admin->row_list('Select field type', 'type', false); + + $admin->row_submit(); + $admin->table_end(); + } + // have type, do that funkay thing! + else + { + if (!isset($TYPES["$type"])) + { + $admin->error(phrase('error_invalid_id')); + } + + // global fields + $admin->row_span('Global Fields', 'thead', 'center'); + $admin->row_text('Field Type', $TYPES["$type"]); + $admin->row_input('Short Name/Call Name', 'shortname', $field['shortname']); + $admin->row_input('Display Name', 'name', $field['name']); + $admin->row_textarea('Description', 'description', $field['description']); + $admin->row_yesno('Required', 'required', $field['required']); + $admin->row_yesno('Private Field', 'private', $field['private']); + + // type-specific fields + $admin->row_span('Type-Specific Fields', 'thead', 'center'); + + switch ($bugsys->in['type']) + { + case 'input_text': + $admin->row_input('Default Value', 'defaultvalue', $field['defaultvalue']); + $admin->row_input('Regular Expression Match', 'regexmatch', $field['regexmatch']); + $admin->row_input('Maximum Length', 'maxlength', $field['maxlength'], 2, 10); + break; + + case 'textarea': + $admin->row_textarea('Default Value', 'defaultvalue', $field['defaultvalue']); + $admin->row_input('Regular Expression Match', 'regexmatch', $field['regexmatch']); + $admin->row_input('Maximum Length', 'maxlength', $field['maxlength'], 2, 10); + break; + + case 'input_checkbox': + $admin->row_yesno('Checked By Default', 'default', $field['default']); + break; + + case 'select_single': + $admin->row_textarea('Selection Values', 'selects', $field['selects']); + $admin->row_yesno('Make the First Option Default', 'usedefault', $field['usedefault']); + break; + } + + // end table + $admin->row_submit((($edit) ? '[Delete Field]' : '')); + $admin->table_end(); + $admin->form_end(); + } + + $admin->page_end(); } // ################################################################### if ($_REQUEST['do'] == 'modify') { - // show default branch for this script + $admin->page_start(phrase('additional_bug_fields')); + + $admin->table_start(); + $admin->table_head(phrase('additional_bug_fields')); + + $fields = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfields ORDER BY fieldid ASC"); + while ($field = $db->fetch_array($fields)) + { + $admin->row_text(); + } + + $admin->row_span('', 'tfoot', 'center', 3); + $admin->table_end(); + + $admin->page_end(); } /*=====================================================================*\ -- 2.22.5