Update the front end output to handle event name searching
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 1 Apr 2016 13:57:17 +0000 (09:57 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 1 Apr 2016 13:57:17 +0000 (09:57 -0400)
Using jquery ui autocomplete to get names when they search for them.
Output only the first date if the search by name. This keeps it from
giving the default date range with one event.

js/dashboard.js
models/admin/ajax/nameSearch.php [new file with mode: 0644]
models/front/events/baseAction.php
models/front/events/list.php
setup/validActions.php

index 6b765c5..86d7389 100644 (file)
         eventForm = $('#glm-member-event-search');
         eventForm.attr('action', '../wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=pdfOutput&glm-event-pdf=1');
     });
+    var cache = {};
+    $("#glm-event-name").autocomplete({
+        minLength: 2,
+        source: function(request, response) {
+            var term = request.term;
+            if ( term in cache ) {
+                response( cache[term]);
+                return;
+            }
+            var url = '../wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=nameSearch';
+            $.getJSON( url, request, function(data, status, xhr){
+                //cache[term] = data;
+                response( data );
+            });
+        }
+    });
 }).call(this);
diff --git a/models/admin/ajax/nameSearch.php b/models/admin/ajax/nameSearch.php
new file mode 100644 (file)
index 0000000..364df40
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * PDF Output by admin-ajax
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/front/events/list.php';
+/**
+ * Steve Note...
+ *
+ * You can get to this using the following URL.
+ *
+ *  {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=pdfOutput&mystuff=THIS
+ *
+ * You should be able to do this as POST or GET and should be able to add and read additional parameters.
+ * I added a "mystuff" parameter to the URL above and it does output from the code in the
+ * modelAction() function below.
+ *
+ * To add another model under models/admin/ajax all you need to do is create it and add it to the
+ * setup/validActions.php file.
+ *
+ */
+
+// Load Members data abstract
+// require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php');
+
+/**
+ * This class performs the work of handling images passed to it via
+ * an AJAX call that goes through the WorPress AJAX Handler.
+ *
+ */
+class GlmMembersAdmin_ajax_nameSearch extends GlmMembersFront_events_list
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+}
index 4df4e84..110b426 100644 (file)
@@ -77,8 +77,22 @@ abstract class GlmMembersFront_events_baseAction extends GlmDataEvents
     public function getModelEventsData($categoryId = null, $limit = null)
     {
         $this->postAddTimes = true;
-        if ($categoryId) {
-            $events = $this->getEventsByCategory( $categoryId, $limit );
+        if ($catId = filter_var( $categoryId, FILTER_VALIDATE_INT )) {
+            $events = $this->getEventsByCategory( $catId, $limit );
+        } else if ($term = filter_var( $categoryId, FILTER_SANITIZE_STRING )) {
+            $searchTerm = $this->wpdb->esc_like( $term );
+            $where = "T.name like '%" . $searchTerm . "%' 
+                AND T.status = " . $this->config['status_numb']['Active'] . "
+                AND T.id IN (
+                        SELECT event
+                          FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times AS ET
+                         WHERE active
+                           AND " . $this->dateRange . "
+                    )
+                ";
+            $order  = "T.id";
+            $order .= " LIMIT 2 OFFSET 0";
+            $events = $this->getList($where, $order);
         } else {
             $where = "T.status = " . $this->config['status_numb']['Active'] . "
                 AND T.id IN (
@@ -88,7 +102,6 @@ abstract class GlmMembersFront_events_baseAction extends GlmDataEvents
                            AND " . $this->dateRange . "
                     )
                 ";
-            //echo '<pre>$where: ' . print_r($where, true) . '</pre>';
             $events = $this->getList($where);
         }
         //echo '<pre>$events: ' . print_r($events, true) . '</pre>';
index db4a50a..2004315 100644 (file)
@@ -45,8 +45,8 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
     public function modelAction($actionData = false)
     {
         //$allEvents = $this->getList();
-        $status = $categoryId = null;
-        $action = '';
+        $status   = $categoryId = null;
+        $action   = '';
         $settings = $events = $event = $categoryEvents = array();
         if (isset($_REQUEST['eventId']) && $eventId = filter_var($_REQUEST['eventId'], FILTER_VALIDATE_INT)) {
             $search = true;
@@ -64,6 +64,10 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
             $search = true;
             $action = 'pdf';
         }
+        if (isset($_REQUEST['term']) && $term = filter_var($_REQUEST['term'], FILTER_SANITIZE_STRING) ) {
+            $search = true;
+            $action = 'nameSearch';
+        }
         if ( isset($_REQUEST['glm_event_from']) ) {
             //$fromDate = filter_var($_REQUEST['glm_event_from'], FILTER_SANITIZE_STRING);
             $fromDate = filter_var(
@@ -123,7 +127,7 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
 
         if (isset($_REQUEST['event_name']) && $eventNameSearch = filter_var($_REQUEST['event_name'], FILTER_SANITIZE_STRING)) {
             $search = true;
-            $action = 'event-list';
+            $action = 'event-list-name';
         } else {
             $eventNameSearch = false;
         }
@@ -132,7 +136,7 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
         wp_register_script(
             'event-dashboard-js',
             GLM_MEMBERS_EVENTS_PLUGIN_BASE_URL . '/js/dashboard.js',
-            'jquery-datepicker',
+            array('jquery-ui-datepicker','jquery-ui-autocomplete'),
             GLM_MEMBERS_EVENTS_PLUGIN_VERSION,
             true
         );
@@ -140,10 +144,38 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
 
 
         switch ($action) {
+        case 'event-list-name':
+            $events = $this->getModelEventsData($eventNameSearch);
+            $view = 'agenda.html';
+            break;
         case 'event-list':
             $events = $this->getModelEventsData($categoryId);
             $view = 'agenda.html';
             break;
+        case 'nameSearch':
+            // get list of names based on term
+            if ($term) {
+                $searchResults = array();
+                $searchTerm = $this->wpdb->esc_like( $term );
+                $sql = "
+                SELECT name
+                  FROM ".GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX. "events
+                 WHERE name like '%" . $searchTerm . "%'";
+                $results = $this->wpdb->get_results( $sql, ARRAY_A );
+                if ( !empty( $results ) ) {
+                    foreach ( $results as $result ) {
+                        $searchResults[] =  array(
+                            'id'    => $result['name'],
+                            'label' => $result['name'],
+                            'value' => $result['name'],
+                        );
+                    }
+                }
+                echo json_encode( $searchResults );
+                return 1;
+                exit;
+            }
+            break;
         case 'pdf':
             include_once '/var/www/localhost/Setasign/SetaPDF-Core_2.18.0.817_Ioncubed-PHP5.4/library/SetaPDF/Autoload.php';
             $this->postAddLocations   = true;
@@ -179,6 +211,7 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
                 );
 
                 $totalEvents = count($events);
+                var_dump($totalEvents);
                 $index = 0;
 
                 foreach ( $events as $eventId => $event ) {
@@ -351,6 +384,10 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
                     $event['ending_date']   = $eventTime['end_time']['timestamp'];
                     $event['hasSameTimes']  = ($event['starting_date'] == $event['ending_date']) ? 1 : 0;
                     $eventsByDate[$eventDateTime][$eventTime['start_time']['timestamp']] = $event;
+                    // if there's a name search stop for the first occurrence of that event
+                    if ( $action == 'event-list-name' ) {
+                        break;
+                    }
                 }
             }
             ksort($eventsByDate);
index 1eff6e6..751b17d 100644 (file)
@@ -33,7 +33,8 @@
 $glmMembersEventsAddOnValidActions = array(
     'adminActions' => array(
         'ajax' => array(
-                'pdfOutputs' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG
+            'pdfOutputs' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG,
+            'nameSearch' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG
         ),
         'member' => array(
             'events' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG,