Add complete custom fields implementation and entry editing
- Add custom field CRUD in admin UI (create, read, update, delete) - Display custom fields on frontend form with proper field types - Save custom field values on form submission - Display custom field columns in entries table (admin and public) - Include custom field values in all export formats (CSV, PairGoTh, OpenGotha, McMahon) - Add Edit button and form for entries - Update entry handler to support editing with custom field values - Add success/error notices for all admin actions Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -11,17 +11,19 @@ function go_form_activate()
|
||||
$charset = $wpdb->get_charset_collate();
|
||||
$forms = $wpdb->prefix . 'go_form_forms';
|
||||
$entries = $wpdb->prefix . 'go_form_entries';
|
||||
$custom_fields = $wpdb->prefix . 'go_form_custom_fields';
|
||||
$custom_values = $wpdb->prefix . 'go_form_entry_custom_values';
|
||||
|
||||
dbDelta("CREATE TABLE $forms (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
id int NOT NULL AUTO_INCREMENT,
|
||||
name varchar(255) NOT NULL,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) $charset;");
|
||||
|
||||
dbDelta("CREATE TABLE $entries (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
form_id mediumint(9) DEFAULT 1,
|
||||
id int NOT NULL AUTO_INCREMENT,
|
||||
form_id int DEFAULT 1,
|
||||
first_name varchar(100) NOT NULL,
|
||||
last_name varchar(100) NOT NULL,
|
||||
country varchar(100) DEFAULT NULL,
|
||||
@@ -31,13 +33,41 @@ function go_form_activate()
|
||||
egd_number varchar(20) DEFAULT NULL,
|
||||
created_at date DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
FOREIGN KEY (form_id) REFERENCES $forms(id) ON DELETE CASCADE
|
||||
FOREIGN KEY (form_id) REFERENCES $forms(id) ON DELETE CASCADE,
|
||||
INDEX (form_id)
|
||||
) $charset;");
|
||||
|
||||
dbDelta("CREATE TABLE $custom_fields (
|
||||
id int NOT NULL AUTO_INCREMENT,
|
||||
form_id int NOT NULL,
|
||||
field_name varchar(100) NOT NULL,
|
||||
field_type enum('select','text','checkbox', 'email') NOT NULL DEFAULT 'text',
|
||||
field_options text,
|
||||
is_public tinyint(1) NOT NULL DEFAULT 0,
|
||||
is_required tinyint(1) NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (id),
|
||||
FOREIGN KEY (form_id) REFERENCES $forms(id) ON DELETE CASCADE,
|
||||
INDEX (form_id)
|
||||
) $charset;");
|
||||
|
||||
dbDelta("CREATE TABLE $custom_values (
|
||||
id int NOT NULL AUTO_INCREMENT,
|
||||
entry_id int NOT NULL,
|
||||
field_id int NOT NULL,
|
||||
value text,
|
||||
PRIMARY KEY (id),
|
||||
FOREIGN KEY (entry_id) REFERENCES $entries(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (field_id) REFERENCES $custom_fields(id) ON DELETE CASCADE,
|
||||
INDEX (entry_id),
|
||||
INDEX (field_id)
|
||||
) $charset;");
|
||||
}
|
||||
|
||||
function go_form_uninstall()
|
||||
{
|
||||
global $wpdb;
|
||||
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_entry_custom_values");
|
||||
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_custom_fields");
|
||||
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_entries");
|
||||
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}go_form_forms");
|
||||
}
|
||||
@@ -76,13 +106,62 @@ function go_form_get_entries($form_id)
|
||||
));
|
||||
}
|
||||
|
||||
function go_form_render_entries_table($entries, $show_admin = false)
|
||||
function go_form_render_entries_table($entries, $show_admin = false, $custom_fields = [], $form_id = 0)
|
||||
{
|
||||
global $ranks;
|
||||
|
||||
include "templates/table.php";
|
||||
}
|
||||
|
||||
// ========== Custom Fields Helpers ==========
|
||||
function go_form_get_custom_fields($form_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT * FROM {$wpdb->prefix}go_form_custom_fields WHERE form_id = %d ORDER BY id ASC",
|
||||
$form_id
|
||||
));
|
||||
}
|
||||
|
||||
function go_form_get_custom_field_by_id($field_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM {$wpdb->prefix}go_form_custom_fields WHERE id = %d",
|
||||
$field_id
|
||||
));
|
||||
}
|
||||
|
||||
function go_form_get_custom_values($entry_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT cf.*, cv.value FROM {$wpdb->prefix}go_form_custom_fields cf
|
||||
JOIN {$wpdb->prefix}go_form_entry_custom_values cv ON cf.id = cv.field_id
|
||||
WHERE cv.entry_id = %d",
|
||||
$entry_id
|
||||
));
|
||||
}
|
||||
|
||||
function go_form_get_custom_value($entry_id, $field_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT value FROM {$wpdb->prefix}go_form_entry_custom_values
|
||||
WHERE entry_id = %d AND field_id = %d",
|
||||
$entry_id, $field_id
|
||||
));
|
||||
}
|
||||
|
||||
function go_form_get_entry_by_id($entry_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM {$wpdb->prefix}go_form_entries WHERE id = %d",
|
||||
$entry_id
|
||||
));
|
||||
}
|
||||
|
||||
// ========== Form Handling ==========
|
||||
function go_form_handle_submission()
|
||||
{
|
||||
@@ -120,6 +199,34 @@ function go_form_handle_submission()
|
||||
}
|
||||
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_entries", $data);
|
||||
$entry_id = $wpdb->insert_id;
|
||||
|
||||
// Save custom field values
|
||||
$custom_fields = go_form_get_custom_fields($form_id);
|
||||
foreach ($custom_fields as $field) {
|
||||
$field_name = 'custom_field_' . $field->id;
|
||||
if (isset($_POST[$field_name])) {
|
||||
$value = $_POST[$field_name];
|
||||
if ($field->field_type === 'checkbox') {
|
||||
$value = isset($_POST[$field_name]) ? '1' : '0';
|
||||
} elseif (is_array($value)) {
|
||||
$value = implode(',', array_map('sanitize_text_field', $value));
|
||||
} else {
|
||||
$value = sanitize_text_field($value);
|
||||
}
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_entry_custom_values", [
|
||||
'entry_id' => $entry_id,
|
||||
'field_id' => $field->id,
|
||||
'value' => $value
|
||||
]);
|
||||
} elseif ($field->is_required) {
|
||||
// If required field is missing, delete the entry and redirect back
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_entries", ['id' => $entry_id]);
|
||||
wp_redirect($_SERVER['HTTP_REFERER']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
wp_redirect($_SERVER['HTTP_REFERER']);
|
||||
exit;
|
||||
}
|
||||
@@ -233,6 +340,157 @@ function go_form_export_mcmahon()
|
||||
}
|
||||
add_action('admin_post_go_form_export_mcmahon', 'go_form_export_mcmahon');
|
||||
|
||||
// ========== Custom Field Admin Actions ==========
|
||||
function go_form_create_custom_field()
|
||||
{
|
||||
go_form_admin_action('create_custom_field');
|
||||
if (isset($_POST['form_id']) && isset($_POST['field_name']) && isset($_POST['field_type'])) {
|
||||
global $wpdb;
|
||||
$form_id = intval($_POST['form_id']);
|
||||
$field_name = sanitize_text_field($_POST['field_name']);
|
||||
$field_type = sanitize_text_field($_POST['field_type']);
|
||||
$field_options = isset($_POST['field_options']) ? sanitize_textarea_field($_POST['field_options']) : '';
|
||||
$is_public = isset($_POST['is_public']) ? 1 : 0;
|
||||
$is_required = isset($_POST['is_required']) ? 1 : 0;
|
||||
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_custom_fields", [
|
||||
'form_id' => $form_id,
|
||||
'field_name' => $field_name,
|
||||
'field_type' => $field_type,
|
||||
'field_options' => $field_options,
|
||||
'is_public' => $is_public,
|
||||
'is_required' => $is_required
|
||||
]);
|
||||
}
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings', 'form_id' => $_POST['form_id'], 'created_field' => 1), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_create_custom_field', 'go_form_create_custom_field');
|
||||
|
||||
function go_form_update_custom_field()
|
||||
{
|
||||
go_form_admin_action('update_custom_field');
|
||||
if (isset($_POST['field_id']) && isset($_POST['field_name']) && isset($_POST['field_type'])) {
|
||||
global $wpdb;
|
||||
$field_id = intval($_POST['field_id']);
|
||||
$field_name = sanitize_text_field($_POST['field_name']);
|
||||
$field_type = sanitize_text_field($_POST['field_type']);
|
||||
$field_options = isset($_POST['field_options']) ? sanitize_textarea_field($_POST['field_options']) : '';
|
||||
$is_public = isset($_POST['is_public']) ? 1 : 0;
|
||||
$is_required = isset($_POST['is_required']) ? 1 : 0;
|
||||
|
||||
$wpdb->update("{$wpdb->prefix}go_form_custom_fields", [
|
||||
'field_name' => $field_name,
|
||||
'field_type' => $field_type,
|
||||
'field_options' => $field_options,
|
||||
'is_public' => $is_public,
|
||||
'is_required' => $is_required
|
||||
], ['id' => $field_id]);
|
||||
}
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings', 'form_id' => $_POST['form_id'], 'updated_field' => 1), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_update_custom_field', 'go_form_update_custom_field');
|
||||
|
||||
function go_form_delete_custom_field()
|
||||
{
|
||||
go_form_admin_action('delete_custom_field');
|
||||
if (isset($_POST['field_id'])) {
|
||||
global $wpdb;
|
||||
$field_id = intval($_POST['field_id']);
|
||||
$field = go_form_get_custom_field_by_id($field_id);
|
||||
$form_id = $field ? $field->form_id : 0;
|
||||
$wpdb->delete("{$wpdb->prefix}go_form_custom_fields", ['id' => $field_id]);
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings', 'form_id' => $form_id, 'deleted_field' => 1), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings'), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_delete_custom_field', 'go_form_delete_custom_field');
|
||||
|
||||
// ========== Entry Edit Admin Action ==========
|
||||
function go_form_update_entry()
|
||||
{
|
||||
go_form_admin_action('update_entry');
|
||||
if (!isset($_POST['entry_id']) || !isset($_POST['form_id'])) {
|
||||
wp_redirect(admin_url('admin.php?page=go-form-settings'));
|
||||
exit;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$entry_id = intval($_POST['entry_id']);
|
||||
$form_id = intval($_POST['form_id']);
|
||||
|
||||
$rating = intval($_POST['rating']);
|
||||
$rank = intval($_POST['rank']);
|
||||
if ($rating < -900) {
|
||||
$rating = ($rank * 100) - 900;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'first_name' => sanitize_text_field($_POST['first_name']),
|
||||
'last_name' => sanitize_text_field($_POST['last_name']),
|
||||
'country' => sanitize_text_field($_POST['country'] ?? ''),
|
||||
'club' => sanitize_text_field($_POST['club'] ?? ''),
|
||||
'rank' => $rank,
|
||||
'rating' => $rating,
|
||||
'egd_number' => sanitize_text_field($_POST['egd_number'] ?? ''),
|
||||
];
|
||||
|
||||
if (empty($data['first_name']) || empty($data['last_name'])) {
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings', 'form_id' => $form_id, 'edit_entry' => $entry_id, 'error' => 'missing_required'), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
|
||||
$wpdb->update("{$wpdb->prefix}go_form_entries", $data, ['id' => $entry_id]);
|
||||
|
||||
// Update custom field values
|
||||
$custom_fields = go_form_get_custom_fields($form_id);
|
||||
foreach ($custom_fields as $field) {
|
||||
$field_name = 'custom_field_' . $field->id;
|
||||
if (isset($_POST[$field_name])) {
|
||||
$value = $_POST[$field_name];
|
||||
if ($field->field_type === 'checkbox') {
|
||||
$value = isset($_POST[$field_name]) ? '1' : '0';
|
||||
} elseif (is_array($value)) {
|
||||
$value = implode(',', array_map('sanitize_text_field', $value));
|
||||
} else {
|
||||
$value = sanitize_text_field($value);
|
||||
}
|
||||
|
||||
// Check if custom value exists
|
||||
$existing = go_form_get_custom_value($entry_id, $field->id);
|
||||
if ($existing !== null) {
|
||||
$wpdb->update("{$wpdb->prefix}go_form_entry_custom_values",
|
||||
['value' => $value],
|
||||
['entry_id' => $entry_id, 'field_id' => $field->id]);
|
||||
} else {
|
||||
$wpdb->insert("{$wpdb->prefix}go_form_entry_custom_values", [
|
||||
'entry_id' => $entry_id,
|
||||
'field_id' => $field->id,
|
||||
'value' => $value
|
||||
]);
|
||||
}
|
||||
} elseif ($field->is_required) {
|
||||
// If required field is missing, redirect back with error
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings', 'form_id' => $form_id, 'edit_entry' => $entry_id, 'error' => 'missing_required_field'), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$redirect_url = add_query_arg(array('page' => 'go-form-settings', 'form_id' => $form_id, 'updated_entry' => 1), admin_url('admin.php'));
|
||||
wp_redirect($redirect_url);
|
||||
exit;
|
||||
}
|
||||
add_action('admin_post_go_form_update_entry', 'go_form_update_entry');
|
||||
|
||||
// ========== Admin Page ====================
|
||||
function go_form_admin_menu()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user