Update to have admin_search field for the fields
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 24 Apr 2017 15:32:16 +0000 (11:32 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 24 Apr 2017 15:32:16 +0000 (11:32 -0400)
Adding new field to custom field table for admin_search. If this is set
then the fields (checkbox or text) will show up in the member list as an
additional filter that can be used for searching members.

classes/data/dataCustomFields.php
index.php
setup/adminHooks.php
setup/databaseScripts/create_database_V0.0.2.sql [deleted file]
setup/databaseScripts/create_database_V0.0.3.sql [new file with mode: 0644]
setup/databaseScripts/dbVersions.php
setup/databaseScripts/update_database_V0.0.2.sql
setup/databaseScripts/update_database_V0.0.3.sql [new file with mode: 0644]
views/admin/management/fields.html

index c2746ea..25f29ef 100644 (file)
@@ -139,6 +139,14 @@ class GlmDataFieldsCustomFields extends GlmDataAbstract
                 'use'      => 'a'
             ),
 
+            // admin_search flag
+            'admin_search' => array (
+                'field'   => 'admin_search',
+                'type'    => 'checkbox',
+                'default' => false,
+                'use'     => 'a'
+            ),
+
         );
 
     }
index 9873cee..9d6970a 100644 (file)
--- a/index.php
+++ b/index.php
@@ -38,7 +38,7 @@
  *  version from this plugin.
  */
 define('GLM_MEMBERS_FIELDS_PLUGIN_VERSION', '0.0.1');
-define('GLM_MEMBERS_FIELDS_PLUGIN_DB_VERSION', '0.0.2');
+define('GLM_MEMBERS_FIELDS_PLUGIN_DB_VERSION', '0.0.3');
 
 // This is the minimum version of the GLM Members DB plugin require for this plugin.
 define('GLM_MEMBERS_FIELDS_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0');
index 8d803e0..c020b98 100644 (file)
@@ -39,14 +39,23 @@ add_filter( 'glm-member-db-custom-filter-search', function ( $content ) {
 $parts = array();
     $customFields = $this->wpdb->get_results(
         "SELECT *
-           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields",
+           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields
+          WHERE admin_search = 1",
         ARRAY_A
     );
     if ( isset( $customFields ) && count( $customFields ) > 0 ) {
         foreach ( $customFields as $key => $field ) {
             switch ( $field['field_type'] ) {
-            case 'text':
             case 'textarea':
+                break;
+            case 'text':
+                // convert name to lower case and replace spaces with _
+                $field_name = preg_replace( '/[ -]/', '_', strtolower( $field['field_name'] ) );
+                $parts[$field['id']] = '<b>' . $field['field_name'] . ':</b> <input type="text" size="10" name="' . $field_name . '"';
+                if ( isset( $_REQUEST[$field_name] ) && $fieldValue = filter_var( $_REQUEST[$field_name], FILTER_SANITIZE_STRING ) ) {
+                    $parts[$field['id']] .= ' value="' . $fieldValue . '"';
+                }
+                $parts[$field['id']] .= '></b>';
 
                 break;
             case 'checkbox':
@@ -68,14 +77,28 @@ add_filter('glm-member-db-admin-search-query', function() {
     // Get all custom fields
     $customFields = $this->wpdb->get_results(
         "SELECT *
-           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields",
+           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields
+          WHERE admin_search = 1",
         ARRAY_A
     );
     if ( isset( $customFields ) && count( $customFields ) > 0 ) {
         foreach ( $customFields as $key => $field ) {
             switch ( $field['field_type'] ) {
-            case 'text':
             case 'textarea':
+                break;
+            case 'text':
+                // convert name to lower case and replace spaces with _
+                $field_name = preg_replace( '/[ -]/', '_', strtolower( $field['field_name'] ) );
+                if ( isset( $_REQUEST[$field_name] ) && $fieldValue = filter_var( $_REQUEST[$field_name], FILTER_SANITIZE_STRING ) ) {
+                    $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
+                           AND cfd.field_data like '%" .  esc_sql( $fieldValue ) . "%'
+                           AND cfd.field_id = (SELECT id
+                                           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields
+                                          WHERE field_name = '" . esc_sql( $field['field_name'] ) . "'))";
+                }
 
                 break;
             case 'checkbox':
diff --git a/setup/databaseScripts/create_database_V0.0.2.sql b/setup/databaseScripts/create_database_V0.0.2.sql
deleted file mode 100644 (file)
index 293f7c4..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
--- Gaslight Media Members Database - Fields Add-On
--- File Created: 2017-03-27
--- Database Version: 0.0.2
--- Database Creation Script
---
--- This file is called to create a new set of tables for this
--- add-on for the most recent database version for this add-on.
---
--- There should only be one such file in this directory
---
--- To permit each query below to be executed separately,
--- all queries must be separated by a line with four dashes
-
-
--- Field Setup Table
-CREATE TABLE {prefix}custom_fields (
-  id INT NOT NULL AUTO_INCREMENT,
-  field_name TINYTEXT NOT NULL DEFAULT '',      -- Field Name
-  field_type TINYTEXT NOT NULL DEFAULT '',      -- Field Type
-  field_order SMALLINT NOT NULL DEFAULT 0,      -- Order for Field
-  PRIMARY KEY (id),
-  INDEX(field_name(20))
-);
-
-----
-
--- 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_data TEXT NOT NULL DEFAULT '',          -- Data for the field
-  PRIMARY KEY (id).
-  INDEX(field_id),
-  INDEX(ref_dest)
-);
diff --git a/setup/databaseScripts/create_database_V0.0.3.sql b/setup/databaseScripts/create_database_V0.0.3.sql
new file mode 100644 (file)
index 0000000..d3ec574
--- /dev/null
@@ -0,0 +1,37 @@
+-- Gaslight Media Members Database - Fields Add-On
+-- File Created: 2017-03-27
+-- Database Version: 0.0.2
+-- Database Creation Script
+--
+-- This file is called to create a new set of tables for this
+-- add-on for the most recent database version for this add-on.
+--
+-- There should only be one such file in this directory
+--
+-- To permit each query below to be executed separately,
+-- all queries must be separated by a line with four dashes
+
+
+-- Field Setup Table
+CREATE TABLE {prefix}custom_fields (
+  id INT NOT NULL AUTO_INCREMENT,
+  field_name TINYTEXT NOT NULL DEFAULT '',      -- Field Name
+  field_type TINYTEXT NOT NULL DEFAULT '',      -- Field Type
+  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.
+  PRIMARY KEY (id),
+  INDEX(field_name(20))
+);
+
+----
+
+-- 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_data TEXT NOT NULL DEFAULT '',          -- Data for the field
+  PRIMARY KEY (id).
+  INDEX(field_id),
+  INDEX(ref_dest)
+);
index 29c7bd4..68df00c 100644 (file)
@@ -16,5 +16,6 @@
 $glmMembersFieldsDbVersions = array(
     '0.0.1' => array('version' => '0.0.1', 'tables' => 2, 'date' => '03/27/2017'),
     '0.0.2' => array('version' => '0.0.2', 'tables' => 2, 'date' => '04/14/2017'),
+    '0.0.3' => array('version' => '0.0.3', 'tables' => 2, 'date' => '04/24/2017'),
 );
 
index d1cef4c..939a44c 100644 (file)
@@ -4,7 +4,7 @@
 -- Database Update From Previous Version Script
 --
 -- To permit each query below to be executed separately,
--- all queries must be separated by a line with four dashses
+-- all queries must be separated by a line with four dashes
 
 CREATE INDEX field_name ON {prefix}custom_fields(field_name(20));
 
diff --git a/setup/databaseScripts/update_database_V0.0.3.sql b/setup/databaseScripts/update_database_V0.0.3.sql
new file mode 100644 (file)
index 0000000..6862b46
--- /dev/null
@@ -0,0 +1,9 @@
+-- Gaslit Media Members Database
+-- File Created: 2017-04-14
+-- Database Version: 0.0.2
+-- Database Update From Previous Version Script
+--
+-- To permit each query below to be executed separately,
+-- all queries must be separated by a line with four dashes
+
+ALTER TABLE {prefix}custom_fields ADD COLUMN admin_search BOOLEAN DEFAULT '0';
index e958477..831bc91 100644 (file)
@@ -19,7 +19,7 @@
                     </td>
                 </tr>
                 <tr>
-                    <th>Field Type:</th>
+                    <th class="glm-required">Field Type:</th>
                     <td>
                         <select name="field_type">
                             {foreach $field_types as $val => $label}
                         </select>
                     </td>
                 </tr>
+                <tr>
+                    <th>Admin Searchable</th>
+                    <td>
+                        <input type="hidden" name="admin_search" value="0" />
+                        <input type="checkbox" name="admin_search" value="1" />
+                        (text or checkbox only)
+                    </td>
+                </tr>
             </table>
             <p><span class="glm-required">*</span> Required</p>
             <a id="newFieldCancel" class="button button-primary glm-right">Cancel</a>
@@ -58,7 +66,7 @@
                     </td>
                 </tr>
                 <tr>
-                    <th>Field Type:</th>
+                    <th class="glm-required">Field Type:</th>
                     <td>
                         <select id="editFieldType" name="field_type">
                             {foreach $field_types as $val => $label}
                         </select>
                     </td>
                 </tr>
+                <tr>
+                    <th>Admin Searchable</th>
+                    <td>
+                        <input type="hidden" name="admin_search" value="0" />
+                        <input type="checkbox" id="editAdminSearch" name="admin_search" value="1" />
+                        (text or checkbox only)
+                    </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'}">{$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}">{$t.field_name}</a>
                     </div>
                 </td>
                 <td id="editFieldType_{$t.id}">
@@ -160,12 +176,21 @@ jQuery(document).ready(function($) {
         $("#newFieldDialog").dialog("open");
     });
     $('.editField').click( function() {
-        var fieldID = $(this).attr('data-fieldID');
-        var fieldName = $(this).text();
-        var fieldType = $(this).attr('data-fieldType');
+        var fieldID     = $(this).attr('data-fieldID');
+        var fieldName   = $(this).text();
+        var fieldType   = $(this).attr('data-fieldType');
+        var adminSearch = $(this).attr('data-adminSearch');
         $('#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);
+        } else {
+            console.log('setting the checked to false');
+            $('#editAdminSearch').prop('checked', false);
+        }
         $("#editFieldDialog").dialog("open");
     });
     $('#editFieldCancel').click( function() {