Fix some update issues with fields and add option for tab name.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 25 Jul 2018 19:45:20 +0000 (15:45 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 25 Jul 2018 19:45:20 +0000 (15:45 -0400)
Adding an option for naming the member custom field tab.
Fix update of fields for the checkboxes.

models/admin/management/fields.php
setup/adminHooks.php
views/admin/entity/fields.html
views/admin/management/fields.html

index 93c7862..8a6e9ef 100644 (file)
@@ -124,6 +124,12 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields
         $custom_fields         = false;
         $haveCustomFields      = false;
         $where                 = ' TRUE ';
+        $glm_field_tab_name    = get_option( 'glm_field_tab_name' );
+
+        // If tab name option is empty then set it.
+        if ( !$glm_field_tab_name ) {
+            update_option( 'glm_field_tab_name', 'Custom Fields', true );
+        }
 
         if (isset($_REQUEST['option2'])) {
             $option = $_REQUEST['option2'];
@@ -147,6 +153,13 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields
 
         switch ($option) {
 
+        case 'updateTabNameOption':
+            // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+            $tab_name = filter_var( $_REQUEST['glm_field_tab_name'] );
+            update_option( 'glm_field_tab_name', $tab_name, true );
+            $glm_field_tab_name    = get_option( 'glm_field_tab_name' );
+            break;
+
             case 'deleteOption':
                 // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
                 $option_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT );
@@ -164,39 +177,38 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields
                 $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 );
+                // $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', '%s' ),
                             array( '%d' )
                         );
                     } else {
                         $this->wpdb->insert(
                             GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_options',
                             $option_data,
-                            array( '%d', '%s', '%d' )
+                            array( '%d', '%s' )
                         );
                         $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"
-                    );
-                }
+                // 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':
@@ -295,6 +307,15 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields
                             'field_order'  => $field_order,
                             'admin_search' => $admin_search,
                             'required'     => $required,
+                        ),
+                        array(
+                            '%d', // gid
+                            '%s', // uid
+                            '%s', // field_name
+                            '%s', // field_type
+                            '%d', // field_order
+                            '%s', // admin_search
+                            '%s', // required
                         )
                     );
                     // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
@@ -304,8 +325,33 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields
                 break;
 
             case 'update':
+                $gid          = filter_var( $_REQUEST['gid'], FILTER_VALIDATE_INT );
+                $field_name   = filter_var( $_REQUEST['field_name'] );
+                $admin_search = filter_var( $_REQUEST['admin_search'], FILTER_VALIDATE_BOOLEAN );
+                $required     = filter_var( $_REQUEST['required'], FILTER_VALIDATE_BOOLEAN );
+                // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+                // echo '<pre>$id: ' . print_r( $id, true ) . '</pre>';
                 if ($id > 0) {
-                    $this->updateEntry($id);
+                    // $this->updateEntry($id);
+                    $this->wpdb->update(
+                        GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_fields',
+                        array(
+                            'gid'          => $gid,
+                            'uid'          => $uid,
+                            'field_name'   => $field_name,
+                            'admin_search' => $admin_search,
+                            'required'     => $required,
+                        ),
+                        array( 'id' => $id ),
+                        array(
+                            '%d', // gid
+                            '%s', // uid
+                            '%s', // field_name
+                            '%s', // admin_search
+                            '%s', // required
+                        ),
+                        array( '%d' )
+                    );
                 }
                 break;
 
@@ -404,6 +450,7 @@ class GlmMembersAdmin_management_fields extends GlmDataFieldsCustomFields
             'uid'                 => $uid,
             'glm_action'          => $glm_action,
             'groups'              => $groups,
+            'glm_field_tab_name'  => $glm_field_tab_name,
         );
         // echo "<pre>Template data:" . print_r($template_data, true) . "</pre>";
 
index 54a79a5..5eaeb01 100644 (file)
@@ -89,7 +89,12 @@ add_filter( 'glm-members-customfields-data', function( $content, $uid, $id ){
  * Filter to Return a tab link for multi-page sub-tabs.
  */
 add_filter( 'glm-member-db-custom-fields-nav', function( $content, $tableName ){
-    $tabLabel = ( $tableName == 'contact-info' ) ? 'Directory Info' : 'Custom Fields';
+    $tab_name = get_option( 'glm_field_tab_name' );
+    if ( $tab_name ) {
+        $tabLabel = $tab_name;
+    } else {
+        $tabLabel = ( $tableName == 'contact-info' ) ? 'Directory Info' : 'Custom Fields';
+    }
     $out = '<a id="glm-custom-fields" data-show-table="glm-table-custom-fields" class="glm-' . $tableName . '-tab nav-tab">' . $tabLabel . '</a>';
     return $out;
 },10,2);
index f6173a2..9e11ed8 100644 (file)
                             <select name="{$prefix}[{$field.id}]">
                                 <option value=""></option>
                                 {foreach $field.options as $option}
-                                    <option value="{$option.option_text|escape:'html'}"{if isset($customFieldsData[$field.id]) && $customFieldsData[$field.id] == $option.option_text} selected{/if}>{$option.option_text}</option>
+                                    <option value="{$option.option_text|escape:'html'}"
+                                        {if (isset($customFieldsData[$field.id]) && $customFieldsData[$field.id] == $option.option_text) || $option.option_default}
+                                            selected
+                                        {/if}>{$option.option_text}</option>
                                 {/foreach}
                             </select>
                         {/if}
index 194e1ad..e63216a 100644 (file)
                 <th class="glm-required">Option Name:</th>
                 <td><input id="optionName" type="text" name="option_text" class="glm-form-text-input" required></td>
             </tr>
-            <tr>
-                <th>Selected by Default</th>
-                <td>
-                    <input type="hidden" name="default" value="0" />
-                    <input id="optionDefault" type="checkbox" name="option_default" value="1" />
-                </td>
-            </tr>
         </table>
 
         <a id="cfOptionCancel" class="button button-primary glm-right">Cancel</a>
 {* Updates / Errors *}
 <table id="glm-table-settings" class="glm-admin-table glm-settings-table{if $option2!='settings'} glm-hidden{/if}">
     <tr>
-        <td colspan="2">
+        <td>
             {if $settingsUpdated}<h2 class="glm-notice glm-flash-updated glm-right">Settings Updated</h2>{/if}
             {if $settingsUpdateError}<span class="glm-error glm-flash-updated glm-right">Settings Update Error</span>{/if}
             <h2>Custom Fields</h2>
     </tr>
 </table>
 
+{* Manage name for custom field tab *}
+{if $uid == 'glm-member-db'}
+    <form id="editFieldForm" action="{$thisUrl}?page={$thisPage}&glm_action={$glm_action}&option=customfields" method="post" enctype="multipart/form-data">
+        <input type="hidden" name="glm_action" value="{$glm_action}" />
+        <input type="hidden" name="option" value="customfields" />
+        <input type="hidden" id="editFieldOption2" name="option2" value="updateTabNameOption" />
+        <table>
+            <tr>
+                <th> Tab Name </th>
+                <td>
+                    <input name="glm_field_tab_name" value="{$glm_field_tab_name}" />
+                </td>
+                <th>
+                    <input class="button button-secondary glm-button-small" type="submit" value="Update">
+                </th>
+            </tr>
+        </table>
+    </form>
+{/if}
+
 {* custom fields table *}
 <div id="sortable-groups">
     {if $groups}
                                                     <thead>
                                                         <tr style="background-color: #ddd;">
                                                             <th style="text-align: left;"> Option Name </th>
-                                                            <th style="text-align: left;"> Default </th>
                                                             <th width="10%">
                                                                 <div class="fieldoption button button-secondary glm-button-small" data-field-id="{$field.id}"> Add Option </div>
                                                             </th>
                                                         {foreach $field.options as $option}
                                                             <tr>
                                                                 <td style="text-align: left;"> {$option.option_text} </td>
-                                                                <td style="text-align: left;"> {if $option.option_default}Yes{else}No{/if} </td>
                                                                 <td width="10%">
                                                                     <div class="deleteOptionButton button button-secondary glm-button-small"
                                                                         data-option-id="{$option.id}">Delete</div>
                                                                         data-id="{$option.id}"
                                                                         data-field-id="{$option.field_id}"
                                                                         data-option-name="{$option.option_text|escape:'html'}"
-                                                                        data-option-default="{$option.option_default}"
                                                                     >Edit</div>
                                                                 </td>
                                                             </tr>
@@ -390,7 +400,7 @@ jQuery(document).ready(function($) {
         $('#editFieldName').val('');
         $('#editFieldType').val('');
         // Set the field type to not be disabled
-        $('#editFieldType').prop('disabled', false);
+        //$('#editFieldType').prop('isabled', false);
         $('#editAdminSearch').prop('checked', false);
         $('#editRequired').prop('checked', false);
 
@@ -407,7 +417,7 @@ jQuery(document).ready(function($) {
         $('#editFieldID').val(fieldID);
         $('#editFieldName').val(fieldName.trim());
         $('#editFieldType').val(fieldType);
-        $('#editFieldType').prop('disabled', true);
+        //$('#editFieldType').prop('disabled', true);
         $('#editGroupID').val(groupID);
         $('#editFieldOption2').val('update');
         $('#editFieldSubmit').val('Update Field');
@@ -431,11 +441,9 @@ jQuery(document).ready(function($) {
         var f_id             = $('#optionId');
         var f_field_id       = $('#fieldId');
         var f_option_name    = $('#optionName');
-        var f_option_default = $('#optionDefault');
         f_id.val('');
         f_field_id.val('');
         f_option_name.val('');
-        f_option_default.prop('checked', false);
 
         // if there's a data id attr
         var optionId = $(this).attr('data-id');
@@ -447,9 +455,6 @@ jQuery(document).ready(function($) {
             f_id.val( $(this).attr('data-id') );
             f_field_id.val( $(this).attr('data-field-id') );
             f_option_name.val( $(this).attr('data-option-name') );
-            if ( $(this).attr('data-option-default') === '1' ) {
-                f_option_default.prop('checked', true);
-            }
         }
 
         $('#cfOptionDialog').dialog('open');