renaming ref_dest in db. changing instances of memberID to entityID, adding new method
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 24 Oct 2017 17:34:46 +0000 (13:34 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 24 Oct 2017 17:34:46 +0000 (13:34 -0400)
to get fields to compare values with the required field values

classes/customFieldPluginSupport.php
classes/data/dataCustomFieldsData.php
models/admin/entity/fields.php
models/front/fields/detail.php
models/front/fields/list.php
setup/adminHooks.php
setup/databaseScripts/create_database_V0.0.4.sql
setup/databaseScripts/update_database_V0.0.4.sql
setup/frontHooks.php
views/admin/entity/fields.html
views/admin/management/fields.html

index 647a947..09b8142 100644 (file)
@@ -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 );
+}
index 0bcbd3e..e9ca9bc 100644 (file)
@@ -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',
index 5782124..3115c1a 100644 (file)
@@ -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',
index a7f26c8..ed191f0 100644 (file)
@@ -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);
index c394f2f..7b7464d 100644 (file)
@@ -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);
index 71adb91..f67d9f2 100644 (file)
@@ -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 . '<br>';
-    $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
index a731cd5..1b02eb4 100644 (file)
@@ -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)
 );
index 6ae7b29..c16f26e 100644 (file)
@@ -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);
index 398026a..0ea8e83 100644 (file)
@@ -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'";
 
index 7c54bfc..08c8467 100644 (file)
@@ -2,7 +2,9 @@
     {foreach $customFields as $field}
         <div class="glm-small-12 glm-large-2 glm-columns glm-custom-field-label">{$field.field_name}</div>
         <div class="glm-small-12 glm-large-9 glm-columns glm-custom-field">
+            <input type="hidden" name="custom-required" value="{$field.required.value}">
             {if $field.field_type.name == 'text'}
+       
             <input type="text" name="{$prefix}[{$field.id}]" value="{if isset($customFieldsData[$field.id])}{$customFieldsData[$field.id]|escape}{/if}">
             {elseif $field.field_type.name == 'textarea'}
                 {php}
index 9cca065..8df42e9 100644 (file)
                     (text or checkbox only)
                 </td>
             </tr>
+            <tr>
+                <th>Required?</th>
+                <td>
+                    <input type="hidden" name="required" value="0" />
+                    <input type="checkbox" id="editRequired" name="required" value="1" />
+                </td>
+            </tr>
         </table>
         <p><span class="glm-required">*</span> Required</p>
         <a id="editFieldCancel" class="button button-primary glm-right">Cancel</a>
                 <td>{$t.id}</td>
                 <td>
                     <div>
-                        <a class="editField" data-fieldID="{$t.id}" data-fieldType="{$t.field_type.name|escape:'html'}" data-adminSearch="{$t.admin_search.value}">{$t.field_name}</a>
+                        <a class="editField" data-fieldID="{$t.id}" data-fieldType="{$t.field_type.name|escape:'html'}" data-adminSearch="{$t.admin_search.value}" data-required="{$t.required.value}">{$t.field_name}</a>
                     </div>
                 </td>
                 <td id="editFieldType_{$t.id}">
@@ -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() {