Update for event categories
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 19 Aug 2016 20:06:49 +0000 (16:06 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 19 Aug 2016 20:06:49 +0000 (16:06 -0400)
Need to bring in the event categories too.

models/front/events/icalFeedImport.php

index 31a6173..3fa8f81 100644 (file)
@@ -39,6 +39,34 @@ class GlmMembersFront_events_icalFeedImport
         }
         exit;
     }
+    public function getCategoryId( $category )
+    {
+        $category_id = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "categories
+                  WHERE name = %d",
+                $category
+            )
+        );
+        if ( $category_id ) {
+            return $category_id;
+        } else {
+            $this->wpdb->insert(
+                GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'categories',
+                array(
+                    'name'   => $category,
+                    'parent' => 0
+                ),
+                array(
+                    '%s',
+                    '%d'
+                )
+            );
+            $category_id = $this->wpdb->insert_id;
+            return $category_id;
+        }
+    }
     public function clearData()
     {
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events" );
@@ -131,7 +159,7 @@ class GlmMembersFront_events_icalFeedImport
                             $old_image = $this->wpdb->get_var(
                                 $this->wpdb->prepare(
                                     "SELECT image
-                                       FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "
+                                       FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events
                                       WHERE id = %d",
                                     $event_id
                                 )
@@ -202,6 +230,21 @@ class GlmMembersFront_events_icalFeedImport
                 // generate the slug name for this new event
                 $event_abstract = new GlmDataEvents( $this->wpdb, $this->config );
                 $event_abstract->updateSlug( $event_id );
+
+                // Categories
+                if ( isset( $event['CATEGORIES'] ) ) {
+                    $category_data = explode( ',', $event['CATEGORIES'] );
+                    foreach ( $category_data as $category ) {
+                        $this->wpdb->insert(
+                            GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'event_categories',
+                            array(
+                                'event'    => $event_id,
+                                'category' => $this->getCategoryId( $category )
+                            ),
+                            '%d'
+                        );
+                    }
+                }
                 // Location Data
                 if ( isset( $event['LOCATION'] ) ) {
                     $location_data = explode( ',', str_replace( '\\', '', $event['LOCATION'] ) );