Create a check for number of events and keep going out to get at least 5
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 2 Aug 2016 17:33:40 +0000 (13:33 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 2 Aug 2016 17:33:40 +0000 (13:33 -0400)
Create a dynamic check for number of events to get at least 5 events in
list.

models/front/events/list.php

index 69a7a05..9cd2392 100644 (file)
@@ -135,7 +135,21 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
                     break;
                 }
             } else {
-                $toDate = date('m/d/Y', strtotime( '+ 2 weeks' ));
+                // This is the default date range for this agenda view
+                $total_current_events  = $this->checkHaveAnyEvents();
+                echo '<pre>$total_current_events: ' . print_r( $total_current_events, true ) . '</pre>';
+                $weeks_to_check = 2;
+                $event_count    = 0;
+                do {
+                    $event_count = $this->getEventCountForWeekRange( $weeks_to_check );
+                    ++$weeks_to_check;
+                } while ( $event_count <= 5 );
+
+                echo '<pre>$event_count: ' . print_r( $event_count, true ) . '</pre>';
+                echo '<pre>$weeks_to_check: ' . print_r( $weeks_to_check, true ) . '</pre>';
+                $week_string = ( $weeks === 1 ) ? 'weeks': 'week';
+                $toDate = date('m/d/Y', strtotime( '+ ' . $weeks_to_check . ' ' . $week_string ));
+                echo '<pre>$toDate: ' . print_r( $toDate, true ) . '</pre>';
             }
         }
         if ( $fromDate && $toDate && !(isset($_REQUEST['event_name']) && $_REQUEST['event_name'])) {
@@ -642,4 +656,44 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
         $text = str_replace("\r", '', $text);
         return $text;
     }
+    public function checkHaveAnyEvents()
+    {
+        $from = date( 'Y-m-d' );
+        return $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT count(id)
+                   FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times
+                  WHERE DATE(start_time) >= %s ",
+                $from
+            )
+        );
+    }
+    /**
+     * getEventCountForWeekRange
+     *
+     * Returns the count of events from the current date based on given weeks.
+     *
+     * @param mixed $weeks Number of weeks to check.
+     *
+     * @access public
+     * @return mixed
+     */
+    public function getEventCountForWeekRange( $weeks )
+    {
+        if ( !filter_var( $weeks, FILTER_VALIDATE_INT ) ) {
+            return false;
+        }
+        $from = date( 'Y-m-d' );
+        $week_string = ( $weeks === 1 ) ? 'weeks': 'week';
+        $to   = date( 'Y-m-d', strtotime( '+ ' . $weeks . ' ' . $week_string) );
+        return $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT count(id)
+                   FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times
+                  WHERE DATE(start_time) BETWEEN %s AND %s",
+                $from,
+                $to
+            )
+        );
+    }
 }