adding required fields and required fields checking
authorAnthony Talarico <talarico@gaslightmedia.com>
Thu, 26 Oct 2017 12:02:28 +0000 (08:02 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Thu, 26 Oct 2017 12:02:28 +0000 (08:02 -0400)
removed entity id from the fields table, using request array to re insert input
on failed attempts

classes/customFieldPluginSupport.php
classes/data/dataCustomFields.php
models/admin/entity/fields.php
setup/adminHooks.php
setup/databaseScripts/create_database_V0.0.4.sql
setup/databaseScripts/update_database_V0.0.4.sql

index 09b8142..e974327 100644 (file)
@@ -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 '<pre>',  print_r($fieldData), '</pre>';
 }
index f70c2b8..1136351 100644 (file)
@@ -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',
index bae35c2..f5feabe 100644 (file)
@@ -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
index aa4131a..f82fb26 100644 (file)
@@ -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 . '<br>';
-    $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
index 1b02eb4..70a416a 100644 (file)
@@ -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))
 );
index c16f26e..015dc2c 100644 (file)
@@ -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);