From ce6c4c6c64eb0562317a713dc30979153cbe724d Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 1 Apr 2016 09:57:17 -0400 Subject: [PATCH] Update the front end output to handle event name searching 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 | 16 ++++++++ models/admin/ajax/nameSearch.php | 59 ++++++++++++++++++++++++++++++ models/front/events/baseAction.php | 19 ++++++++-- models/front/events/list.php | 45 +++++++++++++++++++++-- setup/validActions.php | 3 +- 5 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 models/admin/ajax/nameSearch.php diff --git a/js/dashboard.js b/js/dashboard.js index 6b765c5..86d7389 100644 --- a/js/dashboard.js +++ b/js/dashboard.js @@ -23,4 +23,20 @@ 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 index 0000000..364df40 --- /dev/null +++ b/models/admin/ajax/nameSearch.php @@ -0,0 +1,59 @@ + + * @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; + +} diff --git a/models/front/events/baseAction.php b/models/front/events/baseAction.php index 4df4e84..110b426 100644 --- a/models/front/events/baseAction.php +++ b/models/front/events/baseAction.php @@ -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 '
$where: ' . print_r($where, true) . '
'; $events = $this->getList($where); } //echo '
$events: ' . print_r($events, true) . '
'; diff --git a/models/front/events/list.php b/models/front/events/list.php index db4a50a..2004315 100644 --- a/models/front/events/list.php +++ b/models/front/events/list.php @@ -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); diff --git a/setup/validActions.php b/setup/validActions.php index 1eff6e6..751b17d 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -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, -- 2.17.1