filtering events by member id if specified in event list shortcode
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 23 May 2017 20:14:56 +0000 (16:14 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 23 May 2017 20:14:56 +0000 (16:14 -0400)
if event list shortcode has a member id to get events based on members, adjusting queries to
only get the events by that id, also fixing the dropdown to only have the dates from events
associated with that member id as well

models/admin/ajax/eventsCalMonthAJAX.php
models/front/events/list.php
views/front/events/agenda.html

index dc24261..01e6d09 100644 (file)
@@ -81,19 +81,27 @@ class GlmMembersAdmin_ajax_eventsCalMonthAJAX extends GlmDataEventsTimes
      * from within a given date range. Left out of the where statement because it is
      * already performed by $this->dateRange
      */
-    public function getModelTimesData( )
+    public function getModelTimesData($member_id = false )
     {
         $this->postAddTimes = true;
         $where = '';
-
+       
+        if($member_id !== false && $member_id !== ''){
+            $ref_dest = "= $member_id";
+        } else {
+            $ref_dest = 'IS NOT NULL';
+        }
+        
         $where .= "T.active = 1
             AND T.event IN (
                 SELECT ET.id
                 FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events AS ET
                 WHERE ET.status = " . $this->config['status_numb']['Active'] . "
+                AND ref_dest $ref_dest
             )
             AND $this->dateRangeTimes
         ";
+
         if ( isset( $_REQUEST['category'] ) && $catId = filter_var( $_REQUEST['category'], FILTER_VALIDATE_INT ) ) {
             $where .= "
             AND T.event IN (
@@ -130,7 +138,6 @@ class GlmMembersAdmin_ajax_eventsCalMonthAJAX extends GlmDataEventsTimes
     {
 
         global $wpdb;
-
         $event_data = [];
         $image_url = false;
         $datesArray = array();
@@ -217,7 +224,6 @@ class GlmMembersAdmin_ajax_eventsCalMonthAJAX extends GlmDataEventsTimes
    // end section for front page events widget data
         } else if( isset($_REQUEST['month']) ){
 
-
             $month = filter_var_array(
                 $_REQUEST['month'],
                 array(
@@ -238,8 +244,8 @@ class GlmMembersAdmin_ajax_eventsCalMonthAJAX extends GlmDataEventsTimes
                   WHERE DATE(start_time) BETWEEN '{$from}' AND '{$to}'
 
             )";
-
-            $times = $this->getModelTimesData();
+            $member_id = (isset($_REQUEST['member_id']) ? filter_var($_REQUEST['member_id'], FILTER_SANITIZE_STRING) : '');
+            $times = $this->getModelTimesData($member_id);
             if ( is_array( $times ) ) {
                 foreach ($times as $e=>$val){
                     $sql = "SELECT * FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events WHERE id = " . $val['event'];
@@ -266,8 +272,7 @@ class GlmMembersAdmin_ajax_eventsCalMonthAJAX extends GlmDataEventsTimes
 //            'status' => false,       // Assume nothing works
             'events'          => $event_data  ,    // Where our events list will go
             'message'         => $image_url,
-            'array_dates'     => $datesArray,
-//            'event'           => $dates
+            'array_dates'     => $datesArray
         );
 
         header('Content-type:application/json;charset=utf-8', true);
index 2d3f68c..eece508 100644 (file)
@@ -765,17 +765,35 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
         $years = array('current' => $current_year = date("Y"), 'next' => date('Y') +1 );
         $months = [];
         $count = 0;
+        
         foreach($years as $key=>$year){
-
-            if($current_year == $year){
-                $sql = 'SELECT MONTH(start_time) as month FROM '. GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX .'times ';
-                $sql .= "WHERE YEAR(start_time) = $year AND MONTH(start_time) >= MONTH(CURDATE()) GROUP BY month";
-                $results[$year] = $wpdb->get_results($sql, ARRAY_A);
+            // if member id we need to limit the dates for month dropdown by member id
+            if($memberId){
+                if($current_year == $year){
+                    $sql = 'SELECT MONTH(start_time) as month FROM '. GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX .'times ';
+                    $sql .= "WHERE event IN (select id from ".GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX ."events WHERE ref_dest = $memberId )";
+                    $sql .= " AND YEAR(start_time) = $year AND MONTH(start_time) >= MONTH(CURDATE()) GROUP BY month";
+                    $results[$year] = $wpdb->get_results($sql, ARRAY_A);
+                } else {
+                    $sql = 'SELECT MONTH(start_time) as month FROM '. GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX .'times ';
+                    $sql .= "WHERE event IN (select id from ".GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX ."events WHERE ref_dest = $memberId )";
+                    $sql .= " AND YEAR(start_time) = $year GROUP BY month";
+                    $results[$year] = $wpdb->get_results($sql, ARRAY_A);
+                }
+                // otherwise we can just pull all of the dates for the events as usual
             } else {
-                $sql = 'SELECT MONTH(start_time) as month FROM '. GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX .'times ';
-                $sql .= "WHERE YEAR(start_time) = $year GROUP BY month";
-                $results[$year] = $wpdb->get_results($sql, ARRAY_A);
+                
+                if($current_year == $year){
+                    $sql = 'SELECT MONTH(start_time) as month FROM '. GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX .'times ';
+                    $sql .= "WHERE YEAR(start_time) = $year AND MONTH(start_time) >= MONTH(CURDATE()) GROUP BY month";
+                    $results[$year] = $wpdb->get_results($sql, ARRAY_A);
+                } else {
+                    $sql = 'SELECT MONTH(start_time) as month FROM '. GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX .'times ';
+                    $sql .= "WHERE YEAR(start_time) = $year GROUP BY month";
+                    $results[$year] = $wpdb->get_results($sql, ARRAY_A);
+                }
             }
+            
         }
         foreach($results as $year => $month){
             foreach($month as $key=>$value){
index afc1825..42badfd 100644 (file)
@@ -2,6 +2,7 @@
     <div class="row">
         {include file='front/events/searchForm.html'}
     </div>
+    
     <div id="glm-event-agenda-view" class="row">
         <div class="small-12 columns">
             {foreach $eventsByDate as $date => $key}
         var calendar            = $("#eventCalendar");
         var event_search        = $(".glm-search-icon");
         var main_content        = $("#main-content");
+        var member_id           = '{$memberId}';
         var view                = '{$cal_view}';
         var months              = '{$json_months}';
         var category            = $('#glm-event-category').val();
         var current_year        = $('{$current_year}');
         var retain_date;
         
+        if(!member_id){
+            member_id = null;
+        }
+
         // add 7 days to ensure we always get the current month and not any other month view that may
         // be visible (last few days of previous month or first few days of next month)
         function get_current_view(){
                 action: 'glm_members_admin_ajax',
                 glm_action: 'eventsCalMonthAJAX',
                 month: month,
-                category: category
+                category: category,
+                member_id: member_id
 
             };
             $('.fc-event').remove();
                 cache: false,
                 success: function (response){
                     var buildingEvents = [];
-//                    console.log(response.message);
                     //var events_obj = jQuery.parseJSON(response);
                     var events_obj = response;
                     var events = events_obj.events;