From d74b943b1b07d0efbd747ed47c684d180bb850d2 Mon Sep 17 00:00:00 2001 From: Anthony Talarico Date: Tue, 24 Oct 2017 13:34:46 -0400 Subject: [PATCH] renaming ref_dest in db. changing instances of memberID to entityID, adding new method to get fields to compare values with the required field values --- classes/customFieldPluginSupport.php | 50 ++++++++++++++----- classes/data/dataCustomFieldsData.php | 9 ---- models/admin/entity/fields.php | 8 +-- models/front/fields/detail.php | 2 +- models/front/fields/list.php | 2 +- setup/adminHooks.php | 18 +++---- .../create_database_V0.0.4.sql | 9 ++-- .../update_database_V0.0.4.sql | 8 ++- setup/frontHooks.php | 6 +-- views/admin/entity/fields.html | 2 + views/admin/management/fields.html | 20 +++++++- 11 files changed, 85 insertions(+), 49 deletions(-) diff --git a/classes/customFieldPluginSupport.php b/classes/customFieldPluginSupport.php index 647a947..09b8142 100644 --- a/classes/customFieldPluginSupport.php +++ b/classes/customFieldPluginSupport.php @@ -9,22 +9,23 @@ * Save the data for the Member Info custom fields. * Uses the $_REQUEST variable glm_custom_field. * - * @param mixed $memberId Id for the member info record + * @param mixed $entityID Id for the member info record * * @access public * @return void */ -function customFieldsSaveFields( $memberId ) -{ +function customFieldsSaveFields( $entityID ) +{ global $wpdb; + echo $entityID; // If no data for custom field then return if ( !isset( $_REQUEST['glm_custom_field'] ) ) { return false; } //echo "Trying to save entity of type $uid"; - + print_r(customFieldsGetFields($entityID)); // See if this memberInfo has current field data - $currentCustomFields = customFieldsGetFields( $memberId ); + $currentCustomFields = customFieldsGetFieldData( $entityID ); // Insert the custom field record $fieldDataFormat = array( '%d', @@ -34,7 +35,7 @@ function customFieldsSaveFields( $memberId ) // Loop through the glm_custom_field array foreach ( $_REQUEST['glm_custom_field'] as $fieldId => $fieldValue ) { $fieldData =array( - 'ref_dest' => $memberId, + 'entity_id' => $entityID, 'field_id' => $fieldId, 'field_data' => wp_kses_post(stripslashes($fieldValue)) ); @@ -51,7 +52,7 @@ function customFieldsSaveFields( $memberId ) GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data", $fieldData, array( - 'ref_dest' => $memberId, + 'entity_id' => $entityID, 'field_id' => $fieldId ), $fieldDataFormat, @@ -73,7 +74,7 @@ function customFieldsCloneFields( $oldId, $newId ) if ( isset( $customFields ) ) { foreach ( $customFields as $fieldId => $fieldData ) { $fieldData =array( - 'ref_dest' => $newId, + 'entity_id' => $newId, 'field_id' => $fieldId, 'field_data' => $fieldData ); @@ -92,23 +93,23 @@ function customFieldsCloneFields( $oldId, $newId ) * Pull the member info custom field data. * This will be used more for admin side. * - * @param mixed $memberId Id for the member info record + * @param mixed $entityID Id for the member info record * * @access public * @return void */ -function customFieldsGetFields( $memberId ) +function customFieldsGetFieldData( $entityID ) { global $wpdb; - if ( !$memberId ) { + if ( !$entityID ) { return false; } $data = array(); $sql = " SELECT * FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data - WHERE ref_dest = %d"; - $fieldData = $wpdb->get_results( $wpdb->prepare( $sql, $memberId ), ARRAY_A ); + WHERE entity_id = %d"; + $fieldData = $wpdb->get_results( $wpdb->prepare( $sql, $entityID ), ARRAY_A ); if ( $fieldData ) { foreach ( $fieldData as $key => $val ) { $data[$val['field_id']] = $val['field_data']; @@ -118,3 +119,26 @@ function customFieldsGetFields( $memberId ) return false; } } +/** + * customFieldsGetFields + * Pull the member info custom field data. + * This will be used more for admin side. + * + * @param mixed $entityID Id for the member info record + * + * @access public + * @return void + */ +function customFieldsGetFields( $entityID, $where = ' AND true ' ) +{ + global $wpdb; + if ( !$entityID) { + return false; + } + $data = array(); + $sql = " + SELECT * + FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields + WHERE id IN (select field_id from ".GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data WHERE entity_id = %d)" . $where; + $fieldData = $wpdb->get_results( $wpdb->prepare( $sql, $entityID ), ARRAY_A ); +} diff --git a/classes/data/dataCustomFieldsData.php b/classes/data/dataCustomFieldsData.php index 0bcbd3e..e9ca9bc 100644 --- a/classes/data/dataCustomFieldsData.php +++ b/classes/data/dataCustomFieldsData.php @@ -136,15 +136,6 @@ class GlmDataFieldsCustomFieldsData extends GlmDataAbstract 'use' => 'a' ), - 'ref_dest' => array ( - 'field' => 'ref_dest', - 'type' => 'pointer', - 'p_table' => GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info', - 'p_field' => 'member_name', - 'p_orderby' => 'member_name', - 'p_blank' => true, - 'use' => 'a' - ), 'field_data' => array( 'field' => 'field_data', 'type' => 'text', diff --git a/models/admin/entity/fields.php b/models/admin/entity/fields.php index 5782124..3115c1a 100644 --- a/models/admin/entity/fields.php +++ b/models/admin/entity/fields.php @@ -132,9 +132,9 @@ class GlmMembersAdmin_entity_fields extends GlmDataFieldsCustomFields if (isset($_REQUEST['id'])) { $id = $_REQUEST['id']-0; } - $memberId = 0; + $entityID = 0; if (isset($actionData)) { - $memberId = $actionData['memberId']; + $entityID = $actionData['entityID']; $uid = $actionData['uid']; } @@ -156,8 +156,8 @@ class GlmMembersAdmin_entity_fields extends GlmDataFieldsCustomFields } require_once GLM_MEMBERS_FIELDS_PLUGIN_CLASS_PATH . '/customFieldPluginSupport.php'; - $customFieldsData = customFieldsGetFields( $memberId ); - + $customFieldsData = customFieldsGetFieldData( $entityID ); + // Compile template data $template_data = array( 'prefix' => 'glm_custom_field', diff --git a/models/front/fields/detail.php b/models/front/fields/detail.php index a7f26c8..ed191f0 100644 --- a/models/front/fields/detail.php +++ b/models/front/fields/detail.php @@ -65,7 +65,7 @@ class GlmMembersFront_fields_detail extends GlmDataFieldsCustomFields $member_name = $member_info['member_name']; $member_name = urlencode($member_name); - $where = "ref_dest = $member_info_id"; + $where = "entity_id = $member_info_id"; $fields_data = new GlmDataFieldsCustomFieldsData($this->wpdb, $this->config); $field_values = $fields_data->getList($where); diff --git a/models/front/fields/list.php b/models/front/fields/list.php index c394f2f..7b7464d 100644 --- a/models/front/fields/list.php +++ b/models/front/fields/list.php @@ -60,7 +60,7 @@ class GlmMembersFront_fields_list extends GlmDataFieldsCustomFields if ( isset( $actionData['request']['member'] ) ) { $member_info_id = $actionData['request']['member'] - 0; - $where = "ref_dest = $member_info_id"; + $where = "entity_id = $member_info_id"; $fields_data = new GlmDataFieldsCustomFieldsData($this->wpdb, $this->config); $field_values = $fields_data->getList($where); diff --git a/setup/adminHooks.php b/setup/adminHooks.php index 71adb91..f67d9f2 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -45,7 +45,7 @@ add_filter( 'glm-members-customfields-edit', function( $content, $uid ){ add_filter( 'glm-members-custom-fields-form', function( $content, $uid, $id ){ unset( $_REQUEST['glm_action'] ); // echo "CONTENT: " . $content . " UID: " . $uid . " ID: " . $id . '
'; - $content = $this->controller( 'entity', 'fields', array( 'uid' => $uid, 'memberId' => $id ), true); + $content = $this->controller( 'entity', 'fields', array( 'uid' => $uid, 'entityID' => $id ), true); return $content; }, 10, 3 ); /** @@ -67,9 +67,9 @@ add_filter( 'glm-member-db-custom-fields-nav', function( $content, $tableName ){ return $out; },10,2); -add_filter( 'glm-member-db-custom-fields-tab', function( $content, $memberId = '' ){ +add_filter( 'glm-member-db-custom-fields-tab', function( $content, $entityID = '' ){ unset( $_REQUEST['glm_action'] ); - $content = $this->controller( 'fields', 'fields', $memberId ); + $content = $this->controller( 'fields', 'fields', $entityID ); return $content; },1,2); add_filter( 'glm-member-db-custom-filter-search', function ( $content ) { @@ -130,7 +130,7 @@ add_filter('glm-member-db-admin-search-query', function() { $queryParts[] = " T.id IN ( SELECT mi.member FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info mi, " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data cfd - WHERE cfd.ref_dest = mi.id + WHERE cfd.entity_id = mi.id AND cfd.field_data like '%" . esc_sql( $fieldValue ) . "%' AND cfd.field_id = (SELECT id FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields @@ -145,7 +145,7 @@ add_filter('glm-member-db-admin-search-query', function() { $queryParts[] = " T.id IN ( SELECT mi.member FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info mi, " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data cfd - WHERE cfd.ref_dest = mi.id + WHERE cfd.entity_id = mi.id AND cfd.field_data = 'Yes' AND cfd.field_id = (SELECT id FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields @@ -157,11 +157,11 @@ add_filter('glm-member-db-admin-search-query', function() { } return $queryParts; }); -add_filter('glm-member-db-save-custom-fields', function( $entityId ) { +add_filter('glm-member-db-save-custom-fields', function( $entityID ) { require_once GLM_MEMBERS_FIELDS_PLUGIN_CLASS_PATH . '/customFieldPluginSupport.php'; // this will save the member data //echo "uid = $uid"; - customFieldsSaveFields( $entityId ); + customFieldsSaveFields( $entityID ); return true; }, 10, @@ -169,10 +169,10 @@ add_filter('glm-member-db-save-custom-fields', function( $entityId ) { ); add_action( 'glm-member-db-clone-custom-fields', - function( $memberInfoId, $newId ){ + function( $entityID, $newId ){ require_once GLM_MEMBERS_FIELDS_PLUGIN_CLASS_PATH . '/customFieldPluginSupport.php'; // this will clone the custom fields data - customFieldsCloneFields( $memberInfoId, $newId ); + customFieldsCloneFields( $entityID, $newId ); }, 1, 2 diff --git a/setup/databaseScripts/create_database_V0.0.4.sql b/setup/databaseScripts/create_database_V0.0.4.sql index a731cd5..1b02eb4 100644 --- a/setup/databaseScripts/create_database_V0.0.4.sql +++ b/setup/databaseScripts/create_database_V0.0.4.sql @@ -20,8 +20,8 @@ CREATE TABLE {prefix}custom_fields ( field_order SMALLINT NOT NULL DEFAULT 0, -- Order for Field admin_search BOOLEAN NOT NULL DEFAULT '0', -- If the field is added to member list filters. required BOOLEAN NOT NULL DEFAULT '0', -- If the field is required. - uid TINYTEXT NOT NULL DEFAULT '', -- id for the series of custom fields associated with an entity - entity_id INT NULL, -- id for the specific field associated with an entity + uid varchar(255) NOT NULL DEFAULT '', -- id for the series of custom fields associated with an entity + entity_id INT NULL, -- id for the specific field associated with an entity PRIMARY KEY (id), INDEX(field_name(20)) ); @@ -31,11 +31,10 @@ CREATE TABLE {prefix}custom_fields ( -- Data Table CREATE TABLE {prefix}custom_field_data ( id INT NOT NULL AUTO_INCREMENT, - field_id INT NOT NULL DEFAULT 0, -- Field Id - ref_dest INT NOT NULL DEFAULT 0, -- Member Info Id + field_id INT NOT NULL DEFAULT 0, -- Field Id -- Member Info Id field_data TEXT NOT NULL DEFAULT '', -- Data for the field entity_id INT NOT NULL DEFAULT 0, -- id for the specific field associated with an entity PRIMARY KEY (id), INDEX(field_id), - INDEX(ref_dest) + INDEX(entity_id) ); diff --git a/setup/databaseScripts/update_database_V0.0.4.sql b/setup/databaseScripts/update_database_V0.0.4.sql index 6ae7b29..c16f26e 100644 --- a/setup/databaseScripts/update_database_V0.0.4.sql +++ b/setup/databaseScripts/update_database_V0.0.4.sql @@ -10,12 +10,16 @@ ALTER TABLE {prefix}custom_fields ADD COLUMN required BOOLEAN DEFAULT '0'; ---- -ALTER TABLE {prefix}custom_fields ADD COLUMN uid TINYTEXT NOT NULL DEFAULT 'glm-member-db'; +ALTER TABLE {prefix}custom_fields ADD COLUMN uid varchar(255) NOT NULL DEFAULT 'glm-member-db'; ---- -ALTER TABLE {prefix}custom_field_data ADD COLUMN entity_id INT NOT NULL DEFAULT 0; +ALTER TABLE {prefix}custom_field_data CHANGE `ref_dest` `entity_id` INT NOT NULL; ---- ALTER TABLE {prefix}custom_fields ADD COLUMN entity_id INT NULL; + +---- + +CREATE INDEX entity_id ON {prefix}custom_field_data(entity_id); diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 398026a..0ea8e83 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -58,7 +58,7 @@ add_filter( 'glm-member-db-front-search-query', function( $queryParts ) { if ( isset( $_REQUEST[$field_name] ) && filter_var( $_REQUEST[$field_name], FILTER_SANITIZE_STRING ) ) { $textSearch = $this->wpdb->esc_like( $_REQUEST[$field_name] ); $queryParts[] = " T.id IN ( - SELECT ref_dest + SELECT entity_id FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data WHERE field_data like '%" . $textSearch . "%' AND field_id = (SELECT id @@ -73,7 +73,7 @@ add_filter( 'glm-member-db-front-search-query', function( $queryParts ) { $field_name = preg_replace( '/[ -]/', '_', strtolower( $field['field_name'] ) ); if ( isset( $_REQUEST[$field_name] ) && filter_var( $_REQUEST[$field_name], FILTER_VALIDATE_BOOLEAN ) ) { $queryParts[] = " T.id IN ( - SELECT ref_dest + SELECT entity_id FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data WHERE field_data = 'Yes' AND field_id = (SELECT id @@ -130,7 +130,7 @@ add_filter('glm_custom_fields', function($attribute, $id = 0) { global $wpdb; $query = "SELECT D.field_data, F.field_type FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data D, ".GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX."custom_fields F " - . "WHERE D.ref_dest = $id " + . "WHERE D.entity_id = $id " . "AND D.field_id IN (SELECT id FROM ".GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX."custom_fields WHERE field_name LIKE '$attribute' ) " . "AND F.field_name LIKE '$attribute'"; diff --git a/views/admin/entity/fields.html b/views/admin/entity/fields.html index 7c54bfc..08c8467 100644 --- a/views/admin/entity/fields.html +++ b/views/admin/entity/fields.html @@ -2,7 +2,9 @@ {foreach $customFields as $field}
{$field.field_name}
+ {if $field.field_type.name == 'text'} + {elseif $field.field_type.name == 'textarea'} {php} diff --git a/views/admin/management/fields.html b/views/admin/management/fields.html index 9cca065..8df42e9 100644 --- a/views/admin/management/fields.html +++ b/views/admin/management/fields.html @@ -87,6 +87,13 @@ (text or checkbox only) + + Required? + + + + +

* Required

Cancel @@ -126,7 +133,7 @@ {$t.id}
- {$t.field_name} + {$t.field_name}
@@ -196,10 +203,12 @@ jQuery(document).ready(function($) { var fieldName = $(this).text(); var fieldType = $(this).attr('data-fieldType'); var adminSearch = $(this).attr('data-adminSearch'); + var required = $(this).attr('data-required'); + $('#editFieldID').val(fieldID); $('#editFieldName').val(fieldName.trim()); $('#editFieldType').val(fieldType); - console.log(adminSearch); + if (adminSearch === '1') { console.log('setting the checked to true'); $('#editAdminSearch').prop('checked', true); @@ -207,6 +216,13 @@ jQuery(document).ready(function($) { console.log('setting the checked to false'); $('#editAdminSearch').prop('checked', false); } + // check required fields + if (required === '1') { + $('#editRequired').prop('checked', true); + } else { + console.log('setting the checked to false'); + $('#editRequired').prop('checked', false); + } $("#editFieldDialog").dialog("open"); }); $('#editFieldCancel').click( function() { -- 2.17.1