importing events
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 16 Mar 2016 19:05:23 +0000 (15:05 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 16 Mar 2016 19:05:23 +0000 (15:05 -0400)
now it imports the times,categories, location, recurrences, event_times
still doing some testing to see if everything is working correctly.

models/admin/management/events.php

index 55eea7e..7198e38 100644 (file)
@@ -454,6 +454,26 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
         return $dateTime->format('H:i');
     }
 
+    public function getCityId($cityName)
+    {
+        $sql = "
+        SELECT id
+          FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+         WHERE name like '" . esc_sql( trim( $cityName ) ) . "'";
+        $cityId = $this->wpdb->get_row($sql, ARRAY_A);
+        if ( !$cityId ) {
+            // then add the city to the city table
+            $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
+                array( 'name' => trim( $cityName ) ),
+                array( '%s' )
+            );
+            return $this->wpdb->insert_id;
+        } else {
+            return $cityId['id'];
+        }
+    }
+
     public function addEvents()
     {
         // clear the events tables first
@@ -461,13 +481,14 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "recurrences" );
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times" );
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "event_categories" );
+        $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "locations" );
         $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config);
         $return      = '';
         $sql         = "
         SELECT *
           FROM {$this->settings['schema']}.{$this->settings['tablename']}
          WHERE edate >= '{$this->settings['sdate']}'::DATE";
-        $sql .=" AND reacur ";
+        //$sql .=" AND reacur ";
         try {
             $events = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
             $return .= '<pre>$events: ' . print_r($events, true) . '</pre>';
@@ -549,10 +570,12 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                         echo '<pre>SQL Error: ' . $this->wpdb->last_error . '</pre>';
                     }
                 }
+                // checking for $eventId
                 echo '<pre>$eventId: ' . print_r($eventId, true) . '</pre>';
                 if (!$eventId) {
                     die('something is wrong no eventId');
                 }
+                // category for event
                 if ( $event['topicid'] ) {
                     $this->wpdb->insert(
                         GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'event_categories',
@@ -566,6 +589,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                         )
                     );
                 }
+                // recurrences for the event
                 $allDates  = !$event['reacur'];
                 if ( !$allDates && ( $event['bdate'] == $event['edate'] ) ) {
                     $allDates = 1;
@@ -648,6 +672,74 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                 );
                 $recurId    = $this->wpdb->insert_id;
                 $Recurrences->createRecurrenceTimesEntries( $recurId, true, true );
+                // location for the event
+                $hasLocation = (
+                    ($event['loc'])
+                    || ($event['contact'])
+                    || ($event['phone'])
+                    || ($event['url'])
+                    || ($event['email'])
+                );
+                if ( $hasLocation ) {
+                    $locationData = $locationFormat = array();
+                    $locationData['event'] = $eventId;
+                    $locationFormat[] = '%d';
+                    if ($event['loc']) {
+                        $locationData['name'] = $event['loc'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['address']) {
+                        $locationData['address'] = $event['address'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['city']) {
+                        $locationData['city'] = $this->getCityId( $event['city'] );
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['state']) {
+                        $locationData['state'] = $event['state'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['lat']) {
+                        $locationData['lat'] = $event['lat'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['lon']) {
+                        $locationData['lon'] = $event['lon'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['contact']) {
+                        // break up the contact name db is expecting first and
+                        // last name separately
+                        list( $firstName, $lastName ) = explode( ' ', $event['contact'], 2 );
+                        $locationData['contact_fname'] = $firstName;
+                        $locationFormat[] = '%s';
+                        $locationData['contact_lname'] = $lastName;
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['phone']) {
+                        $locationData['contact_phone'] = $event['phone'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['url']) {
+                        $locationData['url'] = $event['url'];
+                        $locationFormat[] = '%s';
+                    }
+                    if ($event['email']) {
+                        $locationData['contact_email'] = $event['email'];
+                        $locationFormat[] = '%s';
+                    }
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations',
+                        $locationData,
+                        $locationFormat
+                    );
+                    $locationId = $this->wpdb->insert_id;
+                    if ( !$locationId ) {
+                        echo '<pre>$locationData: ' . print_r($locationData, true) . '</pre>';
+                        die('no return id for location');
+                    }
+                }
             }
         } catch(PDOException $e) {
             echo '<pre>$e: ' . print_r($e, true) . '</pre>';