From 29fc6693d6a2ef98eeeea687cc0690b4807619f7 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 24 Jul 2018 14:45:28 -0400 Subject: [PATCH] Custom Field updates for grouping Fix sorting of fields and groups after getting the field options into the custom field management. Update the custom fields grouping and output of select picklist for the edit contact custom field output. Working on management for custom fields. --- models/admin/entity/fields.php | 42 ++++- models/admin/management/fields.php | 84 +++++++++- views/admin/entity/fields.html | 130 +++++++++++---- views/admin/management/fields.html | 246 ++++++++++++++++++++++++----- 4 files changed, 419 insertions(+), 83 deletions(-) diff --git a/models/admin/entity/fields.php b/models/admin/entity/fields.php index bdbd46f..80c7d4c 100644 --- a/models/admin/entity/fields.php +++ b/models/admin/entity/fields.php @@ -164,7 +164,45 @@ class GlmMembersAdmin_entity_fields extends GlmDataFieldsCustomFields }else { $customFieldsData = customFieldsGetFieldData( $entityID ); } + // Get all groups + $groups = $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_groups + WHERE uid = '$uid' + ORDER BY group_order", + ARRAY_A + ); + if ( $groups ) { + foreach ( $groups as $key => $group ) { + $group_custom_fields = $this->getList( "uid = '$uid' AND gid = {$group['id']}", 'field_order' ); + // Go through all fields and get any picklist options + if ( $group_custom_fields ) { + foreach ( $group_custom_fields as $fkey => $field ) { + if ( $field['field_type']['value'] === 'picklist' ) { + // get the options for this field + $options = $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_options + WHERE field_id = %d + ORDER BY option_text", + $field['id'] + ), + ARRAY_A + ); + if ( $options ) { + $group_custom_fields[$fkey]['options'] = $options; + } + } + } + } + $groups[$key]['custom_fields'] = $group_custom_fields; + } + } + if ( isset($groups) && $groups && count( $groups ) > 0 ) { + $haveGroups = true; + } // Compile template data $template_data = array( @@ -174,7 +212,9 @@ class GlmMembersAdmin_entity_fields extends GlmDataFieldsCustomFields 'fieldFail' => $fieldFail, 'fieldTypes' => $this->config['custom_field_types'], 'haveCustomFields' => $haveCustomFields, - 'uid' => $uid + 'uid' => $uid, + 'groups' => $groups, + 'haveGroups' => $haveGroups, ); // Return status, suggested view, and data to controller diff --git a/models/admin/management/fields.php b/models/admin/management/fields.php index d3fa638..93dd529 100644 --- a/models/admin/management/fields.php +++ b/models/admin/management/fields.php @@ -147,6 +147,58 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields switch ($option) { + case 'deleteOption': + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $option_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); + if ( $option_id ) { + $this->wpdb->delete( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_options', + array( 'id' => $option_id ) + ); + } + break; + + case 'fieldoption': + // Create or update field option + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); + $field_id = filter_var( $_REQUEST['field_id'], FILTER_VALIDATE_INT ); + $option_text = filter_var( $_REQUEST['option_text'] ); + $option_default = filter_var( $_REQUEST['option_default'], FILTER_VALIDATE_BOOLEAN ); + if ( $field_id ) { + $option_data = array( + 'field_id' => $field_id, + 'option_text' => $option_text, + 'option_default' => $option_default, + ); + if ( $id ) { + $this->wpdb->update( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_options', + $option_data, + array( 'id' => $id ), + array( '%d', '%s', '%d' ), + array( '%d' ) + ); + } else { + $this->wpdb->insert( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_options', + $option_data, + array( '%d', '%s', '%d' ) + ); + $id = $this->wpdb->insert_id; + } + } + // If the default is selected then set all but this option to false for option_default + if ( $option_default && $id ) { + $this->wpdb->query( + "UPDATE " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_options + SET option_default = 0 + WHERE field_id = $field_id + AND id != $id" + ); + } + break; + case 'updateNewGroup': // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; $gid = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); @@ -236,6 +288,11 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_data', array( 'field_id' => $id ) ); + // Also delete any options for the field + $this->wpdb->delete( + GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_options', + array( 'field_id' => $id ) + ); } break; @@ -276,13 +333,32 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields if ( $groups ) { foreach ( $groups as $key => $group ) { - $groups[$key]['custom_fields'] = $this->getList( "uid = '$uid' AND gid = {$group['id']}", 'field_order' ); + $group_custom_fields = $this->getList( "uid = '$uid' AND gid = {$group['id']}", 'field_order' ); + // Go through all fields and get any picklist options + if ( $group_custom_fields ) { + foreach ( $group_custom_fields as $fkey => $field ) { + if ( $field['field_type']['value'] === 'picklist' ) { + // get the options for this field + $options = $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_options + WHERE field_id = %d + ORDER BY option_text", + $field['id'] + ), + ARRAY_A + ); + if ( $options ) { + $group_custom_fields[$fkey]['options'] = $options; + } + } + } + } + $groups[$key]['custom_fields'] = $group_custom_fields; } - // echo '
$groups: ' . print_r( $groups, true ) . '
'; } - // echo "
REQUEST " . print_r($_REQUEST, true) . "
"; - // echo "
GET " . print_r($_GET, true) . "
"; if ( isset($custom_fields) && $custom_fields && count( $custom_fields ) > 0 ) { $haveCustomFields = true; } diff --git a/views/admin/entity/fields.html b/views/admin/entity/fields.html index 2c69862..f6173a2 100644 --- a/views/admin/entity/fields.html +++ b/views/admin/entity/fields.html @@ -1,38 +1,98 @@
- {foreach $customFields as $field} -
{$field.field_name}
-
- - - {$fail = ''} - {if is_array($customFieldsData)} - {if array_key_exists('fieldFail', $customFieldsData)} - {if array_key_exists($field.id,$customFieldsData['fieldFail'])} - {$fail = 'glm-form-bad-input'} - {/if} - {/if} - {/if} -
- {if $field.field_type.name == 'text'} - - - {elseif $field.field_type.name == 'textarea'} - {php} - wp_editor('{if isset($customFieldsData[$field.id])}{$customFieldsData[$field.id]|escape:quotes}{/if}', 'custom-field-{$field.id}', array( - 'media_buttons' => false, - // 'quicktags' => false, - // 'wpautop' => false, NOTE: Dont's use. Problem when numerous spaces before text. - 'textarea_name' => '{$prefix}[{$field.id}]', - 'editor_height' => 200, // Height in px, overrides editor_rows - // 'textarea_rows' => 8, - {if $field.required.value == 1}'editor_class' => 'glm-required' {/if} - )); - {/php} - {elseif $field.field_type.name == 'checkbox'} - - + {if $haveGroups} + + {foreach $groups as $group} + +
+ {$group.group_name} + + {foreach $group.custom_fields as $field} +
{$field.field_name}
+
+ + + {$fail = ''} + {if is_array($customFieldsData)} + {if array_key_exists('fieldFail', $customFieldsData)} + {if array_key_exists($field.id,$customFieldsData['fieldFail'])} + {$fail = 'glm-form-bad-input'} + {/if} + {/if} + {/if} +
+ {if $field.field_type.value == 'text'} + + + {elseif $field.field_type.value == 'textarea'} + {php} + wp_editor('{if isset($customFieldsData[$field.id])}{$customFieldsData[$field.id]|escape:quotes}{/if}', 'custom-field-{$field.id}', array( + 'media_buttons' => false, + // 'quicktags' => false, + // 'wpautop' => false, NOTE: Dont's use. Problem when numerous spaces before text. + 'textarea_name' => '{$prefix}[{$field.id}]', + 'editor_height' => 200, // Height in px, overrides editor_rows + // 'textarea_rows' => 8, + {if $field.required.value == 1}'editor_class' => 'glm-required' {/if} + )); + {/php} + {elseif $field.field_type.value == 'checkbox'} + + + {elseif $field.field_type.value == 'picklist'} + {if $field.options} + + {/if} + {/if} +
+
+ {/foreach} + +
+ + {/foreach} + + {else} + + {foreach $customFields as $field} +
{$field.field_name}
+
+ + + {$fail = ''} + {if is_array($customFieldsData)} + {if array_key_exists('fieldFail', $customFieldsData)} + {if array_key_exists($field.id,$customFieldsData['fieldFail'])} + {$fail = 'glm-form-bad-input'} + {/if} {/if} -
-
- {/foreach} + {/if} +
+ {if $field.field_type.value == 'text'} + + + {elseif $field.field_type.value == 'textarea'} + {php} + wp_editor('{if isset($customFieldsData[$field.id])}{$customFieldsData[$field.id]|escape:quotes}{/if}', 'custom-field-{$field.id}', array( + 'media_buttons' => false, + // 'quicktags' => false, + // 'wpautop' => false, NOTE: Dont's use. Problem when numerous spaces before text. + 'textarea_name' => '{$prefix}[{$field.id}]', + 'editor_height' => 200, // Height in px, overrides editor_rows + // 'textarea_rows' => 8, + {if $field.required.value == 1}'editor_class' => 'glm-required' {/if} + )); + {/php} + {elseif $field.field_type.value == 'checkbox'} + + + {/if} +
+
+ {/foreach} + {/if}
diff --git a/views/admin/management/fields.html b/views/admin/management/fields.html index bc3e8b3..54baaca 100644 --- a/views/admin/management/fields.html +++ b/views/admin/management/fields.html @@ -2,7 +2,8 @@
Add A Custom Field Group
-
+ + @@ -26,6 +27,35 @@
+{* Add / Edit Option Dialog Box *} +
+
+ + + + + + + + + + + + + + + + +
Option Name:
Selected by Default + + +
+ + Cancel + +
+
+ {* Delete Field Dialog Box *}
@@ -45,10 +75,18 @@
+{* Delete Option Dialog Box *} +
+
+

Are you sure you want to delete this Option?

+

Yes, delete this Option

+

Cancel

+
+
{* Edit Field Dialog Box *}
-
+ @@ -109,8 +147,8 @@
{if $groups} {foreach $groups as $group} -
-
+
+
{$group.group_name} ( {if $group.publish}Published{else}Not Published{/if} ) {if $group.custom_fields} {foreach $group.custom_fields as $field} - - - {$field.field_name} - - {$field.field_type.name} - {$field.required.name} - {$field.admin_search.name} - -
Delete
- -
Edit
- - + + + + + + + + + + + {if $field.field_type.value == 'picklist'} + + + + {/if} +
+ {$field.field_name} + {$field.field_type.name} {$field.required.name} {$field.admin_search.name} +
Delete
+ +
Edit
+
+ + + + + + + + + {if $field.options && !empty($field.options)} + {foreach $field.options as $option} + + + + + + {/foreach} + {/if} +
Option Name Default +
Add Option
+
{$option.option_text} {if $option.option_default}Yes{else}No{/if} +
Delete
+ +
Edit
+
+
+ + {/foreach} {/if} @@ -167,11 +247,14 @@