From 938a9351c7dd336ff7f19917c1eb3bd4871d8f7e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 19 Jul 2006 15:34:02 +0000 Subject: [PATCH] r904: Adding and implementing the FieldAPI --- admin/field.php | 171 +++++++++++-------------------------- includes/api_field.php | 187 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+), 120 deletions(-) create mode 100644 includes/api_field.php diff --git a/admin/field.php b/admin/field.php index de7dde0..9c2cf89 100644 --- a/admin/field.php +++ b/admin/field.php @@ -20,19 +20,13 @@ \*=====================================================================*/ require_once('./global.php'); -require_once('./includes/functions_datastore.php'); +require_once('./includes/api_field.php'); if (!can_perform('canadminfields')) { admin_login(); } -$TYPES = array( - 'input_text' => $lang->string('Single-Line Text Box'), - 'input_checkbox' => $lang->string('Checkbox Flag'), - 'select_single' => $lang->string('Drop-Down Menu'), -); - // ################################################################### if (empty($_REQUEST['do'])) @@ -44,18 +38,10 @@ if (empty($_REQUEST['do'])) if ($_REQUEST['do'] == 'kill') { - $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->input_clean('fieldid', TYPE_UINT)); - if (!$field) - { - $admin->error($lang->getlex('error_invalid_id')); - } - - $db->query("DELETE FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = $field[fieldid]"); - $db->query("DELETE FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = $field[fieldid]"); - $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill DROP field$field[fieldid]"); - $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill"); - - build_user_help(); + $field = new FieldAPI($bugsys); + $field->set('fieldid', $bugsys->in['fieldid']); + $field->set_condition(); + $field->delete(); $admin->redirect('field.php?do=modify'); } @@ -63,14 +49,8 @@ if ($_REQUEST['do'] == 'kill') // ################################################################### if ($_REQUEST['do'] == 'delete') -{ - $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->input_clean('fieldid', TYPE_UINT)); - if (!$field) - { - $admin->error($lang->getlex('error_invalid_id')); - } - - $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&fieldid=$field[fieldid]"); +{ + $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&fieldid=" . $bugsys->in['fieldid']); } // ################################################################### @@ -82,97 +62,55 @@ if ($_REQUEST['do'] == 'update') $type = $bugsys->in['type']; + $field = new FieldAPI($bugsys); + if ($bugsys->in['fieldid']) { - $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->input_clean('fieldid', TYPE_UINT)); - if (!$field) - { - $admin->error($lang->getlex('error_invalid_id')); - } + $field->set('fieldid', $bugsys->in['fieldid']); + $field->set_condition(); + $field->fetch(); $edit = true; $add = false; - $type = $field['type']; - } - - if (empty($bugsys->in['name'])) - { - $admin->error($lang->string('You must specify a display name.')); - } - if (!isset($TYPES["$type"]) AND $add) - { - $admin->error($lang->string('Invalid field type specified.')); + $type = $field->objdata['type']; } - $bugsys->input_clean_array(array( - 'maxlength' => TYPE_UINT, - 'usedefault' => TYPE_UINT, - 'required' => TYPE_UINT, - 'cansearch' => TYPE_UINT, - 'fieldid' => TYPE_UINT - )); - switch ($type) { case 'input_text': - $extrafields = 'defaultvalue, regexmatch, maxlength'; - $extradata = "'" . $bugsys->in['defaultvalue'] . "', '" . $bugsys->in['regexmatch'] . "', " . $bugsys->in['maxlength']; - $extraupdate = "defaultvalue = '" . $bugsys->in['defaultvalue'] . "', regexmatch = '" . $bugsys->in['regexmatch'] . "', maxlength = " . $bugsys->in['maxlength']; + $field->set('defaultvalue', $bugsys->in['defaultvalue']); + $field->set('regexmatch', $bugsys->in['regexmatch']); + $field->set('maxlength', $bugsys->in['maxlength']); break; case 'input_checkbox': - $extrafields = 'defaultvalue'; - $extradata = $bugsys->input_clean('defaultvalue', TYPE_UINT); - $extraupdate = "defaultvalue = " . $bugsys->in['defaultvalue']; + $field->set('defaultvalue', $bugsys->in['defaultvalue']); 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 (sizeof($selects) < 1) - { - $admin->error($lang->string('You need to specify some select values.')); - } - - $extradata = "'" . $bugsys->escape(serialize($selects)) . "', " . $bugsys->in['usedefault']; - $extraupdate = "selects = '" . $bugsys->escape(serialize($selects)) . "', usedefault = " . $bugsys->in['usedefault']; + $field->set('selects', $bugsys->in['selects']); + $field->set('usedefault', $bugsys->in['usedefault']); break; } + $field->set('name', $bugsys->in['name']); + $field->set('description', $bugsys->in['description']); + $field->set('required', $bugsys->in['required']); + $field->set('cansearch', $bugsys->in['cansearch']); + if ($add) { - $db->query(" - INSERT INTO " . TABLE_PREFIX . "bugfield - (name, description, type, required, cansearch, $extrafields) - VALUES - ('" . $bugsys->in['name'] . "', - '" . $bugsys->in['description'] . "', '$type', " . $bugsys->input_clean('required', TYPE_UINT) . ", - " . $bugsys->input_clean('cansearch', TYPE_UINT) . ", $extradata - )" - ); - - $fieldid = $db->insert_id(); + $field->set('type', $type); + $field->insert(); + $fieldid = $field->insertid; $db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill ADD field$fieldid MEDIUMTEXT NULL"); $db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill"); } else { - $db->query(" - UPDATE " . TABLE_PREFIX . "bugfield - SET name = '" . $bugsys->in['name'] . "', - description = '" . $bugsys->in['description'] . "', - required = " . $bugsys->in['required'] . ", - cansearch = " . $bugsys->in['cansearch'] . ", - $extraupdate - WHERE fieldid = " . $bugsys->in['fieldid'] - ); - - $fieldid = $bugsys->in['fieldid']; + $field->update(); + $fieldid = $field->values['fieldid']; } $bugsys->input_clean('custom', TYPE_UINT); @@ -188,8 +126,6 @@ if ($_REQUEST['do'] == 'update') (" . implode("),\n\t\t\t(", $values) . ")" ); - build_user_help(); - $admin->redirect('field.php?do=modify', ($add ? $lang->string('The custom field has been added') : $lang->string('The custom field has been updated'))); } @@ -197,7 +133,7 @@ if ($_REQUEST['do'] == 'update') if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') { - $bugsys->input_clean('fieldid', TYPE_UINT); + $field = new FieldAPI($bugsys); $add = (($_REQUEST['do'] == 'add') ? true : false); $typeselect = (($add AND empty($bugsys->in['step'])) ? true : false); @@ -218,29 +154,27 @@ if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') else { $admin->table_start(); - $admin->table_head(($add ? $lang->string('Add New Bug Field') . ' - ' . $TYPES[ $bugsys->in['type'] ] : $lang->string('Edit Field')), 2, 'custom_bug_fields_options'); + $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'); } if ($edit) { - $field = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $bugsys->in['fieldid']); - if (!$field) - { - $admin->error($lang->getlex('error_invalid_id')); - } + $field->set('fieldid', $bugsys->in['fieldid']); + $field->set_condition(); + $field->fetch(); - $admin->form_hidden_field('fieldid', $field['fieldid']); + $admin->form_hidden_field('fieldid', $field->objdata['fieldid']); } if (!$typeselect) { - $type = (($add) ? $bugsys->in['type'] : $field['type']); + $type = (($add) ? $bugsys->in['type'] : $field->objdata['type']); } // show type selector if (empty($bugsys->in['step']) AND $add) { - foreach ($TYPES AS $name => $description) + foreach (FieldAPI::field_types() AS $name => $description) { $admin->list_item($description, $name); } @@ -252,18 +186,15 @@ if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') // have type, do that funkay thing! else { - if (!isset($TYPES["$type"])) - { - $admin->error($lang->getlex('error_invalid_id')); - } + $field->set('type', $type); // global fields $admin->row_span($lang->string('Global Fields'), 'thead', 'center'); - $admin->row_text($lang->string('Field Type'), $TYPES["$type"]); - $admin->row_input($lang->string('Display Name'), 'name', $field['name']); - $admin->row_textarea($lang->string('Description'), 'description', $field['description']); - $admin->row_yesno($lang->string('Required'), 'required', $field['required']); - $admin->row_yesno($lang->string('Can Be Searched'), 'cansearch', ((isset($field['cansearch'])) ? $field['cansearch'] : true)); + $admin->row_text($lang->string('Field Type'), FieldAPI::field_types($type)); + $admin->row_input($lang->string('Display Name'), 'name', $field->objdata['name']); + $admin->row_textarea($lang->string('Description'), 'description', $field->objdata['description']); + $admin->row_yesno($lang->string('Required'), 'required', $field->objdata['required']); + $admin->row_yesno($lang->string('Can Be Searched'), 'cansearch', ((isset($field->objdata['cansearch'])) ? $field->objdata['cansearch'] : true)); // type-specific fields $admin->row_span($lang->string('Type-Specific Fields'), 'thead', 'center'); @@ -271,18 +202,18 @@ if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') switch ($type) { case 'input_text': - $admin->row_input($lang->string('Default Value'), 'defaultvalue', $field['defaultvalue']); - $admin->row_input($lang->string('Regular Expression Match'), 'regexmatch', $field['regexmatch']); - $admin->row_input($lang->string('Maximum Length'), 'maxlength', $field['maxlength'], 2, 10); + $admin->row_input($lang->string('Default Value'), 'defaultvalue', $field->objdata['defaultvalue']); + $admin->row_input($lang->string('Regular Expression Match'), 'regexmatch', $field->objdata['regexmatch']); + $admin->row_input($lang->string('Maximum Length'), 'maxlength', $field->objdata['maxlength'], 2, 10); break; case 'input_checkbox': - $admin->row_yesno($lang->string('Checked By Default'), 'defaultvalue', $field['defaultvalue']); + $admin->row_yesno($lang->string('Checked By Default'), 'defaultvalue', $field->objdata['defaultvalue']); break; case 'select_single': - $admin->row_textarea($lang->string('Selection Values'), 'selects', stripslashes(implode("\n", unserialize($field['selects'])))); - $admin->row_yesno($lang->string('Make the First Option Default'), 'usedefault', $field['usedefault']); + $admin->row_textarea($lang->string('Selection Values'), 'selects', stripslashes(implode("\n", unserialize($field->objdata['selects'])))); + $admin->row_yesno($lang->string('Make the First Option Default'), 'usedefault', $field->objdata['usedefault']); break; } @@ -294,7 +225,7 @@ if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') if ($edit) { - $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = $field[fieldid]"); + $perms = $db->query("SELECT usergroupid, mask FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $field->objdata['fieldid']); while ($perm = $db->fetch_array($perms)) { $permissions["$perm[usergroupid]"] = $perm['mask']; @@ -315,7 +246,7 @@ if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit') // end table $admin->table_start(); - $admin->row_submit((($edit) ? '[' . $lang->string('Delete Field') . ']' : '')); + $admin->row_submit((($edit) ? '[' . $lang->string('Delete Field') . ']' : '')); $admin->table_end(); $admin->form_end(); } diff --git a/includes/api_field.php b/includes/api_field.php new file mode 100644 index 0000000..7c3ec34 --- /dev/null +++ b/includes/api_field.php @@ -0,0 +1,187 @@ +load('api', null); + +require_once('./includes/functions_datastore.php'); + +/** +* API: Field +* +* @author Iris Studios, Inc. +* @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc. +* @version $Revision$ +* @package Bugdar +* +*/ +class FieldAPI extends API +{ + /** + * Fields + * @var array + * @access private + */ + var $fields = array( + 'fieldid' => array(TYPE_UINT, REQ_AUTO, 'verify_nozero'), + 'name' => array(TYPE_STR, REQ_YES, 'verify_noempty'), + 'description' => array(TYPE_STR, REQ_NO), + 'type' => array(TYPE_STR, REQ_YES, ':self'), + 'selects' => array(TYPE_STR, REQ_NO, ':self'), + 'required' => array(TYPE_BOOL, REQ_NO), + 'cansearch' => array(TYPE_BOOL, REQ_NO), + 'regexmatch' => array(TYPE_STR, REQ_NO), + 'defaultvalue' => array(TYPE_STR, REQ_NO), + 'usedefault' => array(TYPE_BOOL, REQ_NO), + 'maxlength' => array(TYPE_UINT, REQ_NO) + ); + + /** + * Database table + * @var string + * @access private + */ + var $table = 'bugfield'; + + /** + * Table prefix + * @var string + * @access private + */ + var $prefix = TABLE_PREFIX; + + // ################################################################### + /** + * A static method that is used to return an array of all the allowed + * field types. + * + * @access public + * + * @param string Return the display name of a type if the key is passed + * + * @return array An array of all the allowed field types + */ + function field_types($type = null) + { + global $lang; + $types = array( + 'input_text' => $lang->string('Single-Line Text Box'), + 'input_checkbox' => $lang->string('Checkbox Flag'), + 'select_single' => $lang->string('Drop-Down Menu'), + ); + + if ($type == null) + { + return $types; + } + else + { + return $types["$type"]; + } + } + + // ################################################################### + /** + * Post-insert + * + * @access private + */ + function post_insert() + { + build_user_help(); + } + + // ################################################################### + /** + * Post-update + * + * @access private + */ + function post_update() + { + build_user_help(); + } + + // ################################################################### + /** + * Post-delete + * + * @access private + */ + function post_delete() + { + $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "bugfield WHERE fieldid = " . $this->values['fieldid']); + $this->registry->db->query("DELETE FROM " . TABLE_PREFIX . "bugfieldpermission WHERE fieldid = " . $this->values['fieldid']); + $this->registry->db->query("ALTER TABLE " . TABLE_PREFIX . "bugvaluefill DROP field" . $this->values['fieldid']); + $this->registry->db->query("OPTIMIZE TABLE " . TABLE_PREFIX . "bugvaluefill"); + + build_user_help(); + } + + // ################################################################### + /** + * Verify: type + * + * @access private + */ + function verify_type() + { + if (!in_array($this->values['type'], array_keys(FieldAPI::field_types()))) + { + return false; + } + return true; + } + + // ################################################################### + /** + * Verify: selects + * + * @access private + */ + function verify_selects() + { + if ($this->values['type'] == 'select_single') + { + // 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($this->values['selects']), 0, PREG_SPLIT_NO_EMPTY); + array_walk($selects, 'trim'); + if (sizeof($selects) < 1) + { + return $this->registry->lang->string('You need to specify some select values.'); + } + else + { + $this->set('selects', serialize($selects)); + } + } + + return true; + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.22.5