From ed1bbb57a86d8ab188be438e43bacfc81d0eb9ee Mon Sep 17 00:00:00 2001 From: Anthony Talarico Date: Thu, 26 Oct 2017 08:02:28 -0400 Subject: [PATCH] adding required fields and required fields checking removed entity id from the fields table, using request array to re insert input on failed attempts --- classes/customFieldPluginSupport.php | 33 ++++++++++++++----- classes/data/dataCustomFields.php | 7 ---- models/admin/entity/fields.php | 10 +++++- setup/adminHooks.php | 14 ++++---- .../create_database_V0.0.4.sql | 1 - .../update_database_V0.0.4.sql | 4 --- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/classes/customFieldPluginSupport.php b/classes/customFieldPluginSupport.php index 09b8142..e974327 100644 --- a/classes/customFieldPluginSupport.php +++ b/classes/customFieldPluginSupport.php @@ -14,8 +14,7 @@ * @access public * @return void */ -function customFieldsSaveFields( $entityID ) -{ +function customFieldsSaveFields( $entityID ){ global $wpdb; echo $entityID; // If no data for custom field then return @@ -23,7 +22,7 @@ function customFieldsSaveFields( $entityID ) return false; } //echo "Trying to save entity of type $uid"; - print_r(customFieldsGetFields($entityID)); +// print_r(customFieldsGetFields($entityID)); // See if this memberInfo has current field data $currentCustomFields = customFieldsGetFieldData( $entityID ); // Insert the custom field record @@ -32,10 +31,26 @@ function customFieldsSaveFields( $entityID ) '%d', '%s' ); + $sql = "SELECT * FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields WHERE required=1"; + $required = $wpdb->get_results($sql); + + if(!empty($required)){ + foreach($required as $req){ + $requiredFields[] = $req->id; + } + } + foreach($_REQUEST['glm_custom_field'] as $fieldID =>$fieldValue){ + if( in_array( $fieldID,$requiredFields )){ + if($fieldValue === ''){ + return $_REQUEST['glm_custom_field']; + } + } + } + // Loop through the glm_custom_field array foreach ( $_REQUEST['glm_custom_field'] as $fieldId => $fieldValue ) { $fieldData =array( - 'entity_id' => $entityID, + 'entity_id' => $entityID, 'field_id' => $fieldId, 'field_data' => wp_kses_post(stripslashes($fieldValue)) ); @@ -53,13 +68,14 @@ function customFieldsSaveFields( $entityID ) $fieldData, array( 'entity_id' => $entityID, - 'field_id' => $fieldId + 'field_id' => $fieldId ), $fieldDataFormat, '%d' ); } } + return true; } function customFieldsCloneFields( $oldId, $newId ) @@ -129,16 +145,17 @@ function customFieldsGetFieldData( $entityID ) * @access public * @return void */ -function customFieldsGetFields( $entityID, $where = ' AND true ' ) +function customFieldsGetFields( $entityID, $where = ' AND true ', $uid ) { global $wpdb; if ( !$entityID) { return false; } - $data = array(); + $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 ); + $fieldData = $wpdb->get_results( $wpdb->prepare( $sql, $uid ), ARRAY_A ); + echo '
',  print_r($fieldData), '
'; } diff --git a/classes/data/dataCustomFields.php b/classes/data/dataCustomFields.php index f70c2b8..1136351 100644 --- a/classes/data/dataCustomFields.php +++ b/classes/data/dataCustomFields.php @@ -129,13 +129,6 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract 'required' => true, 'use' => 'a' ), - // Entity ID (event, member, package) - 'entity_id' => array ( - 'field' => 'entity_id', - 'type' => 'integer', - 'view_only' => false, - 'use' => 'a' - ), // group id 'uid' => array ( 'field' => 'uid', diff --git a/models/admin/entity/fields.php b/models/admin/entity/fields.php index bae35c2..f5feabe 100644 --- a/models/admin/entity/fields.php +++ b/models/admin/entity/fields.php @@ -136,6 +136,7 @@ class GlmMembersAdmin_entity_fields extends GlmDataFieldsCustomFields if (isset($actionData)) { $entityID = $actionData['entityID']; $uid = $actionData['uid']; + $fieldFail = $actionData['cfData']; } switch ($option) { @@ -156,13 +157,20 @@ class GlmMembersAdmin_entity_fields extends GlmDataFieldsCustomFields } require_once GLM_MEMBERS_FIELDS_PLUGIN_CLASS_PATH . '/customFieldPluginSupport.php'; - $customFieldsData = customFieldsGetFieldData( $entityID ); + // is array ensures that the data passed BAD + if(is_array($fieldFail)){ + $customFieldsData = $fieldFail; + }else { + $customFieldsData = customFieldsGetFieldData( $entityID ); + } + // Compile template data $template_data = array( 'prefix' => 'glm_custom_field', 'customFields' => $customFields, 'customFieldsData' => $customFieldsData, + 'fieldFail' => $fieldFail, 'fieldTypes' => $this->config['custom_field_types'], 'haveCustomFields' => $haveCustomFields, 'uid' => $uid diff --git a/setup/adminHooks.php b/setup/adminHooks.php index aa4131a..f82fb26 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -38,16 +38,16 @@ add_filter( 'glm-members-customfields-edit', function( $content, $uid ){ unset( $_REQUEST['glm_action'] ); $content = $this->controller( 'management', 'fields', array( 'uid' => $uid ), true ); return $content; -}, 10, 3 ); +}, 10, 2 ); /** * Filter returns the html for the form segment */ -add_filter( 'glm-members-custom-fields-form', function( $content, $uid, $id ){ +add_filter( 'glm-members-custom-fields-form', function( $content, $uid, $id,$cfData = false ){ unset( $_REQUEST['glm_action'] ); // echo "CONTENT: " . $content . " UID: " . $uid . " ID: " . $id . '
'; - $content = $this->controller( 'entity', 'fields', array( 'uid' => $uid, 'entityID' => $id ), true); + $content = $this->controller( 'entity', 'fields', array( 'uid' => $uid, 'entityID' => $id,'cfData'=>$cfData ), true); return $content; -}, 10, 3 ); +}, 10, 4 ); /** * Filter Captured Data Success Status * @@ -157,12 +157,12 @@ add_filter('glm-member-db-admin-search-query', function() { } return $queryParts; }); -add_filter('glm-member-db-save-custom-fields', function( $entityID, $uid ) { +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, $uid ); - return true; + return customFieldsSaveFields( $entityID); + }, 10, 1 diff --git a/setup/databaseScripts/create_database_V0.0.4.sql b/setup/databaseScripts/create_database_V0.0.4.sql index 1b02eb4..70a416a 100644 --- a/setup/databaseScripts/create_database_V0.0.4.sql +++ b/setup/databaseScripts/create_database_V0.0.4.sql @@ -21,7 +21,6 @@ CREATE TABLE {prefix}custom_fields ( 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 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)) ); diff --git a/setup/databaseScripts/update_database_V0.0.4.sql b/setup/databaseScripts/update_database_V0.0.4.sql index c16f26e..015dc2c 100644 --- a/setup/databaseScripts/update_database_V0.0.4.sql +++ b/setup/databaseScripts/update_database_V0.0.4.sql @@ -18,8 +18,4 @@ 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); -- 2.17.1