Adding group for misc settings.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 12 Apr 2019 20:39:49 +0000 (16:39 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 12 Apr 2019 20:39:49 +0000 (16:39 -0400)
Working with dataAbstract class to get the group to work.
Not saving data when you try to update management setting though.
Testing grabbing data for glm_members_members capability.

classes/data/settings/dataSettingsGeneral.php
js/glmApiSettings.js [new file with mode: 0644]
lib/GlmDataAbstract/DataAbstract.php
models/admin/management/index.php
setup/routes.php [new file with mode: 0644]
views/admin/management/index.html

index d652855..85b6280 100755 (executable)
@@ -103,6 +103,206 @@ class GlmDataSettingsGeneral extends GlmDataAbstract
          */
         $this->table = GLM_MEMBERS_PLUGIN_DB_PREFIX . 'settings_general';
 
+        /**
+         * Grouping of Fields
+         */
+        $this->groups = array(
+            'misc' => array(
+                'name'   => 'misc',
+                'title'  => 'Misc. Settings',
+                'fields' => array(
+                    'admin_debug' => array(
+                        'name'    => 'admin_debug',
+                        'prompt'  => 'Admin Debug Information',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'admin_debug_verbose' => array(
+                        'name'    => 'admin_debug_verbose',
+                        'prompt'  => 'Admin Debug Verbose',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'front_debug' => array(
+                        'name'    => 'front_debug',
+                        'prompt'  => 'Admin Debug Information',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'front_debug_verbose' => array(
+                        'name'    => 'front_debug_verbose',
+                        'prompt'  => 'Admin Debug Verbose',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'enable_members' => array(
+                        'name'    => 'enable_members',
+                        'prompt'  => 'Enable Members',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'enable_contact_edit_only' => array(
+                        'name'    => 'enable_contact_edit_only',
+                        'prompt'  => 'Enable Contact Edits Only',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'enable_member_reports' => array(
+                        'name'    => 'enable_member_reports',
+                        'prompt'  => 'Enable Member Reports',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'enable_caching' => array(
+                        'name'    => 'enable_caching',
+                        'prompt'  => 'Enable Caching',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+
+                    'enable_counties' => array(
+                        'name'    => 'enable_counties',
+                        'prompt'  => 'Enable Counties',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'enable_multiple_profiles' => array(
+                        'name'    => 'enable_multiple_profiles',
+                        'prompt'  => 'Enable Multiple Active Member Profile',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'enable_single_profile' => array(
+                        'name'    => 'enable_single_profile',
+                        'prompt'  => 'Enable Single Member Profile (members only)',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+
+
+                    'memb_info_location' => array(
+                        'name'    => 'memb_info_location',
+                        'prompt'  => 'Address & Location',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'memb_info_contact' => array(
+                        'name'    => 'memb_info_contact',
+                        'prompt'  => 'Contact Info',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'memb_info_categories' => array(
+                        'name'    => 'memb_info_categories',
+                        'prompt'  => 'Categories & Amenities',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'memb_info_images' => array(
+                        'name'    => 'memb_info_images',
+                        'prompt'  => 'Images',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'memb_info_files' => array(
+                        'name'    => 'memb_info_files',
+                        'prompt'  => 'Files',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'memb_info_video' => array(
+                        'name'    => 'memb_info_video',
+                        'prompt'  => 'Video',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'memb_info_cam' => array(
+                        'name'    => 'memb_info_cam',
+                        'prompt'  => 'Live Cam',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+
+
+                    'selected_map_interface' => array(
+                        'name'    => 'selected_map_interface',
+                        'prompt'  => 'Selected Map Interface',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'leaflet_tile_server' => array(
+                        'name'    => 'leaflet_tile_server',
+                        'prompt'  => 'Leaflet Tile Server',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'leaflet_tile_server_key' => array(
+                        'name'    => 'leaflet_tile_server_key',
+                        'prompt'  => 'Leaflet Tile Server Key',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'google_maps_api_key' => array(
+                        'name'    => 'google_maps_api_key',
+                        'prompt'  => 'Google Maps API Key',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'maps_default_zoom' => array(
+                        'name'    => 'maps_default_zoom',
+                        'prompt'  => 'Maps Default Zoom',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'maps_default_lat' => array(
+                        'name'    => 'maps_default_lat',
+                        'prompt'  => 'Lat',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'maps_default_lon' => array(
+                        'name'    => 'maps_default_lon',
+                        'prompt'  => 'Lon',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'file_library' => array(
+                        'name'    => 'file_library',
+                        'prompt'  => 'Enable File Library',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'time_zone' => array(
+                        'name'    => 'time_zone',
+                        'prompt'  => 'Time Zone',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+
+                    'canonical_member_page' => array(
+                        'name'    => 'canonical_member_page',
+                        'prompt'  => 'Member Detail Page Permalink Name',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+                    'phone_format' => array(
+                        'name'    => 'phone_format',
+                        'prompt'  => 'Phone # Formatting',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+
+                    'default_state' => array(
+                        'name'    => 'default_state',
+                        'prompt'  => 'Default State',
+                        'message' => '',
+                        'option'  => ''
+                    ),
+
+                )
+            ),
+        );
+
         /*
          * Table Data Fields
          */
diff --git a/js/glmApiSettings.js b/js/glmApiSettings.js
new file mode 100644 (file)
index 0000000..e69de29
index 11fcfdb..00162c4 100755 (executable)
@@ -153,10 +153,21 @@ abstract class GlmDataAbstract
     /**
      * Groups Array
      *
-     * Used for Grouping the fields.
+     * Used to setup the Grouping of the fields.
      */
     public $groups = array();
 
+    /**
+     * Group Select
+     *
+     * To select the groups of fields to use.
+     */
+    public $groupSelect = false;
+
+    /**
+     * Primary key for the Table.
+     */
+    public $primaryKey = 'id';
 
     /*
      * Field processing for various types of fields
@@ -2950,6 +2961,7 @@ $forEdit = true;
      */
     private function buildFieldsList($op = 'l', $options = false, $defaults = false)
     {
+        // $this->selectGroupFields();
         $this->fieldData = array();
         $this->select = '';
         $sep = ''; // Used as separator for $select list
@@ -3013,6 +3025,7 @@ $forEdit = true;
 
         }
 
+        // $this->restoreFields();
         return;
     }
 
@@ -3390,6 +3403,7 @@ $forEdit = true;
     public function getEntry($id, $idfield = 'id', $where = 'true', $fieldVals = false)
     {
 
+        $this->selectGroupFields();
         // If there's an ID supplied, add that to the query
         $idWhere = '';
         if ($id-0 > 0) {
@@ -3420,6 +3434,7 @@ $forEdit = true;
         // Process individual fields
         $detail = $this->processOutputData($detail, 'g', $fieldVals);
 
+        $this->restoreFields();
         return $detail;
     }
 
@@ -4192,5 +4207,33 @@ $forEdit = true;
     }
 
 
+    public function selectGroupFields()
+    {
+        if ( $this->groupSelect && is_array( $this->groupSelect ) && !empty( $this->groupSelect ) ) {
+            $this->saveFields = $this->fields;
+            $this->fields     = false;
+
+            // Add the primary key
+            $this->fields[] = $this->saveFields[$this->primaryKey];
+
+            foreach ( $this->groupSelect as $groupSelected ) {
+                // Find the group
+                if ( isset( $this->groups[$groupSelected] ) && $this->groups[$groupSelected] ) {
+                    trigger_error( print_r( $this->groups[$groupSelected], true ), E_USER_NOTICE );
+                    if ( isset( $this->groups[$groupSelected]['fields'] ) && is_array( $this->groups[$groupSelected]['fields'] ) ) {
+                        foreach ( $this->groups[$groupSelected]['fields'] as $fieldKey => $field ) {
+                            trigger_error( print_r( $field, true ), E_USER_NOTICE );
+                            $this->fields[] = $this->saveFields[$field['name']];
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    public function restoreFields()
+    {
+        $this->fields     = $this->saveFields;
+        $this->saveFields = false;
+    }
 }
-?>
index 23449ac..22604ac 100755 (executable)
@@ -233,6 +233,19 @@ class GlmMembersAdmin_management_index extends GlmDataSettingsGeneral
             $settings_updated = false;
         }
 
+        wp_register_script( 'glm-api', GLM_MEMBERS_PLUGIN_BASE_URL . '/js/glmApiSettings.js' );
+
+        wp_localize_script(
+            'glm-api',
+            'glmApiSettings',
+            array(
+                'root'  => esc_url_raw( rest_url() ),
+                'nonce' => wp_create_nonce( 'wp_rest' )
+            )
+        );
+
+        wp_enqueue_script( 'glm-api' );
+
         // Compile template data
         $templateData = array(
             'reason'                   => '',
diff --git a/setup/routes.php b/setup/routes.php
new file mode 100644 (file)
index 0000000..a40f325
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * GLM Members Misc Routes For API
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @release  adminHooks.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
+ * @link     http://dev.gaslightmedia.com/
+ */
+
+/**
+ * Require the Contact Data Class.
+ */
+require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/settings/dataSettingsGeneral.php';
+
+/**
+ * Documentation for Wordpress API and adding custom endpoints.
+ * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
+ */
+class glmAssoc_Controller_Contacts
+{
+    /**
+     * __construct
+     *
+     * Class Constructor
+     *
+     * @param $wpdb   Wordpress wpdb Database Object
+     * @param $config GlmAssociate config array
+     */
+    public function __construct( $wpdb, $config )
+    {
+        $this->namespace     = '/glm/v1/';
+        $this->resource_name = 'settings';
+
+        // Setup wpdb and config
+        $this->wpdb   = $wpdb;
+        $this->config = $config;
+    }
+
+    /**
+     * register_routes
+     *
+     * Registers the routes for this class.
+     *
+     * @return void
+     */
+    public function register_routes()
+    {
+        register_rest_route(
+            'glm/v1',   // namespace
+            'settings', // route
+            array(      // array of arrays for multiple methods. Add more arrays for different endpoints.
+                array(  // endpoint for getting settings
+                    'methods'  => WP_REST_Server::READABLE,
+                    'callback' => function( $request ) {
+                        // Callback function can be a lot shorter.
+                        // You could just create a new model to get the results in an array
+                        // and use return rest_ensure_response( $data );
+                        $settingsData = new GlmDataSettingsGeneral( $this->wpdb, $this->config );
+
+                        $settingsData->groupSelect = array( 'misc' );
+                        // $settingsData->selectGroupFields();
+                        $settings = $settingsData->getEntry( 1 );
+                        // $settingsData->restoreFields();
+
+                        return rest_ensure_response( $settings );
+                    },
+                    'permission_callback' => function(){ // can require capability for this rest endpoint.
+                        if ( ! current_user_can( 'glm_members_members' ) ) {
+                            return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => $this->authorization_status_code() ) );
+                        }
+                        return true;
+                    },
+                )
+            )
+        );
+    }
+
+    public function authorization_status_code()
+    {
+        $status = 401;
+
+        if ( is_user_logged_in() ) {
+            $status = 403;
+        }
+
+        return $status;
+    }
+
+}
+
+/**
+ * Create Action for the REST API Route.
+ */
+add_action(
+    'rest_api_init',
+    function(){
+        $controller = new glmAssoc_Controller_Contacts( $this->wpdb, $this->config );
+        $controller->register_routes();
+    }
+);
index 9ae0814..268077d 100755 (executable)
 
         jQuery(document).ready(function($) {
 
+            console.log( 'glmApiSettings', glmApiSettings.root );
+            $.ajax({
+                url: glmApiSettings.root + 'glm/v1/settings',
+                method: 'GET',
+                beforeSend: function( xhr ) {
+                    xhr.setRequestHeader( 'X-WP-Nonce', glmApiSettings.nonce );
+                }
+            }).done( function( response ){
+                console.log( response );
+            } );
+
             /*
              * Edit area tabs
              */