Getting the data into front end templates
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 10 Mar 2016 19:28:58 +0000 (14:28 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 10 Mar 2016 19:28:58 +0000 (14:28 -0500)
Working with the Data Abstract event class

classes/data/dataCategories.php
classes/data/dataEvents.php
models/front/events/baseAction.php
models/front/events/list.php
setup/frontHooks.php
views/front/events/dashboard.html

index e7997f2..1680137 100644 (file)
@@ -155,7 +155,7 @@ class GlmDataEventsCategories extends GlmDataAbstract
 
     }
 
-    /*
+    /**
      * Entry Post Processing Call-Back Method
      *
      * Perform post-processing for all result entries.
@@ -331,7 +331,32 @@ class GlmDataEventsCategories extends GlmDataAbstract
 
     }
 
-    /*
+    /**
+     * getCategoriesWithCurrentEvents
+     *
+     * Returns a list of categories ordered by name that have an event with
+     * current event times.
+     *
+     * @access public
+     * @return void
+     */
+    public function getCategoriesWithCurrentEvents()
+    {
+        $where = " T.id IN (
+        SELECT category
+          FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "event_categories
+         WHERE event IN (
+                SELECT event
+                  FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times
+                 WHERE active
+                   AND start_time >= now()
+            )
+        ) ";
+        $order = "T.name";
+        return $this->getList($where, $order);
+    }
+
+    /**
      * Check other requirements
      *
      * When deleteing categories, check for references in sub-categories and set them to no parent.
@@ -358,7 +383,7 @@ class GlmDataEventsCategories extends GlmDataAbstract
         return $r;
     }
 
-    /*
+    /**
      * Sort a database result array (from DataAbstract.php) by parent, child relationships
      *
      * @param array $array Array to be sorted
index c58c8d1..43346a3 100644 (file)
@@ -79,6 +79,13 @@ class GlmDataEvents extends GlmDataAbstract
      * @access public
      */
     public $MemberInfo;
+    /**
+     * postAddTimes
+     *
+     * @var bool
+     * @access public
+     */
+    public $postAddTimes = false;
 
     /**
      * Constructor
@@ -295,7 +302,7 @@ class GlmDataEvents extends GlmDataAbstract
 
     }
 
-    /*
+    /**
      * Entry Post Processing Call-Back Method
      *
      * Perform post-processing for all result entries.
@@ -329,10 +336,28 @@ class GlmDataEvents extends GlmDataAbstract
         ;";
         $r['categories'] = $this->wpdb->get_results($sql, ARRAY_A);
 
+        if ($this->postAddTimes) {
+            $sql = "
+            SELECT ET.*
+              FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times AS ET
+             WHERE ET.start_time >= now()
+               AND ET.active
+               AND ET.event = {$r['id']}
+            ORDER BY ET.start_time";
+            $times = $this->wpdb->get_results($sql, ARRAY_A);
+            $r['times'] = $times;
+            // get the first element of times array
+            if ( !empty( $times ) ) {
+                 $r['starting_date'] = $times[0]['start_time'];
+                 $r['ending_date'] = $times[count($times) - 1]['end_time'];
+            }
+        }
+
+
         return $r;
     }
 
-    /*
+    /**
      * Update event slug - should be called after an event record is added or updated
      *
      * @param integer id ID of event that needs the slug updated
@@ -362,7 +387,7 @@ class GlmDataEvents extends GlmDataAbstract
 
     }
 
-    /*
+    /**
      * Update timestamps for created, updated, approved
      *
      * @param string $field Field to update
@@ -387,7 +412,7 @@ class GlmDataEvents extends GlmDataAbstract
 
     }
 
-    /*
+    /**
      * Delete an event and all data associated with it.
      *
      * @param integer $id ID of event
@@ -433,6 +458,44 @@ class GlmDataEvents extends GlmDataAbstract
 
     }
 
+    /**
+     * getEventsByCategory
+     *
+     * Return array with a list of events by the category
+     * Option parameter to limit the results
+     *
+     * @param mixed $category Category to filter by
+     * @param bool  $limit    The limit of records to return
+     *
+     * @access public
+     * @return void
+     */
+    public function getEventsByCategory( $category, $limit = null )
+    {
+        $this->postAddTimes = true;
+        var_dump('hello there');
+        $where = "
+            T.id IN (
+                SELECT event
+                  FROM " .GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "event_categories
+                 WHERE category = {$category}
+                AND event IN (
+                        SELECT event
+                          FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times AS ET
+                         WHERE active
+                           AND start_time >= now()
+                    )
+                )
+            ";
+        $order = "T.id";
+        if ($limit = filter_var($limit, FILTER_VALIDATE_INT)) {
+            $order .= " LIMIT {$limit} OFFSET 0";
+        }
+        $events = $this->getList($where, $order);
+
+        $this->postAddTimes = false;
+        return $events;
+    }
 }
 
 ?>
index c9985bd..3be1e38 100644 (file)
@@ -8,6 +8,8 @@
  */
 
 require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH . '/data/dataEvents.php';
+require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH . '/data/dataCategories.php';
+require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH . '/data/dataEventCategories.php';
 
 /**
  * GlmMembersFront_events_list
@@ -43,19 +45,19 @@ abstract class GlmMembersFront_events_baseAction extends GlmDataEvents
 
     public function getCategories()
     {
-        include GLM_MEMBERS_EVENTS_PLUGIN_PATH. '/models/front/events/testCategoryData.php';
-        return $categories;
+        $categoryData = new GlmDataEventsCategories( $this->wpdb, $this->config );
+        return $categoryData->getCategoriesWithCurrentEvents();
     }
 
-    public function getModelEventsData($categoryId = null)
+    public function getModelEventsData($categoryId = null, $limit = null)
     {
-        include GLM_MEMBERS_EVENTS_PLUGIN_PATH. '/models/front/events/testEventsData.php';
+        //echo '<pre>categoryId: ' . print_r( $categoryId, true ) . '</pre>';
+        //echo '<pre>limit: ' . print_r( $limit, true ) . '</pre>';
         if ($categoryId) {
-            return array_filter($events, function($data) use($categoryId) {
-                return ($data['category'] == $categoryId) ? 1 : 0;
-            });
+            return $this->getEventsByCategory( $categoryId, $limit );
+        } else {
+            return $this->getList();
         }
-        return $events;
     }
 
     public function getModelEventData($eventId)
index 3eb1df4..8a45bef 100644 (file)
@@ -68,6 +68,7 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
         }
 
         $categories = $this->getCategories();
+        //echo '<pre>' . print_r( $categories, true ) . '</pre>';
         wp_register_script(
             'event-dashboard-js',
             GLM_MEMBERS_EVENTS_PLUGIN_BASE_URL . '/js/dashboard.js',
@@ -83,16 +84,12 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
             $view = 'list.html';
             break;
         default:
-            // for the dashboard need to get list of event categories and
-            // filter out the events
-            $events = $this->getModelEventsData();
             $view = 'dashboard.html';
-            foreach ( $categories as $cat ) {
-                $catEvents = array_filter( $events, function($data) use($cat) {
-                    return ($data['category'] == $cat['id']) ? 1 : 0;
-                } );
+            foreach ( $categories as $catid => $catData ) {
+                $catEvents = $this->getModelEventsData( $catid, 3 );
+                //echo '<pre>' . print_r( $catEvents, true ) . '</pre>';
                 if ( !empty($catEvents) ) {
-                    $categoryEvents[$cat['id']] = $catEvents;
+                    $categoryEvents[$catid] = $catEvents;
                 }
             }
             break;
index f6ca254..d51f93d 100644 (file)
@@ -33,7 +33,7 @@ add_filter('rewrite_rules_array', function($rules) {
     return $newrules + $rules;
 });
 
-// Add memberslug query var
+// Add eventId query var
 add_filter('query_vars', function($vars) {
     array_push($vars, 'eventId');
     return $vars;
index f0cc0dd..440f90d 100644 (file)
@@ -20,8 +20,9 @@
                         {break}
                     {/if}
                 <li class="description text-left">
+                    {$event.starting_date|date_format}<br>
                     <a href="{$siteBaseUrl}event-detail/{$event.slug}">
-                    {$event.title}
+                    {$event.name}
                     </a>
                 </li>
                 {/foreach}